From 230e56370a084e24071beca436f9f291474a8398 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Fri, 21 Oct 2022 19:24:39 +0200 Subject: [PATCH] Migrated NotFoundRedirectHandlerTest to use PHPUnit mocks --- module/Core/test/Action/QrCodeActionTest.php | 2 +- .../Core/test/Action/RedirectActionTest.php | 2 +- .../NotFoundRedirectHandlerTest.php | 119 +++++++++--------- 3 files changed, 61 insertions(+), 62 deletions(-) diff --git a/module/Core/test/Action/QrCodeActionTest.php b/module/Core/test/Action/QrCodeActionTest.php index 8bec70e4..06df1ce8 100644 --- a/module/Core/test/Action/QrCodeActionTest.php +++ b/module/Core/test/Action/QrCodeActionTest.php @@ -42,7 +42,7 @@ class QrCodeActionTest extends TestCase { $shortCode = 'abc123'; $this->urlResolver->expects($this->once())->method('resolveEnabledShortUrl')->with( - $this->equalTo(ShortUrlIdentifier::fromShortCodeAndDomain($shortCode, '')) + $this->equalTo(ShortUrlIdentifier::fromShortCodeAndDomain($shortCode, '')), )->willThrowException(ShortUrlNotFoundException::fromNotFound(ShortUrlIdentifier::fromShortCodeAndDomain(''))); $delegate = $this->createMock(RequestHandlerInterface::class); $delegate->expects($this->once())->method('handle')->withAnyParameters()->willReturn(new Response()); diff --git a/module/Core/test/Action/RedirectActionTest.php b/module/Core/test/Action/RedirectActionTest.php index f17d32ba..06b7ac40 100644 --- a/module/Core/test/Action/RedirectActionTest.php +++ b/module/Core/test/Action/RedirectActionTest.php @@ -69,7 +69,7 @@ class RedirectActionTest extends TestCase { $shortCode = 'abc123'; $this->urlResolver->expects($this->once())->method('resolveEnabledShortUrl')->with( - $this->equalTo(ShortUrlIdentifier::fromShortCodeAndDomain($shortCode, '')) + $this->equalTo(ShortUrlIdentifier::fromShortCodeAndDomain($shortCode, '')), )->willThrowException(ShortUrlNotFoundException::fromNotFound(ShortUrlIdentifier::fromShortCodeAndDomain(''))); $this->requestTracker->expects($this->never())->method('trackIfApplicable'); diff --git a/module/Core/test/ErrorHandler/NotFoundRedirectHandlerTest.php b/module/Core/test/ErrorHandler/NotFoundRedirectHandlerTest.php index d08073e2..964f5da4 100644 --- a/module/Core/test/ErrorHandler/NotFoundRedirectHandlerTest.php +++ b/module/Core/test/ErrorHandler/NotFoundRedirectHandlerTest.php @@ -6,10 +6,8 @@ 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\Message\UriInterface; use Psr\Http\Server\RequestHandlerInterface; @@ -22,31 +20,25 @@ use Shlinkio\Shlink\Core\Options\NotFoundRedirectOptions; class NotFoundRedirectHandlerTest extends TestCase { - use ProphecyTrait; - private NotFoundRedirectHandler $middleware; private NotFoundRedirectOptions $redirectOptions; - private ObjectProphecy $resolver; - private ObjectProphecy $domainService; - private ObjectProphecy $next; + private MockObject $resolver; + private MockObject $domainService; + private MockObject $next; private ServerRequestInterface $req; protected function setUp(): void { $this->redirectOptions = new NotFoundRedirectOptions(); - $this->resolver = $this->prophesize(NotFoundRedirectResolverInterface::class); - $this->domainService = $this->prophesize(DomainServiceInterface::class); + $this->resolver = $this->createMock(NotFoundRedirectResolverInterface::class); + $this->domainService = $this->createMock(DomainServiceInterface::class); - $this->middleware = new NotFoundRedirectHandler( - $this->redirectOptions, - $this->resolver->reveal(), - $this->domainService->reveal(), - ); + $this->middleware = new NotFoundRedirectHandler($this->redirectOptions, $this->resolver, $this->domainService); - $this->next = $this->prophesize(RequestHandlerInterface::class); + $this->next = $this->createMock(RequestHandlerInterface::class); $this->req = ServerRequestFactory::fromGlobals()->withAttribute( NotFoundType::class, - $this->prophesize(NotFoundType::class)->reveal(), + $this->createMock(NotFoundType::class), ); } @@ -59,40 +51,49 @@ class NotFoundRedirectHandlerTest extends TestCase $expectedResp = new Response(); $setUp($this->domainService, $this->resolver); - $handle = $this->next->handle($this->req)->willReturn($expectedResp); + $this->next->expects($this->once())->method('handle')->with($this->equalTo($this->req))->willReturn( + $expectedResp, + ); - $result = $this->middleware->process($this->req, $this->next->reveal()); + $result = $this->middleware->process($this->req, $this->next); self::assertSame($expectedResp, $result); - $handle->shouldHaveBeenCalledOnce(); } public function provideNonRedirectScenarios(): iterable { - yield 'no domain' => [function (ObjectProphecy $domainService, ObjectProphecy $resolver): void { - $domainService->findByAuthority(Argument::cetera()) - ->willReturn(null) - ->shouldBeCalledOnce(); - $resolver->resolveRedirectResponse( - Argument::type(NotFoundType::class), - Argument::type(NotFoundRedirectOptions::class), - Argument::type(UriInterface::class), - )->willReturn(null)->shouldBeCalledOnce(); + yield 'no domain' => [function ( + MockObject&DomainServiceInterface $domainService, + MockObject&NotFoundRedirectResolverInterface $resolver, + ): void { + $domainService->expects($this->once())->method('findByAuthority')->withAnyParameters()->willReturn( + null, + ); + $resolver->expects($this->once())->method('resolveRedirectResponse')->with( + $this->isInstanceOf(NotFoundType::class), + $this->isInstanceOf(NotFoundRedirectOptions::class), + $this->isInstanceOf(UriInterface::class), + )->willReturn(null); }]; - yield 'non-redirecting domain' => [function (ObjectProphecy $domainService, ObjectProphecy $resolver): void { - $domainService->findByAuthority(Argument::cetera()) - ->willReturn(Domain::withAuthority('')) - ->shouldBeCalledOnce(); - $resolver->resolveRedirectResponse( - Argument::type(NotFoundType::class), - Argument::type(NotFoundRedirectOptions::class), - Argument::type(UriInterface::class), - )->willReturn(null)->shouldBeCalledOnce(); - $resolver->resolveRedirectResponse( - Argument::type(NotFoundType::class), - Argument::type(Domain::class), - Argument::type(UriInterface::class), - )->willReturn(null)->shouldBeCalledOnce(); + yield 'non-redirecting domain' => [function ( + MockObject&DomainServiceInterface $domainService, + MockObject&NotFoundRedirectResolverInterface $resolver, + ): void { + $domainService->expects($this->once())->method('findByAuthority')->withAnyParameters()->willReturn( + Domain::withAuthority(''), + ); + $resolver->expects($this->exactly(2))->method('resolveRedirectResponse')->withConsecutive( + [ + $this->isInstanceOf(NotFoundType::class), + $this->isInstanceOf(Domain::class), + $this->isInstanceOf(UriInterface::class), + ], + [ + $this->isInstanceOf(NotFoundType::class), + $this->isInstanceOf(NotFoundRedirectOptions::class), + $this->isInstanceOf(UriInterface::class), + ], + )->willReturn(null); }]; } @@ -101,19 +102,17 @@ class NotFoundRedirectHandlerTest extends TestCase { $expectedResp = new Response(); - $findDomain = $this->domainService->findByAuthority(Argument::cetera())->willReturn(null); - $resolveRedirect = $this->resolver->resolveRedirectResponse( - Argument::type(NotFoundType::class), - $this->redirectOptions, - Argument::type(UriInterface::class), + $this->domainService->expects($this->once())->method('findByAuthority')->withAnyParameters()->willReturn(null); + $this->resolver->expects($this->once())->method('resolveRedirectResponse')->with( + $this->isInstanceOf(NotFoundType::class), + $this->equalTo($this->redirectOptions), + $this->isInstanceOf(UriInterface::class), )->willReturn($expectedResp); + $this->next->expects($this->never())->method('handle'); - $result = $this->middleware->process($this->req, $this->next->reveal()); + $result = $this->middleware->process($this->req, $this->next); self::assertSame($expectedResp, $result); - $findDomain->shouldHaveBeenCalledOnce(); - $resolveRedirect->shouldHaveBeenCalledOnce(); - $this->next->handle(Argument::cetera())->shouldNotHaveBeenCalled(); } /** @test */ @@ -122,18 +121,18 @@ class NotFoundRedirectHandlerTest extends TestCase $expectedResp = new Response(); $domain = Domain::withAuthority(''); - $findDomain = $this->domainService->findByAuthority(Argument::cetera())->willReturn($domain); - $resolveRedirect = $this->resolver->resolveRedirectResponse( - Argument::type(NotFoundType::class), + $this->domainService->expects($this->once())->method('findByAuthority')->withAnyParameters()->willReturn( $domain, - Argument::type(UriInterface::class), + ); + $this->resolver->expects($this->once())->method('resolveRedirectResponse')->with( + $this->isInstanceOf(NotFoundType::class), + $this->equalTo($domain), + $this->isInstanceOf(UriInterface::class), )->willReturn($expectedResp); + $this->next->expects($this->never())->method('handle'); - $result = $this->middleware->process($this->req, $this->next->reveal()); + $result = $this->middleware->process($this->req, $this->next); self::assertSame($expectedResp, $result); - $findDomain->shouldHaveBeenCalledOnce(); - $resolveRedirect->shouldHaveBeenCalledOnce(); - $this->next->handle(Argument::cetera())->shouldNotHaveBeenCalled(); } }