From fca389181966ccb8f5794d2e32cfd442855b250f Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sat, 9 Nov 2024 09:47:47 +0100 Subject: [PATCH] Inject ShortUrlRepository in ShortCodeUniquenessHelper --- module/Core/config/dependencies.config.php | 5 ++++- .../Helper/ShortCodeUniquenessHelper.php | 16 ++++++---------- .../Helper/ShortCodeUniquenessHelperTest.php | 19 ++++++------------- 3 files changed, 16 insertions(+), 24 deletions(-) diff --git a/module/Core/config/dependencies.config.php b/module/Core/config/dependencies.config.php index 7ae8ae27..ad3452e4 100644 --- a/module/Core/config/dependencies.config.php +++ b/module/Core/config/dependencies.config.php @@ -175,7 +175,10 @@ return [ Visit\Repository\VisitDeleterRepository::class, ShortUrl\ShortUrlResolver::class, ], - ShortUrl\Helper\ShortCodeUniquenessHelper::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, diff --git a/module/Core/src/ShortUrl/Helper/ShortCodeUniquenessHelper.php b/module/Core/src/ShortUrl/Helper/ShortCodeUniquenessHelper.php index 7f863f6c..7c7f2a76 100644 --- a/module/Core/src/ShortUrl/Helper/ShortCodeUniquenessHelper.php +++ b/module/Core/src/ShortUrl/Helper/ShortCodeUniquenessHelper.php @@ -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; diff --git a/module/Core/test/ShortUrl/Helper/ShortCodeUniquenessHelperTest.php b/module/Core/test/ShortUrl/Helper/ShortCodeUniquenessHelperTest.php index f341585e..c08a95af 100644 --- a/module/Core/test/ShortUrl/Helper/ShortCodeUniquenessHelperTest.php +++ b/module/Core/test/ShortUrl/Helper/ShortCodeUniquenessHelperTest.php @@ -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');