Inject DomainRepository in DomainService

This commit is contained in:
Alejandro Celaya 2024-11-09 09:34:24 +01:00
parent dba9302f78
commit 102169b6c7
3 changed files with 27 additions and 22 deletions

View File

@ -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],

View File

@ -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);
}
/**

View File

@ -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');