2017-02-16 11:09:37 -05:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
/**
|
|
|
|
* Hummingbird Anime List Client
|
|
|
|
*
|
2018-08-22 13:48:27 -04:00
|
|
|
* An API client for Kitsu to manage anime and manga watch lists
|
2017-02-16 11:09:37 -05:00
|
|
|
*
|
2021-02-04 11:57:01 -05:00
|
|
|
* PHP version 8
|
2017-02-16 11:09:37 -05:00
|
|
|
*
|
|
|
|
* @package HummingbirdAnimeClient
|
2022-03-04 12:32:17 -05:00
|
|
|
* @author Timothy J. Warren <tim@timshome.page>
|
|
|
|
* @copyright 2015 - 2022 Timothy J. Warren
|
2017-02-16 11:09:37 -05:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
2020-12-10 17:06:50 -05:00
|
|
|
* @version 5.2
|
2017-03-07 20:53:58 -05:00
|
|
|
* @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
|
2017-02-16 11:09:37 -05:00
|
|
|
*/
|
2015-09-14 10:54:50 -04:00
|
|
|
|
2017-02-15 11:49:38 -05:00
|
|
|
namespace Aviat\AnimeClient\Tests;
|
|
|
|
|
2019-12-06 09:15:49 -05:00
|
|
|
use Aura\Router\Route;
|
2022-03-04 12:19:47 -05:00
|
|
|
use Aviat\AnimeClient\{Controller, Dispatcher, UrlGenerator};
|
2016-10-20 22:32:17 -04:00
|
|
|
use Aviat\Ion\Config;
|
2021-02-26 14:42:07 -05:00
|
|
|
use Aviat\Ion\Di\ContainerInterface;
|
2022-03-04 12:19:47 -05:00
|
|
|
use InvalidArgumentException;
|
|
|
|
use JetBrains\PhpStorm\ArrayShape;
|
2016-10-20 22:32:17 -04:00
|
|
|
use Monolog\Handler\TestHandler;
|
|
|
|
use Monolog\Logger;
|
2016-01-11 14:39:53 -05:00
|
|
|
|
2022-03-04 12:19:47 -05:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
final class DispatcherTest extends AnimeClientTestCase
|
|
|
|
{
|
2021-02-26 14:42:07 -05:00
|
|
|
protected ContainerInterface $container;
|
2015-09-14 10:54:50 -04:00
|
|
|
protected $router;
|
|
|
|
protected $config;
|
2017-02-22 15:08:29 -05:00
|
|
|
protected $urlGenerator;
|
2015-09-14 10:54:50 -04:00
|
|
|
|
2019-12-06 09:15:49 -05:00
|
|
|
protected function doSetUp($config, $uri, $host): void
|
2015-09-14 10:54:50 -04:00
|
|
|
{
|
|
|
|
// Set up the environment
|
2020-12-11 14:26:54 -05:00
|
|
|
$GLOBALS['_SERVER'] = array_merge($GLOBALS['_SERVER'], [
|
2015-09-14 10:54:50 -04:00
|
|
|
'REQUEST_METHOD' => 'GET',
|
|
|
|
'REQUEST_URI' => $uri,
|
|
|
|
'PATH_INFO' => $uri,
|
|
|
|
'HTTP_HOST' => $host,
|
2022-03-04 12:19:47 -05:00
|
|
|
'SERVER_NAME' => $host,
|
2015-09-14 10:54:50 -04:00
|
|
|
]);
|
|
|
|
|
2016-08-29 15:36:36 -04:00
|
|
|
$this->setSuperGlobals([
|
2022-03-04 12:19:47 -05:00
|
|
|
'_SERVER' => $GLOBALS['_SERVER'],
|
2016-08-29 15:36:36 -04:00
|
|
|
]);
|
2015-10-09 14:34:55 -04:00
|
|
|
|
2016-01-11 14:39:53 -05: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);
|
2016-08-29 15:36:36 -04:00
|
|
|
$this->container->setInstance('config', $config);
|
2015-10-09 14:34:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->router = new Dispatcher($this->container);
|
2015-09-14 10:54:50 -04:00
|
|
|
$this->config = $this->container->get('config');
|
2015-09-14 15:49:20 -04:00
|
|
|
$this->urlGenerator = new UrlGenerator($this->container);
|
2016-08-29 15:36:36 -04:00
|
|
|
$this->container->setInstance('url-generator', $this->urlGenerator);
|
2015-09-14 10:54:50 -04:00
|
|
|
}
|
|
|
|
|
2019-12-06 09:15:49 -05:00
|
|
|
public function testRouterSanity(): void
|
2015-09-14 10:54:50 -04:00
|
|
|
{
|
2017-02-15 11:57:29 -05:00
|
|
|
$this->doSetUp([], '/', 'localhost');
|
2019-12-06 09:15:49 -05:00
|
|
|
$this->assertIsObject($this->router);
|
2015-09-14 10:54:50 -04:00
|
|
|
}
|
|
|
|
|
2019-12-06 09:15:49 -05:00
|
|
|
public function dataRoute(): array
|
2015-09-14 10:54:50 -04:00
|
|
|
{
|
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',
|
2022-03-04 12:19:47 -05:00
|
|
|
'verb' => 'get',
|
2017-03-30 16:16:40 -04:00
|
|
|
],
|
|
|
|
'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' => [
|
2022-03-04 12:19:47 -05:00
|
|
|
'view' => '[a-z_]+',
|
|
|
|
],
|
2017-03-30 16:16:40 -04:00
|
|
|
],
|
|
|
|
'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' => [
|
2022-03-04 12:19:47 -05:00
|
|
|
'view' => '[a-z_]+',
|
|
|
|
],
|
2016-01-06 11:08:56 -05:00
|
|
|
],
|
2015-09-14 15:49:20 -04:00
|
|
|
],
|
2018-10-05 14:32:05 -04:00
|
|
|
'config' => [
|
2017-03-30 16:16:40 -04:00
|
|
|
'anime_path' => 'anime',
|
|
|
|
'manga_path' => 'manga',
|
2022-03-04 12:19:47 -05:00
|
|
|
'default_list' => 'anime',
|
|
|
|
],
|
2017-02-15 11:49:38 -05:00
|
|
|
];
|
2015-09-14 10:54:50 -04: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',
|
2018-11-01 22:16:45 -04:00
|
|
|
'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',
|
2018-11-01 22:16:45 -04:00
|
|
|
'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',
|
2022-03-04 12:19:47 -05:00
|
|
|
'uri' => '/manga/plan_to_read',
|
|
|
|
],
|
2015-09-14 10:54:50 -04:00
|
|
|
];
|
|
|
|
|
2018-10-05 14:32:05 -04:00
|
|
|
$data['manga_default_routing_anime']['config']['default_list'] = 'manga';
|
|
|
|
$data['manga_default_routing_manga']['config']['default_list'] = 'manga';
|
2015-09-14 10:54:50 -04:00
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataRoute
|
2022-03-04 12:19:47 -05:00
|
|
|
* @param mixed $config
|
|
|
|
* @param mixed $controller
|
|
|
|
* @param mixed $host
|
|
|
|
* @param mixed $uri
|
2015-09-14 10:54:50 -04:00
|
|
|
*/
|
2019-12-06 09:15:49 -05:00
|
|
|
public function testRoute($config, $controller, $host, $uri): void
|
2015-09-14 10:54:50 -04:00
|
|
|
{
|
2017-02-15 11:57:29 -05:00
|
|
|
$this->doSetUp($config, $uri, $host);
|
2015-09-14 10:54:50 -04:00
|
|
|
|
|
|
|
$request = $this->container->get('request');
|
2015-09-14 15:49:20 -04:00
|
|
|
|
2015-09-14 10:54:50 -04:00
|
|
|
// Check route setup
|
2022-03-04 12:19:47 -05:00
|
|
|
$this->assertSame($config['routes'], $this->config->get('routes'), 'Incorrect route path');
|
2019-12-06 09:15:49 -05:00
|
|
|
$this->assertIsArray($this->router->getOutputRoutes());
|
2015-09-14 10:54:50 -04:00
|
|
|
|
|
|
|
// Check environment variables
|
2022-03-04 12:19:47 -05:00
|
|
|
$this->assertSame($uri, $request->getServerParams()['REQUEST_URI']);
|
|
|
|
$this->assertSame($host, $request->getServerParams()['HTTP_HOST']);
|
2015-09-14 10:54:50 -04:00
|
|
|
|
|
|
|
// Make sure the route is an anime type
|
2018-11-01 22:16:45 -04:00
|
|
|
//$this->assertTrue($matcher->count() > 0, '0 routes');
|
2022-03-04 12:19:47 -05:00
|
|
|
$this->assertSame($controller, $this->router->getController(), 'Incorrect Route type');
|
2015-09-14 10:54:50 -04:00
|
|
|
|
|
|
|
// 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();
|
2019-12-06 09:15:49 -05:00
|
|
|
$this->assertInstanceOf(Route::class, $route, 'Route is invalid, not matched');
|
2015-09-14 10:54:50 -04:00
|
|
|
}
|
|
|
|
|
2019-12-06 09:15:49 -05:00
|
|
|
public function testDefaultRoute(): void
|
2015-09-14 10:54:50 -04:00
|
|
|
{
|
|
|
|
$config = [
|
2018-10-05 14:32:05 -04:00
|
|
|
'config' => [
|
2017-03-30 16:16:40 -04:00
|
|
|
'anime_path' => 'anime',
|
|
|
|
'manga_path' => 'manga',
|
2018-11-01 22:16:45 -04:00
|
|
|
'default_anime_list_path' => 'watching',
|
2017-03-30 16:16:40 -04:00
|
|
|
'default_manga_list_path' => 'all',
|
2022-03-04 12:19:47 -05:00
|
|
|
'default_list' => 'manga',
|
2017-03-30 16:16:40 -04:00
|
|
|
],
|
2015-09-14 10:54:50 -04:00
|
|
|
'routes' => [
|
2017-03-30 16:16:40 -04:00
|
|
|
'login_form' => [
|
|
|
|
'path' => '/login',
|
|
|
|
'action' => ['login'],
|
2022-03-04 12:19:47 -05:00
|
|
|
'verb' => 'get',
|
2015-09-16 12:25:35 -04:00
|
|
|
],
|
2017-03-30 16:16:40 -04:00
|
|
|
'index' => [
|
|
|
|
'path' => '/',
|
|
|
|
'action' => ['redirect'],
|
|
|
|
'params' => [
|
|
|
|
'url' => '', // Determined by config
|
|
|
|
'code' => '301',
|
2022-03-04 12:19:47 -05:00
|
|
|
'type' => 'manga',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
2015-09-14 10:54:50 -04:00
|
|
|
];
|
|
|
|
|
2022-03-04 12:19:47 -05:00
|
|
|
$this->expectException(InvalidArgumentException::class);
|
2018-10-05 14:32:05 -04:00
|
|
|
|
2018-11-01 22:16:45 -04:00
|
|
|
$this->doSetUp($config, '/', 'localhost');
|
2022-03-04 12:19:47 -05:00
|
|
|
$this->assertSame('//localhost/manga/all', $this->urlGenerator->defaultUrl('manga'), 'Incorrect default url');
|
|
|
|
$this->assertSame('//localhost/anime/watching', $this->urlGenerator->defaultUrl('anime'), 'Incorrect default url');
|
2015-10-20 16:41:51 -04:00
|
|
|
|
2017-02-15 15:56:10 -05:00
|
|
|
$this->urlGenerator->defaultUrl('foo');
|
2015-09-14 10:54:50 -04:00
|
|
|
}
|
2015-09-16 12:25:35 -04:00
|
|
|
|
2022-03-04 12:25:29 -05:00
|
|
|
#[ArrayShape(['controller_list_sanity_check' => 'array', 'empty_controller_list' => 'array'])]
|
2019-12-06 09:15:49 -05:00
|
|
|
public function dataGetControllerList(): array
|
2015-09-16 12:25:35 -04:00
|
|
|
{
|
2018-11-01 22:16:45 -04:00
|
|
|
$expectedList = [
|
|
|
|
'anime' => Controller\Anime::class,
|
|
|
|
'anime-collection' => Controller\AnimeCollection::class,
|
|
|
|
'character' => Controller\Character::class,
|
|
|
|
'misc' => Controller\Misc::class,
|
|
|
|
'manga' => Controller\Manga::class,
|
|
|
|
'people' => Controller\People::class,
|
|
|
|
'settings' => Controller\Settings::class,
|
|
|
|
'user' => Controller\User::class,
|
|
|
|
'images' => Controller\Images::class,
|
2020-04-21 20:09:37 -04:00
|
|
|
'history' => Controller\History::class,
|
2018-11-01 22:16:45 -04:00
|
|
|
];
|
|
|
|
|
2017-02-15 15:35:41 -05:00
|
|
|
return [
|
2015-09-16 12:25:35 -04:00
|
|
|
'controller_list_sanity_check' => [
|
|
|
|
'config' => [
|
2018-10-05 14:32:05 -04:00
|
|
|
'anime_path' => 'anime',
|
|
|
|
'manga_path' => 'manga',
|
2018-11-01 22:16:45 -04:00
|
|
|
'default_anime_list_path' => 'watching',
|
2018-10-05 14:32:05 -04:00
|
|
|
'default_manga_list_path' => 'all',
|
|
|
|
'default_list' => 'manga',
|
|
|
|
'routes' => [],
|
2015-09-16 12:25:35 -04:00
|
|
|
],
|
2018-11-01 22:16:45 -04:00
|
|
|
'expected' => $expectedList,
|
2015-09-16 12:25:35 -04:00
|
|
|
],
|
|
|
|
'empty_controller_list' => [
|
|
|
|
'config' => [
|
2018-10-05 14:32:05 -04:00
|
|
|
'anime_path' => 'anime',
|
|
|
|
'manga_path' => 'manga',
|
2018-11-01 22:16:45 -04:00
|
|
|
'default_anime_path' => '/anime/watching',
|
2018-10-05 14:32:05 -04:00
|
|
|
'default_manga_path' => '/manga/all',
|
2018-11-01 22:16:45 -04:00
|
|
|
'default_list' => 'manga',
|
|
|
|
'routes' => [],
|
2015-09-16 12:25:35 -04:00
|
|
|
],
|
2022-03-04 12:19:47 -05:00
|
|
|
'expected' => $expectedList,
|
|
|
|
],
|
2017-02-15 15:35:41 -05:00
|
|
|
];
|
2015-09-16 12:25:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataGetControllerList
|
|
|
|
*/
|
2022-03-04 12:19:47 -05:00
|
|
|
public function testGetControllerList(array $config, array $expected): void
|
2015-09-16 12:25:35 -04:00
|
|
|
{
|
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());
|
2015-09-16 12:25:35 -04:00
|
|
|
}
|
2022-03-04 12:19:47 -05:00
|
|
|
}
|