From dfe0b3a6cf2f79ca8438815a3bf0ae9035e5052f Mon Sep 17 00:00:00 2001 From: Timothy J Warren Date: Mon, 14 Sep 2015 15:49:20 -0400 Subject: [PATCH] Pass the tests! --- README.md | 2 +- app/bootstrap.php | 2 + app/config/base_config.php | 4 +- app/config/config.php | 48 ++--------- app/config/minify_config.php | 6 +- app/config/routing.php | 24 ++++++ src/Base/Config.php | 107 ------------------------ src/Base/Container.php | 2 +- src/Base/Controller.php | 17 +++- src/Base/Router.php | 42 ++-------- src/Base/RoutingBase.php | 52 ++++++++++++ src/Base/UrlGenerator.php | 49 +++-------- src/Controller/Anime.php | 80 +----------------- src/Controller/Collection.php | 6 +- src/Controller/Manga.php | 4 +- src/views/collection/cover.php | 4 +- src/views/header.php | 18 ++-- tests/Base/ConfigTest.php | 116 +------------------------ tests/Base/RouterTest.php | 112 ++++++++++++++----------- tests/Base/UrlGeneratorTest.php | 144 ++++++++++++++++++++++++++++++++ 20 files changed, 348 insertions(+), 491 deletions(-) create mode 100644 app/config/routing.php create mode 100644 src/Base/RoutingBase.php create mode 100644 tests/Base/UrlGeneratorTest.php diff --git a/README.md b/README.md index bdeed7b1..4f8c5677 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ A self-hosted client that allows custom formatting of data from the hummingbird 1. Install dependencies via composer: `composer install` 2. Change the `WHOSE` constant declaration in `index.php` to your name -3. Configure settings in `app/config/config.php` to your liking +3. Configure settings in `app/config/config.php` and `app/config/routing.php` to your liking 4. Create the following directories if they don't exist, and make sure they are world writable * app/cache * public/images/manga diff --git a/app/bootstrap.php b/app/bootstrap.php index e9f621ae..82f615bc 100644 --- a/app/bootstrap.php +++ b/app/bootstrap.php @@ -64,6 +64,8 @@ $container->set('response', $web_factory->newResponse()); // ----------------------------------------------------------------------------- // Router // ----------------------------------------------------------------------------- +$container->set('url-generator', new Base\UrlGenerator($container)); + $router = new Base\Router($container); $router->dispatch(); diff --git a/app/config/base_config.php b/app/config/base_config.php index dbe4ad4f..47346dfa 100644 --- a/app/config/base_config.php +++ b/app/config/base_config.php @@ -10,6 +10,6 @@ $base_config = [ 'img_cache_path' => _dir(ROOT_DIR, 'public/images'), // Included config files - 'routes' => require _dir(CONF_DIR, 'routes.php'), - 'database' => require _dir(CONF_DIR, 'database.php'), + 'routes' => require __DIR__ . '/routes.php', + 'database' => require __DIR__ . '/database.php', ]; \ No newline at end of file diff --git a/app/config/config.php b/app/config/config.php index 040982c9..198199fa 100644 --- a/app/config/config.php +++ b/app/config/config.php @@ -1,4 +1,4 @@ - TRUE, // path to public directory on the server - 'asset_dir' => __DIR__ . '/../../public', + 'asset_dir' => realpath(__DIR__ . '/../../public'), // ---------------------------------------------------------------------------- - // Routing + // Included config files // ---------------------------------------------------------------------------- - 'routing' => [ - // Subfolder prefix for url - 'subfolder_prefix' => '', - - // Path to public directory, where images/css/javascript are located, - // appended to the url - 'asset_path' => '/public', - - // Url paths to each content type - 'anime_path' => 'anime', - 'manga_path' => 'manga', - 'collection_path' => 'collection', - 'stats_path' => 'stats', - - // Which list should be the default? - 'default_list' => 'anime', // anime or manga - - // Default pages for anime/manga - 'default_anime_path' => "/anime/watching", - 'default_manga_path' => '/manga/all', - - // Default to list view? - 'default_to_list_view' => FALSE, - ], - - // Url paths to each - 'anime_path' => 'anime', - 'manga_path' => 'manga', - 'collection_path' => 'collection', - 'stats_path' => 'stats', - - // Which list should be the default? - 'default_list' => 'anime', // anime or manga - - // Default pages for anime/manga - 'default_anime_path' => "/anime/watching", - 'default_manga_path' => '/manga/all', - - // Default to list view? - 'default_to_list_view' => FALSE, + // Routing paths and options + 'routing' => require __DIR__ . '/routing.php', ]; \ No newline at end of file diff --git a/app/config/minify_config.php b/app/config/minify_config.php index c28bbdd2..0a93a533 100644 --- a/app/config/minify_config.php +++ b/app/config/minify_config.php @@ -14,8 +14,6 @@ /* $config = */require 'config.php'; -$config = (object)$config; - // Should we use myth to preprocess? $use_myth = FALSE; @@ -27,7 +25,7 @@ $use_myth = FALSE; | The folder where css files exist, in relation to the document root | */ -$css_root = $config->asset_dir. '/css/'; +$css_root = $config['asset_dir'] . '/css/'; /* |-------------------------------------------------------------------------- @@ -57,4 +55,4 @@ $path_to = ''; | The folder where javascript files exist, in relation to the document root | */ -$js_root = $config->asset_dir. '/js/'; \ No newline at end of file +$js_root = $config['asset_dir'] . '/js/'; \ No newline at end of file diff --git a/app/config/routing.php b/app/config/routing.php new file mode 100644 index 00000000..b1626e00 --- /dev/null +++ b/app/config/routing.php @@ -0,0 +1,24 @@ + '', + + // 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_path' => "/anime/watching", + 'default_manga_path' => '/manga/all', + + // Default to list view? + 'default_to_list_view' => FALSE, +]; \ No newline at end of file diff --git a/src/Base/Config.php b/src/Base/Config.php index fc5c3654..5f9a0bc6 100644 --- a/src/Base/Config.php +++ b/src/Base/Config.php @@ -53,112 +53,5 @@ class Config { return NULL; } - - /** - * Get the base url for css/js/images - * - * @return string - */ - public function asset_url(/*...*/) - { - $args = func_get_args(); - $base_url = rtrim($this->url(""), '/'); - - $routing_config = $this->__get("routing"); - - - $base_url = "{$base_url}" . $routing_config['asset_path']; - - array_unshift($args, $base_url); - - return implode("/", $args); - } - - /** - * Get the base url from the config - * - * @param string $type - (optional) The controller - * @return string - */ - public function base_url($type="anime") - { - $config_path = trim($this->__get("{$type}_path"), "/"); - - // Set the appropriate HTTP host - $host = $_SERVER['HTTP_HOST']; - $path = ($config_path !== '') ? $config_path : ""; - - return implode("/", ['/', $host, $path]); - } - - /** - * Generate a proper url from the path - * - * @param string $path - * @return string - */ - public function url($path) - { - $path = trim($path, '/'); - - // Remove any optional parameters from the route - $path = preg_replace('`{/.*?}`i', '', $path); - - // Set the appropriate HTTP host - $host = $_SERVER['HTTP_HOST']; - - return "//{$host}/{$path}"; - } - - public function default_url($type) - { - $type = trim($type); - $default_path = $this->__get("default_{$type}_path"); - - if ( ! is_null($default_path)) - { - return $this->url($default_path); - } - - return ""; - } - - /** - * 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 full_url($path="", $type="anime") - { - $config_path = trim($this->__get("{$type}_path"), "/"); - $config_default_route = $this->__get("default_{$type}_path"); - - // Remove beginning/trailing slashes - $config_path = trim($config_path, '/'); - $path = trim($path, '/'); - - // Remove any optional parameters from the route - $path = preg_replace('`{/.*?}`i', '', $path); - - // Set the appropriate HTTP host - $host = $_SERVER['HTTP_HOST']; - - // Set the default view - if ($path === '') - { - $path .= trim($config_default_route, '/'); - if ($this->__get('default_to_list_view')) $path .= '/list'; - } - - // Set an leading folder - /*if ($config_path !== '') - { - $path = "{$config_path}/{$path}"; - }*/ - - return "//{$host}/{$path}"; - } } // End of config.php \ No newline at end of file diff --git a/src/Base/Container.php b/src/Base/Container.php index e267a472..4ce404f9 100644 --- a/src/Base/Container.php +++ b/src/Base/Container.php @@ -1,6 +1,6 @@ config = $container->get('config'); $this->base_data['config'] = $this->config; + $this->base_data['urlGenerator'] = $container->get('url-generator'); $this->request = $container->get('request'); $this->response = $container->get('response'); + + $this->urlGenerator = $container->get('url-generator'); } /** @@ -169,7 +178,7 @@ class Controller { */ public function redirect($url, $code, $type="anime") { - $url = $this->config->full_url($url, $type); + $url = $this->urlGenerator->full_url($url, $type); $this->response->redirect->to($url, $code); } @@ -199,7 +208,7 @@ class Controller { public function logout() { session_destroy(); - $this->response->redirect->seeOther($this->config->full_url('')); + $this->response->redirect->seeOther($this->urlGenerator->full_url('')); } /** @@ -238,7 +247,7 @@ class Controller { ) ) { - $this->response->redirect->afterPost($this->config->full_url('', $this->base_data['url_type'])); + $this->response->redirect->afterPost($this->urlGenerator->full_url('', $this->base_data['url_type'])); return; } diff --git a/src/Base/Router.php b/src/Base/Router.php index 9c7be4bb..cd46b811 100644 --- a/src/Base/Router.php +++ b/src/Base/Router.php @@ -10,7 +10,7 @@ use \Aura\Web\Response; /** * Basic routing/ dispatch */ -class Router { +class Router extends RoutingBase { /** * The route-matching object @@ -18,12 +18,6 @@ class Router { */ protected $router; - /** - * The global configuration object - * @var object $config - */ - protected $config; - /** * Class wrapper for input superglobals * @var object @@ -42,12 +36,6 @@ class Router { */ protected $output_routes; - /** - * Injection Container - * @var Container $container - */ - protected $container; - /** * Constructor * @@ -58,14 +46,12 @@ class Router { */ public function __construct(Container $container) { - $this->config = $container->get('config'); + parent::__construct($container); $this->router = $container->get('aura-router'); $this->request = $container->get('request'); $this->web = [$this->request, $container->get('response')]; $this->output_routes = $this->_setup_routes(); - - $this->container = $container; } /** @@ -141,7 +127,6 @@ class Router { $controller = new $controller_name($this->container); // Run the appropriate controller method - $error_handler->addDataTable('controller_args', $params); call_user_func_array([$controller, $action_method], $params); } @@ -153,29 +138,20 @@ class Router { */ public function get_controller() { - $route_type = $this->config->default_list; + $error_handler = $this->container->get('error-handler'); + $route_type = $this->__get('default_list'); $host = $this->request->server->get("HTTP_HOST"); $request_uri = $this->request->server->get('PATH_INFO'); $path = trim($request_uri, '/'); - $route_type_map = [ - $this->config->anime_path => 'anime', - $this->config->manga_path => 'manga', - $this->config->collection_path => 'collection', - $this->config->stats_path => 'stats' - ]; - $segments = explode('/', $path); - $controller = array_shift($segments); + $controller = reset($segments); - if (array_key_exists($controller, array_keys($route_type_map))) - { - return $route_type_map[$controller]; - } + //$controller_class = '\\AnimeClient\\Controller\\' . ucfirst($controller); - return $route_type; + return $controller; } /** @@ -190,9 +166,9 @@ class Router { $route_type = $this->get_controller(); // Return early if invalid route array - if ( ! array_key_exists($route_type, $this->config->routes)) return []; + if ( ! array_key_exists($route_type, $this->routes)) return []; - $applied_routes = array_merge($this->config->routes[$route_type], $this->config->routes['common']); + $applied_routes = array_merge($this->routes[$route_type], $this->routes['common']); // Add routes foreach($applied_routes as $name => &$route) diff --git a/src/Base/RoutingBase.php b/src/Base/RoutingBase.php new file mode 100644 index 00000000..1887ae30 --- /dev/null +++ b/src/Base/RoutingBase.php @@ -0,0 +1,52 @@ +container = $container; + $this->config = $container->get('config'); + $this->routes = $this->config->__get('routes'); + } + + /** + * Retreive the appropriate value for the routing key + * + * @param string $key + * @return mixed + */ + public function __get($key) + { + $routing_config = $this->config->routing; + + if (array_key_exists($key, $routing_config)) + { + return $routing_config[$key]; + } + } +} +// End of RoutingBase.php \ No newline at end of file diff --git a/src/Base/UrlGenerator.php b/src/Base/UrlGenerator.php index a7ccdacd..0d7aac9d 100644 --- a/src/Base/UrlGenerator.php +++ b/src/Base/UrlGenerator.php @@ -5,44 +5,7 @@ namespace AnimeClient\Base; /** * UrlGenerator class. */ -class UrlGenerator { - - /** - * Config Object - * @var Config - */ - protected $config; - - /** - * Constructor - * - * @param Container $container - */ - public function __construct(Container $container) - { - $this->config = $container->get('config'); - } - - /** - * Retreive the appropriate value for the routing key - * - * @param string $key - * @return mixed - */ - protected function __get($key) - { - $routing_config = $this->config->__get('routing'); - - if (array_key_exists($key, $routing_config)) - { - return $routing_config[$key]; - } - } - - public function __invoke() - { - $args = func_get_args(); - } +class UrlGenerator extends RoutingBase { /** * Get the base url for css/js/images @@ -52,7 +15,9 @@ class UrlGenerator { public function asset_url(/*...*/) { $args = func_get_args(); - $base_url = rtrim($this->__get('asset_path'), '/'); + $base_url = rtrim($this->url(""), '/'); + + $base_url = "{$base_url}" . $this->__get("asset_path"); array_unshift($args, $base_url); @@ -95,6 +60,12 @@ class UrlGenerator { return "//{$host}/{$path}"; } + /** + * Full default path for the list pages + * + * @param string $type + * @return string + */ public function default_url($type) { $type = trim($type); diff --git a/src/Controller/Anime.php b/src/Controller/Anime.php index ac2a261b..8cdef827 100644 --- a/src/Controller/Anime.php +++ b/src/Controller/Anime.php @@ -64,13 +64,13 @@ class Anime extends BaseController { $this->model = new AnimeModel($container); $this->collection_model = new AnimeCollectionModel($container); - $this->base_data = [ + $this->base_data = array_merge($this->base_data, [ 'message' => '', 'url_type' => 'anime', 'other_type' => 'manga', 'nav_routes' => $this->nav_routes, 'config' => $this->config, - ]; + ]); } /** @@ -108,82 +108,6 @@ class Anime extends BaseController { ]); } - /** - * Show the anime collection page - * - * @return void - */ - public function collection($view) - { - $view_map = [ - '' => 'collection', - 'list' => 'collection_list' - ]; - - $data = $this->collection_model->get_collection(); - - $this->outputHTML('anime/' . $view_map[$view], [ - 'title' => WHOSE . " Anime Collection", - 'sections' => $data, - 'genres' => $this->collection_model->get_genre_list() - ]); - } - - /** - * Show the anime collection add/edit form - * - * @param int $id - * @return void - */ - public function collection_form($id=NULL) - { - $action = (is_null($id)) ? "Add" : "Edit"; - - $this->outputHTML('anime/collection_' . strtolower($action), [ - 'action' => $action, - 'action_url' => $this->config->full_url("collection/" . strtolower($action)), - 'title' => WHOSE . " Anime Collection · {$action}", - 'media_items' => $this->collection_model->get_media_type_list(), - 'item' => ($action === "Edit") ? $this->collection_model->get($id) : [] - ]); - } - - /** - * Update a collection item - * - * @return void - */ - public function collection_edit() - { - $data = $this->request->post->get(); - if ( ! array_key_exists('hummingbird_id', $data)) - { - $this->redirect("collection/view", 303, "anime"); - } - - $this->collection_model->update($data); - - $this->redirect("collection/view", 303, "anime"); - } - - /** - * Add a collection item - * - * @return void - */ - public function collection_add() - { - $data = $this->request->post->get(); - if ( ! array_key_exists('id', $data)) - { - $this->redirect("collection/view", 303, "anime"); - } - - $this->collection_model->add($data); - - $this->redirect("collection/view", 303, "anime"); - } - /** * Update an anime item * diff --git a/src/Controller/Collection.php b/src/Controller/Collection.php index 2349e1df..03fdfd97 100644 --- a/src/Controller/Collection.php +++ b/src/Controller/Collection.php @@ -54,14 +54,14 @@ class Collection extends BaseController { unset($this->nav_routes['Collection']); } - $this->collection_model = new AnimeCollectionModel($this->config); - $this->base_data = [ + $this->collection_model = new AnimeCollectionModel($container); + $this->base_data = array_merge($this->base_data, [ 'message' => '', 'url_type' => 'anime', 'other_type' => 'manga', 'nav_routes' => $this->nav_routes, 'config' => $this->config, - ]; + ]); } /** diff --git a/src/Controller/Manga.php b/src/Controller/Manga.php index b669cdac..8f3709e6 100644 --- a/src/Controller/Manga.php +++ b/src/Controller/Manga.php @@ -48,12 +48,12 @@ class Manga extends Controller { parent::__construct($container); $config = $container->get('config'); $this->model = new MangaModel($container); - $this->base_data = [ + $this->base_data = array_merge($this->base_data, [ 'config' => $this->config, 'url_type' => 'manga', 'other_type' => 'anime', 'nav_routes' => $this->nav_routes - ]; + ]); } /** diff --git a/src/views/collection/cover.php b/src/views/collection/cover.php index 23b93e22..d08285f2 100644 --- a/src/views/collection/cover.php +++ b/src/views/collection/cover.php @@ -11,7 +11,7 @@
- [">Edit] + [">Edit]
diff --git a/src/views/header.php b/src/views/header.php index ee979a88..431d3e9b 100644 --- a/src/views/header.php +++ b/src/views/header.php @@ -3,24 +3,24 @@ <?= $title ?> - +

