Fixed serializing classes in Dispatcher

This commit is contained in:
Marcin Kurczewski 2014-08-31 16:56:00 +02:00
parent 1104eaf591
commit 7be8061aa8
5 changed files with 115 additions and 9 deletions

View file

@ -1,7 +1,7 @@
<?php
namespace Szurubooru;
final class ControllerRepository
class ControllerRepository
{
private $controllers = [];

View file

@ -1,18 +1,21 @@
<?php
namespace Szurubooru;
//if script fails prematurely, mark it as fail from advance
http_response_code(500);
final class Dispatcher
{
private $router;
public function __construct(
\Szurubooru\Router $router,
\Szurubooru\Helpers\HttpHelper $httpHelper,
\Szurubooru\ControllerRepository $controllerRepository)
{
$this->router = $router;
$this->httpHelper = $httpHelper;
//if script fails prematurely, mark it as fail from advance
$this->httpHelper->setResponseCode(500);
foreach ($controllerRepository->getControllers() as $controller)
$controller->registerRoutes($router);
}
@ -23,7 +26,9 @@ final class Dispatcher
try
{
$code = 200;
$json = $this->router->handle($_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI']);
$json = (array) $this->router->handle(
$this->httpHelper->getRequestMethod(),
$this->httpHelper->getRequestUri());
}
catch (\Exception $e)
{
@ -36,8 +41,10 @@ final class Dispatcher
$end = microtime(true);
$json['__time'] = $end - $start;
http_response_code($code);
header('Content-Type: application/json');
echo json_encode($json);
$this->httpHelper->setResponseCode($code);
$this->httpHelper->setHeader('Content-Type', 'application/json');
$this->httpHelper->outputJSON($json);
return $json;
}
}

View file

@ -0,0 +1,35 @@
<?php
namespace Szurubooru\Helpers;
class HttpHelper
{
public function setResponseCode($code)
{
http_response_code($code);
}
public function setHeader($key, $value)
{
header("$key: $value");
}
public function output($data)
{
echo $data;
}
public function outputJSON($data)
{
$this->output(json_encode((array) $data));
}
public function getRequestMethod()
{
return $_SERVER['REQUEST_METHOD'];
}
public function getRequestUri()
{
return $_SERVER['REQUEST_URI'];
}
}

View file

@ -1,7 +1,7 @@
<?php
namespace Szurubooru;
final class Router
class Router
{
private $routes;

64
tests/DispatcherTest.php Normal file
View file

@ -0,0 +1,64 @@
<?php
namespace Szurubooru\Tests;
final class DispatcherTest extends \PHPUnit_Framework_TestCase
{
public function testDispatchingArrays()
{
$expected = ['test' => 'toy'];
$httpHelperMock = $this->getHttpHelperMock();
$httpHelperMock
->expects($this->exactly(2))
->method('setResponseCode')
->withConsecutive([$this->equalTo(500)], [$this->equalTo(200)]);
$routerMock = $this->getRouterMock();
$routerMock->expects($this->once())->method('handle')->willReturn($expected);
$controllerRepositoryMock = $this->getControllerRepositoryMock();
$controllerRepositoryMock->method('getControllers')->willReturn([]);
$dispatcher = new \Szurubooru\Dispatcher($routerMock, $httpHelperMock, $controllerRepositoryMock);
$actual = $dispatcher->run();
unset($actual['__time']);
$this->assertEquals($expected, $actual);
}
public function testDispatchingObjects()
{
$classData = new \StdClass;
$classData->bunny = 5;
$expected = ['bunny' => 5];
$httpHelperMock = $this->getHttpHelperMock();
$routerMock = $this->getRouterMock();
$routerMock->expects($this->once())->method('handle')->willReturn($classData);
$controllerRepositoryMock = $this->getControllerRepositoryMock();
$controllerRepositoryMock->method('getControllers')->willReturn([]);
$dispatcher = new \Szurubooru\Dispatcher($routerMock, $httpHelperMock, $controllerRepositoryMock);
$actual = $dispatcher->run();
unset($actual['__time']);
$this->assertEquals($expected, $actual);
}
private function getHttpHelperMock()
{
return $this->getMockBuilder(\Szurubooru\Helpers\HttpHelper::class)->disableOriginalConstructor()->getMock();
}
private function getRouterMock()
{
return $this->getMockBuilder(\Szurubooru\Router::class)->disableOriginalConstructor()->getMock();
}
private function getControllerRepositoryMock()
{
return $this->getMockBuilder(\Szurubooru\ControllerRepository::class)->disableOriginalConstructor()->getMock();
}
}