From 843754b7e7cdfe6df7cebe6f7f4f24768dec58f1 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Fri, 21 Oct 2022 18:32:34 +0200 Subject: [PATCH] Migrated PixelActionTest to use PHPUnit mocks --- module/Core/test/Action/PixelActionTest.php | 27 +++++++++------------ 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/module/Core/test/Action/PixelActionTest.php b/module/Core/test/Action/PixelActionTest.php index 3d301945..7a150970 100644 --- a/module/Core/test/Action/PixelActionTest.php +++ b/module/Core/test/Action/PixelActionTest.php @@ -5,10 +5,8 @@ declare(strict_types=1); namespace ShlinkioTest\Shlink\Core\Action; use Laminas\Diactoros\ServerRequest; +use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; -use Prophecy\Argument; -use Prophecy\PhpUnit\ProphecyTrait; -use Prophecy\Prophecy\ObjectProphecy; use Psr\Http\Server\RequestHandlerInterface; use Shlinkio\Shlink\Common\Response\PixelResponse; use Shlinkio\Shlink\Core\Action\PixelAction; @@ -19,32 +17,29 @@ use Shlinkio\Shlink\Core\Visit\RequestTrackerInterface; class PixelActionTest extends TestCase { - use ProphecyTrait; - private PixelAction $action; - private ObjectProphecy $urlResolver; - private ObjectProphecy $requestTracker; + private MockObject $urlResolver; + private MockObject $requestTracker; protected function setUp(): void { - $this->urlResolver = $this->prophesize(ShortUrlResolverInterface::class); - $this->requestTracker = $this->prophesize(RequestTrackerInterface::class); + $this->urlResolver = $this->createMock(ShortUrlResolverInterface::class); + $this->requestTracker = $this->createMock(RequestTrackerInterface::class); - $this->action = new PixelAction($this->urlResolver->reveal(), $this->requestTracker->reveal()); + $this->action = new PixelAction($this->urlResolver, $this->requestTracker); } /** @test */ public function imageIsReturned(): void { $shortCode = 'abc123'; - $this->urlResolver->resolveEnabledShortUrl( - ShortUrlIdentifier::fromShortCodeAndDomain($shortCode, ''), - )->willReturn(ShortUrl::withLongUrl('http://domain.com/foo/bar')) - ->shouldBeCalledOnce(); - $this->requestTracker->trackIfApplicable(Argument::cetera())->shouldBeCalledOnce(); + $this->urlResolver->expects($this->once())->method('resolveEnabledShortUrl')->with( + $this->equalTo(ShortUrlIdentifier::fromShortCodeAndDomain($shortCode, '')), + )->willReturn(ShortUrl::withLongUrl('http://domain.com/foo/bar')); + $this->requestTracker->expects($this->once())->method('trackIfApplicable')->withAnyParameters(); $request = (new ServerRequest())->withAttribute('shortCode', $shortCode); - $response = $this->action->process($request, $this->prophesize(RequestHandlerInterface::class)->reveal()); + $response = $this->action->process($request, $this->createMock(RequestHandlerInterface::class)); self::assertInstanceOf(PixelResponse::class, $response); self::assertEquals(200, $response->getStatusCode());