diff --git a/module/Core/config/dependencies.config.php b/module/Core/config/dependencies.config.php index 4afb28d5..7ae8ae27 100644 --- a/module/Core/config/dependencies.config.php +++ b/module/Core/config/dependencies.config.php @@ -138,6 +138,7 @@ return [ ShortUrl\Resolver\PersistenceShortUrlRelationResolver::class, ShortUrl\Helper\ShortCodeUniquenessHelper::class, EventDispatcherInterface::class, + ShortUrl\Repository\ShortUrlRepository::class, ], Visit\VisitsTracker::class => [ 'em', diff --git a/module/Core/src/ShortUrl/UrlShortener.php b/module/Core/src/ShortUrl/UrlShortener.php index a0692e06..2a4d7571 100644 --- a/module/Core/src/ShortUrl/UrlShortener.php +++ b/module/Core/src/ShortUrl/UrlShortener.php @@ -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 diff --git a/module/Core/test/ShortUrl/UrlShortenerTest.php b/module/Core/test/ShortUrl/UrlShortenerTest.php index b332afd2..a6cacc46 100644 --- a/module/Core/test/ShortUrl/UrlShortenerTest.php +++ b/module/Core/test/ShortUrl/UrlShortenerTest.php @@ -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);