mirror of
https://github.com/shlinkio/shlink.git
synced 2024-12-27 01:11:39 -06:00
Inject ShortUrlRepository in ShortCodeUniquenessHelper
This commit is contained in:
parent
3ec24e3c67
commit
fca3891819
@ -175,7 +175,10 @@ return [
|
|||||||
Visit\Repository\VisitDeleterRepository::class,
|
Visit\Repository\VisitDeleterRepository::class,
|
||||||
ShortUrl\ShortUrlResolver::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 => [
|
Domain\DomainService::class => [
|
||||||
'em',
|
'em',
|
||||||
Config\Options\UrlShortenerOptions::class,
|
Config\Options\UrlShortenerOptions::class,
|
||||||
|
@ -4,25 +4,21 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Shlinkio\Shlink\Core\ShortUrl\Helper;
|
namespace Shlinkio\Shlink\Core\ShortUrl\Helper;
|
||||||
|
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
|
||||||
use Shlinkio\Shlink\Core\Config\Options\UrlShortenerOptions;
|
use Shlinkio\Shlink\Core\Config\Options\UrlShortenerOptions;
|
||||||
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
|
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
|
||||||
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlIdentifier;
|
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(
|
public function __construct(private ShortUrlRepositoryInterface $repo, private UrlShortenerOptions $options)
|
||||||
private readonly EntityManagerInterface $em,
|
{
|
||||||
private readonly UrlShortenerOptions $options,
|
|
||||||
) {
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function ensureShortCodeUniqueness(ShortUrl $shortUrlToBeCreated, bool $hasCustomSlug): bool
|
public function ensureShortCodeUniqueness(ShortUrl $shortUrlToBeCreated, bool $hasCustomSlug): bool
|
||||||
{
|
{
|
||||||
/** @var ShortUrlRepository $repo */
|
$identifier = ShortUrlIdentifier::fromShortUrl($shortUrlToBeCreated);
|
||||||
$repo = $this->em->getRepository(ShortUrl::class);
|
$otherShortUrlsExist = $this->repo->shortCodeIsInUseWithLock($identifier);
|
||||||
$otherShortUrlsExist = $repo->shortCodeIsInUseWithLock(ShortUrlIdentifier::fromShortUrl($shortUrlToBeCreated));
|
|
||||||
|
|
||||||
if (! $otherShortUrlsExist) {
|
if (! $otherShortUrlsExist) {
|
||||||
return true;
|
return true;
|
||||||
|
@ -4,7 +4,6 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace ShlinkioTest\Shlink\Core\ShortUrl\Helper;
|
namespace ShlinkioTest\Shlink\Core\ShortUrl\Helper;
|
||||||
|
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
|
||||||
use PHPUnit\Framework\Attributes\DataProvider;
|
use PHPUnit\Framework\Attributes\DataProvider;
|
||||||
use PHPUnit\Framework\Attributes\Test;
|
use PHPUnit\Framework\Attributes\Test;
|
||||||
use PHPUnit\Framework\MockObject\MockObject;
|
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\Entity\ShortUrl;
|
||||||
use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortCodeUniquenessHelper;
|
use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortCodeUniquenessHelper;
|
||||||
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlIdentifier;
|
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
|
class ShortCodeUniquenessHelperTest extends TestCase
|
||||||
{
|
{
|
||||||
private ShortCodeUniquenessHelper $helper;
|
private ShortCodeUniquenessHelper $helper;
|
||||||
private MockObject & EntityManagerInterface $em;
|
private MockObject & ShortUrlRepositoryInterface $repo;
|
||||||
private MockObject & ShortUrl $shortUrl;
|
private MockObject & ShortUrl $shortUrl;
|
||||||
|
|
||||||
protected function setUp(): void
|
protected function setUp(): void
|
||||||
{
|
{
|
||||||
$this->em = $this->createMock(EntityManagerInterface::class);
|
$this->repo = $this->createMock(ShortUrlRepositoryInterface::class);
|
||||||
$this->helper = new ShortCodeUniquenessHelper($this->em, new UrlShortenerOptions());
|
$this->helper = new ShortCodeUniquenessHelper($this->repo, new UrlShortenerOptions());
|
||||||
|
|
||||||
$this->shortUrl = $this->createMock(ShortUrl::class);
|
$this->shortUrl = $this->createMock(ShortUrl::class);
|
||||||
$this->shortUrl->method('getShortCode')->willReturn('abc123');
|
$this->shortUrl->method('getShortCode')->willReturn('abc123');
|
||||||
@ -36,16 +35,12 @@ class ShortCodeUniquenessHelperTest extends TestCase
|
|||||||
{
|
{
|
||||||
$callIndex = 0;
|
$callIndex = 0;
|
||||||
$expectedCalls = 3;
|
$expectedCalls = 3;
|
||||||
$repo = $this->createMock(ShortUrlRepository::class);
|
$this->repo->expects($this->exactly($expectedCalls))->method('shortCodeIsInUseWithLock')->with(
|
||||||
$repo->expects($this->exactly($expectedCalls))->method('shortCodeIsInUseWithLock')->with(
|
|
||||||
ShortUrlIdentifier::fromShortCodeAndDomain('abc123', $expectedAuthority),
|
ShortUrlIdentifier::fromShortCodeAndDomain('abc123', $expectedAuthority),
|
||||||
)->willReturnCallback(function () use (&$callIndex, $expectedCalls) {
|
)->willReturnCallback(function () use (&$callIndex, $expectedCalls) {
|
||||||
$callIndex++;
|
$callIndex++;
|
||||||
return $callIndex < $expectedCalls;
|
return $callIndex < $expectedCalls;
|
||||||
});
|
});
|
||||||
$this->em->expects($this->exactly($expectedCalls))->method('getRepository')->with(ShortUrl::class)->willReturn(
|
|
||||||
$repo,
|
|
||||||
);
|
|
||||||
$this->shortUrl->method('getDomain')->willReturn($domain);
|
$this->shortUrl->method('getDomain')->willReturn($domain);
|
||||||
$this->shortUrl->expects($this->exactly($expectedCalls - 1))->method('regenerateShortCode')->with();
|
$this->shortUrl->expects($this->exactly($expectedCalls - 1))->method('regenerateShortCode')->with();
|
||||||
|
|
||||||
@ -63,11 +58,9 @@ class ShortCodeUniquenessHelperTest extends TestCase
|
|||||||
#[Test]
|
#[Test]
|
||||||
public function inUseSlugReturnsError(): void
|
public function inUseSlugReturnsError(): void
|
||||||
{
|
{
|
||||||
$repo = $this->createMock(ShortUrlRepository::class);
|
$this->repo->expects($this->once())->method('shortCodeIsInUseWithLock')->with(
|
||||||
$repo->expects($this->once())->method('shortCodeIsInUseWithLock')->with(
|
|
||||||
ShortUrlIdentifier::fromShortCodeAndDomain('abc123'),
|
ShortUrlIdentifier::fromShortCodeAndDomain('abc123'),
|
||||||
)->willReturn(true);
|
)->willReturn(true);
|
||||||
$this->em->expects($this->once())->method('getRepository')->with(ShortUrl::class)->willReturn($repo);
|
|
||||||
$this->shortUrl->method('getDomain')->willReturn(null);
|
$this->shortUrl->method('getDomain')->willReturn(null);
|
||||||
$this->shortUrl->expects($this->never())->method('regenerateShortCode');
|
$this->shortUrl->expects($this->never())->method('regenerateShortCode');
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user