From ff543b151cd05d90b982d591092ac9a1b4363347 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Fri, 21 Oct 2022 19:29:02 +0200 Subject: [PATCH] Migrated NotFoundTrackerMiddlewareTest to use PHPUnit mocks --- .../NotFoundTrackerMiddlewareTest.php | 31 +++++++------------ 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/module/Core/test/ErrorHandler/NotFoundTrackerMiddlewareTest.php b/module/Core/test/ErrorHandler/NotFoundTrackerMiddlewareTest.php index 81fef1a6..823a7546 100644 --- a/module/Core/test/ErrorHandler/NotFoundTrackerMiddlewareTest.php +++ b/module/Core/test/ErrorHandler/NotFoundTrackerMiddlewareTest.php @@ -4,12 +4,9 @@ declare(strict_types=1); namespace ShlinkioTest\Shlink\Core\ErrorHandler; -use Laminas\Diactoros\Response; use Laminas\Diactoros\ServerRequestFactory; +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,35 +15,31 @@ use Shlinkio\Shlink\Core\Visit\RequestTrackerInterface; class NotFoundTrackerMiddlewareTest extends TestCase { - use ProphecyTrait; - private NotFoundTrackerMiddleware $middleware; private ServerRequestInterface $request; - private ObjectProphecy $requestTracker; - private ObjectProphecy $notFoundType; - private ObjectProphecy $handler; + private MockObject $handler; + private MockObject $requestTracker; protected function setUp(): void { - $this->notFoundType = $this->prophesize(NotFoundType::class); - $this->handler = $this->prophesize(RequestHandlerInterface::class); - $this->handler->handle(Argument::cetera())->willReturn(new Response()); - - $this->requestTracker = $this->prophesize(RequestTrackerInterface::class); - $this->middleware = new NotFoundTrackerMiddleware($this->requestTracker->reveal()); + $this->handler = $this->createMock(RequestHandlerInterface::class); + $this->requestTracker = $this->createMock(RequestTrackerInterface::class); + $this->middleware = new NotFoundTrackerMiddleware($this->requestTracker); $this->request = ServerRequestFactory::fromGlobals()->withAttribute( NotFoundType::class, - $this->notFoundType->reveal(), + $this->createMock(NotFoundType::class), ); } /** @test */ public function delegatesIntoRequestTracker(): void { - $this->middleware->process($this->request, $this->handler->reveal()); + $this->handler->expects($this->once())->method('handle')->with($this->equalTo($this->request)); + $this->requestTracker->expects($this->once())->method('trackNotFoundIfApplicable')->with( + $this->equalTo($this->request), + ); - $this->requestTracker->trackNotFoundIfApplicable($this->request)->shouldHaveBeenCalledOnce(); - $this->handler->handle($this->request)->shouldHaveBeenCalledOnce(); + $this->middleware->process($this->request, $this->handler); } }