2018-08-11 23:46:28 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Engelsystem\Test\Unit\Middleware;
|
|
|
|
|
|
|
|
use Engelsystem\Application;
|
2022-12-21 15:11:37 +01:00
|
|
|
use Engelsystem\Http\Request;
|
|
|
|
use Engelsystem\Http\Response;
|
2018-08-11 23:46:28 +02:00
|
|
|
use Engelsystem\Middleware\Dispatcher;
|
2022-12-21 15:11:37 +01:00
|
|
|
use Engelsystem\Test\Unit\Middleware\Stub\ReturnResponseMiddlewareHandler;
|
|
|
|
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;
|
2022-12-14 19:15:20 +01:00
|
|
|
use TypeError;
|
2018-08-11 23:46:28 +02:00
|
|
|
|
|
|
|
class DispatcherTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Middleware\Dispatcher::__construct
|
|
|
|
*/
|
|
|
|
public function testInit(): void
|
|
|
|
{
|
|
|
|
/** @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(): void
|
|
|
|
{
|
|
|
|
/** @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(): void
|
|
|
|
{
|
|
|
|
/** @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(): void
|
|
|
|
{
|
|
|
|
/** @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(): void
|
|
|
|
{
|
|
|
|
/** @var ServerRequestInterface|MockObject $request */
|
|
|
|
$request = $this->createMock(ServerRequestInterface::class);
|
|
|
|
|
2022-12-21 15:11:37 +01:00
|
|
|
$this->expectException(LogicException::class);
|
2018-08-11 23:46:28 +02:00
|
|
|
|
|
|
|
$dispatcher = new Dispatcher();
|
|
|
|
$dispatcher->handle($request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Middleware\Dispatcher::handle
|
|
|
|
*/
|
|
|
|
public function testHandleCallResolve(): void
|
|
|
|
{
|
|
|
|
/** @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(TypeError::class);
|
|
|
|
$dispatcher->handle($request);
|
2018-08-11 23:46:28 +02:00
|
|
|
}
|
|
|
|
|
2022-12-21 15:11:37 +01:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Middleware\Dispatcher::handle
|
|
|
|
*/
|
|
|
|
public function testHandleCallResolveInvalidTypeResolved(): void
|
|
|
|
{
|
|
|
|
$instance = new Dispatcher([new ReturnResponseMiddlewareHandler(new Response())]);
|
|
|
|
|
|
|
|
$this->expectException(InvalidArgumentException::class);
|
|
|
|
$instance->handle(new 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(): void
|
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
|
|
|
}
|
|
|
|
}
|