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