2015-09-14 10:54:50 -04:00
|
|
|
<?php
|
|
|
|
|
2015-09-25 13:41:12 -04:00
|
|
|
use Aviat\Ion\Di\Container;
|
2015-10-09 14:34:55 -04:00
|
|
|
use Aviat\AnimeClient\Dispatcher;
|
2015-09-15 13:19:29 -04:00
|
|
|
use Aviat\AnimeClient\Config;
|
|
|
|
use Aviat\AnimeClient\UrlGenerator;
|
2015-09-14 10:54:50 -04:00
|
|
|
use Aura\Web\WebFactory;
|
|
|
|
use Aura\Router\RouterFactory;
|
|
|
|
|
2015-10-09 14:34:55 -04:00
|
|
|
class DispatcherTest extends AnimeClient_TestCase {
|
2015-09-14 10:54:50 -04:00
|
|
|
|
|
|
|
protected $container;
|
|
|
|
protected $router;
|
|
|
|
protected $config;
|
|
|
|
|
|
|
|
protected function _set_up($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
|
|
|
|
]);
|
|
|
|
|
|
|
|
$router_factory = new RouterFactory();
|
|
|
|
$web_factory = new WebFactory([
|
|
|
|
'_GET' => [],
|
|
|
|
'_POST' => [],
|
|
|
|
'_COOKIE' => [],
|
|
|
|
'_SERVER' => $_SERVER,
|
|
|
|
'_FILES' => []
|
|
|
|
]);
|
|
|
|
|
2015-10-09 14:34:55 -04:00
|
|
|
$old_config = $this->container->get('config');
|
|
|
|
|
2015-09-14 10:54:50 -04:00
|
|
|
// Add the appropriate objects to the container
|
|
|
|
$this->container = new Container([
|
2015-10-09 14:34:55 -04:00
|
|
|
'config' => $old_config,
|
2015-09-14 10:54:50 -04:00
|
|
|
'request' => $web_factory->newRequest(),
|
|
|
|
'response' => $web_factory->newResponse(),
|
|
|
|
'aura-router' => $router_factory->newInstance(),
|
|
|
|
'error-handler' => new MockErrorHandler()
|
|
|
|
]);
|
|
|
|
|
2015-10-09 14:34:55 -04:00
|
|
|
if ( ! empty($config))
|
|
|
|
{
|
|
|
|
$config = new Config($config);
|
|
|
|
$this->container->set('config', $config);
|
|
|
|
}
|
|
|
|
|
|
|
|
$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);
|
|
|
|
$this->container->set('url-generator', $this->urlGenerator);
|
2015-09-14 10:54:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testRouterSanity()
|
|
|
|
{
|
|
|
|
$this->_set_up([], '/', 'localhost');
|
|
|
|
$this->assertTrue(is_object($this->router));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function dataRoute()
|
|
|
|
{
|
|
|
|
$default_config = array(
|
2015-09-14 15:49:20 -04:00
|
|
|
'routes' => [
|
2015-09-16 12:25:35 -04:00
|
|
|
'convention' => [
|
2015-10-01 16:02:51 -04:00
|
|
|
'default_namespace' => '\\Aviat\\AnimeClient\\Controller',
|
|
|
|
'default_controller' => '\\Aviat\\AnimeClient\\Controller\\Anime',
|
|
|
|
'default_method' => 'index'
|
2015-09-16 12:25:35 -04:00
|
|
|
],
|
2015-09-14 15:49:20 -04:00
|
|
|
'common' => [
|
|
|
|
'login_form' => [
|
|
|
|
'path' => '/login',
|
2015-10-01 16:02:51 -04:00
|
|
|
'action' => 'login',
|
2015-09-14 15:49:20 -04:00
|
|
|
'verb' => 'get'
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'anime' => [
|
|
|
|
'watching' => [
|
|
|
|
'path' => '/anime/watching{/view}',
|
2015-10-01 16:02:51 -04:00
|
|
|
'action' => 'anime_list',
|
2015-09-14 15:49:20 -04:00
|
|
|
'params' => [
|
|
|
|
'type' => 'currently-watching',
|
|
|
|
],
|
|
|
|
'tokens' => [
|
|
|
|
'view' => '[a-z_]+'
|
|
|
|
]
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'manga' => [
|
|
|
|
'plan_to_read' => [
|
|
|
|
'path' => '/manga/plan_to_read{/view}',
|
2015-10-01 16:02:51 -04:00
|
|
|
'action' => 'manga_list',
|
2015-09-14 15:49:20 -04:00
|
|
|
'params' => [
|
|
|
|
'type' => 'Plan to Read',
|
|
|
|
],
|
|
|
|
'tokens' => [
|
|
|
|
'view' => '[a-z_]+'
|
|
|
|
]
|
|
|
|
],
|
|
|
|
]
|
|
|
|
],
|
2015-09-14 10:54:50 -04:00
|
|
|
'routing' => [
|
|
|
|
'anime_path' => 'anime',
|
|
|
|
'manga_path' => 'manga',
|
|
|
|
'default_list' => 'anime'
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
$data = [
|
2015-09-14 15:49:20 -04:00
|
|
|
'anime_default_routing_manga' => array(
|
2015-09-14 10:54:50 -04:00
|
|
|
'config' => $default_config,
|
2015-09-14 15:49:20 -04:00
|
|
|
'controller' => 'manga',
|
2015-09-14 10:54:50 -04:00
|
|
|
'host' => "localhost",
|
|
|
|
'uri' => "/manga/plan_to_read",
|
|
|
|
),
|
2015-09-14 15:49:20 -04:00
|
|
|
'manga_default_routing_anime' => array(
|
2015-09-14 10:54:50 -04:00
|
|
|
'config' => $default_config,
|
2015-09-14 15:49:20 -04:00
|
|
|
'controller' => 'anime',
|
2015-09-14 10:54:50 -04:00
|
|
|
'host' => "localhost",
|
|
|
|
'uri' => "/anime/watching",
|
2015-09-14 15:49:20 -04:00
|
|
|
),
|
|
|
|
'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'
|
2015-09-14 10:54:50 -04:00
|
|
|
)
|
|
|
|
];
|
|
|
|
|
2015-09-14 15:49:20 -04:00
|
|
|
$data['manga_default_routing_anime']['config']['routing']['default_list'] = 'manga';
|
|
|
|
$data['manga_default_routing_manga']['config']['routing']['default_list'] = 'manga';
|
2015-09-14 10:54:50 -04:00
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataRoute
|
|
|
|
*/
|
2015-09-14 15:49:20 -04:00
|
|
|
public function testRoute($config, $controller, $host, $uri)
|
2015-09-14 10:54:50 -04:00
|
|
|
{
|
|
|
|
$this->_set_up($config, $uri, $host);
|
|
|
|
|
|
|
|
$request = $this->container->get('request');
|
|
|
|
$aura_router = $this->container->get('aura-router');
|
|
|
|
|
2015-09-14 15:49:20 -04:00
|
|
|
|
2015-09-14 10:54:50 -04:00
|
|
|
// Check route setup
|
2015-10-06 11:38:20 -04:00
|
|
|
$this->assertEquals($config['routes'], $this->config->get('routes'), "Incorrect route path");
|
2015-09-14 10:54:50 -04:00
|
|
|
$this->assertTrue(is_array($this->router->get_output_routes()));
|
|
|
|
|
|
|
|
// Check environment variables
|
|
|
|
$this->assertEquals($uri, $request->server->get('REQUEST_URI'));
|
|
|
|
$this->assertEquals($host, $request->server->get('HTTP_HOST'));
|
|
|
|
|
|
|
|
// Make sure the route is an anime type
|
|
|
|
$this->assertTrue($aura_router->count() > 0, "0 routes");
|
2015-09-14 15:49:20 -04:00
|
|
|
$this->assertEquals($controller, $this->router->get_controller(), "Incorrect Route type");
|
2015-09-14 10:54:50 -04:00
|
|
|
|
|
|
|
// Make sure the route matches, by checking that it is actually an object
|
|
|
|
$route = $this->router->get_route();
|
|
|
|
$this->assertInstanceOf('Aura\\Router\\Route', $route, "Route is invalid, not matched");
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDefaultRoute()
|
|
|
|
{
|
|
|
|
$config = [
|
|
|
|
'routing' => [
|
|
|
|
'anime_path' => 'anime',
|
|
|
|
'manga_path' => 'manga',
|
2015-10-20 16:41:51 -04:00
|
|
|
'default_anime_list_path' => "watching",
|
|
|
|
'default_manga_list_path' => 'all',
|
2015-09-14 10:54:50 -04:00
|
|
|
'default_list' => 'manga'
|
|
|
|
],
|
|
|
|
'routes' => [
|
2015-09-16 12:25:35 -04:00
|
|
|
'convention' => [
|
2015-10-01 16:02:51 -04:00
|
|
|
'default_namespace' => '\\Aviat\\AnimeClient\\Controller',
|
|
|
|
'default_controller' => '\\Aviat\\AnimeClient\\Controller\\Anime',
|
|
|
|
'default_method' => 'index'
|
2015-09-16 12:25:35 -04:00
|
|
|
],
|
2015-09-14 10:54:50 -04:00
|
|
|
'common' => [
|
|
|
|
'login_form' => [
|
|
|
|
'path' => '/login',
|
|
|
|
'action' => ['login'],
|
|
|
|
'verb' => 'get'
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'anime' => [
|
|
|
|
'index' => [
|
|
|
|
'path' => '/',
|
|
|
|
'action' => ['redirect'],
|
|
|
|
'params' => [
|
|
|
|
'url' => '', // Determined by config
|
|
|
|
'code' => '301'
|
|
|
|
]
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'manga' => [
|
|
|
|
'index' => [
|
|
|
|
'path' => '/',
|
|
|
|
'action' => ['redirect'],
|
|
|
|
'params' => [
|
|
|
|
'url' => '', // Determined by config
|
|
|
|
'code' => '301',
|
|
|
|
'type' => 'manga'
|
|
|
|
]
|
|
|
|
],
|
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
|
|
|
$this->_set_up($config, "/", "localhost");
|
2015-09-14 15:49:20 -04:00
|
|
|
$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");
|
2015-10-20 16:41:51 -04:00
|
|
|
|
|
|
|
$this->setExpectedException('\InvalidArgumentException');
|
|
|
|
$this->urlGenerator->default_url('foo');
|
2015-09-14 10:54:50 -04:00
|
|
|
}
|
2015-09-16 12:25:35 -04:00
|
|
|
|
|
|
|
public function dataGetControllerList()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
'controller_list_sanity_check' => [
|
|
|
|
'config' => [
|
|
|
|
'routing' => [
|
|
|
|
'anime_path' => 'anime',
|
|
|
|
'manga_path' => 'manga',
|
2015-10-20 16:41:51 -04:00
|
|
|
'default_anime_list_path' => "watching",
|
|
|
|
'default_manga_list_path' => 'all',
|
2015-09-16 12:25:35 -04:00
|
|
|
'default_list' => 'manga'
|
|
|
|
],
|
|
|
|
'routes' => [
|
|
|
|
'convention' => [
|
2015-10-01 16:02:51 -04:00
|
|
|
'default_namespace' => '\\Aviat\\AnimeClient\\Controller',
|
|
|
|
'default_controller' => '\\Aviat\\AnimeClient\\Controller\\Anime',
|
|
|
|
'default_method' => 'index'
|
2015-09-16 12:25:35 -04:00
|
|
|
]
|
|
|
|
]
|
|
|
|
],
|
|
|
|
'expected' => [
|
|
|
|
'anime' => 'Aviat\AnimeClient\Controller\Anime',
|
|
|
|
'manga' => 'Aviat\AnimeClient\Controller\Manga',
|
|
|
|
'collection' => 'Aviat\AnimeClient\Controller\Collection',
|
|
|
|
]
|
|
|
|
],
|
|
|
|
'empty_controller_list' => [
|
|
|
|
'config' => [
|
|
|
|
'routing' => [
|
|
|
|
'anime_path' => 'anime',
|
|
|
|
'manga_path' => 'manga',
|
|
|
|
'default_anime_path' => "/anime/watching",
|
|
|
|
'default_manga_path' => '/manga/all',
|
|
|
|
'default_list' => 'manga'
|
|
|
|
],
|
|
|
|
'routes' => [
|
|
|
|
'convention' => [
|
2015-10-01 16:02:51 -04:00
|
|
|
'default_namespace' => '\\Aviat\\Ion\\Controller',
|
|
|
|
'default_controller' => '\\Aviat\\Ion\\Controller\\Anime',
|
|
|
|
'default_method' => 'index'
|
2015-09-16 12:25:35 -04:00
|
|
|
]
|
|
|
|
]
|
|
|
|
],
|
|
|
|
'expected' => []
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataGetControllerList
|
|
|
|
*/
|
|
|
|
public function testGetControllerList($config, $expected)
|
|
|
|
{
|
|
|
|
$this->_set_up($config, '/', 'localhost');
|
|
|
|
$this->assertEquals($expected, $this->router->get_controller_list());
|
|
|
|
}
|
2015-09-14 10:54:50 -04:00
|
|
|
}
|