HummingBirdAnimeClient/tests/DispatcherTest.php

267 lines
6.9 KiB
PHP
Raw Normal View History

<?php declare(strict_types=1);
/**
* Hummingbird Anime List Client
*
* An API client for Kitsu and MyAnimeList to manage anime and manga watch lists
*
* PHP version 7
*
* @package HummingbirdAnimeClient
* @author Timothy J. Warren <tim@timshomepage.net>
* @copyright 2015 - 2017 Timothy J. Warren
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @version 4.0
* @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
*/
2017-02-15 11:49:38 -05:00
namespace Aviat\AnimeClient\Tests;
2015-10-09 14:34:55 -04:00
use Aviat\AnimeClient\Dispatcher;
2015-09-15 13:19:29 -04:00
use Aviat\AnimeClient\UrlGenerator;
2016-10-20 22:32:17 -04:00
use Aviat\Ion\Config;
use Monolog\Handler\TestHandler;
use Monolog\Logger;
2017-02-15 16:40:18 -05:00
class DispatcherTest extends AnimeClientTestCase {
protected $container;
protected $router;
protected $config;
2017-02-22 15:08:29 -05:00
protected $urlGenerator;
2017-02-15 11:57:29 -05:00
protected function doSetUp($config, $uri, $host)
{
// Set up the environment
$_SERVER = array_merge($_SERVER, [
'REQUEST_METHOD' => 'GET',
'REQUEST_URI' => $uri,
'PATH_INFO' => $uri,
'HTTP_HOST' => $host,
'SERVER_NAME' => $host
]);
$this->setSuperGlobals([
'_SERVER' => $_SERVER
]);
2015-10-09 14:34:55 -04:00
$logger = new Logger('test_logger');
$logger->pushHandler(new TestHandler(Logger::DEBUG));
$this->container->setLogger($logger, 'default');
2015-10-09 14:34:55 -04:00
if ( ! empty($config))
{
$config = new Config($config);
$this->container->setInstance('config', $config);
2015-10-09 14:34:55 -04:00
}
$this->router = new Dispatcher($this->container);
$this->config = $this->container->get('config');
2015-09-14 15:49:20 -04:00
$this->urlGenerator = new UrlGenerator($this->container);
$this->container->setInstance('url-generator', $this->urlGenerator);
}
public function testRouterSanity()
{
2017-02-15 11:57:29 -05:00
$this->doSetUp([], '/', 'localhost');
$this->assertTrue(is_object($this->router));
}
public function dataRoute()
{
2017-02-15 16:11:52 -05:00
$defaultConfig = [
2015-09-14 15:49:20 -04:00
'routes' => [
2017-03-30 16:16:40 -04:00
'login_form' => [
'path' => '/login',
'action' => 'login',
'verb' => 'get'
],
'watching' => [
'path' => '/anime/watching{/view}',
'action' => 'anime_list',
'params' => [
'type' => 'currently-watching',
2015-09-14 15:49:20 -04:00
],
2017-03-30 16:16:40 -04:00
'tokens' => [
'view' => '[a-z_]+'
]
],
'plan_to_read' => [
'path' => '/manga/plan_to_read{/view}',
'action' => 'manga_list',
'params' => [
'type' => 'Plan to Read',
2015-09-14 15:49:20 -04:00
],
2017-03-30 16:16:40 -04:00
'tokens' => [
'view' => '[a-z_]+'
]
2016-01-06 11:08:56 -05:00
],
2015-09-14 15:49:20 -04:00
],
2017-03-30 16:16:40 -04:00
'route_config' => [
'anime_path' => 'anime',
'manga_path' => 'manga',
'default_list' => 'anime'
]
2017-02-15 11:49:38 -05:00
];
$data = [
2017-02-15 11:49:38 -05:00
'anime_default_routing_manga' => [
2017-02-15 16:11:52 -05:00
'config' => $defaultConfig,
2015-09-14 15:49:20 -04:00
'controller' => 'manga',
'host' => "localhost",
'uri' => "/manga/plan_to_read",
2017-02-15 11:49:38 -05:00
],
'manga_default_routing_anime' => [
2017-02-15 16:11:52 -05:00
'config' => $defaultConfig,
2015-09-14 15:49:20 -04:00
'controller' => 'anime',
'host' => "localhost",
'uri' => "/anime/watching",
2017-02-15 11:49:38 -05:00
],
'anime_default_routing_anime' => [
2017-02-15 16:11:52 -05:00
'config' => $defaultConfig,
2015-09-14 15:49:20 -04:00
'controller' => 'anime',
'host' => 'localhost',
'uri' => '/anime/watching',
2017-02-15 11:49:38 -05:00
],
'manga_default_routing_manga' => [
2017-02-15 16:11:52 -05:00
'config' => $defaultConfig,
2015-09-14 15:49:20 -04:00
'controller' => 'manga',
'host' => 'localhost',
'uri' => '/manga/plan_to_read'
2017-02-15 11:49:38 -05:00
]
];
2017-03-30 16:16:40 -04:00
$data['manga_default_routing_anime']['config']['route_config']['default_list'] = 'manga';
$data['manga_default_routing_manga']['config']['route_config']['default_list'] = 'manga';
return $data;
}
/**
* @dataProvider dataRoute
*/
2015-09-14 15:49:20 -04:00
public function testRoute($config, $controller, $host, $uri)
{
2017-02-15 11:57:29 -05:00
$this->doSetUp($config, $uri, $host);
$request = $this->container->get('request');
2015-09-14 15:49:20 -04:00
// Check route setup
2015-10-06 11:38:20 -04:00
$this->assertEquals($config['routes'], $this->config->get('routes'), "Incorrect route path");
2016-12-20 12:58:37 -05:00
$this->assertTrue(is_array($this->router->getOutputRoutes()));
// Check environment variables
$this->assertEquals($uri, $request->getServerParams()['REQUEST_URI']);
$this->assertEquals($host, $request->getServerParams()['HTTP_HOST']);
// Make sure the route is an anime type
//$this->assertTrue($matcher->count() > 0, "0 routes");
2016-12-20 12:58:37 -05:00
$this->assertEquals($controller, $this->router->getController(), "Incorrect Route type");
// Make sure the route matches, by checking that it is actually an object
2016-12-20 12:58:37 -05:00
$route = $this->router->getRoute();
$this->assertInstanceOf('Aura\\Router\\Route', $route, "Route is invalid, not matched");
}
public function testDefaultRoute()
{
$config = [
2017-03-30 16:16:40 -04:00
'route_config' => [
'anime_path' => 'anime',
'manga_path' => 'manga',
'default_anime_list_path' => "watching",
'default_manga_list_path' => 'all',
'default_list' => 'manga'
],
'routes' => [
2017-03-30 16:16:40 -04:00
'login_form' => [
'path' => '/login',
'action' => ['login'],
'verb' => 'get'
],
2017-03-30 16:16:40 -04:00
'index' => [
'path' => '/',
'action' => ['redirect'],
'params' => [
'url' => '', // Determined by config
'code' => '301'
]
],
'index' => [
'path' => '/',
'action' => ['redirect'],
'params' => [
'url' => '', // Determined by config
'code' => '301',
'type' => 'manga'
2016-01-06 11:08:56 -05:00
]
]
]
];
2017-02-15 11:57:29 -05:00
$this->doSetUp($config, "/", "localhost");
2017-02-15 15:56:10 -05:00
$this->assertEquals('//localhost/manga/all', $this->urlGenerator->defaultUrl('manga'), "Incorrect default url");
$this->assertEquals('//localhost/anime/watching', $this->urlGenerator->defaultUrl('anime'), "Incorrect default url");
2015-10-20 16:41:51 -04:00
2017-02-06 11:00:18 -05:00
$this->expectException(\InvalidArgumentException::class);
2017-02-15 15:56:10 -05:00
$this->urlGenerator->defaultUrl('foo');
}
public function dataGetControllerList()
{
2017-02-15 15:35:41 -05:00
return [
'controller_list_sanity_check' => [
'config' => [
'routes' => [
2016-01-06 11:08:56 -05:00
2017-03-30 16:16:40 -04:00
],
'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',
'manga' => 'Aviat\AnimeClient\Controller\Manga',
'collection' => 'Aviat\AnimeClient\Controller\Collection',
2017-03-08 13:46:50 -05:00
'character' => 'Aviat\AnimeClient\Controller\Character',
'index' => 'Aviat\AnimeClient\Controller\Index',
]
],
'empty_controller_list' => [
'config' => [
'routes' => [
2016-01-06 11:08:56 -05:00
2017-03-30 16:16:40 -04:00
],
'route_config' => [
'anime_path' => 'anime',
'manga_path' => 'manga',
'default_anime_path' => "/anime/watching",
'default_manga_path' => '/manga/all',
'default_list' => 'manga'
],
],
2016-01-06 11:08:56 -05:00
'expected' => [
'anime' => 'Aviat\AnimeClient\Controller\Anime',
'manga' => 'Aviat\AnimeClient\Controller\Manga',
'collection' => 'Aviat\AnimeClient\Controller\Collection',
2017-03-08 13:46:50 -05:00
'character' => 'Aviat\AnimeClient\Controller\Character',
'index' => 'Aviat\AnimeClient\Controller\Index',
2016-01-06 11:08:56 -05:00
]
]
2017-02-15 15:35:41 -05:00
];
}
/**
* @dataProvider dataGetControllerList
*/
public function testGetControllerList($config, $expected)
{
2017-02-15 11:57:29 -05:00
$this->doSetUp($config, '/', 'localhost');
2016-12-20 12:58:37 -05:00
$this->assertEquals($expected, $this->router->getControllerList());
}
}