diff --git a/module/Core/config/dependencies.config.php b/module/Core/config/dependencies.config.php index 67b6bff6..9852bdad 100644 --- a/module/Core/config/dependencies.config.php +++ b/module/Core/config/dependencies.config.php @@ -67,6 +67,7 @@ return [ Tag\Repository\TagRepository::class => [EntityRepositoryFactory::class, Tag\Entity\Tag::class], Domain\DomainService::class => ConfigAbstractFactory::class, + Domain\Repository\DomainRepository::class => [EntityRepositoryFactory::class, Domain\Entity\Domain::class], Visit\VisitsTracker::class => ConfigAbstractFactory::class, Visit\RequestTracker::class => ConfigAbstractFactory::class, @@ -167,7 +168,11 @@ return [ ShortUrl\ShortUrlResolver::class, ], ShortUrl\Helper\ShortCodeUniquenessHelper::class => ['em', Config\Options\UrlShortenerOptions::class], - Domain\DomainService::class => ['em', Config\Options\UrlShortenerOptions::class], + Domain\DomainService::class => [ + 'em', + Config\Options\UrlShortenerOptions::class, + Domain\Repository\DomainRepository::class, + ], Util\DoctrineBatchHelper::class => ['em'], Util\RedirectResponseHelper::class => [Config\Options\RedirectOptions::class], diff --git a/module/Core/src/Domain/DomainService.php b/module/Core/src/Domain/DomainService.php index 18d66328..52bd6082 100644 --- a/module/Core/src/Domain/DomainService.php +++ b/module/Core/src/Domain/DomainService.php @@ -19,8 +19,11 @@ use function array_map; readonly class DomainService implements DomainServiceInterface { - public function __construct(private EntityManagerInterface $em, private UrlShortenerOptions $urlShortenerOptions) - { + public function __construct( + private EntityManagerInterface $em, + private UrlShortenerOptions $urlShortenerOptions, + private DomainRepositoryInterface $repo, + ) { } /** @@ -49,9 +52,7 @@ readonly class DomainService implements DomainServiceInterface */ private function defaultDomainAndRest(ApiKey|null $apiKey): array { - /** @var DomainRepositoryInterface $repo */ - $repo = $this->em->getRepository(Domain::class); - $allDomains = $repo->findDomains($apiKey); + $allDomains = $this->repo->findDomains($apiKey); $defaultDomain = null; $restOfDomains = []; @@ -71,7 +72,6 @@ readonly class DomainService implements DomainServiceInterface */ public function getDomain(string $domainId): Domain { - /** @var Domain|null $domain */ $domain = $this->em->find(Domain::class, $domainId); if ($domain === null) { throw DomainNotFoundException::fromId($domainId); @@ -82,7 +82,7 @@ readonly class DomainService implements DomainServiceInterface public function findByAuthority(string $authority, ApiKey|null $apiKey = null): Domain|null { - return $this->em->getRepository(Domain::class)->findOneByAuthority($authority, $apiKey); + return $this->repo->findOneByAuthority($authority, $apiKey); } /** diff --git a/module/Core/test/Domain/DomainServiceTest.php b/module/Core/test/Domain/DomainServiceTest.php index b7f78c6b..fb601d51 100644 --- a/module/Core/test/Domain/DomainServiceTest.php +++ b/module/Core/test/Domain/DomainServiceTest.php @@ -15,7 +15,7 @@ use Shlinkio\Shlink\Core\Config\Options\UrlShortenerOptions; use Shlinkio\Shlink\Core\Domain\DomainService; use Shlinkio\Shlink\Core\Domain\Entity\Domain; use Shlinkio\Shlink\Core\Domain\Model\DomainItem; -use Shlinkio\Shlink\Core\Domain\Repository\DomainRepository; +use Shlinkio\Shlink\Core\Domain\Repository\DomainRepositoryInterface; use Shlinkio\Shlink\Core\Exception\DomainNotFoundException; use Shlinkio\Shlink\Rest\ApiKey\Model\ApiKeyMeta; use Shlinkio\Shlink\Rest\ApiKey\Model\RoleDefinition; @@ -25,19 +25,23 @@ class DomainServiceTest extends TestCase { private DomainService $domainService; private MockObject & EntityManagerInterface $em; + private MockObject & DomainRepositoryInterface $repo; protected function setUp(): void { $this->em = $this->createMock(EntityManagerInterface::class); - $this->domainService = new DomainService($this->em, new UrlShortenerOptions(defaultDomain: 'default.com')); + $this->repo = $this->createMock(DomainRepositoryInterface::class); + $this->domainService = new DomainService( + $this->em, + new UrlShortenerOptions(defaultDomain: 'default.com'), + $this->repo, + ); } #[Test, DataProvider('provideExcludedDomains')] public function listDomainsDelegatesIntoRepository(array $domains, array $expectedResult, ApiKey|null $apiKey): void { - $repo = $this->createMock(DomainRepository::class); - $repo->expects($this->once())->method('findDomains')->with($apiKey)->willReturn($domains); - $this->em->expects($this->once())->method('getRepository')->with(Domain::class)->willReturn($repo); + $this->repo->expects($this->once())->method('findDomains')->with($apiKey)->willReturn($domains); $result = $this->domainService->listDomains($apiKey); @@ -127,11 +131,9 @@ class DomainServiceTest extends TestCase public function getOrCreateAlwaysPersistsDomain(Domain|null $foundDomain, ApiKey|null $apiKey): void { $authority = 'example.com'; - $repo = $this->createMock(DomainRepository::class); - $repo->method('findOneByAuthority')->with($authority, $apiKey)->willReturn( + $this->repo->expects($this->once())->method('findOneByAuthority')->with($authority, $apiKey)->willReturn( $foundDomain, ); - $this->em->expects($this->once())->method('getRepository')->with(Domain::class)->willReturn($repo); $this->em->expects($this->once())->method('persist')->with($foundDomain ?? $this->isInstanceOf(Domain::class)); $this->em->expects($this->once())->method('flush'); @@ -149,9 +151,7 @@ class DomainServiceTest extends TestCase $domain = Domain::withAuthority($authority); $domain->setId('1'); $apiKey = ApiKey::fromMeta(ApiKeyMeta::withRoles(RoleDefinition::forDomain($domain))); - $repo = $this->createMock(DomainRepository::class); - $repo->method('findOneByAuthority')->with($authority, $apiKey)->willReturn(null); - $this->em->expects($this->once())->method('getRepository')->with(Domain::class)->willReturn($repo); + $this->repo->expects($this->once())->method('findOneByAuthority')->with($authority, $apiKey)->willReturn(null); $this->em->expects($this->never())->method('persist'); $this->em->expects($this->never())->method('flush'); @@ -166,9 +166,9 @@ class DomainServiceTest extends TestCase ApiKey|null $apiKey, ): void { $authority = 'example.com'; - $repo = $this->createMock(DomainRepository::class); - $repo->method('findOneByAuthority')->with($authority, $apiKey)->willReturn($foundDomain); - $this->em->expects($this->once())->method('getRepository')->with(Domain::class)->willReturn($repo); + $this->repo->expects($this->once())->method('findOneByAuthority')->with($authority, $apiKey)->willReturn( + $foundDomain, + ); $this->em->expects($this->once())->method('persist')->with($foundDomain ?? $this->isInstanceOf(Domain::class)); $this->em->expects($this->once())->method('flush');