2014-08-31 16:56:00 +02:00
|
|
|
<?php
|
|
|
|
namespace Szurubooru\Tests;
|
2014-10-08 14:47:47 +02:00
|
|
|
use Szurubooru\Dispatcher;
|
|
|
|
use Szurubooru\Helpers\HttpHelper;
|
2014-11-19 22:00:50 +01:00
|
|
|
use Szurubooru\RouteRepository;
|
2014-10-08 14:47:47 +02:00
|
|
|
use Szurubooru\Router;
|
|
|
|
use Szurubooru\Services\AuthService;
|
|
|
|
use Szurubooru\Services\TokenService;
|
|
|
|
use Szurubooru\Tests\AbstractDatabaseTestCase;
|
2014-08-31 16:56:00 +02:00
|
|
|
|
2014-10-08 14:47:47 +02:00
|
|
|
final class DispatcherTest extends AbstractDatabaseTestCase
|
2014-08-31 16:56:00 +02:00
|
|
|
{
|
2015-11-25 09:48:03 +01:00
|
|
|
private $routerMock;
|
|
|
|
private $configMock;
|
|
|
|
private $httpHelperMock;
|
|
|
|
private $authServiceMock;
|
|
|
|
private $tokenServiceMock;
|
|
|
|
private $routeRepositoryMock;
|
2014-09-04 19:21:18 +02:00
|
|
|
|
2015-11-25 09:48:03 +01:00
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
$this->routerMock = $this->mock(Router::class);
|
|
|
|
$this->configMock = $this->mockConfig();
|
|
|
|
$this->httpHelperMock = $this->mock(HttpHelper::class);
|
|
|
|
$this->authServiceMock = $this->mock(AuthService::class);
|
|
|
|
$this->tokenServiceMock = $this->mock(TokenService::class);
|
|
|
|
$this->routeRepositoryMock = $this->mock(RouteRepository::class);
|
|
|
|
$this->configMock->set('misc/dumpSqlIntoQueries', 0);
|
|
|
|
}
|
2014-09-04 19:21:18 +02:00
|
|
|
|
2015-11-25 09:48:03 +01:00
|
|
|
public function testDispatchingArrays()
|
|
|
|
{
|
|
|
|
$expected = ['test' => 'toy'];
|
2014-08-31 16:56:00 +02:00
|
|
|
|
2015-11-25 09:48:03 +01:00
|
|
|
$this->httpHelperMock
|
|
|
|
->expects($this->exactly(2))
|
|
|
|
->method('setResponseCode')
|
|
|
|
->withConsecutive([$this->equalTo(500)], [$this->equalTo(200)]);
|
|
|
|
$this->routerMock->expects($this->once())->method('handle')->willReturn($expected);
|
|
|
|
$this->routeRepositoryMock->expects($this->once())->method('injectRoutes');
|
2014-08-31 16:56:00 +02:00
|
|
|
|
2015-11-25 09:48:03 +01:00
|
|
|
$dispatcher = $this->getDispatcher();
|
|
|
|
$actual = $dispatcher->run('GET', '/');
|
2014-08-31 16:56:00 +02:00
|
|
|
|
2015-11-25 09:48:03 +01:00
|
|
|
unset($actual['__time']);
|
|
|
|
$this->assertEquals($expected, $actual);
|
|
|
|
}
|
2014-08-31 16:56:00 +02:00
|
|
|
|
2015-11-25 09:48:03 +01:00
|
|
|
public function testDispatchingObjects()
|
|
|
|
{
|
|
|
|
$classData = new \StdClass;
|
|
|
|
$classData->bunny = 5;
|
|
|
|
$expected = ['bunny' => 5];
|
2014-08-31 16:56:00 +02:00
|
|
|
|
2015-11-25 09:48:03 +01:00
|
|
|
$this->routerMock->expects($this->once())->method('handle')->willReturn($classData);
|
|
|
|
$this->routeRepositoryMock->expects($this->once())->method('injectRoutes');
|
2014-08-31 16:56:00 +02:00
|
|
|
|
2015-11-25 09:48:03 +01:00
|
|
|
$dispatcher = $this->getDispatcher();
|
|
|
|
$actual = $dispatcher->run('GET', '/');
|
2014-08-31 16:56:00 +02:00
|
|
|
|
2015-11-25 09:48:03 +01:00
|
|
|
unset($actual['__time']);
|
|
|
|
$this->assertEquals($expected, $actual);
|
|
|
|
}
|
2014-08-31 16:56:00 +02:00
|
|
|
|
2015-11-25 09:48:03 +01:00
|
|
|
public function testAuthorization()
|
|
|
|
{
|
|
|
|
$this->httpHelperMock->expects($this->once())->method('getRequestHeader')->with($this->equalTo('X-Authorization-Token'))->willReturn('test');
|
|
|
|
$this->tokenServiceMock->expects($this->once())->method('getByName');
|
|
|
|
$this->routeRepositoryMock->expects($this->once())->method('injectRoutes');
|
2014-09-09 19:38:16 +02:00
|
|
|
|
2015-11-25 09:48:03 +01:00
|
|
|
$dispatcher = $this->getDispatcher();
|
|
|
|
$dispatcher->run('GET', '/');
|
|
|
|
}
|
2014-09-09 19:38:16 +02:00
|
|
|
|
2015-11-25 09:48:03 +01:00
|
|
|
private function getDispatcher()
|
|
|
|
{
|
|
|
|
return new Dispatcher(
|
|
|
|
$this->routerMock,
|
|
|
|
$this->configMock,
|
|
|
|
$this->databaseConnection,
|
|
|
|
$this->httpHelperMock,
|
|
|
|
$this->authServiceMock,
|
|
|
|
$this->tokenServiceMock,
|
|
|
|
$this->routeRepositoryMock);
|
|
|
|
}
|
2014-08-31 16:56:00 +02:00
|
|
|
}
|