Migrated ShortUrlResolverTest to use PHPUnit mocks

This commit is contained in:
Alejandro Celaya 2022-10-23 11:09:40 +02:00
parent bd884e85d4
commit ee8cab8455

View file

@ -7,9 +7,8 @@ namespace ShlinkioTest\Shlink\Core\ShortUrl;
use Cake\Chronos\Chronos; use Cake\Chronos\Chronos;
use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\ArrayCollection;
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\Exception\ShortUrlNotFoundException; use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException;
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl; use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlCreation; use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlCreation;
@ -27,15 +26,16 @@ use function range;
class ShortUrlResolverTest extends TestCase class ShortUrlResolverTest extends TestCase
{ {
use ApiKeyHelpersTrait; use ApiKeyHelpersTrait;
use ProphecyTrait;
private ShortUrlResolver $urlResolver; private ShortUrlResolver $urlResolver;
private ObjectProphecy $em; private MockObject $em;
private MockObject $repo;
protected function setUp(): void protected function setUp(): void
{ {
$this->em = $this->prophesize(EntityManagerInterface::class); $this->em = $this->createMock(EntityManagerInterface::class);
$this->urlResolver = new ShortUrlResolver($this->em->reveal()); $this->repo = $this->createMock(ShortUrlRepositoryInterface::class);
$this->urlResolver = new ShortUrlResolver($this->em);
} }
/** /**
@ -48,15 +48,17 @@ class ShortUrlResolverTest extends TestCase
$shortCode = $shortUrl->getShortCode(); $shortCode = $shortUrl->getShortCode();
$identifier = ShortUrlIdentifier::fromShortCodeAndDomain($shortCode); $identifier = ShortUrlIdentifier::fromShortCodeAndDomain($shortCode);
$repo = $this->prophesize(ShortUrlRepositoryInterface::class); $this->repo->expects($this->once())->method('findOne')->with(
$findOne = $repo->findOne($identifier, $apiKey?->spec())->willReturn($shortUrl); $this->equalTo($identifier),
$getRepo = $this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal()); $this->equalTo($apiKey?->spec()),
)->willReturn($shortUrl);
$this->em->expects($this->once())->method('getRepository')->with($this->equalTo(ShortUrl::class))->willReturn(
$this->repo,
);
$result = $this->urlResolver->resolveShortUrl($identifier, $apiKey); $result = $this->urlResolver->resolveShortUrl($identifier, $apiKey);
self::assertSame($shortUrl, $result); self::assertSame($shortUrl, $result);
$findOne->shouldHaveBeenCalledOnce();
$getRepo->shouldHaveBeenCalledOnce();
} }
/** /**
@ -68,13 +70,15 @@ class ShortUrlResolverTest extends TestCase
$shortCode = 'abc123'; $shortCode = 'abc123';
$identifier = ShortUrlIdentifier::fromShortCodeAndDomain($shortCode); $identifier = ShortUrlIdentifier::fromShortCodeAndDomain($shortCode);
$repo = $this->prophesize(ShortUrlRepositoryInterface::class); $this->repo->expects($this->once())->method('findOne')->with(
$findOne = $repo->findOne($identifier, $apiKey?->spec())->willReturn(null); $this->equalTo($identifier),
$getRepo = $this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal(), $apiKey); $this->equalTo($apiKey?->spec()),
)->willReturn(null);
$this->em->expects($this->once())->method('getRepository')->with($this->equalTo(ShortUrl::class))->willReturn(
$this->repo,
);
$this->expectException(ShortUrlNotFoundException::class); $this->expectException(ShortUrlNotFoundException::class);
$findOne->shouldBeCalledOnce();
$getRepo->shouldBeCalledOnce();
$this->urlResolver->resolveShortUrl($identifier, $apiKey); $this->urlResolver->resolveShortUrl($identifier, $apiKey);
} }
@ -85,17 +89,16 @@ class ShortUrlResolverTest extends TestCase
$shortUrl = ShortUrl::withLongUrl('expected_url'); $shortUrl = ShortUrl::withLongUrl('expected_url');
$shortCode = $shortUrl->getShortCode(); $shortCode = $shortUrl->getShortCode();
$repo = $this->prophesize(ShortUrlRepositoryInterface::class); $this->repo->expects($this->once())->method('findOneWithDomainFallback')->with(
$findOneByShortCode = $repo->findOneWithDomainFallback( $this->equalTo(ShortUrlIdentifier::fromShortCodeAndDomain($shortCode)),
ShortUrlIdentifier::fromShortCodeAndDomain($shortCode),
)->willReturn($shortUrl); )->willReturn($shortUrl);
$getRepo = $this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal()); $this->em->expects($this->once())->method('getRepository')->with($this->equalTo(ShortUrl::class))->willReturn(
$this->repo,
);
$result = $this->urlResolver->resolveEnabledShortUrl(ShortUrlIdentifier::fromShortCodeAndDomain($shortCode)); $result = $this->urlResolver->resolveEnabledShortUrl(ShortUrlIdentifier::fromShortCodeAndDomain($shortCode));
self::assertSame($shortUrl, $result); self::assertSame($shortUrl, $result);
$findOneByShortCode->shouldHaveBeenCalledOnce();
$getRepo->shouldHaveBeenCalledOnce();
} }
/** /**
@ -106,15 +109,14 @@ class ShortUrlResolverTest extends TestCase
{ {
$shortCode = $shortUrl->getShortCode(); $shortCode = $shortUrl->getShortCode();
$repo = $this->prophesize(ShortUrlRepositoryInterface::class); $this->repo->expects($this->once())->method('findOneWithDomainFallback')->with(
$findOneByShortCode = $repo->findOneWithDomainFallback( $this->equalTo(ShortUrlIdentifier::fromShortCodeAndDomain($shortCode)),
ShortUrlIdentifier::fromShortCodeAndDomain($shortCode),
)->willReturn($shortUrl); )->willReturn($shortUrl);
$getRepo = $this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal()); $this->em->expects($this->once())->method('getRepository')->with($this->equalTo(ShortUrl::class))->willReturn(
$this->repo,
);
$this->expectException(ShortUrlNotFoundException::class); $this->expectException(ShortUrlNotFoundException::class);
$findOneByShortCode->shouldBeCalledOnce();
$getRepo->shouldBeCalledOnce();
$this->urlResolver->resolveEnabledShortUrl(ShortUrlIdentifier::fromShortCodeAndDomain($shortCode)); $this->urlResolver->resolveEnabledShortUrl(ShortUrlIdentifier::fromShortCodeAndDomain($shortCode));
} }