Inject ShortUrlRepository in UrlShortener

This commit is contained in:
Alejandro Celaya 2024-11-09 09:43:55 +01:00
parent 532102e662
commit 3ec24e3c67
3 changed files with 14 additions and 13 deletions

View File

@ -138,6 +138,7 @@ return [
ShortUrl\Resolver\PersistenceShortUrlRelationResolver::class, ShortUrl\Resolver\PersistenceShortUrlRelationResolver::class,
ShortUrl\Helper\ShortCodeUniquenessHelper::class, ShortUrl\Helper\ShortCodeUniquenessHelper::class,
EventDispatcherInterface::class, EventDispatcherInterface::class,
ShortUrl\Repository\ShortUrlRepository::class,
], ],
Visit\VisitsTracker::class => [ Visit\VisitsTracker::class => [
'em', 'em',

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

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