From bf0b58b344a5aaca68e393178962de2ec4de2a9a Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Fri, 21 Oct 2022 19:32:25 +0200 Subject: [PATCH] Migrated NotFoundTypeResolverMiddlewareTest to use PHPUnit mocks --- .../NotFoundTypeResolverMiddlewareTest.php | 24 ++++++++----------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/module/Core/test/ErrorHandler/NotFoundTypeResolverMiddlewareTest.php b/module/Core/test/ErrorHandler/NotFoundTypeResolverMiddlewareTest.php index c5d9be79..c2ef419b 100644 --- a/module/Core/test/ErrorHandler/NotFoundTypeResolverMiddlewareTest.php +++ b/module/Core/test/ErrorHandler/NotFoundTypeResolverMiddlewareTest.php @@ -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) { - Assert::assertArrayHasKey(NotFoundType::class, $req->getAttributes()); + $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()); - return true; - }))->willReturn(new Response()); - - $this->middleware->process($request, $this->handler->reveal()); + $this->middleware->process($request, $this->handler); self::assertArrayNotHasKey(NotFoundType::class, $request->getAttributes()); - $handle->shouldHaveBeenCalledOnce(); } }