- + - [ List] + [ List] - [">Logout] + [">Logout] - ["> Login] + ["> Login]

@@ -28,14 +28,14 @@ diff --git a/tests/Base/ConfigTest.php b/tests/Base/ConfigTest.php index 2a46d654..c71a81ea 100644 --- a/tests/Base/ConfigTest.php +++ b/tests/Base/ConfigTest.php @@ -8,7 +8,7 @@ class ConfigTest extends AnimeClient_TestCase { { $this->config = new Config([ 'foo' => 'bar', - 'asset_path' => '//localhost/assets/', + 'asset_path' => '/assets', 'bar' => 'baz' ]); } @@ -24,118 +24,4 @@ class ConfigTest extends AnimeClient_TestCase { { $this->assertEquals(NULL, $this->config->foobar); } - - public function assetUrlProvider() - { - return [ - 'single argument' => [ - 'args' => [ - 'images' - ], - 'expected' => '//localhost/assets/images', - ], - 'multiple arguments' => [ - 'args' => [ - 'images', 'anime', 'foo.png' - ], - 'expected' => '//localhost/assets/images/anime/foo.png' - ] - ]; - } - - /** - * @dataProvider assetUrlProvider - */ - public function testAssetUrl($args, $expected) - { - $result = call_user_func_array([$this->config, 'asset_url'], $args); - - $this->assertEquals($expected, $result); - } - - public function dataFullUrl() - { - return [ - 'default_view' => [ - 'config' => [ - 'routing' => [ - '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' => [ - 'routing' => [ - '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) - { - $this->config = new Config($config); - - $result = $this->config->full_url($path, $type); - - $this->assertEquals($expected, $result); - } - - public function dataBaseUrl() - { - $config = [ - 'routing' => [ - 'anime_path' => 'anime', - 'manga_path' => 'manga', - 'default_list' => 'manga', - 'default_anime_path' => '/watching', - 'default_manga_path' => '/all', - 'default_to_list_view' => TRUE, - ], - ]; - - return [ - 'path_based_routing_anime' => [ - 'config' => $config, - 'type' => 'anime', - 'expected' => '//localhost/anime' - ], - 'path_based_routing_manga' => [ - 'config' => $config, - 'type' => 'manga', - 'expected' => '//localhost/manga' - ] - ]; - } - - /** - * @dataProvider dataBaseUrl - */ - public function testBaseUrl($config, $type, $expected) - { - $this->config = new Config($config); - $result = $this->config->base_url($type); - - $this->assertEquals($expected, $result); - } } \ No newline at end of file diff --git a/tests/Base/RouterTest.php b/tests/Base/RouterTest.php index 026d4b60..072c3bfb 100644 --- a/tests/Base/RouterTest.php +++ b/tests/Base/RouterTest.php @@ -3,6 +3,7 @@ use AnimeClient\Base\Router; use AnimeClient\Base\Config; use AnimeClient\Base\Container; +use AnimeClient\Base\UrlGenerator; use Aura\Web\WebFactory; use Aura\Router\RouterFactory; @@ -43,6 +44,8 @@ class RouterTest extends AnimeClient_TestCase { $this->router = new Router($this->container); $this->config = $this->container->get('config'); + $this->urlGenerator = new UrlGenerator($this->container); + $this->container->set('url-generator', $this->urlGenerator); } public function testRouterSanity() @@ -54,6 +57,41 @@ class RouterTest extends AnimeClient_TestCase { public function dataRoute() { $default_config = array( + 'routes' => [ + 'common' => [ + 'login_form' => [ + 'path' => '/login', + 'action' => ['login'], + 'verb' => 'get' + ], + ], + 'anime' => [ + 'watching' => [ + 'path' => '/anime/watching{/view}', + 'action' => ['anime_list'], + 'params' => [ + 'type' => 'currently-watching', + 'title' => WHOSE . " Anime List · Watching" + ], + 'tokens' => [ + 'view' => '[a-z_]+' + ] + ], + ], + 'manga' => [ + 'plan_to_read' => [ + 'path' => '/manga/plan_to_read{/view}', + 'action' => ['manga_list'], + 'params' => [ + 'type' => 'Plan to Read', + 'title' => WHOSE . " Manga List · Plan to Read" + ], + 'tokens' => [ + 'view' => '[a-z_]+' + ] + ], + ] + ], 'routing' => [ 'anime_path' => 'anime', 'manga_path' => 'manga', @@ -62,21 +100,34 @@ class RouterTest extends AnimeClient_TestCase { ); $data = [ - 'manga_path_routing' => array( + 'anime_default_routing_manga' => array( 'config' => $default_config, - 'type' => 'manga', + 'controller' => 'manga', 'host' => "localhost", 'uri' => "/manga/plan_to_read", ), - 'anime_path_routing' => array( + 'manga_default_routing_anime' => array( 'config' => $default_config, - 'type' => 'anime', + 'controller' => 'anime', 'host' => "localhost", 'uri' => "/anime/watching", + ), + 'anime_default_routing_anime' => array( + 'config' => $default_config, + 'controller' => 'anime', + 'host' => 'localhost', + 'uri' => '/anime/watching', + ), + 'manga_default_routing_manga' => array( + 'config' => $default_config, + 'controller' => 'manga', + 'host' => 'localhost', + 'uri' => '/manga/plan_to_read' ) ]; - $data['anime_path_routing']['config']['routing']['default_list'] = 'manga'; + $data['manga_default_routing_anime']['config']['routing']['default_list'] = 'manga'; + $data['manga_default_routing_manga']['config']['routing']['default_list'] = 'manga'; return $data; } @@ -84,52 +135,16 @@ class RouterTest extends AnimeClient_TestCase { /** * @dataProvider dataRoute */ - public function testRoute($config, $type, $host, $uri) + public function testRoute($config, $controller, $host, $uri) { - $check_var = "{$type}_path"; - $config['base_config']['routes'] = [ - 'common' => [ - 'login_form' => [ - 'path' => '/login', - 'action' => ['login'], - 'verb' => 'get' - ], - ], - 'anime' => [ - 'watching' => [ - 'path' => '/anime/watching{/view}', - 'action' => ['anime_list'], - 'params' => [ - 'type' => 'currently-watching', - 'title' => WHOSE . " Anime List · Watching" - ], - 'tokens' => [ - 'view' => '[a-z_]+' - ] - ], - ], - 'manga' => [ - 'plan_to_read' => [ - 'path' => '/manga/plan_to_read{/view}', - 'action' => ['manga_list'], - 'params' => [ - 'type' => 'Plan to Read', - 'title' => WHOSE . " Manga List · Plan to Read" - ], - 'tokens' => [ - 'view' => '[a-z_]+' - ] - ], - ] - ]; - $this->_set_up($config, $uri, $host); $request = $this->container->get('request'); $aura_router = $this->container->get('aura-router'); + // Check route setup - $this->assertEquals($config['base_config']['routes'], $this->config->routes, "Incorrect route path"); + $this->assertEquals($config['routes'], $this->config->routes, "Incorrect route path"); $this->assertTrue(is_array($this->router->get_output_routes())); // Check environment variables @@ -138,8 +153,7 @@ class RouterTest extends AnimeClient_TestCase { // Make sure the route is an anime type $this->assertTrue($aura_router->count() > 0, "0 routes"); - $this->assertTrue($this->config->$check_var !== '', "Check variable is empty"); - $this->assertEquals($type, $this->router->get_controller(), "Incorrect Route type"); + $this->assertEquals($controller, $this->router->get_controller(), "Incorrect Route type"); // Make sure the route matches, by checking that it is actually an object $route = $this->router->get_route(); @@ -152,6 +166,8 @@ class RouterTest extends AnimeClient_TestCase { 'routing' => [ 'anime_path' => 'anime', 'manga_path' => 'manga', + 'default_anime_path' => "/anime/watching", + 'default_manga_path' => '/manga/all', 'default_list' => 'manga' ], 'routes' => [ @@ -187,7 +203,7 @@ class RouterTest extends AnimeClient_TestCase { ]; $this->_set_up($config, "/", "localhost"); - //$this->assertEquals($this->config->full_url('', 'manga'), $this->response->headers->get('location')); - $this->assertEquals('//localhost/manga/', $this->config->full_url('', 'manga'), "Incorrect default url"); + $this->assertEquals('//localhost/manga/all', $this->urlGenerator->default_url('manga'), "Incorrect default url"); + $this->assertEquals('//localhost/anime/watching', $this->urlGenerator->default_url('anime'), "Incorrect default url"); } } \ No newline at end of file diff --git a/tests/Base/UrlGeneratorTest.php b/tests/Base/UrlGeneratorTest.php new file mode 100644 index 00000000..1a1a7bf3 --- /dev/null +++ b/tests/Base/UrlGeneratorTest.php @@ -0,0 +1,144 @@ +container = new Container([ + 'config' => new Config([ + 'foo' => 'bar', + 'routing' => [ + 'asset_path' => '/assets', + ], + 'bar' => 'baz' + ]) + ]); + $this->config = $this->container->get('config'); + } + + public function assetUrlProvider() + { + return [ + 'single argument' => [ + 'args' => [ + 'images' + ], + 'expected' => '//localhost/assets/images', + ], + 'multiple arguments' => [ + 'args' => [ + 'images', 'anime', 'foo.png' + ], + 'expected' => '//localhost/assets/images/anime/foo.png' + ] + ]; + } + + /** + * @dataProvider assetUrlProvider + */ + public function testAssetUrl($args, $expected) + { + $urlGenerator = new UrlGenerator($this->container); + + $result = call_user_func_array([$urlGenerator, 'asset_url'], $args); + + $this->assertEquals($expected, $result); + } + + public function dataFullUrl() + { + return [ + 'default_view' => [ + 'config' => [ + 'routing' => [ + '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' => [ + 'routing' => [ + '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->set('config', $config); + $urlGenerator = new UrlGenerator($this->container); + + $result = $urlGenerator->full_url($path, $type); + + $this->assertEquals($expected, $result); + } + + public function dataBaseUrl() + { + $config = [ + 'routing' => [ + 'anime_path' => 'anime', + 'manga_path' => 'manga', + 'default_list' => 'manga', + 'default_anime_path' => '/watching', + 'default_manga_path' => '/all', + 'default_to_list_view' => TRUE, + ], + ]; + + return [ + 'path_based_routing_anime' => [ + 'config' => $config, + 'type' => 'anime', + 'expected' => '//localhost/anime' + ], + 'path_based_routing_manga' => [ + 'config' => $config, + 'type' => 'manga', + 'expected' => '//localhost/manga' + ] + ]; + } + + /** + * @dataProvider dataBaseUrl + */ + public function testBaseUrl($config, $type, $expected) + { + $config = new Config($config); + $this->container->set('config', $config); + $urlGenerator = new UrlGenerator($this->container); + + $result = $urlGenerator->base_url($type); + + $this->assertEquals($expected, $result); + } + +} \ No newline at end of file