2018-08-25 21:16:20 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Engelsystem\Test\Unit\Middleware;
|
|
|
|
|
|
|
|
use Engelsystem\Container\Container;
|
|
|
|
use Engelsystem\Http\Response;
|
|
|
|
use Engelsystem\Middleware\CallableHandler;
|
|
|
|
use Engelsystem\Test\Unit\Middleware\Stub\HasStaticMethod;
|
2019-04-24 11:01:37 +02:00
|
|
|
use InvalidArgumentException;
|
2018-08-25 21:16:20 +02:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Psr\Http\Message\ResponseInterface;
|
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
|
|
use Psr\Http\Server\RequestHandlerInterface;
|
|
|
|
use stdClass;
|
|
|
|
|
|
|
|
class CallableHandlerTest extends TestCase
|
|
|
|
{
|
2022-12-14 19:15:20 +01:00
|
|
|
public function provideCallable(): array
|
2018-08-25 21:16:20 +02:00
|
|
|
{
|
|
|
|
return [
|
2022-12-14 19:15:20 +01:00
|
|
|
[function (): void {
|
2019-04-24 11:01:37 +02:00
|
|
|
}],
|
2018-08-25 21:16:20 +02:00
|
|
|
[[$this, 'provideCallable']],
|
|
|
|
[[HasStaticMethod::class, 'foo']],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideCallable
|
|
|
|
* @covers \Engelsystem\Middleware\CallableHandler::__construct
|
2018-11-14 02:17:27 +01:00
|
|
|
* @covers \Engelsystem\Middleware\CallableHandler::getCallable
|
2018-08-25 21:16:20 +02:00
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testInit(callable $callable): void
|
2018-08-25 21:16:20 +02:00
|
|
|
{
|
|
|
|
$handler = new CallableHandler($callable);
|
|
|
|
|
2018-11-14 02:17:27 +01:00
|
|
|
$this->assertEquals($callable, $handler->getCallable());
|
2018-08-25 21:16:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Middleware\CallableHandler::process
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testProcess(): void
|
2018-08-25 21:16:20 +02:00
|
|
|
{
|
|
|
|
/** @var ServerRequestInterface|MockObject $request */
|
|
|
|
/** @var ResponseInterface|MockObject $response */
|
|
|
|
/** @var callable|MockObject $callable */
|
|
|
|
/** @var RequestHandlerInterface|MockObject $handler */
|
|
|
|
list($request, $response, $callable, $handler) = $this->getMocks();
|
|
|
|
|
|
|
|
$callable->expects($this->once())
|
|
|
|
->method('__invoke')
|
|
|
|
->with($request, $handler)
|
|
|
|
->willReturn($response);
|
|
|
|
|
|
|
|
$middleware = new CallableHandler($callable);
|
|
|
|
$middleware->process($request, $handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Middleware\CallableHandler::handle
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testHandler(): void
|
2018-08-25 21:16:20 +02:00
|
|
|
{
|
|
|
|
/** @var ServerRequestInterface|MockObject $request */
|
|
|
|
/** @var ResponseInterface|MockObject $response */
|
|
|
|
/** @var callable|MockObject $callable */
|
|
|
|
list($request, $response, $callable) = $this->getMocks();
|
|
|
|
|
|
|
|
$callable->expects($this->once())
|
|
|
|
->method('__invoke')
|
|
|
|
->with($request)
|
|
|
|
->willReturn($response);
|
|
|
|
|
|
|
|
$middleware = new CallableHandler($callable);
|
|
|
|
$middleware->handle($request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Middleware\CallableHandler::execute
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testExecute(): void
|
2018-08-25 21:16:20 +02:00
|
|
|
{
|
|
|
|
/** @var ServerRequestInterface|MockObject $request */
|
|
|
|
/** @var Response|MockObject $response */
|
|
|
|
/** @var callable|MockObject $callable */
|
|
|
|
list($request, $response, $callable) = $this->getMocks();
|
|
|
|
/** @var Container|MockObject $container */
|
|
|
|
$container = $this->createMock(Container::class);
|
|
|
|
|
|
|
|
$callable->expects($this->exactly(3))
|
|
|
|
->method('__invoke')
|
|
|
|
->with($request)
|
|
|
|
->willReturnOnConsecutiveCalls($response, 'Lorem ipsum?', 'I\'m not an exception!');
|
|
|
|
|
|
|
|
$container->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->with('response')
|
|
|
|
->willReturn($response);
|
|
|
|
|
|
|
|
$response->expects($this->once())
|
|
|
|
->method('withContent')
|
|
|
|
->with('Lorem ipsum?')
|
|
|
|
->willReturn($response);
|
|
|
|
|
|
|
|
$middleware = new CallableHandler($callable, $container);
|
|
|
|
$return = $middleware->handle($request);
|
|
|
|
$this->assertInstanceOf(ResponseInterface::class, $return);
|
|
|
|
$this->assertEquals($response, $return);
|
|
|
|
|
|
|
|
$return = $middleware->handle($request);
|
|
|
|
$this->assertInstanceOf(ResponseInterface::class, $return);
|
|
|
|
$this->assertEquals($response, $return);
|
|
|
|
|
|
|
|
$middleware = new CallableHandler($callable);
|
2019-04-24 11:01:37 +02:00
|
|
|
$this->expectException(InvalidArgumentException::class);
|
2018-08-25 21:16:20 +02:00
|
|
|
$middleware->handle($request);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getMocks(): array
|
|
|
|
{
|
|
|
|
/** @var ServerRequestInterface|MockObject $request */
|
|
|
|
$request = $this->getMockForAbstractClass(ServerRequestInterface::class);
|
|
|
|
/** @var RequestHandlerInterface|MockObject $handler */
|
|
|
|
$handler = $this->getMockForAbstractClass(RequestHandlerInterface::class);
|
|
|
|
/** @var Response|MockObject $response */
|
|
|
|
$response = $this->createMock(Response::class);
|
|
|
|
/** @var callable|MockObject $callable */
|
|
|
|
$callable = $this->getMockBuilder(stdClass::class)
|
2019-11-06 12:29:58 +01:00
|
|
|
->addMethods(['__invoke'])
|
2018-08-25 21:16:20 +02:00
|
|
|
->getMock();
|
2018-09-04 21:44:34 +02:00
|
|
|
return [$request, $response, $callable, $handler];
|
2018-08-25 21:16:20 +02:00
|
|
|
}
|
|
|
|
}
|