From 5aaf50d68e01638877a5680deb0b301ce04b436e Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 23 Oct 2022 17:35:50 +0200 Subject: [PATCH] Migrated ShortCodeUniquenessHelperTest to use PHPUnit mocks --- .../Helper/ShortCodeUniquenessHelperTest.php | 55 +++++++++---------- 1 file changed, 26 insertions(+), 29 deletions(-) diff --git a/module/Core/test/ShortUrl/Helper/ShortCodeUniquenessHelperTest.php b/module/Core/test/ShortUrl/Helper/ShortCodeUniquenessHelperTest.php index 23f45506..0891c8ae 100644 --- a/module/Core/test/ShortUrl/Helper/ShortCodeUniquenessHelperTest.php +++ b/module/Core/test/ShortUrl/Helper/ShortCodeUniquenessHelperTest.php @@ -5,9 +5,8 @@ declare(strict_types=1); namespace ShlinkioTest\Shlink\Core\ShortUrl\Helper; use Doctrine\ORM\EntityManagerInterface; +use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; -use Prophecy\PhpUnit\ProphecyTrait; -use Prophecy\Prophecy\ObjectProphecy; use Shlinkio\Shlink\Core\Domain\Entity\Domain; use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl; use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortCodeUniquenessHelper; @@ -16,19 +15,17 @@ use Shlinkio\Shlink\Core\ShortUrl\Repository\ShortUrlRepository; class ShortCodeUniquenessHelperTest extends TestCase { - use ProphecyTrait; - private ShortCodeUniquenessHelper $helper; - private ObjectProphecy $em; - private ObjectProphecy $shortUrl; + private MockObject $em; + private MockObject $shortUrl; protected function setUp(): void { - $this->em = $this->prophesize(EntityManagerInterface::class); - $this->helper = new ShortCodeUniquenessHelper($this->em->reveal()); + $this->em = $this->createMock(EntityManagerInterface::class); + $this->helper = new ShortCodeUniquenessHelper($this->em); - $this->shortUrl = $this->prophesize(ShortUrl::class); - $this->shortUrl->getShortCode()->willReturn('abc123'); + $this->shortUrl = $this->createMock(ShortUrl::class); + $this->shortUrl->method('getShortCode')->willReturn('abc123'); } /** @@ -39,22 +36,22 @@ class ShortCodeUniquenessHelperTest extends TestCase { $callIndex = 0; $expectedCalls = 3; - $repo = $this->prophesize(ShortUrlRepository::class); - $shortCodeIsInUse = $repo->shortCodeIsInUseWithLock( - ShortUrlIdentifier::fromShortCodeAndDomain('abc123', $expectedAuthority), - )->will(function () use (&$callIndex, $expectedCalls) { + $repo = $this->createMock(ShortUrlRepository::class); + $repo->expects($this->exactly($expectedCalls))->method('shortCodeIsInUseWithLock')->with( + $this->equalTo(ShortUrlIdentifier::fromShortCodeAndDomain('abc123', $expectedAuthority)), + )->willReturnCallback(function () use (&$callIndex, $expectedCalls) { $callIndex++; return $callIndex < $expectedCalls; }); - $getRepo = $this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal()); - $this->shortUrl->getDomain()->willReturn($domain); + $this->em->expects($this->exactly($expectedCalls))->method('getRepository')->with( + $this->equalTo(ShortUrl::class), + )->willReturn($repo); + $this->shortUrl->method('getDomain')->willReturn($domain); + $this->shortUrl->expects($this->exactly($expectedCalls - 1))->method('regenerateShortCode')->with(); - $result = $this->helper->ensureShortCodeUniqueness($this->shortUrl->reveal(), false); + $result = $this->helper->ensureShortCodeUniqueness($this->shortUrl, false); self::assertTrue($result); - $this->shortUrl->regenerateShortCode()->shouldHaveBeenCalledTimes($expectedCalls - 1); - $getRepo->shouldBeCalledTimes($expectedCalls); - $shortCodeIsInUse->shouldBeCalledTimes($expectedCalls); } public function provideDomains(): iterable @@ -66,18 +63,18 @@ class ShortCodeUniquenessHelperTest extends TestCase /** @test */ public function inUseSlugReturnsError(): void { - $repo = $this->prophesize(ShortUrlRepository::class); - $shortCodeIsInUse = $repo->shortCodeIsInUseWithLock( - ShortUrlIdentifier::fromShortCodeAndDomain('abc123'), + $repo = $this->createMock(ShortUrlRepository::class); + $repo->expects($this->once())->method('shortCodeIsInUseWithLock')->with( + $this->equalTo(ShortUrlIdentifier::fromShortCodeAndDomain('abc123')), )->willReturn(true); - $getRepo = $this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal()); - $this->shortUrl->getDomain()->willReturn(null); + $this->em->expects($this->once())->method('getRepository')->with($this->equalTo(ShortUrl::class))->willReturn( + $repo, + ); + $this->shortUrl->method('getDomain')->willReturn(null); + $this->shortUrl->expects($this->never())->method('regenerateShortCode'); - $result = $this->helper->ensureShortCodeUniqueness($this->shortUrl->reveal(), true); + $result = $this->helper->ensureShortCodeUniqueness($this->shortUrl, true); self::assertFalse($result); - $this->shortUrl->regenerateShortCode()->shouldNotHaveBeenCalled(); - $getRepo->shouldBeCalledOnce(); - $shortCodeIsInUse->shouldBeCalledOnce(); } }