From fb567e85e93a466cb06fd04e25d180f4fb18e6f1 Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Thu, 30 Mar 2017 16:16:40 -0400 Subject: [PATCH] Simplify routing code a bit --- app/appConf/base_config.php | 8 +- app/appConf/menus.php | 41 ---- app/appConf/menus.toml | 19 ++ app/appConf/route_config.toml | 19 ++ app/appConf/routes.php | 370 ++++++++++++++++----------------- app/views/anime/list.php | 6 +- app/views/collection/cover.php | 7 +- app/views/collection/list.php | 7 +- app/views/main-menu.php | 4 +- app/views/manga/cover.php | 13 +- app/views/manga/list.php | 8 +- src/Controller.php | 3 +- src/Controller/Anime.php | 7 +- src/Controller/Collection.php | 18 +- src/Controller/Manga.php | 7 +- src/ControllerTrait.php | 6 + src/MenuGenerator.php | 5 +- src/RoutingBase.php | 5 +- src/UrlGenerator.php | 27 --- tests/AnimeClientTestCase.php | 8 +- tests/DispatcherTest.php | 154 +++++++------- tests/UrlGeneratorTest.php | 56 ----- 22 files changed, 347 insertions(+), 451 deletions(-) delete mode 100644 app/appConf/menus.php create mode 100644 app/appConf/menus.toml create mode 100644 app/appConf/route_config.toml diff --git a/app/appConf/base_config.php b/app/appConf/base_config.php index 710e9ad8..b8b645ad 100644 --- a/app/appConf/base_config.php +++ b/app/appConf/base_config.php @@ -14,6 +14,7 @@ * @link https://github.com/timw4mail/HummingBirdAnimeClient */ +use function Aviat\AnimeClient\loadToml; // ---------------------------------------------------------------------------- // Lower level configuration @@ -23,7 +24,9 @@ $APP_DIR = realpath(__DIR__ . '/../'); $ROOT_DIR = realpath("{$APP_DIR}/../"); -$base_config = [ +$tomlConfig = loadToml(__DIR__); + +$base_config = array_merge($tomlConfig, [ 'asset_dir' => "{$ROOT_DIR}/public", // Template file path @@ -34,6 +37,5 @@ $base_config = [ 'img_cache_path' => "{$ROOT_DIR}/public/images", // Included config files - 'menus' => require 'menus.php', 'routes' => require 'routes.php', -]; \ No newline at end of file +]); \ No newline at end of file diff --git a/app/appConf/menus.php b/app/appConf/menus.php deleted file mode 100644 index 9ff73ea0..00000000 --- a/app/appConf/menus.php +++ /dev/null @@ -1,41 +0,0 @@ - - * @copyright 2015 - 2017 Timothy J. Warren - * @license http://www.opensource.org/licenses/mit-license.html MIT License - * @version 4.0 - * @link https://github.com/timw4mail/HummingBirdAnimeClient - */ - - -return [ - 'anime_list' => [ - 'route_prefix' => '/anime', - 'items' => [ - 'watching' => '/watching', - 'plan_to_watch' => '/plan_to_watch', - 'on_hold' => '/on_hold', - 'dropped' => '/dropped', - 'completed' => '/completed', - 'all' => '/all' - ] - ], - 'manga_list' => [ - 'route_prefix' => '/manga', - 'items' => [ - 'reading' => '/reading', - 'plan_to_read' => '/plan_to_read', - 'on_hold' => '/on_hold', - 'dropped' => '/dropped', - 'completed' => '/completed', - 'all' => '/all' - ] - ] -]; \ No newline at end of file diff --git a/app/appConf/menus.toml b/app/appConf/menus.toml new file mode 100644 index 00000000..fd1df9fa --- /dev/null +++ b/app/appConf/menus.toml @@ -0,0 +1,19 @@ +[anime_list] + route_prefix = "/anime" + [anime_list.items] + watching = '/watching' + plan_to_watch = '/plan_to_watch' + on_hold = '/on_hold' + dropped = '/dropped' + completed = '/completed' + all = '/all' + +[manga_list] + route_prefix = "/manga" + [manga_list.items] + reading = '/reading' + plan_to_read = '/plan_to_read' + on_hold = '/on_hold' + dropped = '/dropped' + completed = '/completed' + all = '/all' \ No newline at end of file diff --git a/app/appConf/route_config.toml b/app/appConf/route_config.toml new file mode 100644 index 00000000..12d61068 --- /dev/null +++ b/app/appConf/route_config.toml @@ -0,0 +1,19 @@ +################################################################################ +# Route config +# +# Default views and paths +################################################################################ + +# Path to public directory, where images/css/javascript are located, +# appended to the url +asset_path = "/public" + +# Which list should be the default? +default_lis = "anime" # anime or manga + +# Default pages for anime/manga +default_anime_list_path = "watching" # watching|plan_to_watch|on_hold|dropped|completed|all +default_manga_list_path = "reading" # reading|plan_to_read|on_hold|dropped|completed|all + +# Default view type (cover_view/list_view) +default_view_type = "cover_view" \ No newline at end of file diff --git a/app/appConf/routes.php b/app/appConf/routes.php index 936dd29e..21f92986 100644 --- a/app/appConf/routes.php +++ b/app/appConf/routes.php @@ -22,211 +22,189 @@ use const Aviat\AnimeClient\{ use Aviat\AnimeClient\AnimeClient; +// ------------------------------------------------------------------------- +// Routing Config +// +// Maps paths to controlers and methods +// ------------------------------------------------------------------------- return [ - // ------------------------------------------------------------------------- - // Routing options - // - // Specify default paths and views - // ------------------------------------------------------------------------- - 'route_config' => [ - // Path to public directory, where images/css/javascript are located, - // appended to the url - 'asset_path' => '/public', - - // Which list should be the default? - 'default_list' => 'anime', // anime or manga - - // Default pages for anime/manga - 'default_anime_list_path' => "watching", // watching|plan_to_watch|on_hold|dropped|completed|all - 'default_manga_list_path' => "reading", // reading|plan_to_read|on_hold|dropped|completed|all - - // Default view type (cover_view/list_view) - 'default_view_type' => 'cover_view', + // --------------------------------------------------------------------- + // Anime List Routes + // --------------------------------------------------------------------- + 'anime.add.get' => [ + 'path' => '/anime/add', + 'action' => 'addForm', + 'verb' => 'get', ], - // ------------------------------------------------------------------------- - // Routing Config - // - // Maps paths to controlers and methods - // ------------------------------------------------------------------------- - 'routes' => [ - // --------------------------------------------------------------------- - // Anime List Routes - // --------------------------------------------------------------------- - 'anime.add.get' => [ - 'path' => '/anime/add', - 'action' => 'addForm', - 'verb' => 'get', + 'anime.add.post' => [ + 'path' => '/anime/add', + 'action' => 'add', + 'verb' => 'post', + ], + 'anime.details' => [ + 'path' => '/anime/details/{id}', + 'action' => 'details', + 'tokens' => [ + 'id' => '[a-z0-9\-]+', ], - 'anime.add.post' => [ - 'path' => '/anime/add', - 'action' => 'add', - 'verb' => 'post', + ], + 'anime.delete' => [ + 'path' => '/anime/delete', + 'action' => 'delete', + 'verb' => 'post', + ], + // --------------------------------------------------------------------- + // Manga Routes + // --------------------------------------------------------------------- + 'manga.search' => [ + 'path' => '/manga/search', + 'action' => 'search', + ], + 'manga.add.get' => [ + 'path' => '/manga/add', + 'action' => 'addForm', + 'verb' => 'get', + ], + 'manga.add.post' => [ + 'path' => '/manga/add', + 'action' => 'add', + 'verb' => 'post', + ], + 'manga.delete' => [ + 'path' => '/manga/delete', + 'action' => 'delete', + 'verb' => 'post', + ], + 'manga.details' => [ + 'path' => '/manga/details/{id}', + 'action' => 'details', + 'tokens' => [ + 'id' => '[a-z0-9\-]+', ], - 'anime.details' => [ - 'path' => '/anime/details/{id}', - 'action' => 'details', - 'tokens' => [ - 'id' => '[a-z0-9\-]+', - ], + ], + // --------------------------------------------------------------------- + // Anime Collection Routes + // --------------------------------------------------------------------- + 'collection.search' => [ + 'path' => '/collection/search', + 'action' => 'search', + ], + 'collection.add.get' => [ + 'path' => '/collection/add', + 'action' => 'form', + 'params' => [], + ], + 'collection.edit.get' => [ + 'path' => '/collection/edit/{id}', + 'action' => 'form', + 'tokens' => [ + 'id' => '[0-9]+', ], - 'anime.delete' => [ - 'path' => '/anime/delete', - 'action' => 'delete', - 'verb' => 'post', + ], + 'collection.add.post' => [ + 'path' => '/collection/add', + 'action' => 'add', + 'verb' => 'post', + ], + 'collection.edit.post' => [ + 'path' => '/collection/edit', + 'action' => 'edit', + 'verb' => 'post', + ], + 'collection.view' => [ + 'path' => '/collection/view{/view}', + 'action' => 'index', + 'params' => [], + 'tokens' => [ + 'view' => '[a-z_]+', ], - // --------------------------------------------------------------------- - // Manga Routes - // --------------------------------------------------------------------- - 'manga.search' => [ - 'path' => '/manga/search', - 'action' => 'search', + ], + 'collection.delete' => [ + 'path' => '/collection/delete', + 'action' => 'delete', + 'verb' => 'post', + ], + // --------------------------------------------------------------------- + // Manga Collection Routes + // --------------------------------------------------------------------- + // --------------------------------------------------------------------- + // Other Routes + // --------------------------------------------------------------------- + 'character' => [ + 'path' => '/character/{slug}', + 'action' => 'index', + 'params' => [], + 'tokens' => [ + 'slug' => '[a-z0-9\-]+' + ] + ], + 'user_info' => [ + 'path' => '/me', + 'action' => 'me', + 'controller' => 'me', + 'verb' => 'get', + ], + // --------------------------------------------------------------------- + // Default / Shared routes + // --------------------------------------------------------------------- + 'cache_purge' => [ + 'path' => '/cache_purge', + 'action' => 'clearCache', + 'controller' => DEFAULT_CONTROLLER_NAMESPACE, + 'verb' => 'get', + ], + 'login' => [ + 'path' => '/login', + 'action' => 'login', + 'controller' => DEFAULT_CONTROLLER_NAMESPACE, + 'verb' => 'get', + ], + 'login.post' => [ + 'path' => '/login', + 'action' => 'loginAction', + 'controller' => DEFAULT_CONTROLLER_NAMESPACE, + 'verb' => 'post', + ], + 'logout' => [ + 'path' => '/logout', + 'action' => 'logout', + 'controller' => DEFAULT_CONTROLLER_NAMESPACE, + ], + 'update' => [ + 'path' => '/{controller}/update', + 'action' => 'update', + 'verb' => 'post', + 'tokens' => [ + 'controller' => '[a-z_]+', ], - 'manga.add.get' => [ - 'path' => '/manga/add', - 'action' => 'addForm', - 'verb' => 'get', + ], + 'update.post' => [ + 'path' => '/{controller}/update_form', + 'action' => 'formUpdate', + 'verb' => 'post', + 'tokens' => [ + 'controller' => '[a-z_]+', ], - 'manga.add.post' => [ - 'path' => '/manga/add', - 'action' => 'add', - 'verb' => 'post', + ], + 'edit' => [ + 'path' => '/{controller}/edit/{id}/{status}', + 'action' => 'edit', + 'tokens' => [ + 'id' => '[0-9a-z_]+', + 'status' => '([a-zA-Z\-_]|%20)+', ], - 'manga.delete' => [ - 'path' => '/manga/delete', - 'action' => 'delete', - 'verb' => 'post', - ], - 'manga.details' => [ - 'path' => '/manga/details/{id}', - 'action' => 'details', - 'tokens' => [ - 'id' => '[a-z0-9\-]+', - ], - ], - // --------------------------------------------------------------------- - // Anime Collection Routes - // --------------------------------------------------------------------- - 'collection.search' => [ - 'path' => '/collection/search', - 'action' => 'search', - ], - 'collection.add.get' => [ - 'path' => '/collection/add', - 'action' => 'form', - 'params' => [], - ], - 'collection.edit.get' => [ - 'path' => '/collection/edit/{id}', - 'action' => 'form', - 'tokens' => [ - 'id' => '[0-9]+', - ], - ], - 'collection.add.post' => [ - 'path' => '/collection/add', - 'action' => 'add', - 'verb' => 'post', - ], - 'collection.edit.post' => [ - 'path' => '/collection/edit', - 'action' => 'edit', - 'verb' => 'post', - ], - 'collection.view' => [ - 'path' => '/collection/view{/view}', - 'action' => 'index', - 'params' => [], - 'tokens' => [ - 'view' => '[a-z_]+', - ], - ], - 'collection.delete' => [ - 'path' => '/collection/delete', - 'action' => 'delete', - 'verb' => 'post', - ], - // --------------------------------------------------------------------- - // Manga Collection Routes - // --------------------------------------------------------------------- - // --------------------------------------------------------------------- - // Other Routes - // --------------------------------------------------------------------- - 'character' => [ - 'path' => '/character/{slug}', - 'action' => 'index', - 'params' => [], - 'tokens' => [ - 'slug' => '[a-z0-9\-]+' - ] - ], - 'user_info' => [ - 'path' => '/me', - 'action' => 'me', - 'controller' => 'me', - 'verb' => 'get', - ], - // --------------------------------------------------------------------- - // Default / Shared routes - // --------------------------------------------------------------------- - 'cache_purge' => [ - 'path' => '/cache_purge', - 'action' => 'clearCache', - 'controller' => DEFAULT_CONTROLLER_NAMESPACE, - 'verb' => 'get', - ], - 'login' => [ - 'path' => '/login', - 'action' => 'login', - 'controller' => DEFAULT_CONTROLLER_NAMESPACE, - 'verb' => 'get', - ], - 'login.post' => [ - 'path' => '/login', - 'action' => 'loginAction', - 'controller' => DEFAULT_CONTROLLER_NAMESPACE, - 'verb' => 'post', - ], - 'logout' => [ - 'path' => '/logout', - 'action' => 'logout', - 'controller' => DEFAULT_CONTROLLER_NAMESPACE, - ], - 'update' => [ - 'path' => '/{controller}/update', - 'action' => 'update', - 'verb' => 'post', - 'tokens' => [ - 'controller' => '[a-z_]+', - ], - ], - 'update.post' => [ - 'path' => '/{controller}/update_form', - 'action' => 'formUpdate', - 'verb' => 'post', - 'tokens' => [ - 'controller' => '[a-z_]+', - ], - ], - 'edit' => [ - 'path' => '/{controller}/edit/{id}/{status}', - 'action' => 'edit', - 'tokens' => [ - 'id' => '[0-9a-z_]+', - 'status' => '([a-zA-Z\-_]|%20)+', - ], - ], - 'list' => [ - 'path' => '/{controller}/{type}{/view}', - 'action' => DEFAULT_CONTROLLER_METHOD, - 'tokens' => [ - 'type' => '[a-z_]+', - 'view' => '[a-z_]+', - ], - ], - 'index_redirect' => [ - 'path' => '/', - 'controller' => DEFAULT_CONTROLLER_NAMESPACE, - 'action' => 'redirectToDefaultRoute', + ], + 'list' => [ + 'path' => '/{controller}/{type}{/view}', + 'action' => DEFAULT_CONTROLLER_METHOD, + 'tokens' => [ + 'type' => '[a-z_]+', + 'view' => '[a-z_]+', ], ], + 'index_redirect' => [ + 'path' => '/', + 'controller' => DEFAULT_CONTROLLER_NAMESPACE, + 'action' => 'redirectToDefaultRoute', + ], ]; \ No newline at end of file diff --git a/app/views/anime/list.php b/app/views/anime/list.php index a9ff92b6..f898125a 100644 --- a/app/views/anime/list.php +++ b/app/views/anime/list.php @@ -30,7 +30,11 @@ isAuthenticated()): ?> - ">Edit + Edit diff --git a/app/views/collection/cover.php b/app/views/collection/cover.php index 996d246a..c6b936e9 100644 --- a/app/views/collection/cover.php +++ b/app/views/collection/cover.php @@ -21,8 +21,11 @@
isAuthenticated()): ?>
- ">Edit - ">Delete */ ?> + + Edit +
diff --git a/app/views/collection/list.php b/app/views/collection/list.php index 0bb4daee..9aadb119 100644 --- a/app/views/collection/list.php +++ b/app/views/collection/list.php @@ -1,6 +1,6 @@
isAuthenticated()): ?> -Add Item +Add Item

