From def424da72ff35287c164ae3b0d60acb01e957dd Mon Sep 17 00:00:00 2001 From: Timothy J Warren Date: Tue, 20 Oct 2015 16:41:51 -0400 Subject: [PATCH] Fix default redirect and tests --- src/Aviat/AnimeClient/Controller.php | 16 ++++++---- src/Aviat/AnimeClient/Controller/Anime.php | 34 +++++++--------------- src/Aviat/AnimeClient/Controller/Manga.php | 33 +++++++-------------- src/Aviat/AnimeClient/Dispatcher.php | 6 ++++ src/Aviat/AnimeClient/UrlGenerator.php | 2 +- tests/AnimeClient/DispatcherTest.php | 12 ++++---- 6 files changed, 47 insertions(+), 56 deletions(-) diff --git a/src/Aviat/AnimeClient/Controller.php b/src/Aviat/AnimeClient/Controller.php index 2716a527..549344e8 100644 --- a/src/Aviat/AnimeClient/Controller.php +++ b/src/Aviat/AnimeClient/Controller.php @@ -75,6 +75,15 @@ class Controller { $this->urlGenerator = $urlGenerator; } + /** + * Redirect to the default controller/url from an empty path + */ + public function redirect_to_default() + { + $default_type = $this->config->get(['routing','default_list']); + $this->redirect($this->urlGenerator->default_url($default_type), 303); + } + /** * Get a class member * @@ -169,16 +178,13 @@ class Controller { /** * Redirect to the selected page * - * @param string $path + * @param string $url * @param int $code - * @param string $type * @return void */ - public function redirect($path, $code, $type = "anime") + public function redirect($url, $code) { - $url = $this->urlGenerator->full_url($path, $type); $http = new HttpView($this->container); - $http->redirect($url, $code); } } diff --git a/src/Aviat/AnimeClient/Controller/Anime.php b/src/Aviat/AnimeClient/Controller/Anime.php index 0e3005dc..ad44486c 100644 --- a/src/Aviat/AnimeClient/Controller/Anime.php +++ b/src/Aviat/AnimeClient/Controller/Anime.php @@ -54,29 +54,6 @@ class Anime extends BaseController { * @return void */ public function index($type = "watching", $view = '') - { - return $this->anime_list($type, $view); - } - - /** - * Search for anime - * - * @return void - */ - public function search() - { - $query = $this->request->query->get('query'); - $this->outputJSON($this->model->search($query)); - } - - /** - * Show a portion, or all of the anime list - * - * @param string $type - The section of the list - * @param string $view - List or cover view - * @return void - */ - protected function anime_list($type, $view) { $type_title_map = [ 'all' => 'All', @@ -114,6 +91,17 @@ class Anime extends BaseController { ]); } + /** + * Search for anime + * + * @return void + */ + public function search() + { + $query = $this->request->query->get('query'); + $this->outputJSON($this->model->search($query)); + } + /** * Update an anime item * diff --git a/src/Aviat/AnimeClient/Controller/Manga.php b/src/Aviat/AnimeClient/Controller/Manga.php index 04195b52..c3cc2c7d 100644 --- a/src/Aviat/AnimeClient/Controller/Manga.php +++ b/src/Aviat/AnimeClient/Controller/Manga.php @@ -34,6 +34,7 @@ class Manga extends Controller { public function __construct(ContainerInterface $container) { parent::__construct($container); + $this->model = new MangaModel($container); $this->base_data = array_merge($this->base_data, [ 'menu_name' => 'manga_list', @@ -51,28 +52,6 @@ class Manga extends Controller { * @return void */ public function index($status = "all", $view = "") - { - return $this->manga_list($status, $view); - } - - /** - * Update an anime item - * - * @return boolean|null - */ - public function update() - { - $this->outputJSON($this->model->update($this->request->post->get())); - } - - /** - * Get a section of the manga list - * - * @param string $status - * @param string $view - * @return void - */ - protected function manga_list($status, $view) { $map = [ 'all' => 'All', @@ -99,5 +78,15 @@ class Manga extends Controller { 'sections' => $data, ]); } + + /** + * Update an anime item + * + * @return boolean|null + */ + public function update() + { + $this->outputJSON($this->model->update($this->request->post->get())); + } } // End of MangaController.php \ No newline at end of file diff --git a/src/Aviat/AnimeClient/Dispatcher.php b/src/Aviat/AnimeClient/Dispatcher.php index 1eb11978..54e6ed46 100644 --- a/src/Aviat/AnimeClient/Dispatcher.php +++ b/src/Aviat/AnimeClient/Dispatcher.php @@ -54,6 +54,12 @@ class Dispatcher extends RoutingBase { */ protected function generate_convention_routes() { + $this->output_routes[] = $this->router->add('index_redirect', '/') + ->setValues([ + 'controller' => 'Aviat\\AnimeClient\\Controller', + 'action' => 'redirect_to_default' + ]); + $this->output_routes[] = $this->router->add('list', '/{controller}/{type}{/view}') ->setValues([ 'controller' => $this->routes['convention']['default_controller'], diff --git a/src/Aviat/AnimeClient/UrlGenerator.php b/src/Aviat/AnimeClient/UrlGenerator.php index 924fbced..2356abfe 100644 --- a/src/Aviat/AnimeClient/UrlGenerator.php +++ b/src/Aviat/AnimeClient/UrlGenerator.php @@ -106,7 +106,7 @@ class UrlGenerator extends RoutingBase { return $this->url("{$type}/{$default_path}"); } - return ""; + throw new \InvalidArgumentException("Invalid default type: '{$type}'"); } /** diff --git a/tests/AnimeClient/DispatcherTest.php b/tests/AnimeClient/DispatcherTest.php index 480678a3..5bd1d28a 100644 --- a/tests/AnimeClient/DispatcherTest.php +++ b/tests/AnimeClient/DispatcherTest.php @@ -177,8 +177,8 @@ class DispatcherTest extends AnimeClient_TestCase { 'routing' => [ 'anime_path' => 'anime', 'manga_path' => 'manga', - 'default_anime_path' => "/anime/watching", - 'default_manga_path' => '/manga/all', + 'default_anime_list_path' => "watching", + 'default_manga_list_path' => 'all', 'default_list' => 'manga' ], 'routes' => [ @@ -221,7 +221,9 @@ class DispatcherTest extends AnimeClient_TestCase { $this->_set_up($config, "/", "localhost"); $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"); - $this->assertEquals('', $this->urlGenerator->default_url('foo'), "Incorrect default url"); + + $this->setExpectedException('\InvalidArgumentException'); + $this->urlGenerator->default_url('foo'); } public function dataGetControllerList() @@ -232,8 +234,8 @@ class DispatcherTest extends AnimeClient_TestCase { 'routing' => [ 'anime_path' => 'anime', 'manga_path' => 'manga', - 'default_anime_path' => "/anime/watching", - 'default_manga_path' => '/manga/all', + 'default_anime_list_path' => "watching", + 'default_manga_list_path' => 'all', 'default_list' => 'manga' ], 'routes' => [