mirror of
https://github.com/shlinkio/shlink.git
synced 2026-11-01 09:38:04 -06:00
Merge pull request #2614 from acelaya-forks/short-url-props
Use asymmetric visibility instead of getters in ShortUrl entity
This commit is contained in:
31 files changed
+89
-125
No files matched your search
@@ -51,7 +51,7 @@ class CreateShortUrlCommand extends Command
|
||||
);
|
||||
|
||||
$io->writeln([
|
||||
sprintf('Processed long URL: <info>%s</info>', $result->shortUrl->getLongUrl()),
|
||||
sprintf('Processed long URL: <info>%s</info>', $result->shortUrl->longUrl),
|
||||
sprintf('Generated short URL: <info>%s</info>', $this->stringifier->stringify($result->shortUrl)),
|
||||
]);
|
||||
return self::SUCCESS;
|
||||
|
||||
@@ -107,7 +107,7 @@ class ListShortUrlsCommand extends Command
|
||||
}
|
||||
if ($params->showDomain) {
|
||||
$columnsMap['Domain'] = static fn (array $_, ShortUrl $shortUrl): string => (
|
||||
$shortUrl->getDomain()->authority ?? Domain::DEFAULT_AUTHORITY
|
||||
$shortUrl->domain->authority ?? Domain::DEFAULT_AUTHORITY
|
||||
);
|
||||
}
|
||||
if ($params->showApiKey) {
|
||||
|
||||
@@ -39,7 +39,7 @@ class ResolveUrlCommand extends Command
|
||||
|
||||
try {
|
||||
$url = $this->urlResolver->resolveShortUrl($identifier);
|
||||
$io->writeln(sprintf('Long URL: <info>%s</info>', $url->getLongUrl()));
|
||||
$io->writeln(sprintf('Long URL: <info>%s</info>', $url->longUrl));
|
||||
return self::SUCCESS;
|
||||
} catch (ShortUrlNotFoundException $e) {
|
||||
$io->error($e->getMessage());
|
||||
|
||||
@@ -82,7 +82,7 @@ class CreateShortUrlCommandTest extends TestCase
|
||||
);
|
||||
$this->stringifier->method('stringify')->willReturnMap([[$shortUrl, 'stringified_short_url']]);
|
||||
|
||||
$this->commandTester->setInputs([$shortUrl->getLongUrl()]);
|
||||
$this->commandTester->setInputs([$shortUrl->longUrl]);
|
||||
$this->commandTester->execute([]);
|
||||
}
|
||||
|
||||
|
||||
@@ -144,8 +144,8 @@ class ListShortUrlsCommandTest extends TestCase
|
||||
apiKey: ApiKey::fromMeta(ApiKeyMeta::fromParams(name: 'my api key')),
|
||||
tags: ['foo', 'bar', 'baz'],
|
||||
));
|
||||
$shortCode = $shortUrl->getShortCode();
|
||||
$created = $shortUrl->dateCreated()->toAtomString();
|
||||
$shortCode = $shortUrl->shortCode;
|
||||
$created = $shortUrl->dateCreated->toAtomString();
|
||||
|
||||
yield 'tags only' => [
|
||||
['--show-tags' => true],
|
||||
|
||||
@@ -31,7 +31,7 @@ final readonly class PublishingUpdatesGenerator implements PublishingUpdatesGene
|
||||
public function newShortUrlVisitUpdate(Visit $visit): Update
|
||||
{
|
||||
$shortUrl = $visit->shortUrl;
|
||||
$topic = Topic::newShortUrlVisit($shortUrl?->getShortCode());
|
||||
$topic = Topic::newShortUrlVisit($shortUrl?->shortCode);
|
||||
|
||||
return Update::forTopicAndPayload($topic, [
|
||||
'shortUrl' => $this->transformShortUrl($shortUrl),
|
||||
|
||||
@@ -77,7 +77,7 @@ readonly class MatomoVisitSender implements MatomoVisitSenderInterface
|
||||
}
|
||||
|
||||
// Send the short URL title or an empty document title to avoid different actions to be created by matomo
|
||||
$tracker->doTrackPageView($visit->shortUrl?->title() ?? '');
|
||||
$tracker->doTrackPageView($visit->shortUrl->title ?? '');
|
||||
}
|
||||
|
||||
private function resolveUrlToTrack(Visit $visit): string
|
||||
|
||||
@@ -21,6 +21,6 @@ readonly class ShortUrlRedirectionResolver implements ShortUrlRedirectionResolve
|
||||
}
|
||||
}
|
||||
|
||||
return $shortUrl->getLongUrl();
|
||||
return $shortUrl->longUrl;
|
||||
}
|
||||
}
|
||||
@@ -36,28 +36,28 @@ class ShortUrl extends AbstractEntity
|
||||
{
|
||||
/**
|
||||
* @param Collection<int, Tag> $tags
|
||||
* @param Collection<int, Visit> & Selectable<int, Visit> $visits
|
||||
* @param Collection<int, ShortUrlVisitsCount> & Selectable<int, ShortUrlVisitsCount> $visitsCounts
|
||||
* @param Collection<int, Visit>&Selectable<int, Visit> $visits
|
||||
* @param Collection<int, ShortUrlVisitsCount>&Selectable<int, ShortUrlVisitsCount> $visitsCounts
|
||||
* @param Collection<int, ShortUrlRedirectRule> $redirectRules
|
||||
*/
|
||||
private function __construct(
|
||||
private string $longUrl,
|
||||
private string $shortCode,
|
||||
private Chronos $dateCreated = new Chronos(),
|
||||
private Collection $tags = new ArrayCollection(),
|
||||
private(set) string $longUrl,
|
||||
private(set) string $shortCode,
|
||||
private(set) Chronos $dateCreated = new Chronos(),
|
||||
private(set) Collection $tags = new ArrayCollection(),
|
||||
private Collection&Selectable $visits = new ArrayCollection(),
|
||||
private Collection&Selectable $visitsCounts = new ArrayCollection(),
|
||||
private Chronos|null $validSince = null,
|
||||
private Chronos|null $validUntil = null,
|
||||
private int|null $maxVisits = null,
|
||||
private Domain|null $domain = null,
|
||||
private(set) Chronos|null $validSince = null,
|
||||
private(set) Chronos|null $validUntil = null,
|
||||
private(set) int|null $maxVisits = null,
|
||||
private(set) Domain|null $domain = null,
|
||||
private bool $customSlugWasProvided = false,
|
||||
private int $shortCodeLength = 0,
|
||||
public readonly ApiKey|null $authorApiKey = null,
|
||||
private string|null $title = null,
|
||||
private(set) string|null $title = null,
|
||||
private bool $titleWasAutoResolved = false,
|
||||
private bool $crawlable = false,
|
||||
private bool $forwardQuery = true,
|
||||
private(set) bool $crawlable = false,
|
||||
private(set) bool $forwardQuery = true,
|
||||
private string|null $importSource = null,
|
||||
private string|null $importOriginalShortCode = null,
|
||||
private Collection $redirectRules = new ArrayCollection(),
|
||||
@@ -91,8 +91,6 @@ class ShortUrl extends AbstractEntity
|
||||
shortCode: sprintf(
|
||||
'%s%s',
|
||||
$creation->pathPrefix ?? '',
|
||||
// TODO Encapsulate Generating the random short code into ShortUrlCreation, when custom slug is not set,
|
||||
// then expose it as shortCode or something generic
|
||||
$creation->customSlug ?? generateRandomShortCode($shortCodeLength, $creation->shortUrlMode),
|
||||
),
|
||||
tags: $relationResolver->resolveTags($creation->tags),
|
||||
@@ -170,36 +168,6 @@ class ShortUrl extends AbstractEntity
|
||||
}
|
||||
}
|
||||
|
||||
public function getLongUrl(): string
|
||||
{
|
||||
return $this->longUrl;
|
||||
}
|
||||
|
||||
public function getShortCode(): string
|
||||
{
|
||||
return $this->shortCode;
|
||||
}
|
||||
|
||||
public function getDomain(): Domain|null
|
||||
{
|
||||
return $this->domain;
|
||||
}
|
||||
|
||||
public function forwardQuery(): bool
|
||||
{
|
||||
return $this->forwardQuery;
|
||||
}
|
||||
|
||||
public function title(): string|null
|
||||
{
|
||||
return $this->title;
|
||||
}
|
||||
|
||||
public function dateCreated(): Chronos
|
||||
{
|
||||
return $this->dateCreated;
|
||||
}
|
||||
|
||||
public function reachedVisits(int $visitsAmount): bool
|
||||
{
|
||||
return count($this->visits) >= $visitsAmount;
|
||||
|
||||
@@ -27,7 +27,7 @@ readonly class ShortUrlRedirectionBuilder implements ShortUrlRedirectionBuilderI
|
||||
string|null $extraPath = null,
|
||||
): string {
|
||||
$uri = new Uri($this->redirectionResolver->resolveLongUrl($shortUrl, $request));
|
||||
$shouldForwardQuery = $shortUrl->forwardQuery();
|
||||
$shouldForwardQuery = $shortUrl->forwardQuery;
|
||||
$baseQueryString = $uri->getQuery();
|
||||
$basePath = $uri->getPath();
|
||||
|
||||
|
||||
@@ -31,8 +31,8 @@ final readonly class ShortUrlIdentifier
|
||||
|
||||
public static function fromShortUrl(ShortUrl $shortUrl): self
|
||||
{
|
||||
$domain = $shortUrl->getDomain()?->authority;
|
||||
return new self($shortUrl->getShortCode(), $domain);
|
||||
$domain = $shortUrl->domain?->authority;
|
||||
return new self($shortUrl->shortCode, $domain);
|
||||
}
|
||||
|
||||
public static function fromShortCodeAndDomain(string $shortCode, string|null $domain = null): self
|
||||
|
||||
@@ -32,12 +32,12 @@ final readonly class ShortUrlWithDeps
|
||||
|
||||
public static function fromShortUrl(ShortUrl $shortUrl): self
|
||||
{
|
||||
return new self($shortUrl, authority: $shortUrl->getDomain()?->authority);
|
||||
return new self($shortUrl, authority: $shortUrl->domain?->authority);
|
||||
}
|
||||
|
||||
public function toIdentifier(): ShortUrlIdentifier
|
||||
{
|
||||
return ShortUrlIdentifier::fromShortCodeAndDomain($this->shortUrl->getShortCode(), $this->authority);
|
||||
return ShortUrlIdentifier::fromShortCodeAndDomain($this->shortUrl->shortCode, $this->authority);
|
||||
}
|
||||
|
||||
public function toArray(): array
|
||||
|
||||
@@ -81,10 +81,8 @@ readonly class UrlShortener implements UrlShortenerInterface
|
||||
);
|
||||
|
||||
if (!$couldBeMadeUnique) {
|
||||
$domain = $shortUrlToBeCreated->getDomain();
|
||||
$domainAuthority = $domain?->authority;
|
||||
|
||||
throw NonUniqueSlugException::fromSlug($shortUrlToBeCreated->getShortCode(), $domainAuthority);
|
||||
$domainAuthority = $shortUrlToBeCreated->domain?->authority;
|
||||
throw NonUniqueSlugException::fromSlug($shortUrlToBeCreated->shortCode, $domainAuthority);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -41,10 +41,10 @@ class CrawlableShortCodesQueryTest extends DatabaseTestCase
|
||||
$results = [...($this->query)()];
|
||||
|
||||
self::assertCount(3, $results);
|
||||
self::assertContains($shortUrl1->getShortCode(), $results);
|
||||
self::assertContains($shortUrl3->getShortCode(), $results);
|
||||
self::assertContains($shortUrl4->getShortCode(), $results);
|
||||
self::assertNotContains($shortUrl2->getShortCode(), $results);
|
||||
self::assertNotContains($shortUrl5->getShortCode(), $results);
|
||||
self::assertContains($shortUrl1->shortCode, $results);
|
||||
self::assertContains($shortUrl3->shortCode, $results);
|
||||
self::assertContains($shortUrl4->shortCode, $results);
|
||||
self::assertNotContains($shortUrl2->shortCode, $results);
|
||||
self::assertNotContains($shortUrl5->shortCode, $results);
|
||||
}
|
||||
}
|
||||
@@ -166,10 +166,10 @@ class ShortUrlListRepositoryTest extends DatabaseTestCase
|
||||
$result = $this->repo->findList(new ShortUrlsListFiltering(orderBy: Ordering::fromFieldAsc('longUrl')));
|
||||
|
||||
self::assertCount(count($urls), $result);
|
||||
self::assertEquals('https://a', $result[0]->shortUrl->getLongUrl());
|
||||
self::assertEquals('https://b', $result[1]->shortUrl->getLongUrl());
|
||||
self::assertEquals('https://c', $result[2]->shortUrl->getLongUrl());
|
||||
self::assertEquals('https://z', $result[3]->shortUrl->getLongUrl());
|
||||
self::assertEquals('https://a', $result[0]->shortUrl->longUrl);
|
||||
self::assertEquals('https://b', $result[1]->shortUrl->longUrl);
|
||||
self::assertEquals('https://c', $result[2]->shortUrl->longUrl);
|
||||
self::assertEquals('https://z', $result[3]->shortUrl->longUrl);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
|
||||
@@ -54,7 +54,7 @@ class ShortUrlRepositoryTest extends DatabaseTestCase
|
||||
$this->getEntityManager()->flush();
|
||||
|
||||
self::assertSame($regularOne, $this->repo->findOneWithDomainFallback(
|
||||
ShortUrlIdentifier::fromShortCodeAndDomain($regularOne->getShortCode()),
|
||||
ShortUrlIdentifier::fromShortCodeAndDomain($regularOne->shortCode),
|
||||
ShortUrlMode::STRICT,
|
||||
));
|
||||
self::assertSame($regularOne, $this->repo->findOneWithDomainFallback(
|
||||
@@ -70,22 +70,22 @@ class ShortUrlRepositoryTest extends DatabaseTestCase
|
||||
ShortUrlMode::STRICT,
|
||||
));
|
||||
self::assertSame($regularOne, $this->repo->findOneWithDomainFallback(
|
||||
ShortUrlIdentifier::fromShortCodeAndDomain($withDomainDuplicatingRegular->getShortCode()),
|
||||
ShortUrlIdentifier::fromShortCodeAndDomain($withDomainDuplicatingRegular->shortCode),
|
||||
ShortUrlMode::STRICT,
|
||||
));
|
||||
self::assertSame($withDomain, $this->repo->findOneWithDomainFallback(
|
||||
ShortUrlIdentifier::fromShortCodeAndDomain($withDomain->getShortCode(), 'example.com'),
|
||||
ShortUrlIdentifier::fromShortCodeAndDomain($withDomain->shortCode, 'example.com'),
|
||||
ShortUrlMode::STRICT,
|
||||
));
|
||||
self::assertSame(
|
||||
$withDomainDuplicatingRegular,
|
||||
$this->repo->findOneWithDomainFallback(
|
||||
ShortUrlIdentifier::fromShortCodeAndDomain($withDomainDuplicatingRegular->getShortCode(), 's.test'),
|
||||
ShortUrlIdentifier::fromShortCodeAndDomain($withDomainDuplicatingRegular->shortCode, 's.test'),
|
||||
ShortUrlMode::STRICT,
|
||||
),
|
||||
);
|
||||
self::assertSame($regularOne, $this->repo->findOneWithDomainFallback(ShortUrlIdentifier::fromShortCodeAndDomain(
|
||||
$withDomainDuplicatingRegular->getShortCode(),
|
||||
$withDomainDuplicatingRegular->shortCode,
|
||||
'other-domain.com',
|
||||
), ShortUrlMode::STRICT));
|
||||
self::assertNull($this->repo->findOneWithDomainFallback(
|
||||
@@ -93,11 +93,11 @@ class ShortUrlRepositoryTest extends DatabaseTestCase
|
||||
ShortUrlMode::STRICT,
|
||||
));
|
||||
self::assertNull($this->repo->findOneWithDomainFallback(
|
||||
ShortUrlIdentifier::fromShortCodeAndDomain($withDomain->getShortCode()),
|
||||
ShortUrlIdentifier::fromShortCodeAndDomain($withDomain->shortCode),
|
||||
ShortUrlMode::STRICT,
|
||||
));
|
||||
self::assertNull($this->repo->findOneWithDomainFallback(
|
||||
ShortUrlIdentifier::fromShortCodeAndDomain($withDomain->getShortCode(), 'other-domain.com'),
|
||||
ShortUrlIdentifier::fromShortCodeAndDomain($withDomain->shortCode, 'other-domain.com'),
|
||||
ShortUrlMode::STRICT,
|
||||
));
|
||||
}
|
||||
|
||||
@@ -666,7 +666,7 @@ class VisitRepositoryTest extends DatabaseTestCase
|
||||
tags: $tags,
|
||||
), $this->relationResolver);
|
||||
$domain = is_string($withDomain) ? $withDomain : 'example.com';
|
||||
$shortCode = $shortUrl->getShortCode();
|
||||
$shortCode = $shortUrl->shortCode;
|
||||
$this->getEntityManager()->persist($shortUrl);
|
||||
|
||||
$this->createVisitsForShortUrl($shortUrl, $visitsAmount);
|
||||
|
||||
@@ -57,8 +57,8 @@ class PublishingUpdatesGeneratorTest extends TestCase
|
||||
self::assertEquals(
|
||||
[
|
||||
'shortUrl' => [
|
||||
'shortCode' => $shortUrl->getShortCode(),
|
||||
'shortUrl' => 'http:/' . $shortUrl->getShortCode(),
|
||||
'shortCode' => $shortUrl->shortCode,
|
||||
'shortUrl' => 'http:/' . $shortUrl->shortCode,
|
||||
'longUrl' => 'https://longUrl',
|
||||
'dateCreated' => $this->now->toAtomString(),
|
||||
'tags' => [],
|
||||
@@ -141,8 +141,8 @@ class PublishingUpdatesGeneratorTest extends TestCase
|
||||
self::assertEquals(
|
||||
[
|
||||
'shortUrl' => [
|
||||
'shortCode' => $shortUrl->getShortCode(),
|
||||
'shortUrl' => 'http:/' . $shortUrl->getShortCode(),
|
||||
'shortCode' => $shortUrl->shortCode,
|
||||
'shortUrl' => 'http:/' . $shortUrl->shortCode,
|
||||
'longUrl' => 'https://longUrl',
|
||||
'dateCreated' => $this->now->toAtomString(),
|
||||
'tags' => [],
|
||||
|
||||
@@ -157,7 +157,7 @@ class ImportedLinksProcessorTest extends TestCase
|
||||
$this->isInstanceOf(ShortUrl::class),
|
||||
)
|
||||
->willReturnCallback(static function (ShortUrl $shortUrl): void {
|
||||
if ($shortUrl->getShortCode() === 'baz') {
|
||||
if ($shortUrl->shortCode === 'baz') {
|
||||
throw new RuntimeException('Whatever error');
|
||||
}
|
||||
});
|
||||
|
||||
@@ -57,7 +57,7 @@ class MatomoVisitSenderTest extends TestCase
|
||||
$tracker
|
||||
->expects($this->once())
|
||||
->method('doTrackPageView')
|
||||
->with($visit->shortUrl?->title() ?? '');
|
||||
->with($visit->shortUrl->title ?? '');
|
||||
$tracker
|
||||
->expects($this->once())
|
||||
->method('setForceVisitDateTime')
|
||||
|
||||
@@ -39,7 +39,7 @@ class DeleteShortUrlServiceTest extends TestCase
|
||||
$shortUrl = ShortUrl::createFake()->setVisits(new ArrayCollection(
|
||||
array_map(static fn () => Visit::forValidShortUrl(ShortUrl::createFake(), Visitor::empty()), range(0, 10)),
|
||||
));
|
||||
$this->shortCode = $shortUrl->getShortCode();
|
||||
$this->shortCode = $shortUrl->shortCode;
|
||||
|
||||
$this->em = $this->createMock(EntityManagerInterface::class);
|
||||
|
||||
|
||||
@@ -53,10 +53,10 @@ class ShortUrlTest extends TestCase
|
||||
public function regenerateShortCodeProperlyChangesTheValueOnValidShortUrls(
|
||||
ShortUrl $shortUrl,
|
||||
): void {
|
||||
$firstShortCode = $shortUrl->getShortCode();
|
||||
$firstShortCode = $shortUrl->shortCode;
|
||||
|
||||
$shortUrl->regenerateShortCode(ShortUrlMode::STRICT);
|
||||
$secondShortCode = $shortUrl->getShortCode();
|
||||
$secondShortCode = $shortUrl->shortCode;
|
||||
|
||||
self::assertNotEquals($firstShortCode, $secondShortCode);
|
||||
}
|
||||
@@ -80,7 +80,7 @@ class ShortUrlTest extends TestCase
|
||||
new ShortUrlCreation('https://longUrl', shortCodeLength: $length ?? DEFAULT_SHORT_CODES_LENGTH),
|
||||
);
|
||||
|
||||
self::assertEquals($expectedLength, strlen($shortUrl->getShortCode()));
|
||||
self::assertEquals($expectedLength, strlen($shortUrl->shortCode));
|
||||
}
|
||||
|
||||
public static function provideLengths(): iterable
|
||||
@@ -102,7 +102,7 @@ class ShortUrlTest extends TestCase
|
||||
pathPrefix: $pathPrefix,
|
||||
shortCodeLength: 5,
|
||||
));
|
||||
$shortCode = $shortUrl->getShortCode();
|
||||
$shortCode = $shortUrl->shortCode;
|
||||
|
||||
if (strlen($expectedPrefix) > 0) {
|
||||
self::assertStringStartsWith($expectedPrefix, $shortCode);
|
||||
@@ -116,7 +116,7 @@ class ShortUrlTest extends TestCase
|
||||
$range = range(1, 1000); // Use a "big" number to reduce false negatives
|
||||
$allFor = static fn (ShortUrlMode $mode): bool => every($range, static function () use ($mode): bool {
|
||||
$shortUrl = ShortUrl::create(new ShortUrlCreation('https://foo', shortUrlMode: $mode));
|
||||
$shortCode = $shortUrl->getShortCode();
|
||||
$shortCode = $shortUrl->shortCode;
|
||||
|
||||
return $shortCode === strtolower($shortCode);
|
||||
});
|
||||
|
||||
@@ -9,9 +9,9 @@ use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Shlinkio\Shlink\Core\Config\Options\UrlShortenerOptions;
|
||||
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\ShortUrlCreation;
|
||||
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlIdentifier;
|
||||
use Shlinkio\Shlink\Core\ShortUrl\Repository\ShortUrlRepositoryInterface;
|
||||
|
||||
@@ -19,47 +19,42 @@ class ShortCodeUniquenessHelperTest extends TestCase
|
||||
{
|
||||
private ShortCodeUniquenessHelper $helper;
|
||||
private MockObject&ShortUrlRepositoryInterface $repo;
|
||||
private MockObject&ShortUrl $shortUrl;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$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');
|
||||
}
|
||||
|
||||
#[Test, DataProvider('provideDomains')]
|
||||
public function shortCodeIsRegeneratedIfAlreadyInUse(Domain|null $domain, string|null $expectedAuthority): void
|
||||
public function shortCodeIsRegeneratedIfAlreadyInUse(string|null $domain, string|null $expectedAuthority): void
|
||||
{
|
||||
$shortUrl = $this->shortUrl($domain);
|
||||
$initialShortCode = $shortUrl->shortCode;
|
||||
|
||||
$callIndex = 0;
|
||||
$expectedCalls = 3;
|
||||
$this->repo
|
||||
->expects($this->exactly($expectedCalls))
|
||||
->method('shortCodeIsInUseWithLock')
|
||||
->with(
|
||||
ShortUrlIdentifier::fromShortCodeAndDomain('abc123', $expectedAuthority),
|
||||
)
|
||||
->with($this->callback(
|
||||
static fn (ShortUrlIdentifier $identifier) => $identifier->domain === $expectedAuthority,
|
||||
))
|
||||
->willReturnCallback(static function () use (&$callIndex, $expectedCalls) {
|
||||
$callIndex++;
|
||||
return $callIndex < $expectedCalls;
|
||||
});
|
||||
$this->shortUrl->method('getDomain')->willReturn($domain);
|
||||
$this->shortUrl
|
||||
->expects($this->exactly($expectedCalls - 1))
|
||||
->method('regenerateShortCode')
|
||||
->with();
|
||||
|
||||
$result = $this->helper->ensureShortCodeUniqueness($this->shortUrl, false);
|
||||
$result = $this->helper->ensureShortCodeUniqueness($shortUrl, hasCustomSlug: false);
|
||||
|
||||
self::assertTrue($result);
|
||||
self::assertNotEquals($initialShortCode, $shortUrl->shortCode);
|
||||
}
|
||||
|
||||
public static function provideDomains(): iterable
|
||||
{
|
||||
yield 'no domain' => [null, null];
|
||||
yield 'domain' => [Domain::withAuthority($authority = 's.test'), $authority];
|
||||
yield 'domain' => [$authority = 's.test', $authority];
|
||||
}
|
||||
|
||||
#[Test]
|
||||
@@ -68,15 +63,18 @@ class ShortCodeUniquenessHelperTest extends TestCase
|
||||
$this->repo
|
||||
->expects($this->once())
|
||||
->method('shortCodeIsInUseWithLock')
|
||||
->with(
|
||||
ShortUrlIdentifier::fromShortCodeAndDomain('abc123'),
|
||||
)
|
||||
->willReturn(true);
|
||||
$this->shortUrl->method('getDomain')->willReturn(null);
|
||||
$this->shortUrl->expects($this->never())->method('regenerateShortCode');
|
||||
|
||||
$result = $this->helper->ensureShortCodeUniqueness($this->shortUrl, true);
|
||||
$shortUrl = $this->shortUrl();
|
||||
$initialShortCode = $shortUrl->shortCode;
|
||||
$result = $this->helper->ensureShortCodeUniqueness($shortUrl, hasCustomSlug: true);
|
||||
|
||||
self::assertFalse($result);
|
||||
self::assertEquals($initialShortCode, $shortUrl->shortCode);
|
||||
}
|
||||
|
||||
private function shortUrl(string|null $domain = null): ShortUrl
|
||||
{
|
||||
return ShortUrl::create(new ShortUrlCreation('https://example.com', domain: $domain));
|
||||
}
|
||||
}
|
||||
@@ -49,7 +49,7 @@ class ShortUrlRedirectionBuilderTest extends TestCase
|
||||
$shortUrl,
|
||||
$request,
|
||||
)
|
||||
->willReturn($shortUrl->getLongUrl());
|
||||
->willReturn($shortUrl->longUrl);
|
||||
|
||||
$result = $this->redirectionBuilder->buildShortUrlRedirect($shortUrl, $request, $extraPath);
|
||||
|
||||
@@ -143,7 +143,7 @@ class ShortUrlRedirectionBuilderTest extends TestCase
|
||||
$shortUrl,
|
||||
$request,
|
||||
)
|
||||
->willReturn($shortUrl->getLongUrl());
|
||||
->willReturn($shortUrl->longUrl);
|
||||
|
||||
$result = $this->redirectionBuilder->buildShortUrlRedirect($shortUrl, $request);
|
||||
|
||||
|
||||
@@ -42,7 +42,7 @@ class ShortUrlResolverTest extends TestCase
|
||||
public function shortCodeIsProperlyParsed(ApiKey|null $apiKey): void
|
||||
{
|
||||
$shortUrl = ShortUrl::withLongUrl('https://expected_url');
|
||||
$shortCode = $shortUrl->getShortCode();
|
||||
$shortCode = $shortUrl->shortCode;
|
||||
$identifier = ShortUrlIdentifier::fromShortCodeAndDomain($shortCode);
|
||||
|
||||
$this->repo
|
||||
@@ -75,7 +75,7 @@ class ShortUrlResolverTest extends TestCase
|
||||
public function resolveEnabledShortUrlProperlyParsesShortCode(): void
|
||||
{
|
||||
$shortUrl = ShortUrl::withLongUrl('https://expected_url');
|
||||
$shortCode = $shortUrl->getShortCode();
|
||||
$shortCode = $shortUrl->shortCode;
|
||||
|
||||
$this->repo
|
||||
->expects($this->once())
|
||||
@@ -119,7 +119,7 @@ class ShortUrlResolverTest extends TestCase
|
||||
#[Test, DataProvider('provideDisabledShortUrls')]
|
||||
public function resolveEnabledShortUrlThrowsExceptionIfUrlIsNotEnabled(ShortUrl $shortUrl): void
|
||||
{
|
||||
$shortCode = $shortUrl->getShortCode();
|
||||
$shortCode = $shortUrl->shortCode;
|
||||
|
||||
$this->repo
|
||||
->expects($this->once())
|
||||
|
||||
@@ -82,7 +82,7 @@ class ShortUrlServiceTest extends TestCase
|
||||
$until,
|
||||
);
|
||||
self::assertEquals($shortUrlEdit->maxVisits, $maxVisits);
|
||||
self::assertEquals($shortUrlEdit->longUrl ?? $originalLongUrl, $shortUrl->getLongUrl());
|
||||
self::assertEquals($shortUrlEdit->longUrl ?? $originalLongUrl, $shortUrl->longUrl);
|
||||
}
|
||||
|
||||
public static function provideShortUrlEdits(): iterable
|
||||
|
||||
@@ -74,7 +74,7 @@ class UrlShortenerTest extends TestCase
|
||||
$thereIsError = true;
|
||||
});
|
||||
|
||||
self::assertEquals($longUrl, $result->shortUrl->getLongUrl());
|
||||
self::assertEquals($longUrl, $result->shortUrl->longUrl);
|
||||
self::assertEquals($expectDispatchError, $thereIsError);
|
||||
}
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@ class ListRedirectRulesAction extends AbstractRestAction
|
||||
$rules = $this->ruleService->rulesForShortUrl($shortUrl);
|
||||
|
||||
return new JsonResponse([
|
||||
'defaultLongUrl' => $shortUrl->getLongUrl(),
|
||||
'defaultLongUrl' => $shortUrl->longUrl,
|
||||
'redirectRules' => $rules,
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ class SetRedirectRulesAction extends AbstractRestAction
|
||||
$result = $this->ruleService->setRulesForShortUrl($shortUrl, $data);
|
||||
|
||||
return new JsonResponse([
|
||||
'defaultLongUrl' => $shortUrl->getLongUrl(),
|
||||
'defaultLongUrl' => $shortUrl->longUrl,
|
||||
'redirectRules' => $result,
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -52,7 +52,7 @@ class ListRedirectRulesActionTest extends TestCase
|
||||
|
||||
self::assertEquals(
|
||||
[
|
||||
'defaultLongUrl' => $shortUrl->getLongUrl(),
|
||||
'defaultLongUrl' => $shortUrl->longUrl,
|
||||
'redirectRules' => $redirectRules,
|
||||
],
|
||||
$payload,
|
||||
|
||||
@@ -57,7 +57,7 @@ class SetRedirectRulesActionTest extends TestCase
|
||||
|
||||
self::assertEquals(
|
||||
[
|
||||
'defaultLongUrl' => $shortUrl->getLongUrl(),
|
||||
'defaultLongUrl' => $shortUrl->longUrl,
|
||||
'redirectRules' => $redirectRules,
|
||||
],
|
||||
$payload,
|
||||
|
||||
Reference in new issue
Block a user