Inject ShortUrlRepository in ShortCodeUniquenessHelper

This commit is contained in:
Alejandro Celaya 2024-11-09 09:47:47 +01:00
parent 3ec24e3c67
commit fca3891819
3 changed files with 16 additions and 24 deletions

View File

@ -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,

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