szurubooru/tests/DispatcherTest.php

88 lines
2.6 KiB
PHP
Raw Normal View History

<?php
namespace Szurubooru\Tests;
use Szurubooru\ControllerRepository;
use Szurubooru\Dispatcher;
use Szurubooru\Helpers\HttpHelper;
use Szurubooru\Router;
use Szurubooru\Services\AuthService;
use Szurubooru\Services\TokenService;
use Szurubooru\Tests\AbstractDatabaseTestCase;
final class DispatcherTest extends AbstractDatabaseTestCase
{
2014-09-04 19:21:18 +02:00
private $routerMock;
private $configMock;
2014-09-04 19:21:18 +02:00
private $httpHelperMock;
private $authServiceMock;
private $tokenServiceMock;
2014-09-04 19:21:18 +02:00
private $controllerRepositoryMock;
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->controllerRepositoryMock = $this->mock(ControllerRepository::class);
$this->configMock->set('misc/dumpSqlIntoQueries', 0);
2014-09-04 19:21:18 +02:00
}
public function testDispatchingArrays()
{
$expected = ['test' => 'toy'];
2014-09-04 19:21:18 +02:00
$this->httpHelperMock
->expects($this->exactly(2))
->method('setResponseCode')
->withConsecutive([$this->equalTo(500)], [$this->equalTo(200)]);
2014-09-04 19:21:18 +02:00
$this->routerMock->expects($this->once())->method('handle')->willReturn($expected);
$this->controllerRepositoryMock->method('getControllers')->willReturn([]);
2014-09-04 19:21:18 +02:00
$dispatcher = $this->getDispatcher();
$actual = $dispatcher->run('GET', '/');
unset($actual['__time']);
$this->assertEquals($expected, $actual);
}
public function testDispatchingObjects()
{
$classData = new \StdClass;
$classData->bunny = 5;
$expected = ['bunny' => 5];
2014-09-04 19:21:18 +02:00
$this->routerMock->expects($this->once())->method('handle')->willReturn($classData);
$this->controllerRepositoryMock->method('getControllers')->willReturn([]);
2014-09-04 19:21:18 +02:00
$dispatcher = $this->getDispatcher();
$actual = $dispatcher->run('GET', '/');
unset($actual['__time']);
$this->assertEquals($expected, $actual);
}
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->controllerRepositoryMock->method('getControllers')->willReturn([]);
$dispatcher = $this->getDispatcher();
$dispatcher->run('GET', '/');
}
2014-09-04 19:21:18 +02:00
private function getDispatcher()
{
return new Dispatcher(
2014-09-04 19:21:18 +02:00
$this->routerMock,
$this->configMock,
$this->databaseConnection,
2014-09-04 19:21:18 +02:00
$this->httpHelperMock,
$this->authServiceMock,
$this->tokenServiceMock,
2014-09-04 19:21:18 +02:00
$this->controllerRepositoryMock);
}
}