Merge pull request #2251 from acelaya-forks/feature/inject-repos

Feature/inject repos
This commit is contained in:
Alejandro Celaya 2024-11-09 09:54:06 +01:00 committed by GitHub
commit d6b103de83
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 75 additions and 83 deletions

View File

@ -50,6 +50,10 @@ return [
ShortUrl\Transformer\ShortUrlDataTransformer::class => ConfigAbstractFactory::class,
ShortUrl\Middleware\ExtraPathRedirectMiddleware::class => ConfigAbstractFactory::class,
ShortUrl\Middleware\TrimTrailingSlashMiddleware::class => ConfigAbstractFactory::class,
ShortUrl\Repository\ShortUrlRepository::class => [
EntityRepositoryFactory::class,
ShortUrl\Entity\ShortUrl::class,
],
ShortUrl\Repository\ShortUrlListRepository::class => [
EntityRepositoryFactory::class,
ShortUrl\Entity\ShortUrl::class,
@ -67,6 +71,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,
@ -133,6 +138,7 @@ return [
ShortUrl\Resolver\PersistenceShortUrlRelationResolver::class,
ShortUrl\Helper\ShortCodeUniquenessHelper::class,
EventDispatcherInterface::class,
ShortUrl\Repository\ShortUrlRepository::class,
],
Visit\VisitsTracker::class => [
'em',
@ -161,13 +167,23 @@ return [
ShortUrl\ShortUrlResolver::class,
ShortUrl\Repository\ExpiredShortUrlsRepository::class,
],
ShortUrl\ShortUrlResolver::class => ['em', Config\Options\UrlShortenerOptions::class],
ShortUrl\ShortUrlResolver::class => [
ShortUrl\Repository\ShortUrlRepository::class,
Config\Options\UrlShortenerOptions::class,
],
ShortUrl\ShortUrlVisitsDeleter::class => [
Visit\Repository\VisitDeleterRepository::class,
ShortUrl\ShortUrlResolver::class,
],
ShortUrl\Helper\ShortCodeUniquenessHelper::class => ['em', Config\Options\UrlShortenerOptions::class],
Domain\DomainService::class => ['em', Config\Options\UrlShortenerOptions::class],
ShortUrl\Helper\ShortCodeUniquenessHelper::class => [
ShortUrl\Repository\ShortUrlRepository::class,
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

@ -25,13 +25,13 @@ use Throwable;
use function Shlinkio\Shlink\Core\normalizeDate;
use function sprintf;
class ImportedLinksProcessor implements ImportedLinksProcessorInterface
readonly class ImportedLinksProcessor implements ImportedLinksProcessorInterface
{
public function __construct(
private readonly EntityManagerInterface $em,
private readonly ShortUrlRelationResolverInterface $relationResolver,
private readonly ShortCodeUniquenessHelperInterface $shortCodeHelper,
private readonly DoctrineBatchHelperInterface $batchHelper,
private EntityManagerInterface $em,
private ShortUrlRelationResolverInterface $relationResolver,
private ShortCodeUniquenessHelperInterface $shortCodeHelper,
private DoctrineBatchHelperInterface $batchHelper,
) {
}

View File

@ -4,25 +4,21 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Core\ShortUrl\Helper;
use Doctrine\ORM\EntityManagerInterface;
use Shlinkio\Shlink\Core\Config\Options\UrlShortenerOptions;
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlIdentifier;
use Shlinkio\Shlink\Core\ShortUrl\Repository\ShortUrlRepository;
use Shlinkio\Shlink\Core\ShortUrl\Repository\ShortUrlRepositoryInterface;
class ShortCodeUniquenessHelper implements ShortCodeUniquenessHelperInterface
readonly class ShortCodeUniquenessHelper implements ShortCodeUniquenessHelperInterface
{
public function __construct(
private readonly EntityManagerInterface $em,
private readonly UrlShortenerOptions $options,
) {
public function __construct(private ShortUrlRepositoryInterface $repo, private UrlShortenerOptions $options)
{
}
public function ensureShortCodeUniqueness(ShortUrl $shortUrlToBeCreated, bool $hasCustomSlug): bool
{
/** @var ShortUrlRepository $repo */
$repo = $this->em->getRepository(ShortUrl::class);
$otherShortUrlsExist = $repo->shortCodeIsInUseWithLock(ShortUrlIdentifier::fromShortUrl($shortUrlToBeCreated));
$identifier = ShortUrlIdentifier::fromShortUrl($shortUrlToBeCreated);
$otherShortUrlsExist = $this->repo->shortCodeIsInUseWithLock($identifier);
if (! $otherShortUrlsExist) {
return true;

View File

@ -4,18 +4,17 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Core\ShortUrl;
use Doctrine\ORM\EntityManagerInterface;
use Shlinkio\Shlink\Core\Config\Options\UrlShortenerOptions;
use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException;
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlIdentifier;
use Shlinkio\Shlink\Core\ShortUrl\Repository\ShortUrlRepository;
use Shlinkio\Shlink\Core\ShortUrl\Repository\ShortUrlRepositoryInterface;
use Shlinkio\Shlink\Rest\Entity\ApiKey;
readonly class ShortUrlResolver implements ShortUrlResolverInterface
{
public function __construct(
private EntityManagerInterface $em,
private ShortUrlRepositoryInterface $repo,
private UrlShortenerOptions $urlShortenerOptions,
) {
}
@ -25,9 +24,7 @@ readonly class ShortUrlResolver implements ShortUrlResolverInterface
*/
public function resolveShortUrl(ShortUrlIdentifier $identifier, ApiKey|null $apiKey = null): ShortUrl
{
/** @var ShortUrlRepository $shortUrlRepo */
$shortUrlRepo = $this->em->getRepository(ShortUrl::class);
$shortUrl = $shortUrlRepo->findOne($identifier, $apiKey?->spec());
$shortUrl = $this->repo->findOne($identifier, $apiKey?->spec());
if ($shortUrl === null) {
throw ShortUrlNotFoundException::fromNotFound($identifier);
}
@ -53,9 +50,7 @@ readonly class ShortUrlResolver implements ShortUrlResolverInterface
*/
public function resolvePublicShortUrl(ShortUrlIdentifier $identifier): ShortUrl
{
/** @var ShortUrlRepository $shortUrlRepo */
$shortUrlRepo = $this->em->getRepository(ShortUrl::class);
$shortUrl = $shortUrlRepo->findOneWithDomainFallback($identifier, $this->urlShortenerOptions->mode);
$shortUrl = $this->repo->findOneWithDomainFallback($identifier, $this->urlShortenerOptions->mode);
if ($shortUrl === null) {
throw ShortUrlNotFoundException::fromNotFound($identifier);
}

View File

@ -17,14 +17,15 @@ use Shlinkio\Shlink\Core\ShortUrl\Model\UrlShorteningResult;
use Shlinkio\Shlink\Core\ShortUrl\Repository\ShortUrlRepositoryInterface;
use Shlinkio\Shlink\Core\ShortUrl\Resolver\ShortUrlRelationResolverInterface;
class UrlShortener implements UrlShortenerInterface
readonly class UrlShortener implements UrlShortenerInterface
{
public function __construct(
private readonly ShortUrlTitleResolutionHelperInterface $titleResolutionHelper,
private readonly EntityManagerInterface $em,
private readonly ShortUrlRelationResolverInterface $relationResolver,
private readonly ShortCodeUniquenessHelperInterface $shortCodeHelper,
private readonly EventDispatcherInterface $eventDispatcher,
private ShortUrlTitleResolutionHelperInterface $titleResolutionHelper,
private EntityManagerInterface $em,
private ShortUrlRelationResolverInterface $relationResolver,
private ShortCodeUniquenessHelperInterface $shortCodeHelper,
private EventDispatcherInterface $eventDispatcher,
private ShortUrlRepositoryInterface $repo,
) {
}
@ -70,9 +71,7 @@ class UrlShortener implements UrlShortenerInterface
return null;
}
/** @var ShortUrlRepositoryInterface $repo */
$repo = $this->em->getRepository(ShortUrl::class);
return $repo->findOneMatching($creation);
return $this->repo->findOneMatching($creation);
}
private function verifyShortCodeUniqueness(ShortUrlCreation $meta, ShortUrl $shortUrlToBeCreated): void

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

View File

@ -4,7 +4,6 @@ declare(strict_types=1);
namespace ShlinkioTest\Shlink\Core\ShortUrl\Helper;
use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\MockObject\MockObject;
@ -14,18 +13,18 @@ use Shlinkio\Shlink\Core\Domain\Entity\Domain;
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortCodeUniquenessHelper;
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlIdentifier;
use Shlinkio\Shlink\Core\ShortUrl\Repository\ShortUrlRepository;
use Shlinkio\Shlink\Core\ShortUrl\Repository\ShortUrlRepositoryInterface;
class ShortCodeUniquenessHelperTest extends TestCase
{
private ShortCodeUniquenessHelper $helper;
private MockObject & EntityManagerInterface $em;
private MockObject & ShortUrlRepositoryInterface $repo;
private MockObject & ShortUrl $shortUrl;
protected function setUp(): void
{
$this->em = $this->createMock(EntityManagerInterface::class);
$this->helper = new ShortCodeUniquenessHelper($this->em, new UrlShortenerOptions());
$this->repo = $this->createMock(ShortUrlRepositoryInterface::class);
$this->helper = new ShortCodeUniquenessHelper($this->repo, new UrlShortenerOptions());
$this->shortUrl = $this->createMock(ShortUrl::class);
$this->shortUrl->method('getShortCode')->willReturn('abc123');
@ -36,16 +35,12 @@ class ShortCodeUniquenessHelperTest extends TestCase
{
$callIndex = 0;
$expectedCalls = 3;
$repo = $this->createMock(ShortUrlRepository::class);
$repo->expects($this->exactly($expectedCalls))->method('shortCodeIsInUseWithLock')->with(
$this->repo->expects($this->exactly($expectedCalls))->method('shortCodeIsInUseWithLock')->with(
ShortUrlIdentifier::fromShortCodeAndDomain('abc123', $expectedAuthority),
)->willReturnCallback(function () use (&$callIndex, $expectedCalls) {
$callIndex++;
return $callIndex < $expectedCalls;
});
$this->em->expects($this->exactly($expectedCalls))->method('getRepository')->with(ShortUrl::class)->willReturn(
$repo,
);
$this->shortUrl->method('getDomain')->willReturn($domain);
$this->shortUrl->expects($this->exactly($expectedCalls - 1))->method('regenerateShortCode')->with();
@ -63,11 +58,9 @@ class ShortCodeUniquenessHelperTest extends TestCase
#[Test]
public function inUseSlugReturnsError(): void
{
$repo = $this->createMock(ShortUrlRepository::class);
$repo->expects($this->once())->method('shortCodeIsInUseWithLock')->with(
$this->repo->expects($this->once())->method('shortCodeIsInUseWithLock')->with(
ShortUrlIdentifier::fromShortCodeAndDomain('abc123'),
)->willReturn(true);
$this->em->expects($this->once())->method('getRepository')->with(ShortUrl::class)->willReturn($repo);
$this->shortUrl->method('getDomain')->willReturn(null);
$this->shortUrl->expects($this->never())->method('regenerateShortCode');

View File

@ -6,7 +6,6 @@ namespace ShlinkioTest\Shlink\Core\ShortUrl;
use Cake\Chronos\Chronos;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\DataProviderExternal;
use PHPUnit\Framework\Attributes\Test;
@ -31,14 +30,12 @@ use function range;
class ShortUrlResolverTest extends TestCase
{
private ShortUrlResolver $urlResolver;
private MockObject & EntityManagerInterface $em;
private MockObject & ShortUrlRepository $repo;
protected function setUp(): void
{
$this->em = $this->createMock(EntityManagerInterface::class);
$this->repo = $this->createMock(ShortUrlRepository::class);
$this->urlResolver = new ShortUrlResolver($this->em, new UrlShortenerOptions());
$this->urlResolver = new ShortUrlResolver($this->repo, new UrlShortenerOptions());
}
#[Test, DataProviderExternal(ApiKeyDataProviders::class, 'adminApiKeysProvider')]
@ -51,7 +48,6 @@ class ShortUrlResolverTest extends TestCase
$this->repo->expects($this->once())->method('findOne')->with($identifier, $apiKey?->spec())->willReturn(
$shortUrl,
);
$this->em->expects($this->once())->method('getRepository')->with(ShortUrl::class)->willReturn($this->repo);
$result = $this->urlResolver->resolveShortUrl($identifier, $apiKey);
@ -65,7 +61,6 @@ class ShortUrlResolverTest extends TestCase
$identifier = ShortUrlIdentifier::fromShortCodeAndDomain($shortCode);
$this->repo->expects($this->once())->method('findOne')->with($identifier, $apiKey?->spec())->willReturn(null);
$this->em->expects($this->once())->method('getRepository')->with(ShortUrl::class)->willReturn($this->repo);
$this->expectException(ShortUrlNotFoundException::class);
@ -82,7 +77,6 @@ class ShortUrlResolverTest extends TestCase
ShortUrlIdentifier::fromShortCodeAndDomain($shortCode),
ShortUrlMode::STRICT,
)->willReturn($shortUrl);
$this->em->expects($this->once())->method('getRepository')->with(ShortUrl::class)->willReturn($this->repo);
$result = $this->urlResolver->resolveEnabledShortUrl(ShortUrlIdentifier::fromShortCodeAndDomain($shortCode));
@ -98,7 +92,6 @@ class ShortUrlResolverTest extends TestCase
ShortUrlIdentifier::fromShortCodeAndDomain($shortCode),
ShortUrlMode::STRICT,
)->willReturn(null);
$this->em->expects($this->once())->method('getRepository')->with(ShortUrl::class)->willReturn($this->repo);
$this->expectException(ShortUrlNotFoundException::class);
@ -120,7 +113,6 @@ class ShortUrlResolverTest extends TestCase
ShortUrlIdentifier::fromShortCodeAndDomain($shortCode),
ShortUrlMode::STRICT,
)->willReturn($shortUrl);
$this->em->expects($this->once())->method('getRepository')->with(ShortUrl::class)->willReturn($this->repo);
$this->expectException(ShortUrlNotFoundException::class);

View File

@ -17,7 +17,7 @@ use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortCodeUniquenessHelperInterface;
use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortUrlTitleResolutionHelperInterface;
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlCreation;
use Shlinkio\Shlink\Core\ShortUrl\Repository\ShortUrlRepository;
use Shlinkio\Shlink\Core\ShortUrl\Repository\ShortUrlRepositoryInterface;
use Shlinkio\Shlink\Core\ShortUrl\Resolver\SimpleShortUrlRelationResolver;
use Shlinkio\Shlink\Core\ShortUrl\UrlShortener;
@ -28,6 +28,7 @@ class UrlShortenerTest extends TestCase
private MockObject & ShortUrlTitleResolutionHelperInterface $titleResolutionHelper;
private MockObject & ShortCodeUniquenessHelperInterface $shortCodeHelper;
private MockObject & EventDispatcherInterface $dispatcher;
private MockObject & ShortUrlRepositoryInterface $repo;
protected function setUp(): void
{
@ -42,6 +43,7 @@ class UrlShortenerTest extends TestCase
);
$this->dispatcher = $this->createMock(EventDispatcherInterface::class);
$this->repo = $this->createMock(ShortUrlRepositoryInterface::class);
$this->urlShortener = new UrlShortener(
$this->titleResolutionHelper,
@ -49,6 +51,7 @@ class UrlShortenerTest extends TestCase
new SimpleShortUrlRelationResolver(),
$this->shortCodeHelper,
$this->dispatcher,
$this->repo,
);
}
@ -102,9 +105,7 @@ class UrlShortenerTest extends TestCase
#[Test, DataProvider('provideExistingShortUrls')]
public function existingShortUrlIsReturnedWhenRequested(ShortUrlCreation $meta, ShortUrl $expected): void
{
$repo = $this->createMock(ShortUrlRepository::class);
$repo->expects($this->once())->method('findOneMatching')->willReturn($expected);
$this->em->expects($this->once())->method('getRepository')->with(ShortUrl::class)->willReturn($repo);
$this->repo->expects($this->once())->method('findOneMatching')->willReturn($expected);
$this->titleResolutionHelper->expects($this->never())->method('processTitle');
$this->shortCodeHelper->method('ensureShortCodeUniqueness')->willReturn(true);