There's nothing here!

@@ -26,12 +26,11 @@ isAuthenticated()): ?> - ">Edit - fullUrl("collection/delete/{$item['hummingbird_id']}") ?>">Delete*/ ?> + Edit - + " . $item['alternate_title'] . "" : "" ?> diff --git a/app/views/main-menu.php b/app/views/main-menu.php index d37bae3b..249c1eb9 100644 --- a/app/views/main-menu.php +++ b/app/views/main-menu.php @@ -6,11 +6,11 @@ get('whose_list') ?>'s List get("show_{$url_type}_collection")): ?> - [ Collection] + [ Collection] [ List] - + get('whose_list') ?>'s Collection [Anime List] diff --git a/app/views/manga/cover.php b/app/views/manga/cover.php index 80d052b5..fc4fa9ad 100644 --- a/app/views/manga/cover.php +++ b/app/views/manga/cover.php @@ -1,6 +1,6 @@
isAuthenticated()): ?> -Add Item +Add Item

There's nothing here!

@@ -14,6 +14,7 @@ isAuthenticated()): ?> @@ -29,7 +30,15 @@ isAuthenticated()): ?> diff --git a/app/views/manga/list.php b/app/views/manga/list.php index b373dfb0..8edbc7db 100644 --- a/app/views/manga/list.php +++ b/app/views/manga/list.php @@ -1,6 +1,6 @@
isAuthenticated()): ?> -Add Item +Add Item

