Migrated NotFoundTypeResolverMiddlewareTest to use PHPUnit mocks

This commit is contained in:
Alejandro Celaya 2022-10-21 19:32:25 +02:00
parent ff543b151c
commit bf0b58b344

View file

@ -7,10 +7,8 @@ namespace ShlinkioTest\Shlink\Core\ErrorHandler;
use Laminas\Diactoros\Response;
use Laminas\Diactoros\ServerRequestFactory;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Shlinkio\Shlink\Core\ErrorHandler\Model\NotFoundType;
@ -18,30 +16,28 @@ use Shlinkio\Shlink\Core\ErrorHandler\NotFoundTypeResolverMiddleware;
class NotFoundTypeResolverMiddlewareTest extends TestCase
{
use ProphecyTrait;
private NotFoundTypeResolverMiddleware $middleware;
private ObjectProphecy $handler;
private MockObject $handler;
protected function setUp(): void
{
$this->middleware = new NotFoundTypeResolverMiddleware('');
$this->handler = $this->prophesize(RequestHandlerInterface::class);
$this->handler = $this->createMock(RequestHandlerInterface::class);
}
/** @test */
public function notFoundTypeIsAddedToRequest(): void
{
$request = ServerRequestFactory::fromGlobals();
$handle = $this->handler->handle(Argument::that(function (ServerRequestInterface $req) {
$this->handler->expects($this->once())->method('handle')->with(
$this->callback(function (ServerRequestInterface $req): bool {
Assert::assertArrayHasKey(NotFoundType::class, $req->getAttributes());
return true;
}))->willReturn(new Response());
}),
)->willReturn(new Response());
$this->middleware->process($request, $this->handler->reveal());
$this->middleware->process($request, $this->handler);
self::assertArrayNotHasKey(NotFoundType::class, $request->getAttributes());
$handle->shouldHaveBeenCalledOnce();
}
}