szurubooru/tests/DispatcherTest.php

76 lines
2.3 KiB
PHP
Raw Normal View History

<?php
namespace Szurubooru\Tests;
2014-09-01 20:51:59 +02:00
final class DispatcherTest extends \Szurubooru\Tests\AbstractTestCase
{
2014-09-04 19:21:18 +02:00
private $routerMock;
private $httpHelperMock;
private $authServiceMock;
private $tokenServiceMock;
2014-09-04 19:21:18 +02:00
private $controllerRepositoryMock;
public function setUp()
{
parent::setUp();
2014-09-04 19:21:18 +02:00
$this->routerMock = $this->mock(\Szurubooru\Router::class);
$this->httpHelperMock = $this->mock(\Szurubooru\Helpers\HttpHelper::class);
$this->authServiceMock = $this->mock(\Szurubooru\Services\AuthService::class);
$this->tokenServiceMock = $this->mock(\Szurubooru\Services\TokenService::class);
2014-09-04 19:21:18 +02:00
$this->controllerRepositoryMock = $this->mock(\Szurubooru\ControllerRepository::class);
}
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()
{
2014-09-04 19:21:18 +02:00
return new \Szurubooru\Dispatcher(
$this->routerMock,
$this->httpHelperMock,
$this->authServiceMock,
$this->tokenServiceMock,
2014-09-04 19:21:18 +02:00
$this->controllerRepositoryMock);
}
}