There's nothing here!

@@ -27,7 +27,11 @@ isAuthenticated()): ?> - ">Edit + Edit diff --git a/src/Controller.php b/src/Controller.php index 48d11e4a..2886feb7 100644 --- a/src/Controller.php +++ b/src/Controller.php @@ -55,6 +55,7 @@ class Controller { 'config' => $this->config ]); + $this->url = $auraUrlGenerator; $this->urlGenerator = $urlGenerator; $session = $container->get('session'); @@ -133,7 +134,7 @@ class Controller { } $this->setFlashMessage('Invalid username or password.'); - $this->redirect($this->urlGenerator->url('login'), 303); + $this->redirect($this->url->generate('login'), 303); } /** diff --git a/src/Controller/Anime.php b/src/Controller/Anime.php index 514a039c..48fa800d 100644 --- a/src/Controller/Anime.php +++ b/src/Controller/Anime.php @@ -116,7 +116,7 @@ class Anime extends BaseController { $this->config->get('whose_list') . "'s Anime List", 'Add' ), - 'action_url' => $this->urlGenerator->url('anime/add'), + 'action_url' => $this->url->generate('anime.add.post'), 'status_list' => AnimeWatchingStatus::KITSU_TO_TITLE ]); } @@ -168,8 +168,9 @@ class Anime extends BaseController { ), 'item' => $item, 'statuses' => AnimeWatchingStatus::KITSU_TO_TITLE, - 'action' => $this->container->get('url-generator') - ->url('/anime/update_form'), + 'action' => $this->url->generate('update.post', [ + 'controller' => 'anime' + ]), ]); } diff --git a/src/Controller/Collection.php b/src/Controller/Collection.php index 14c43993..e8d229a9 100644 --- a/src/Controller/Collection.php +++ b/src/Controller/Collection.php @@ -41,18 +41,6 @@ class Collection extends BaseController { */ private $animeModel; - /** - * Data to be sent to all routes in this controller - * @var array $baseData - */ - protected $baseData; - - /** - * Url Generator class - * @var UrlGenerator - */ - protected $urlGenerator; - /** * Constructor * @@ -62,7 +50,6 @@ class Collection extends BaseController { { parent::__construct($container); - $this->urlGenerator = $container->get('url-generator'); $this->animeModel = $container->get('anime-model'); $this->animeCollectionModel = $container->get('anime-collection-model'); $this->baseData = array_merge($this->baseData, [ @@ -118,10 +105,11 @@ class Collection extends BaseController { $this->setSessionRedirect(); $action = (is_null($id)) ? "Add" : "Edit"; + $urlAction = strtolower($action); - $this->outputHTML('collection/' . strtolower($action), [ + $this->outputHTML('collection/' . $urlAction, [ 'action' => $action, - 'action_url' => $this->urlGenerator->fullUrl('collection/' . strtolower($action)), + 'action_url' => $this->url->generate("collection.{$urlAction}.post"), 'title' => $this->formatTitle( $this->config->get('whose_list') . "'s Anime Collection", $action diff --git a/src/Controller/Manga.php b/src/Controller/Manga.php index eb5b9f99..6e490c12 100644 --- a/src/Controller/Manga.php +++ b/src/Controller/Manga.php @@ -106,7 +106,7 @@ class Manga extends Controller { $this->config->get('whose_list') . "'s Manga List", 'Add' ), - 'action_url' => $this->urlGenerator->url('manga/add'), + 'action_url' => $this->url->generate('manga.add.post'), 'status_list' => $statuses ]); } @@ -159,8 +159,9 @@ class Manga extends Controller { 'title' => $title, 'status_list' => MangaReadingStatus::KITSU_TO_TITLE, 'item' => $item, - 'action' => $this->container->get('url-generator') - ->url('/manga/update_form'), + 'action' => $this->url->generate('update.post', [ + 'controller' => 'manga' + ]), ]); } diff --git a/src/ControllerTrait.php b/src/ControllerTrait.php index 86474a3d..fa44caaa 100644 --- a/src/ControllerTrait.php +++ b/src/ControllerTrait.php @@ -65,6 +65,12 @@ trait ControllerTrait { */ protected $urlGenerator; + /** + * Aura url generator + * @var \Aura\Router\Generator + */ + protected $url; + /** * Session segment * @var \Aura\Session\Segment diff --git a/src/MenuGenerator.php b/src/MenuGenerator.php index ec5d9639..d6615d86 100644 --- a/src/MenuGenerator.php +++ b/src/MenuGenerator.php @@ -16,10 +16,7 @@ namespace Aviat\AnimeClient; -use Aviat\Ion\ -{ - ArrayWrapper, StringWrapper -}; +use Aviat\Ion\{ArrayWrapper, StringWrapper}; use Aviat\Ion\Di\ContainerInterface; /** diff --git a/src/RoutingBase.php b/src/RoutingBase.php index 9103a07d..b6269b48 100644 --- a/src/RoutingBase.php +++ b/src/RoutingBase.php @@ -59,9 +59,8 @@ class RoutingBase { { $this->container = $container; $this->config = $container->get('config'); - $baseRoutes = $this->config->get('routes'); - $this->routes = $baseRoutes['routes']; - $this->routeConfig = $baseRoutes['route_config']; + $this->routes = $this->config->get('routes'); + $this->routeConfig = $this->config->get('route_config'); } /** diff --git a/src/UrlGenerator.php b/src/UrlGenerator.php index 741b5e2b..b26399b7 100644 --- a/src/UrlGenerator.php +++ b/src/UrlGenerator.php @@ -108,32 +108,5 @@ class UrlGenerator extends RoutingBase { throw new InvalidArgumentException("Invalid default type: '{$type}'"); } - - /** - * Generate full url path from the route path based on config - * - * @param string $path - (optional) The route path - * @param string $type - (optional) The controller (anime or manga), defaults to anime - * @return string - */ - public function fullUrl(string $path = "", string $type = "anime"): string - { - $configDefaultRoute = $this->__get("default_{$type}_path"); - - // Remove beginning/trailing slashes - $path = trim($path, '/'); - - // Set the default view - if ($path === '') - { - $path .= trim($configDefaultRoute, '/'); - if ($this->__get('default_to_list_view')) - { - $path .= '/list'; - } - } - - return $this->url($path); - } } // End of UrlGenerator.php \ No newline at end of file diff --git a/tests/AnimeClientTestCase.php b/tests/AnimeClientTestCase.php index e7f8bc19..d6040b1d 100644 --- a/tests/AnimeClientTestCase.php +++ b/tests/AnimeClientTestCase.php @@ -95,13 +95,11 @@ class AnimeClientTestCase extends TestCase { 'file' => ':memory:', ] ], + 'route_config' => [ + 'asset_path' => '/assets' + ], 'routes' => [ - 'route_config' => [ - 'asset_path' => '/assets' - ], - 'routes' => [ - ] ], 'mal' => [ 'username' => 'foo', diff --git a/tests/DispatcherTest.php b/tests/DispatcherTest.php index bf3dff2b..e2613a45 100644 --- a/tests/DispatcherTest.php +++ b/tests/DispatcherTest.php @@ -72,39 +72,37 @@ class DispatcherTest extends AnimeClientTestCase { { $defaultConfig = [ 'routes' => [ - 'routes' => [ - 'login_form' => [ - 'path' => '/login', - 'action' => 'login', - 'verb' => 'get' - ], - 'watching' => [ - 'path' => '/anime/watching{/view}', - 'action' => 'anime_list', - 'params' => [ - 'type' => 'currently-watching', - ], - 'tokens' => [ - 'view' => '[a-z_]+' - ] - ], - 'plan_to_read' => [ - 'path' => '/manga/plan_to_read{/view}', - 'action' => 'manga_list', - 'params' => [ - 'type' => 'Plan to Read', - ], - 'tokens' => [ - 'view' => '[a-z_]+' - ] - ], + 'login_form' => [ + 'path' => '/login', + 'action' => 'login', + 'verb' => 'get' + ], + 'watching' => [ + 'path' => '/anime/watching{/view}', + 'action' => 'anime_list', + 'params' => [ + 'type' => 'currently-watching', + ], + 'tokens' => [ + 'view' => '[a-z_]+' + ] + ], + 'plan_to_read' => [ + 'path' => '/manga/plan_to_read{/view}', + 'action' => 'manga_list', + 'params' => [ + 'type' => 'Plan to Read', + ], + 'tokens' => [ + 'view' => '[a-z_]+' + ] ], - 'route_config' => [ - 'anime_path' => 'anime', - 'manga_path' => 'manga', - 'default_list' => 'anime' - ] ], + 'route_config' => [ + 'anime_path' => 'anime', + 'manga_path' => 'manga', + 'default_list' => 'anime' + ] ]; $data = [ @@ -134,8 +132,8 @@ class DispatcherTest extends AnimeClientTestCase { ] ]; - $data['manga_default_routing_anime']['config']['routes']['route_config']['default_list'] = 'manga'; - $data['manga_default_routing_manga']['config']['routes']['route_config']['default_list'] = 'manga'; + $data['manga_default_routing_anime']['config']['route_config']['default_list'] = 'manga'; + $data['manga_default_routing_manga']['config']['route_config']['default_list'] = 'manga'; return $data; } @@ -169,36 +167,34 @@ class DispatcherTest extends AnimeClientTestCase { public function testDefaultRoute() { $config = [ + 'route_config' => [ + 'anime_path' => 'anime', + 'manga_path' => 'manga', + 'default_anime_list_path' => "watching", + 'default_manga_list_path' => 'all', + 'default_list' => 'manga' + ], 'routes' => [ - 'route_config' => [ - 'anime_path' => 'anime', - 'manga_path' => 'manga', - 'default_anime_list_path' => "watching", - 'default_manga_list_path' => 'all', - 'default_list' => 'manga' + 'login_form' => [ + 'path' => '/login', + 'action' => ['login'], + 'verb' => 'get' ], - 'routes' => [ - 'login_form' => [ - 'path' => '/login', - 'action' => ['login'], - 'verb' => 'get' - ], - 'index' => [ - 'path' => '/', - 'action' => ['redirect'], - 'params' => [ - 'url' => '', // Determined by config - 'code' => '301' - ] - ], - 'index' => [ - 'path' => '/', - 'action' => ['redirect'], - 'params' => [ - 'url' => '', // Determined by config - 'code' => '301', - 'type' => 'manga' - ] + 'index' => [ + 'path' => '/', + 'action' => ['redirect'], + 'params' => [ + 'url' => '', // Determined by config + 'code' => '301' + ] + ], + 'index' => [ + 'path' => '/', + 'action' => ['redirect'], + 'params' => [ + 'url' => '', // Determined by config + 'code' => '301', + 'type' => 'manga' ] ] ] @@ -218,17 +214,15 @@ class DispatcherTest extends AnimeClientTestCase { 'controller_list_sanity_check' => [ 'config' => [ 'routes' => [ - 'routes' => [ - ], - 'route_config' => [ - 'anime_path' => 'anime', - 'manga_path' => 'manga', - 'default_anime_list_path' => "watching", - 'default_manga_list_path' => 'all', - 'default_list' => 'manga' - ], - ] + ], + 'route_config' => [ + 'anime_path' => 'anime', + 'manga_path' => 'manga', + 'default_anime_list_path' => "watching", + 'default_manga_list_path' => 'all', + 'default_list' => 'manga' + ], ], 'expected' => [ 'anime' => 'Aviat\AnimeClient\Controller\Anime', @@ -240,17 +234,15 @@ class DispatcherTest extends AnimeClientTestCase { 'empty_controller_list' => [ 'config' => [ 'routes' => [ - 'routes' => [ - ], - 'route_config' => [ - 'anime_path' => 'anime', - 'manga_path' => 'manga', - 'default_anime_path' => "/anime/watching", - 'default_manga_path' => '/manga/all', - 'default_list' => 'manga' - ], - ] + ], + 'route_config' => [ + 'anime_path' => 'anime', + 'manga_path' => 'manga', + 'default_anime_path' => "/anime/watching", + 'default_manga_path' => '/manga/all', + 'default_list' => 'manga' + ], ], 'expected' => [ 'anime' => 'Aviat\AnimeClient\Controller\Anime', diff --git a/tests/UrlGeneratorTest.php b/tests/UrlGeneratorTest.php index e7de3488..48d91cfd 100644 --- a/tests/UrlGeneratorTest.php +++ b/tests/UrlGeneratorTest.php @@ -49,60 +49,4 @@ class UrlGeneratorTest extends AnimeClientTestCase { $result = $urlGenerator->assetUrl(...$args); $this->assertEquals($expected, $result); } - - public function dataFullUrl() - { - return [ - 'default_view' => [ - 'config' => [ - 'routes' => [ - 'routes' => [], - 'route_config' => [ - 'anime_path' => 'anime', - 'manga_path' => 'manga', - 'default_list' => 'manga', - 'default_anime_path' => '/anime/watching', - 'default_manga_path' => '/manga/all', - 'default_to_list_view' => FALSE, - ] - ], - ], - 'path' => '', - 'type' => 'manga', - 'expected' => '//localhost/manga/all', - ], - 'default_view_list' => [ - 'config' => [ - 'routes' => [ - 'routes' => [], - 'route_config' => [ - 'anime_path' => 'anime', - 'manga_path' => 'manga', - 'default_list' => 'manga', - 'default_anime_path' => '/anime/watching', - 'default_manga_path' => '/manga/all', - 'default_to_list_view' => TRUE, - ] - ], - ], - 'path' => '', - 'type' => 'manga', - 'expected' => '//localhost/manga/all/list', - ] - ]; - } - - /** - * @dataProvider dataFullUrl - */ - public function testFullUrl($config, $path, $type, $expected) - { - $config = new Config($config); - $this->container->setInstance('config', $config); - $urlGenerator = new UrlGenerator($this->container); - - $result = $urlGenerator->fullUrl($path, $type); - - $this->assertEquals($expected, $result); - } } \ No newline at end of file