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\Helper\ShortCodeUniquenessHelper::class,
EventDispatcherInterface::class,
ShortUrl\Repository\ShortUrlRepository::class,
],
Visit\VisitsTracker::class => [
'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\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

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