diff --git a/src/AnimeClient/FormGenerator.php b/src/AnimeClient/FormGenerator.php index c07c9934..e2f0109b 100644 --- a/src/AnimeClient/FormGenerator.php +++ b/src/AnimeClient/FormGenerator.php @@ -54,19 +54,21 @@ final class FormGenerator { public function generate(string $name, array $form): string { $type = $form['type']; + $display = $form['display'] ?? TRUE; + $value = $form['value'] ?? ''; - if ($form['display'] === FALSE) + if ($display === FALSE) { return $this->helper->input([ 'type' => 'hidden', 'name' => $name, - 'value' => $form['value'], + 'value' => $value, ]); } $params = [ 'name' => $name, - 'value' => $form['value'], + 'value' => $value, 'attribs' => [ 'id' => $name, ], @@ -95,7 +97,7 @@ final class FormGenerator { foreach (['readonly', 'disabled'] as $key) { - if ($form[$key] !== FALSE) + if (array_key_exists($key, $form) && $form[$key] !== FALSE) { $params['attribs'][$key] = $form[$key]; } diff --git a/tests/AnimeClient/FormGeneratorTest.php b/tests/AnimeClient/FormGeneratorTest.php new file mode 100644 index 00000000..169fee57 --- /dev/null +++ b/tests/AnimeClient/FormGeneratorTest.php @@ -0,0 +1,275 @@ + + * @copyright 2015 - 2020 Timothy J. Warren + * @license http://www.opensource.org/licenses/mit-license.html MIT License + * @version 4.2 + * @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient + */ + +namespace Aviat\AnimeClient\Tests; + +use Aviat\AnimeClient\FormGenerator; + +/** + * Map config settings to form fields + */ +const SETTINGS_MAP = [ + 'anilist' => [ + 'enabled' => [ + 'type' => 'boolean', + 'title' => 'Enable Anilist Integration', + 'default' => FALSE, + 'description' => 'Enable syncing data between Kitsu and Anilist. Requires appropriate API keys to be set in config', + ], + 'client_id' => [ + 'type' => 'string', + 'title' => 'Anilist API Client ID', + 'default' => '', + 'description' => 'The client id for your Anilist API application', + ], + 'client_secret' => [ + 'type' => 'string', + 'title' => 'Anilist API Client Secret', + 'default' => '', + 'description' => 'The client secret for your Anilist API application', + ], + 'username' => [ + 'type' => 'string', + 'title' => 'Anilist Username', + 'default' => '', + 'description' => 'Login username for Anilist account to integrate with', + ], + 'access_token' => [ + 'type' => 'hidden', + 'title' => 'API Access Token', + 'default' => '', + 'description' => 'The Access code for accessing the Anilist API', + 'readonly' => TRUE, + ], + 'access_token_expires' => [ + 'type' => 'string', + 'title' => 'Expiration timestamp of the access token', + 'default' => '0', + 'description' => 'The unix timestamp of when the access token expires.', + 'readonly' => TRUE, + ], + 'refresh_token' => [ + 'type' => 'string', + 'title' => 'API Refresh Token', + 'default' => '', + 'description' => 'Token to refresh the access token before it expires', + 'readonly' => TRUE, + ], + ], + + 'cache' => [ + 'driver' => [ + 'type' => 'select', + 'title' => 'Cache Type', + 'description' => 'The Cache backend', + 'options' => [ + 'APCu' => 'apcu', + 'Memcached' => 'memcached', + 'Redis' => 'redis', + 'No Cache' => 'null' + ], + ], + 'connection' => [ + 'type' => 'subfield', + 'title' => 'Connection', + 'fields' => [ + 'host' => [ + 'type' => 'string', + 'title' => 'Cache Host', + 'description' => 'Host of the cache backend to connect to', + ], + 'port' => [ + 'type' => 'string', + 'title' => 'Cache Port', + 'description' => 'Port of the cache backend to connect to', + 'default' => NULL, + ], + 'password' => [ + 'type' => 'string', + 'title' => 'Cache Password', + 'description' => 'Password to connect to cache backend', + 'default' => NULL, + ], + 'persistent' => [ + 'type' => 'boolean', + 'title' => 'Persistent Cache Connection', + 'description' => 'Whether to have a persistent connection to the cache', + 'default' => FALSE, + ], + 'database' => [ + 'type' => 'string', + 'title' => 'Cache Database', + 'default' => '1', + 'description' => 'Cache database number for Redis', + ], + ], + ], + /* 'options' => [ + 'type' => 'subfield', + 'title' => 'Options', + 'fields' => [], + ] */ + ], + 'config' => [ + 'kitsu_username' => [ + 'type' => 'string', + 'title' => 'Kitsu Username', + 'default' => '', + 'description' => 'Username of the account to pull list data from.', + ], + 'whose_list' => [ + 'type' => 'string', + 'title' => 'Whose List', + 'default' => 'Somebody', + 'description' => 'Name of the owner of the list data.', + ], + 'theme' => [ + 'type' => 'select', + 'title' => 'Theme', + 'default' => 'auto', + 'description' => 'Which color scheme to use?', + 'options' => [ + 'Automatically match OS theme' => 'auto', + 'Original Light Theme' => 'light', + 'Dark Theme' => 'dark', + ] + ], + 'show_anime_collection' => [ + 'type' => 'boolean', + 'title' => 'Show Anime Collection', + 'default' => FALSE, + 'description' => 'Should the anime collection be shown?', + ], + 'show_manga_collection' => [ + 'type' => 'boolean', + 'title' => 'Show Manga Collection', + 'default' => FALSE, + 'description' => 'Should the manga collection be shown?', + ], + 'default_list' => [ + 'type' => 'select', + 'title' => 'Default List', + 'description' => 'Which list to show by default.', + 'options' => [ + 'Anime' => 'anime', + 'Manga' => 'manga', + ], + ], + 'default_anime_list_path' => [ //watching|plan_to_watch|on_hold|dropped|completed|all + 'type' => 'select', + 'title' => 'Default Anime List Section', + 'description' => 'Which part of the anime list to show by default.', + 'options' => [ + 'Watching' => 'watching', + 'Plan to Watch' => 'plan_to_watch', + 'On Hold' => 'on_hold', + 'Dropped' => 'dropped', + 'Completed' => 'completed', + 'All' => 'all', + ] + ], + 'default_manga_list_path' => [ //reading|plan_to_read|on_hold|dropped|completed|all + 'type' => 'select', + 'title' => 'Default Manga List Section', + 'description' => 'Which part of the manga list to show by default.', + 'options' => [ + 'Reading' => 'reading', + 'Plan to Read' => 'plan_to_read', + 'On Hold' => 'on_hold', + 'Dropped' => 'dropped', + 'Completed' => 'completed', + 'All' => 'all', + ] + ] + ], + 'database' => [ + 'type' => [ + 'type' => 'select', + 'title' => 'Database Type', + 'options' => [ + 'MySQL' => 'mysql', + 'PostgreSQL' => 'pgsql', + 'SQLite' => 'sqlite', + ], + 'default' => 'sqlite', + 'description' => 'Type of database to connect to', + ], + 'host' => [ + 'type' => 'string', + 'title' => 'Host', + 'description' => 'The host of the database server', + ], + 'user' => [ + 'type' => 'string', + 'title' => 'User', + 'description' => 'Database connection user', + ], + 'pass' => [ + 'type' => 'string', + 'title' => 'Password', + 'description' => 'Database connection password' + ], + 'port' => [ + 'type' => 'string', + 'title' => 'Port', + 'description' => 'Database connection port', + 'default' => NULL, + ], + 'database' => [ + 'type' => 'string', + 'title' => 'Database Name', + 'description' => 'Name of the database/schema to connect to', + ], + 'file' => [ + 'type' => 'string', + 'title' => 'Database File', + 'description' => 'Path to the database file, if required by the current database type.', + 'default' => 'anime_collection.sqlite', + ], + ], +]; + + +class FormGeneratorTest extends AnimeClientTestCase { + protected $generator; + + public function setUp(): void + { + parent::setUp(); + + $this->generator = new FormGenerator($this->container); + } + + public function testSanity(): void + { + $generator = new FormGenerator($this->container); + $this->assertInstanceOf(FormGenerator::class, $generator); + } + + public function testGeneration(): void + { + // $html = $this->generator->generate('database', SETTINGS_MAP); + // $this->assertMatchesHtmlSnapshot($html); + foreach (SETTINGS_MAP as $section => $fields) + { + foreach ($fields as $name => $config) + { + $html = $this->generator->generate($name, $config); + $this->assertMatchesHtmlSnapshot($html); + } + } + } +} \ No newline at end of file diff --git a/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__1.html b/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__1.html new file mode 100644 index 00000000..7baef2cd --- /dev/null +++ b/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__1.html @@ -0,0 +1,5 @@ + + + + + diff --git a/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__10.html b/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__10.html new file mode 100644 index 00000000..9d0d056e --- /dev/null +++ b/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__10.html @@ -0,0 +1,4 @@ + + + + diff --git a/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__11.html b/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__11.html new file mode 100644 index 00000000..a7fe93fe --- /dev/null +++ b/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__11.html @@ -0,0 +1,4 @@ + + + + diff --git a/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__12.html b/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__12.html new file mode 100644 index 00000000..ce059c77 --- /dev/null +++ b/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__12.html @@ -0,0 +1,8 @@ + + + + diff --git a/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__13.html b/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__13.html new file mode 100644 index 00000000..ac95bc17 --- /dev/null +++ b/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__13.html @@ -0,0 +1,5 @@ + + + + + diff --git a/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__14.html b/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__14.html new file mode 100644 index 00000000..0127f5f2 --- /dev/null +++ b/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__14.html @@ -0,0 +1,5 @@ + + + + + diff --git a/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__15.html b/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__15.html new file mode 100644 index 00000000..40801d06 --- /dev/null +++ b/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__15.html @@ -0,0 +1,7 @@ + + + + diff --git a/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__16.html b/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__16.html new file mode 100644 index 00000000..5c7bd23a --- /dev/null +++ b/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__16.html @@ -0,0 +1,11 @@ + + + + diff --git a/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__17.html b/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__17.html new file mode 100644 index 00000000..89e6ab84 --- /dev/null +++ b/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__17.html @@ -0,0 +1,11 @@ + + + + diff --git a/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__18.html b/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__18.html new file mode 100644 index 00000000..37b8332f --- /dev/null +++ b/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__18.html @@ -0,0 +1,8 @@ + + + + diff --git a/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__19.html b/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__19.html new file mode 100644 index 00000000..ec3c00cf --- /dev/null +++ b/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__19.html @@ -0,0 +1,4 @@ + + + + diff --git a/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__2.html b/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__2.html new file mode 100644 index 00000000..4a440040 --- /dev/null +++ b/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__2.html @@ -0,0 +1,4 @@ + + + + diff --git a/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__20.html b/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__20.html new file mode 100644 index 00000000..75248dc7 --- /dev/null +++ b/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__20.html @@ -0,0 +1,4 @@ + + + + diff --git a/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__21.html b/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__21.html new file mode 100644 index 00000000..f7069230 --- /dev/null +++ b/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__21.html @@ -0,0 +1,4 @@ + + + + diff --git a/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__22.html b/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__22.html new file mode 100644 index 00000000..1074dd4c --- /dev/null +++ b/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__22.html @@ -0,0 +1,4 @@ + + + + diff --git a/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__23.html b/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__23.html new file mode 100644 index 00000000..c0d27a30 --- /dev/null +++ b/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__23.html @@ -0,0 +1,4 @@ + + + + diff --git a/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__24.html b/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__24.html new file mode 100644 index 00000000..adcfd0b6 --- /dev/null +++ b/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__24.html @@ -0,0 +1,4 @@ + + + + diff --git a/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__3.html b/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__3.html new file mode 100644 index 00000000..cf3a702f --- /dev/null +++ b/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__3.html @@ -0,0 +1,4 @@ + + + + diff --git a/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__4.html b/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__4.html new file mode 100644 index 00000000..7dc250d9 --- /dev/null +++ b/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__4.html @@ -0,0 +1,4 @@ + + + + diff --git a/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__5.html b/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__5.html new file mode 100644 index 00000000..439fbd5c --- /dev/null +++ b/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__5.html @@ -0,0 +1,4 @@ + + + + diff --git a/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__6.html b/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__6.html new file mode 100644 index 00000000..d54b6cdc --- /dev/null +++ b/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__6.html @@ -0,0 +1,4 @@ + + + + diff --git a/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__7.html b/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__7.html new file mode 100644 index 00000000..4e23b746 --- /dev/null +++ b/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__7.html @@ -0,0 +1,4 @@ + + + + diff --git a/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__8.html b/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__8.html new file mode 100644 index 00000000..c096fb1d --- /dev/null +++ b/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__8.html @@ -0,0 +1,9 @@ + + + + diff --git a/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__9.html b/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__9.html new file mode 100644 index 00000000..0d6f05c3 --- /dev/null +++ b/tests/AnimeClient/__snapshots__/FormGeneratorTest__testGeneration__9.html @@ -0,0 +1,4 @@ + + + + diff --git a/tests/AnimeClient/bootstrap.php b/tests/AnimeClient/bootstrap.php deleted file mode 100644 index 27878b14..00000000 --- a/tests/AnimeClient/bootstrap.php +++ /dev/null @@ -1,41 +0,0 @@ -