Migrated ShortUrlServiceTest to use PHPUnit mocks

This commit is contained in:
Alejandro Celaya 2022-10-23 11:14:01 +02:00
parent ee8cab8455
commit 36ab455a49

View File

@ -6,10 +6,8 @@ namespace ShlinkioTest\Shlink\Core\ShortUrl;
use Cake\Chronos\Chronos; use Cake\Chronos\Chronos;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl; use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortUrlTitleResolutionHelperInterface; use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortUrlTitleResolutionHelperInterface;
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlEdition; use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlEdition;
@ -27,26 +25,25 @@ use function count;
class ShortUrlServiceTest extends TestCase class ShortUrlServiceTest extends TestCase
{ {
use ApiKeyHelpersTrait; use ApiKeyHelpersTrait;
use ProphecyTrait;
private ShortUrlService $service; private ShortUrlService $service;
private ObjectProphecy $em; private MockObject $em;
private ObjectProphecy $urlResolver; private MockObject $urlResolver;
private ObjectProphecy $titleResolutionHelper; private MockObject $titleResolutionHelper;
protected function setUp(): void protected function setUp(): void
{ {
$this->em = $this->prophesize(EntityManagerInterface::class); $this->em = $this->createMock(EntityManagerInterface::class);
$this->em->persist(Argument::any())->willReturn(null); $this->em->method('persist')->willReturn(null);
$this->em->flush()->willReturn(null); $this->em->method('flush')->willReturn(null);
$this->urlResolver = $this->prophesize(ShortUrlResolverInterface::class); $this->urlResolver = $this->createMock(ShortUrlResolverInterface::class);
$this->titleResolutionHelper = $this->prophesize(ShortUrlTitleResolutionHelperInterface::class); $this->titleResolutionHelper = $this->createMock(ShortUrlTitleResolutionHelperInterface::class);
$this->service = new ShortUrlService( $this->service = new ShortUrlService(
$this->em->reveal(), $this->em,
$this->urlResolver->reveal(), $this->urlResolver,
$this->titleResolutionHelper->reveal(), $this->titleResolutionHelper,
new SimpleShortUrlRelationResolver(), new SimpleShortUrlRelationResolver(),
); );
} }
@ -64,10 +61,10 @@ class ShortUrlServiceTest extends TestCase
ShortUrl::createEmpty(), ShortUrl::createEmpty(),
]; ];
$repo = $this->prophesize(ShortUrlRepository::class); $repo = $this->createMock(ShortUrlRepository::class);
$repo->findList(Argument::cetera())->willReturn($list)->shouldBeCalledOnce(); $repo->expects($this->once())->method('findList')->willReturn($list);
$repo->countList(Argument::cetera())->willReturn(count($list))->shouldBeCalledOnce(); $repo->expects($this->once())->method('countList')->willReturn(count($list));
$this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal()); $this->em->method('getRepository')->with($this->equalTo(ShortUrl::class))->willReturn($repo);
$paginator = $this->service->listShortUrls(ShortUrlsParams::emptyInstance(), $apiKey); $paginator = $this->service->listShortUrls(ShortUrlsParams::emptyInstance(), $apiKey);
@ -87,15 +84,15 @@ class ShortUrlServiceTest extends TestCase
$originalLongUrl = 'originalLongUrl'; $originalLongUrl = 'originalLongUrl';
$shortUrl = ShortUrl::withLongUrl($originalLongUrl); $shortUrl = ShortUrl::withLongUrl($originalLongUrl);
$findShortUrl = $this->urlResolver->resolveShortUrl( $this->urlResolver->expects($this->once())->method('resolveShortUrl')->with(
ShortUrlIdentifier::fromShortCodeAndDomain('abc123'), $this->equalTo(ShortUrlIdentifier::fromShortCodeAndDomain('abc123')),
$apiKey, $this->equalTo($apiKey),
)->willReturn($shortUrl); )->willReturn($shortUrl);
$flush = $this->em->flush()->willReturn(null);
$processTitle = $this->titleResolutionHelper->processTitleAndValidateUrl($shortUrlEdit)->willReturn( $this->titleResolutionHelper->expects($this->exactly($expectedValidateCalls))
$shortUrlEdit, ->method('processTitleAndValidateUrl')
); ->with($this->equalTo($shortUrlEdit))
->willReturn($shortUrlEdit);
$result = $this->service->updateShortUrl( $result = $this->service->updateShortUrl(
ShortUrlIdentifier::fromShortCodeAndDomain('abc123'), ShortUrlIdentifier::fromShortCodeAndDomain('abc123'),
@ -108,9 +105,6 @@ class ShortUrlServiceTest extends TestCase
self::assertEquals($shortUrlEdit->validUntil(), $shortUrl->getValidUntil()); self::assertEquals($shortUrlEdit->validUntil(), $shortUrl->getValidUntil());
self::assertEquals($shortUrlEdit->maxVisits(), $shortUrl->getMaxVisits()); self::assertEquals($shortUrlEdit->maxVisits(), $shortUrl->getMaxVisits());
self::assertEquals($shortUrlEdit->longUrl() ?? $originalLongUrl, $shortUrl->getLongUrl()); self::assertEquals($shortUrlEdit->longUrl() ?? $originalLongUrl, $shortUrl->getLongUrl());
$findShortUrl->shouldHaveBeenCalled();
$flush->shouldHaveBeenCalled();
$processTitle->shouldHaveBeenCalledTimes($expectedValidateCalls);
} }
public function provideShortUrlEdits(): iterable public function provideShortUrlEdits(): iterable