mirror of
https://github.com/shlinkio/shlink.git
synced 2024-11-21 16:38:37 -06:00
Merge pull request #2251 from acelaya-forks/feature/inject-repos
Feature/inject repos
This commit is contained in:
commit
d6b103de83
@ -50,6 +50,10 @@ return [
|
|||||||
ShortUrl\Transformer\ShortUrlDataTransformer::class => ConfigAbstractFactory::class,
|
ShortUrl\Transformer\ShortUrlDataTransformer::class => ConfigAbstractFactory::class,
|
||||||
ShortUrl\Middleware\ExtraPathRedirectMiddleware::class => ConfigAbstractFactory::class,
|
ShortUrl\Middleware\ExtraPathRedirectMiddleware::class => ConfigAbstractFactory::class,
|
||||||
ShortUrl\Middleware\TrimTrailingSlashMiddleware::class => ConfigAbstractFactory::class,
|
ShortUrl\Middleware\TrimTrailingSlashMiddleware::class => ConfigAbstractFactory::class,
|
||||||
|
ShortUrl\Repository\ShortUrlRepository::class => [
|
||||||
|
EntityRepositoryFactory::class,
|
||||||
|
ShortUrl\Entity\ShortUrl::class,
|
||||||
|
],
|
||||||
ShortUrl\Repository\ShortUrlListRepository::class => [
|
ShortUrl\Repository\ShortUrlListRepository::class => [
|
||||||
EntityRepositoryFactory::class,
|
EntityRepositoryFactory::class,
|
||||||
ShortUrl\Entity\ShortUrl::class,
|
ShortUrl\Entity\ShortUrl::class,
|
||||||
@ -67,6 +71,7 @@ return [
|
|||||||
Tag\Repository\TagRepository::class => [EntityRepositoryFactory::class, Tag\Entity\Tag::class],
|
Tag\Repository\TagRepository::class => [EntityRepositoryFactory::class, Tag\Entity\Tag::class],
|
||||||
|
|
||||||
Domain\DomainService::class => ConfigAbstractFactory::class,
|
Domain\DomainService::class => ConfigAbstractFactory::class,
|
||||||
|
Domain\Repository\DomainRepository::class => [EntityRepositoryFactory::class, Domain\Entity\Domain::class],
|
||||||
|
|
||||||
Visit\VisitsTracker::class => ConfigAbstractFactory::class,
|
Visit\VisitsTracker::class => ConfigAbstractFactory::class,
|
||||||
Visit\RequestTracker::class => ConfigAbstractFactory::class,
|
Visit\RequestTracker::class => ConfigAbstractFactory::class,
|
||||||
@ -133,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',
|
||||||
@ -161,13 +167,23 @@ return [
|
|||||||
ShortUrl\ShortUrlResolver::class,
|
ShortUrl\ShortUrlResolver::class,
|
||||||
ShortUrl\Repository\ExpiredShortUrlsRepository::class,
|
ShortUrl\Repository\ExpiredShortUrlsRepository::class,
|
||||||
],
|
],
|
||||||
ShortUrl\ShortUrlResolver::class => ['em', Config\Options\UrlShortenerOptions::class],
|
ShortUrl\ShortUrlResolver::class => [
|
||||||
|
ShortUrl\Repository\ShortUrlRepository::class,
|
||||||
|
Config\Options\UrlShortenerOptions::class,
|
||||||
|
],
|
||||||
ShortUrl\ShortUrlVisitsDeleter::class => [
|
ShortUrl\ShortUrlVisitsDeleter::class => [
|
||||||
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 => [
|
||||||
Domain\DomainService::class => ['em', Config\Options\UrlShortenerOptions::class],
|
ShortUrl\Repository\ShortUrlRepository::class,
|
||||||
|
Config\Options\UrlShortenerOptions::class,
|
||||||
|
],
|
||||||
|
Domain\DomainService::class => [
|
||||||
|
'em',
|
||||||
|
Config\Options\UrlShortenerOptions::class,
|
||||||
|
Domain\Repository\DomainRepository::class,
|
||||||
|
],
|
||||||
|
|
||||||
Util\DoctrineBatchHelper::class => ['em'],
|
Util\DoctrineBatchHelper::class => ['em'],
|
||||||
Util\RedirectResponseHelper::class => [Config\Options\RedirectOptions::class],
|
Util\RedirectResponseHelper::class => [Config\Options\RedirectOptions::class],
|
||||||
|
@ -19,8 +19,11 @@ use function array_map;
|
|||||||
|
|
||||||
readonly class DomainService implements DomainServiceInterface
|
readonly class DomainService implements DomainServiceInterface
|
||||||
{
|
{
|
||||||
public function __construct(private EntityManagerInterface $em, private UrlShortenerOptions $urlShortenerOptions)
|
public function __construct(
|
||||||
{
|
private EntityManagerInterface $em,
|
||||||
|
private UrlShortenerOptions $urlShortenerOptions,
|
||||||
|
private DomainRepositoryInterface $repo,
|
||||||
|
) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -49,9 +52,7 @@ readonly class DomainService implements DomainServiceInterface
|
|||||||
*/
|
*/
|
||||||
private function defaultDomainAndRest(ApiKey|null $apiKey): array
|
private function defaultDomainAndRest(ApiKey|null $apiKey): array
|
||||||
{
|
{
|
||||||
/** @var DomainRepositoryInterface $repo */
|
$allDomains = $this->repo->findDomains($apiKey);
|
||||||
$repo = $this->em->getRepository(Domain::class);
|
|
||||||
$allDomains = $repo->findDomains($apiKey);
|
|
||||||
$defaultDomain = null;
|
$defaultDomain = null;
|
||||||
$restOfDomains = [];
|
$restOfDomains = [];
|
||||||
|
|
||||||
@ -71,7 +72,6 @@ readonly class DomainService implements DomainServiceInterface
|
|||||||
*/
|
*/
|
||||||
public function getDomain(string $domainId): Domain
|
public function getDomain(string $domainId): Domain
|
||||||
{
|
{
|
||||||
/** @var Domain|null $domain */
|
|
||||||
$domain = $this->em->find(Domain::class, $domainId);
|
$domain = $this->em->find(Domain::class, $domainId);
|
||||||
if ($domain === null) {
|
if ($domain === null) {
|
||||||
throw DomainNotFoundException::fromId($domainId);
|
throw DomainNotFoundException::fromId($domainId);
|
||||||
@ -82,7 +82,7 @@ readonly class DomainService implements DomainServiceInterface
|
|||||||
|
|
||||||
public function findByAuthority(string $authority, ApiKey|null $apiKey = null): Domain|null
|
public function findByAuthority(string $authority, ApiKey|null $apiKey = null): Domain|null
|
||||||
{
|
{
|
||||||
return $this->em->getRepository(Domain::class)->findOneByAuthority($authority, $apiKey);
|
return $this->repo->findOneByAuthority($authority, $apiKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -25,13 +25,13 @@ use Throwable;
|
|||||||
use function Shlinkio\Shlink\Core\normalizeDate;
|
use function Shlinkio\Shlink\Core\normalizeDate;
|
||||||
use function sprintf;
|
use function sprintf;
|
||||||
|
|
||||||
class ImportedLinksProcessor implements ImportedLinksProcessorInterface
|
readonly class ImportedLinksProcessor implements ImportedLinksProcessorInterface
|
||||||
{
|
{
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private readonly EntityManagerInterface $em,
|
private EntityManagerInterface $em,
|
||||||
private readonly ShortUrlRelationResolverInterface $relationResolver,
|
private ShortUrlRelationResolverInterface $relationResolver,
|
||||||
private readonly ShortCodeUniquenessHelperInterface $shortCodeHelper,
|
private ShortCodeUniquenessHelperInterface $shortCodeHelper,
|
||||||
private readonly DoctrineBatchHelperInterface $batchHelper,
|
private DoctrineBatchHelperInterface $batchHelper,
|
||||||
) {
|
) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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(private ShortUrlRepositoryInterface $repo, private UrlShortenerOptions $options)
|
||||||
{
|
{
|
||||||
public function __construct(
|
|
||||||
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,18 +4,17 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Shlinkio\Shlink\Core\ShortUrl;
|
namespace Shlinkio\Shlink\Core\ShortUrl;
|
||||||
|
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
|
||||||
use Shlinkio\Shlink\Core\Config\Options\UrlShortenerOptions;
|
use Shlinkio\Shlink\Core\Config\Options\UrlShortenerOptions;
|
||||||
use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException;
|
use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException;
|
||||||
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;
|
||||||
use Shlinkio\Shlink\Rest\Entity\ApiKey;
|
use Shlinkio\Shlink\Rest\Entity\ApiKey;
|
||||||
|
|
||||||
readonly class ShortUrlResolver implements ShortUrlResolverInterface
|
readonly class ShortUrlResolver implements ShortUrlResolverInterface
|
||||||
{
|
{
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private EntityManagerInterface $em,
|
private ShortUrlRepositoryInterface $repo,
|
||||||
private UrlShortenerOptions $urlShortenerOptions,
|
private UrlShortenerOptions $urlShortenerOptions,
|
||||||
) {
|
) {
|
||||||
}
|
}
|
||||||
@ -25,9 +24,7 @@ readonly class ShortUrlResolver implements ShortUrlResolverInterface
|
|||||||
*/
|
*/
|
||||||
public function resolveShortUrl(ShortUrlIdentifier $identifier, ApiKey|null $apiKey = null): ShortUrl
|
public function resolveShortUrl(ShortUrlIdentifier $identifier, ApiKey|null $apiKey = null): ShortUrl
|
||||||
{
|
{
|
||||||
/** @var ShortUrlRepository $shortUrlRepo */
|
$shortUrl = $this->repo->findOne($identifier, $apiKey?->spec());
|
||||||
$shortUrlRepo = $this->em->getRepository(ShortUrl::class);
|
|
||||||
$shortUrl = $shortUrlRepo->findOne($identifier, $apiKey?->spec());
|
|
||||||
if ($shortUrl === null) {
|
if ($shortUrl === null) {
|
||||||
throw ShortUrlNotFoundException::fromNotFound($identifier);
|
throw ShortUrlNotFoundException::fromNotFound($identifier);
|
||||||
}
|
}
|
||||||
@ -53,9 +50,7 @@ readonly class ShortUrlResolver implements ShortUrlResolverInterface
|
|||||||
*/
|
*/
|
||||||
public function resolvePublicShortUrl(ShortUrlIdentifier $identifier): ShortUrl
|
public function resolvePublicShortUrl(ShortUrlIdentifier $identifier): ShortUrl
|
||||||
{
|
{
|
||||||
/** @var ShortUrlRepository $shortUrlRepo */
|
$shortUrl = $this->repo->findOneWithDomainFallback($identifier, $this->urlShortenerOptions->mode);
|
||||||
$shortUrlRepo = $this->em->getRepository(ShortUrl::class);
|
|
||||||
$shortUrl = $shortUrlRepo->findOneWithDomainFallback($identifier, $this->urlShortenerOptions->mode);
|
|
||||||
if ($shortUrl === null) {
|
if ($shortUrl === null) {
|
||||||
throw ShortUrlNotFoundException::fromNotFound($identifier);
|
throw ShortUrlNotFoundException::fromNotFound($identifier);
|
||||||
}
|
}
|
||||||
|
@ -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
|
||||||
|
@ -15,7 +15,7 @@ use Shlinkio\Shlink\Core\Config\Options\UrlShortenerOptions;
|
|||||||
use Shlinkio\Shlink\Core\Domain\DomainService;
|
use Shlinkio\Shlink\Core\Domain\DomainService;
|
||||||
use Shlinkio\Shlink\Core\Domain\Entity\Domain;
|
use Shlinkio\Shlink\Core\Domain\Entity\Domain;
|
||||||
use Shlinkio\Shlink\Core\Domain\Model\DomainItem;
|
use Shlinkio\Shlink\Core\Domain\Model\DomainItem;
|
||||||
use Shlinkio\Shlink\Core\Domain\Repository\DomainRepository;
|
use Shlinkio\Shlink\Core\Domain\Repository\DomainRepositoryInterface;
|
||||||
use Shlinkio\Shlink\Core\Exception\DomainNotFoundException;
|
use Shlinkio\Shlink\Core\Exception\DomainNotFoundException;
|
||||||
use Shlinkio\Shlink\Rest\ApiKey\Model\ApiKeyMeta;
|
use Shlinkio\Shlink\Rest\ApiKey\Model\ApiKeyMeta;
|
||||||
use Shlinkio\Shlink\Rest\ApiKey\Model\RoleDefinition;
|
use Shlinkio\Shlink\Rest\ApiKey\Model\RoleDefinition;
|
||||||
@ -25,19 +25,23 @@ class DomainServiceTest extends TestCase
|
|||||||
{
|
{
|
||||||
private DomainService $domainService;
|
private DomainService $domainService;
|
||||||
private MockObject & EntityManagerInterface $em;
|
private MockObject & EntityManagerInterface $em;
|
||||||
|
private MockObject & DomainRepositoryInterface $repo;
|
||||||
|
|
||||||
protected function setUp(): void
|
protected function setUp(): void
|
||||||
{
|
{
|
||||||
$this->em = $this->createMock(EntityManagerInterface::class);
|
$this->em = $this->createMock(EntityManagerInterface::class);
|
||||||
$this->domainService = new DomainService($this->em, new UrlShortenerOptions(defaultDomain: 'default.com'));
|
$this->repo = $this->createMock(DomainRepositoryInterface::class);
|
||||||
|
$this->domainService = new DomainService(
|
||||||
|
$this->em,
|
||||||
|
new UrlShortenerOptions(defaultDomain: 'default.com'),
|
||||||
|
$this->repo,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[Test, DataProvider('provideExcludedDomains')]
|
#[Test, DataProvider('provideExcludedDomains')]
|
||||||
public function listDomainsDelegatesIntoRepository(array $domains, array $expectedResult, ApiKey|null $apiKey): void
|
public function listDomainsDelegatesIntoRepository(array $domains, array $expectedResult, ApiKey|null $apiKey): void
|
||||||
{
|
{
|
||||||
$repo = $this->createMock(DomainRepository::class);
|
$this->repo->expects($this->once())->method('findDomains')->with($apiKey)->willReturn($domains);
|
||||||
$repo->expects($this->once())->method('findDomains')->with($apiKey)->willReturn($domains);
|
|
||||||
$this->em->expects($this->once())->method('getRepository')->with(Domain::class)->willReturn($repo);
|
|
||||||
|
|
||||||
$result = $this->domainService->listDomains($apiKey);
|
$result = $this->domainService->listDomains($apiKey);
|
||||||
|
|
||||||
@ -127,11 +131,9 @@ class DomainServiceTest extends TestCase
|
|||||||
public function getOrCreateAlwaysPersistsDomain(Domain|null $foundDomain, ApiKey|null $apiKey): void
|
public function getOrCreateAlwaysPersistsDomain(Domain|null $foundDomain, ApiKey|null $apiKey): void
|
||||||
{
|
{
|
||||||
$authority = 'example.com';
|
$authority = 'example.com';
|
||||||
$repo = $this->createMock(DomainRepository::class);
|
$this->repo->expects($this->once())->method('findOneByAuthority')->with($authority, $apiKey)->willReturn(
|
||||||
$repo->method('findOneByAuthority')->with($authority, $apiKey)->willReturn(
|
|
||||||
$foundDomain,
|
$foundDomain,
|
||||||
);
|
);
|
||||||
$this->em->expects($this->once())->method('getRepository')->with(Domain::class)->willReturn($repo);
|
|
||||||
$this->em->expects($this->once())->method('persist')->with($foundDomain ?? $this->isInstanceOf(Domain::class));
|
$this->em->expects($this->once())->method('persist')->with($foundDomain ?? $this->isInstanceOf(Domain::class));
|
||||||
$this->em->expects($this->once())->method('flush');
|
$this->em->expects($this->once())->method('flush');
|
||||||
|
|
||||||
@ -149,9 +151,7 @@ class DomainServiceTest extends TestCase
|
|||||||
$domain = Domain::withAuthority($authority);
|
$domain = Domain::withAuthority($authority);
|
||||||
$domain->setId('1');
|
$domain->setId('1');
|
||||||
$apiKey = ApiKey::fromMeta(ApiKeyMeta::withRoles(RoleDefinition::forDomain($domain)));
|
$apiKey = ApiKey::fromMeta(ApiKeyMeta::withRoles(RoleDefinition::forDomain($domain)));
|
||||||
$repo = $this->createMock(DomainRepository::class);
|
$this->repo->expects($this->once())->method('findOneByAuthority')->with($authority, $apiKey)->willReturn(null);
|
||||||
$repo->method('findOneByAuthority')->with($authority, $apiKey)->willReturn(null);
|
|
||||||
$this->em->expects($this->once())->method('getRepository')->with(Domain::class)->willReturn($repo);
|
|
||||||
$this->em->expects($this->never())->method('persist');
|
$this->em->expects($this->never())->method('persist');
|
||||||
$this->em->expects($this->never())->method('flush');
|
$this->em->expects($this->never())->method('flush');
|
||||||
|
|
||||||
@ -166,9 +166,9 @@ class DomainServiceTest extends TestCase
|
|||||||
ApiKey|null $apiKey,
|
ApiKey|null $apiKey,
|
||||||
): void {
|
): void {
|
||||||
$authority = 'example.com';
|
$authority = 'example.com';
|
||||||
$repo = $this->createMock(DomainRepository::class);
|
$this->repo->expects($this->once())->method('findOneByAuthority')->with($authority, $apiKey)->willReturn(
|
||||||
$repo->method('findOneByAuthority')->with($authority, $apiKey)->willReturn($foundDomain);
|
$foundDomain,
|
||||||
$this->em->expects($this->once())->method('getRepository')->with(Domain::class)->willReturn($repo);
|
);
|
||||||
$this->em->expects($this->once())->method('persist')->with($foundDomain ?? $this->isInstanceOf(Domain::class));
|
$this->em->expects($this->once())->method('persist')->with($foundDomain ?? $this->isInstanceOf(Domain::class));
|
||||||
$this->em->expects($this->once())->method('flush');
|
$this->em->expects($this->once())->method('flush');
|
||||||
|
|
||||||
|
@ -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');
|
||||||
|
|
||||||
|
@ -6,7 +6,6 @@ namespace ShlinkioTest\Shlink\Core\ShortUrl;
|
|||||||
|
|
||||||
use Cake\Chronos\Chronos;
|
use Cake\Chronos\Chronos;
|
||||||
use Doctrine\Common\Collections\ArrayCollection;
|
use Doctrine\Common\Collections\ArrayCollection;
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
|
||||||
use PHPUnit\Framework\Attributes\DataProvider;
|
use PHPUnit\Framework\Attributes\DataProvider;
|
||||||
use PHPUnit\Framework\Attributes\DataProviderExternal;
|
use PHPUnit\Framework\Attributes\DataProviderExternal;
|
||||||
use PHPUnit\Framework\Attributes\Test;
|
use PHPUnit\Framework\Attributes\Test;
|
||||||
@ -31,14 +30,12 @@ use function range;
|
|||||||
class ShortUrlResolverTest extends TestCase
|
class ShortUrlResolverTest extends TestCase
|
||||||
{
|
{
|
||||||
private ShortUrlResolver $urlResolver;
|
private ShortUrlResolver $urlResolver;
|
||||||
private MockObject & EntityManagerInterface $em;
|
|
||||||
private MockObject & ShortUrlRepository $repo;
|
private MockObject & ShortUrlRepository $repo;
|
||||||
|
|
||||||
protected function setUp(): void
|
protected function setUp(): void
|
||||||
{
|
{
|
||||||
$this->em = $this->createMock(EntityManagerInterface::class);
|
|
||||||
$this->repo = $this->createMock(ShortUrlRepository::class);
|
$this->repo = $this->createMock(ShortUrlRepository::class);
|
||||||
$this->urlResolver = new ShortUrlResolver($this->em, new UrlShortenerOptions());
|
$this->urlResolver = new ShortUrlResolver($this->repo, new UrlShortenerOptions());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[Test, DataProviderExternal(ApiKeyDataProviders::class, 'adminApiKeysProvider')]
|
#[Test, DataProviderExternal(ApiKeyDataProviders::class, 'adminApiKeysProvider')]
|
||||||
@ -51,7 +48,6 @@ class ShortUrlResolverTest extends TestCase
|
|||||||
$this->repo->expects($this->once())->method('findOne')->with($identifier, $apiKey?->spec())->willReturn(
|
$this->repo->expects($this->once())->method('findOne')->with($identifier, $apiKey?->spec())->willReturn(
|
||||||
$shortUrl,
|
$shortUrl,
|
||||||
);
|
);
|
||||||
$this->em->expects($this->once())->method('getRepository')->with(ShortUrl::class)->willReturn($this->repo);
|
|
||||||
|
|
||||||
$result = $this->urlResolver->resolveShortUrl($identifier, $apiKey);
|
$result = $this->urlResolver->resolveShortUrl($identifier, $apiKey);
|
||||||
|
|
||||||
@ -65,7 +61,6 @@ class ShortUrlResolverTest extends TestCase
|
|||||||
$identifier = ShortUrlIdentifier::fromShortCodeAndDomain($shortCode);
|
$identifier = ShortUrlIdentifier::fromShortCodeAndDomain($shortCode);
|
||||||
|
|
||||||
$this->repo->expects($this->once())->method('findOne')->with($identifier, $apiKey?->spec())->willReturn(null);
|
$this->repo->expects($this->once())->method('findOne')->with($identifier, $apiKey?->spec())->willReturn(null);
|
||||||
$this->em->expects($this->once())->method('getRepository')->with(ShortUrl::class)->willReturn($this->repo);
|
|
||||||
|
|
||||||
$this->expectException(ShortUrlNotFoundException::class);
|
$this->expectException(ShortUrlNotFoundException::class);
|
||||||
|
|
||||||
@ -82,7 +77,6 @@ class ShortUrlResolverTest extends TestCase
|
|||||||
ShortUrlIdentifier::fromShortCodeAndDomain($shortCode),
|
ShortUrlIdentifier::fromShortCodeAndDomain($shortCode),
|
||||||
ShortUrlMode::STRICT,
|
ShortUrlMode::STRICT,
|
||||||
)->willReturn($shortUrl);
|
)->willReturn($shortUrl);
|
||||||
$this->em->expects($this->once())->method('getRepository')->with(ShortUrl::class)->willReturn($this->repo);
|
|
||||||
|
|
||||||
$result = $this->urlResolver->resolveEnabledShortUrl(ShortUrlIdentifier::fromShortCodeAndDomain($shortCode));
|
$result = $this->urlResolver->resolveEnabledShortUrl(ShortUrlIdentifier::fromShortCodeAndDomain($shortCode));
|
||||||
|
|
||||||
@ -98,7 +92,6 @@ class ShortUrlResolverTest extends TestCase
|
|||||||
ShortUrlIdentifier::fromShortCodeAndDomain($shortCode),
|
ShortUrlIdentifier::fromShortCodeAndDomain($shortCode),
|
||||||
ShortUrlMode::STRICT,
|
ShortUrlMode::STRICT,
|
||||||
)->willReturn(null);
|
)->willReturn(null);
|
||||||
$this->em->expects($this->once())->method('getRepository')->with(ShortUrl::class)->willReturn($this->repo);
|
|
||||||
|
|
||||||
$this->expectException(ShortUrlNotFoundException::class);
|
$this->expectException(ShortUrlNotFoundException::class);
|
||||||
|
|
||||||
@ -120,7 +113,6 @@ class ShortUrlResolverTest extends TestCase
|
|||||||
ShortUrlIdentifier::fromShortCodeAndDomain($shortCode),
|
ShortUrlIdentifier::fromShortCodeAndDomain($shortCode),
|
||||||
ShortUrlMode::STRICT,
|
ShortUrlMode::STRICT,
|
||||||
)->willReturn($shortUrl);
|
)->willReturn($shortUrl);
|
||||||
$this->em->expects($this->once())->method('getRepository')->with(ShortUrl::class)->willReturn($this->repo);
|
|
||||||
|
|
||||||
$this->expectException(ShortUrlNotFoundException::class);
|
$this->expectException(ShortUrlNotFoundException::class);
|
||||||
|
|
||||||
|
@ -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);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user