2018-08-11 23:46:28 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Engelsystem\Test\Unit\Middleware;
|
|
|
|
|
|
|
|
use Engelsystem\Application;
|
|
|
|
use Engelsystem\Middleware\Dispatcher;
|
|
|
|
use InvalidArgumentException;
|
|
|
|
use LogicException;
|
2019-04-24 10:45:00 +02:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2018-08-11 23:46:28 +02:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Psr\Http\Message\ResponseInterface;
|
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
|
|
use Psr\Http\Server\MiddlewareInterface;
|
|
|
|
use Psr\Http\Server\RequestHandlerInterface;
|
|
|
|
use ReflectionClass as Reflection;
|
|
|
|
|
|
|
|
class DispatcherTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Middleware\Dispatcher::__construct
|
|
|
|
*/
|
|
|
|
public function testInit()
|
|
|
|
{
|
|
|
|
/** @var Application|MockObject $container */
|
|
|
|
$container = $this->createMock(Application::class);
|
|
|
|
|
|
|
|
$dispatcher = new Dispatcher([], $container);
|
|
|
|
$this->assertInstanceOf(MiddlewareInterface::class, $dispatcher);
|
|
|
|
$this->assertInstanceOf(RequestHandlerInterface::class, $dispatcher);
|
|
|
|
|
|
|
|
$reflection = new Reflection(get_class($dispatcher));
|
|
|
|
$property = $reflection->getProperty('container');
|
|
|
|
$property->setAccessible(true);
|
|
|
|
$this->assertEquals($container, $property->getValue($dispatcher));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Middleware\Dispatcher::process
|
|
|
|
*/
|
|
|
|
public function testProcess()
|
|
|
|
{
|
|
|
|
/** @var ServerRequestInterface|MockObject $request */
|
|
|
|
$request = $this->createMock(ServerRequestInterface::class);
|
|
|
|
/** @var ResponseInterface|MockObject $response */
|
|
|
|
$response = $this->createMock(ResponseInterface::class);
|
|
|
|
/** @var RequestHandlerInterface|MockObject $handler */
|
|
|
|
$handler = $this->createMock(RequestHandlerInterface::class);
|
|
|
|
|
|
|
|
/** @var Dispatcher|MockObject $dispatcher */
|
|
|
|
$dispatcher = $this->getMockBuilder(Dispatcher::class)
|
2019-11-06 12:29:58 +01:00
|
|
|
->onlyMethods(['handle'])
|
2018-08-11 23:46:28 +02:00
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$dispatcher->expects($this->once())
|
|
|
|
->method('handle')
|
|
|
|
->willReturn($response);
|
|
|
|
|
|
|
|
$return = $dispatcher->process($request, $handler);
|
|
|
|
$this->assertEquals($response, $return);
|
|
|
|
|
|
|
|
$reflection = new Reflection(get_class($dispatcher));
|
|
|
|
$property = $reflection->getProperty('next');
|
|
|
|
$property->setAccessible(true);
|
|
|
|
|
|
|
|
$this->assertEquals($handler, $property->getValue($dispatcher));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Middleware\Dispatcher::handle
|
|
|
|
*/
|
|
|
|
public function testHandle()
|
|
|
|
{
|
|
|
|
/** @var ServerRequestInterface|MockObject $request */
|
|
|
|
$request = $this->createMock(ServerRequestInterface::class);
|
|
|
|
/** @var ResponseInterface|MockObject $response */
|
|
|
|
$response = $this->createMock(ResponseInterface::class);
|
|
|
|
/** @var MiddlewareInterface|MockObject $middleware */
|
|
|
|
$middleware = $this->createMock(MiddlewareInterface::class);
|
|
|
|
|
|
|
|
$dispatcher = new Dispatcher([$middleware]);
|
|
|
|
$middleware->expects($this->once())
|
|
|
|
->method('process')
|
|
|
|
->with($request, $dispatcher)
|
|
|
|
->willReturn($response);
|
|
|
|
|
|
|
|
$return = $dispatcher->handle($request);
|
|
|
|
$this->assertEquals($response, $return);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Middleware\Dispatcher::handle
|
|
|
|
*/
|
|
|
|
public function testHandleNext()
|
|
|
|
{
|
|
|
|
/** @var ServerRequestInterface|MockObject $request */
|
|
|
|
$request = $this->createMock(ServerRequestInterface::class);
|
|
|
|
/** @var ResponseInterface|MockObject $response */
|
|
|
|
$response = $this->createMock(ResponseInterface::class);
|
|
|
|
/** @var RequestHandlerInterface|MockObject $handler */
|
|
|
|
$handler = $this->createMock(RequestHandlerInterface::class);
|
|
|
|
|
|
|
|
$dispatcher = new Dispatcher();
|
|
|
|
$handler->expects($this->once())
|
|
|
|
->method('handle')
|
|
|
|
->with($request)
|
|
|
|
->willReturn($response);
|
|
|
|
|
|
|
|
$reflection = new Reflection(get_class($dispatcher));
|
|
|
|
$property = $reflection->getProperty('next');
|
|
|
|
$property->setAccessible(true);
|
|
|
|
$property->setValue($dispatcher, $handler);
|
|
|
|
|
|
|
|
$return = $dispatcher->handle($request);
|
|
|
|
$this->assertEquals($response, $return);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Middleware\Dispatcher::handle
|
|
|
|
*/
|
|
|
|
public function testHandleNoMiddleware()
|
|
|
|
{
|
|
|
|
/** @var ServerRequestInterface|MockObject $request */
|
|
|
|
$request = $this->createMock(ServerRequestInterface::class);
|
|
|
|
|
|
|
|
$this->expectException(LogicException::class);
|
|
|
|
|
|
|
|
$dispatcher = new Dispatcher();
|
|
|
|
$dispatcher->handle($request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Middleware\Dispatcher::handle
|
|
|
|
*/
|
|
|
|
public function testHandleCallResolve()
|
|
|
|
{
|
|
|
|
/** @var ServerRequestInterface|MockObject $request */
|
|
|
|
$request = $this->createMock(ServerRequestInterface::class);
|
|
|
|
/** @var ResponseInterface|MockObject $response */
|
|
|
|
$response = $this->createMock(ResponseInterface::class);
|
|
|
|
/** @var MiddlewareInterface|MockObject $middleware */
|
|
|
|
$middleware = $this->createMock(MiddlewareInterface::class);
|
|
|
|
|
|
|
|
/** @var Dispatcher|MockObject $dispatcher */
|
|
|
|
$dispatcher = $this->getMockBuilder(Dispatcher::class)
|
2018-08-25 21:16:20 +02:00
|
|
|
->setConstructorArgs([[MiddlewareInterface::class, MiddlewareInterface::class]])
|
2019-11-06 12:29:58 +01:00
|
|
|
->onlyMethods(['resolveMiddleware'])
|
2018-08-11 23:46:28 +02:00
|
|
|
->getMock();
|
|
|
|
|
2018-08-25 21:16:20 +02:00
|
|
|
$dispatcher->expects($this->exactly(2))
|
2018-08-11 23:46:28 +02:00
|
|
|
->method('resolveMiddleware')
|
|
|
|
->with(MiddlewareInterface::class)
|
2018-08-25 21:16:20 +02:00
|
|
|
->willReturnOnConsecutiveCalls($middleware, null);
|
2018-08-11 23:46:28 +02:00
|
|
|
|
|
|
|
$middleware->expects($this->once())
|
|
|
|
->method('process')
|
|
|
|
->with($request, $dispatcher)
|
|
|
|
->willReturn($response);
|
|
|
|
|
|
|
|
$return = $dispatcher->handle($request);
|
|
|
|
$this->assertEquals($response, $return);
|
2018-08-25 21:16:20 +02:00
|
|
|
|
|
|
|
$this->expectException(InvalidArgumentException::class);
|
|
|
|
$dispatcher->handle($request);
|
2018-08-11 23:46:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Middleware\Dispatcher::setContainer
|
|
|
|
*/
|
2018-08-25 21:16:20 +02:00
|
|
|
public function testSetContainer()
|
2018-08-11 23:46:28 +02:00
|
|
|
{
|
|
|
|
/** @var Application|MockObject $container */
|
|
|
|
$container = $this->createMock(Application::class);
|
|
|
|
|
2018-08-25 21:16:20 +02:00
|
|
|
$middleware = new Dispatcher();
|
|
|
|
$middleware->setContainer($container);
|
2018-08-11 23:46:28 +02:00
|
|
|
|
2018-08-25 21:16:20 +02:00
|
|
|
$reflection = new Reflection(get_class($middleware));
|
|
|
|
$property = $reflection->getProperty('container');
|
|
|
|
$property->setAccessible(true);
|
2018-08-11 23:46:28 +02:00
|
|
|
|
2018-08-25 21:16:20 +02:00
|
|
|
$this->assertEquals($container, $property->getValue($middleware));
|
2018-08-11 23:46:28 +02:00
|
|
|
}
|
|
|
|
}
|