Migrated DomainServiceTest to use PHPUnit mocks

This commit is contained in:
Alejandro Celaya 2022-10-21 19:01:41 +02:00
parent 148f7a9cfe
commit a8514a9ae4

View File

@ -5,10 +5,8 @@ declare(strict_types=1);
namespace ShlinkioTest\Shlink\Core\Domain; namespace ShlinkioTest\Shlink\Core\Domain;
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\Config\EmptyNotFoundRedirectConfig; use Shlinkio\Shlink\Core\Config\EmptyNotFoundRedirectConfig;
use Shlinkio\Shlink\Core\Config\NotFoundRedirects; use Shlinkio\Shlink\Core\Config\NotFoundRedirects;
use Shlinkio\Shlink\Core\Domain\DomainService; use Shlinkio\Shlink\Core\Domain\DomainService;
@ -22,15 +20,13 @@ use Shlinkio\Shlink\Rest\Entity\ApiKey;
class DomainServiceTest extends TestCase class DomainServiceTest extends TestCase
{ {
use ProphecyTrait;
private DomainService $domainService; private DomainService $domainService;
private ObjectProphecy $em; private MockObject $em;
protected function setUp(): void protected function setUp(): void
{ {
$this->em = $this->prophesize(EntityManagerInterface::class); $this->em = $this->createMock(EntityManagerInterface::class);
$this->domainService = new DomainService($this->em->reveal(), 'default.com'); $this->domainService = new DomainService($this->em, 'default.com');
} }
/** /**
@ -39,15 +35,15 @@ class DomainServiceTest extends TestCase
*/ */
public function listDomainsDelegatesIntoRepository(array $domains, array $expectedResult, ?ApiKey $apiKey): void public function listDomainsDelegatesIntoRepository(array $domains, array $expectedResult, ?ApiKey $apiKey): void
{ {
$repo = $this->prophesize(DomainRepositoryInterface::class); $repo = $this->createMock(DomainRepositoryInterface::class);
$getRepo = $this->em->getRepository(Domain::class)->willReturn($repo->reveal()); $repo->expects($this->once())->method('findDomains')->with($this->equalTo($apiKey))->willReturn($domains);
$findDomains = $repo->findDomains($apiKey)->willReturn($domains); $this->em->expects($this->once())->method('getRepository')->with($this->equalTo(Domain::class))->willReturn(
$repo,
);
$result = $this->domainService->listDomains($apiKey); $result = $this->domainService->listDomains($apiKey);
self::assertEquals($expectedResult, $result); self::assertEquals($expectedResult, $result);
$getRepo->shouldHaveBeenCalledOnce();
$findDomains->shouldHaveBeenCalledOnce();
} }
public function provideExcludedDomains(): iterable public function provideExcludedDomains(): iterable
@ -109,10 +105,12 @@ class DomainServiceTest extends TestCase
/** @test */ /** @test */
public function getDomainThrowsExceptionWhenDomainIsNotFound(): void public function getDomainThrowsExceptionWhenDomainIsNotFound(): void
{ {
$find = $this->em->find(Domain::class, '123')->willReturn(null); $this->em->expects($this->once())->method('find')->with(
$this->equalTo(Domain::class),
$this->equalTo('123'),
)->willReturn(null);
$this->expectException(DomainNotFoundException::class); $this->expectException(DomainNotFoundException::class);
$find->shouldBeCalledOnce();
$this->domainService->getDomain('123'); $this->domainService->getDomain('123');
} }
@ -121,12 +119,14 @@ class DomainServiceTest extends TestCase
public function getDomainReturnsEntityWhenFound(): void public function getDomainReturnsEntityWhenFound(): void
{ {
$domain = Domain::withAuthority(''); $domain = Domain::withAuthority('');
$find = $this->em->find(Domain::class, '123')->willReturn($domain); $this->em->expects($this->once())->method('find')->with(
$this->equalTo(Domain::class),
$this->equalTo('123'),
)->willReturn($domain);
$result = $this->domainService->getDomain('123'); $result = $this->domainService->getDomain('123');
self::assertSame($domain, $result); self::assertSame($domain, $result);
$find->shouldHaveBeenCalledOnce();
} }
/** /**
@ -136,20 +136,23 @@ class DomainServiceTest extends TestCase
public function getOrCreateAlwaysPersistsDomain(?Domain $foundDomain, ?ApiKey $apiKey): void public function getOrCreateAlwaysPersistsDomain(?Domain $foundDomain, ?ApiKey $apiKey): void
{ {
$authority = 'example.com'; $authority = 'example.com';
$repo = $this->prophesize(DomainRepositoryInterface::class); $repo = $this->createMock(DomainRepositoryInterface::class);
$repo->findOneByAuthority($authority, $apiKey)->willReturn($foundDomain); $repo->method('findOneByAuthority')->with($this->equalTo($authority), $this->equalTo($apiKey))->willReturn(
$getRepo = $this->em->getRepository(Domain::class)->willReturn($repo->reveal()); $foundDomain,
$persist = $this->em->persist($foundDomain ?? Argument::type(Domain::class)); );
$flush = $this->em->flush(); $this->em->expects($this->once())->method('getRepository')->with($this->equalTo(Domain::class))->willReturn(
$repo,
);
$this->em->expects($this->once())->method('persist')->with(
$foundDomain !== null ? $this->equalTo($foundDomain) : $this->isInstanceOf(Domain::class),
);
$this->em->expects($this->once())->method('flush');
$result = $this->domainService->getOrCreate($authority, $apiKey); $result = $this->domainService->getOrCreate($authority, $apiKey);
if ($foundDomain !== null) { if ($foundDomain !== null) {
self::assertSame($result, $foundDomain); self::assertSame($result, $foundDomain);
} }
$getRepo->shouldHaveBeenCalledOnce();
$persist->shouldHaveBeenCalledOnce();
$flush->shouldHaveBeenCalledOnce();
} }
/** @test */ /** @test */
@ -158,14 +161,17 @@ class DomainServiceTest extends TestCase
$authority = 'example.com'; $authority = 'example.com';
$domain = Domain::withAuthority($authority)->setId('1'); $domain = Domain::withAuthority($authority)->setId('1');
$apiKey = ApiKey::fromMeta(ApiKeyMeta::withRoles(RoleDefinition::forDomain($domain))); $apiKey = ApiKey::fromMeta(ApiKeyMeta::withRoles(RoleDefinition::forDomain($domain)));
$repo = $this->prophesize(DomainRepositoryInterface::class); $repo = $this->createMock(DomainRepositoryInterface::class);
$repo->findOneByAuthority($authority, $apiKey)->willReturn(null); $repo->method('findOneByAuthority')->with($this->equalTo($authority), $this->equalTo($apiKey))->willReturn(
$getRepo = $this->em->getRepository(Domain::class)->willReturn($repo->reveal()); null,
);
$this->em->expects($this->once())->method('getRepository')->with($this->equalTo(Domain::class))->willReturn(
$repo,
);
$this->em->expects($this->never())->method('persist');
$this->em->expects($this->never())->method('flush');
$this->expectException(DomainNotFoundException::class); $this->expectException(DomainNotFoundException::class);
$getRepo->shouldBeCalledOnce();
$this->em->persist(Argument::cetera())->shouldNotBeCalled();
$this->em->flush()->shouldNotBeCalled();
$this->domainService->getOrCreate($authority, $apiKey); $this->domainService->getOrCreate($authority, $apiKey);
} }
@ -177,11 +183,17 @@ class DomainServiceTest extends TestCase
public function configureNotFoundRedirectsConfiguresFetchedDomain(?Domain $foundDomain, ?ApiKey $apiKey): void public function configureNotFoundRedirectsConfiguresFetchedDomain(?Domain $foundDomain, ?ApiKey $apiKey): void
{ {
$authority = 'example.com'; $authority = 'example.com';
$repo = $this->prophesize(DomainRepositoryInterface::class); $repo = $this->createMock(DomainRepositoryInterface::class);
$repo->findOneByAuthority($authority, $apiKey)->willReturn($foundDomain); $repo->method('findOneByAuthority')->with($this->equalTo($authority), $this->equalTo($apiKey))->willReturn(
$getRepo = $this->em->getRepository(Domain::class)->willReturn($repo->reveal()); $foundDomain,
$persist = $this->em->persist($foundDomain ?? Argument::type(Domain::class)); );
$flush = $this->em->flush(); $this->em->expects($this->once())->method('getRepository')->with($this->equalTo(Domain::class))->willReturn(
$repo,
);
$this->em->expects($this->once())->method('persist')->with(
$foundDomain !== null ? $this->equalTo($foundDomain) : $this->isInstanceOf(Domain::class),
);
$this->em->expects($this->once())->method('flush');
$result = $this->domainService->configureNotFoundRedirects($authority, NotFoundRedirects::withRedirects( $result = $this->domainService->configureNotFoundRedirects($authority, NotFoundRedirects::withRedirects(
'foo.com', 'foo.com',
@ -195,9 +207,6 @@ class DomainServiceTest extends TestCase
self::assertEquals('foo.com', $result->baseUrlRedirect()); self::assertEquals('foo.com', $result->baseUrlRedirect());
self::assertEquals('bar.com', $result->regular404Redirect()); self::assertEquals('bar.com', $result->regular404Redirect());
self::assertEquals('baz.com', $result->invalidShortUrlRedirect()); self::assertEquals('baz.com', $result->invalidShortUrlRedirect());
$getRepo->shouldHaveBeenCalledOnce();
$persist->shouldHaveBeenCalledOnce();
$flush->shouldHaveBeenCalledOnce();
} }
public function provideFoundDomains(): iterable public function provideFoundDomains(): iterable