Migrated ShortCodeUniquenessHelperTest to use PHPUnit mocks

This commit is contained in:
Alejandro Celaya 2022-10-23 17:35:50 +02:00
parent d2f5be1d18
commit 5aaf50d68e

View File

@ -5,9 +5,8 @@ declare(strict_types=1);
namespace ShlinkioTest\Shlink\Core\ShortUrl\Helper; namespace ShlinkioTest\Shlink\Core\ShortUrl\Helper;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\Core\Domain\Entity\Domain; use Shlinkio\Shlink\Core\Domain\Entity\Domain;
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl; use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortCodeUniquenessHelper; use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortCodeUniquenessHelper;
@ -16,19 +15,17 @@ use Shlinkio\Shlink\Core\ShortUrl\Repository\ShortUrlRepository;
class ShortCodeUniquenessHelperTest extends TestCase class ShortCodeUniquenessHelperTest extends TestCase
{ {
use ProphecyTrait;
private ShortCodeUniquenessHelper $helper; private ShortCodeUniquenessHelper $helper;
private ObjectProphecy $em; private MockObject $em;
private ObjectProphecy $shortUrl; private MockObject $shortUrl;
protected function setUp(): void protected function setUp(): void
{ {
$this->em = $this->prophesize(EntityManagerInterface::class); $this->em = $this->createMock(EntityManagerInterface::class);
$this->helper = new ShortCodeUniquenessHelper($this->em->reveal()); $this->helper = new ShortCodeUniquenessHelper($this->em);
$this->shortUrl = $this->prophesize(ShortUrl::class); $this->shortUrl = $this->createMock(ShortUrl::class);
$this->shortUrl->getShortCode()->willReturn('abc123'); $this->shortUrl->method('getShortCode')->willReturn('abc123');
} }
/** /**
@ -39,22 +36,22 @@ class ShortCodeUniquenessHelperTest extends TestCase
{ {
$callIndex = 0; $callIndex = 0;
$expectedCalls = 3; $expectedCalls = 3;
$repo = $this->prophesize(ShortUrlRepository::class); $repo = $this->createMock(ShortUrlRepository::class);
$shortCodeIsInUse = $repo->shortCodeIsInUseWithLock( $repo->expects($this->exactly($expectedCalls))->method('shortCodeIsInUseWithLock')->with(
ShortUrlIdentifier::fromShortCodeAndDomain('abc123', $expectedAuthority), $this->equalTo(ShortUrlIdentifier::fromShortCodeAndDomain('abc123', $expectedAuthority)),
)->will(function () use (&$callIndex, $expectedCalls) { )->willReturnCallback(function () use (&$callIndex, $expectedCalls) {
$callIndex++; $callIndex++;
return $callIndex < $expectedCalls; return $callIndex < $expectedCalls;
}); });
$getRepo = $this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal()); $this->em->expects($this->exactly($expectedCalls))->method('getRepository')->with(
$this->shortUrl->getDomain()->willReturn($domain); $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); self::assertTrue($result);
$this->shortUrl->regenerateShortCode()->shouldHaveBeenCalledTimes($expectedCalls - 1);
$getRepo->shouldBeCalledTimes($expectedCalls);
$shortCodeIsInUse->shouldBeCalledTimes($expectedCalls);
} }
public function provideDomains(): iterable public function provideDomains(): iterable
@ -66,18 +63,18 @@ class ShortCodeUniquenessHelperTest extends TestCase
/** @test */ /** @test */
public function inUseSlugReturnsError(): void public function inUseSlugReturnsError(): void
{ {
$repo = $this->prophesize(ShortUrlRepository::class); $repo = $this->createMock(ShortUrlRepository::class);
$shortCodeIsInUse = $repo->shortCodeIsInUseWithLock( $repo->expects($this->once())->method('shortCodeIsInUseWithLock')->with(
ShortUrlIdentifier::fromShortCodeAndDomain('abc123'), $this->equalTo(ShortUrlIdentifier::fromShortCodeAndDomain('abc123')),
)->willReturn(true); )->willReturn(true);
$getRepo = $this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal()); $this->em->expects($this->once())->method('getRepository')->with($this->equalTo(ShortUrl::class))->willReturn(
$this->shortUrl->getDomain()->willReturn(null); $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); self::assertFalse($result);
$this->shortUrl->regenerateShortCode()->shouldNotHaveBeenCalled();
$getRepo->shouldBeCalledOnce();
$shortCodeIsInUse->shouldBeCalledOnce();
} }
} }