Enforce a schema to be provided when short URLs are created

This commit is contained in:
Alejandro Celaya
2023-03-25 09:52:47 +01:00
parent 11f94b8306
commit b6e1c65c4c
26 changed files with 135 additions and 107 deletions

View File

@@ -132,7 +132,7 @@ class DomainRepositoryTest extends DatabaseTestCase
{
return ShortUrl::create(
ShortUrlCreation::fromRawData(
['domain' => $domain->authority, 'apiKey' => $apiKey, 'longUrl' => 'foo'],
['domain' => $domain->authority, 'apiKey' => $apiKey, 'longUrl' => 'https://foo'],
),
new class ($domain) implements ShortUrlRelationResolverInterface {
public function __construct(private Domain $domain)

View File

@@ -24,7 +24,7 @@ class CrawlableShortCodesQueryTest extends DatabaseTestCase
public function invokingQueryReturnsExpectedResult(): void
{
$createShortUrl = fn (bool $crawlable) => ShortUrl::create(
ShortUrlCreation::fromRawData(['crawlable' => $crawlable, 'longUrl' => 'foo.com']),
ShortUrlCreation::fromRawData(['crawlable' => $crawlable, 'longUrl' => 'https://foo.com']),
);
$shortUrl1 = $createShortUrl(true);

View File

@@ -43,7 +43,7 @@ class ShortUrlListRepositoryTest extends DatabaseTestCase
{
$count = 5;
for ($i = 0; $i < $count; $i++) {
$this->getEntityManager()->persist(ShortUrl::withLongUrl((string) $i));
$this->getEntityManager()->persist(ShortUrl::withLongUrl('https://' . $i));
}
$this->getEntityManager()->flush();
@@ -54,12 +54,12 @@ class ShortUrlListRepositoryTest extends DatabaseTestCase
public function findListProperlyFiltersResult(): void
{
$foo = ShortUrl::create(
ShortUrlCreation::fromRawData(['longUrl' => 'foo', 'tags' => ['bar']]),
ShortUrlCreation::fromRawData(['longUrl' => 'foo', 'tags' => ['https://bar']]),
$this->relationResolver,
);
$this->getEntityManager()->persist($foo);
$bar = ShortUrl::withLongUrl('bar');
$bar = ShortUrl::withLongUrl('https://bar');
$visits = map(range(0, 5), function () use ($bar) {
$visit = Visit::forValidShortUrl($bar, Visitor::botInstance());
$this->getEntityManager()->persist($visit);
@@ -69,7 +69,7 @@ class ShortUrlListRepositoryTest extends DatabaseTestCase
$bar->setVisits(new ArrayCollection($visits));
$this->getEntityManager()->persist($bar);
$foo2 = ShortUrl::withLongUrl('foo_2');
$foo2 = ShortUrl::withLongUrl('https://foo_2');
$visits2 = map(range(0, 3), function () use ($foo2) {
$visit = Visit::forValidShortUrl($foo2, Visitor::emptyInstance());
$this->getEntityManager()->persist($visit);
@@ -147,7 +147,7 @@ class ShortUrlListRepositoryTest extends DatabaseTestCase
#[Test]
public function findListProperlyMapsFieldNamesToColumnNamesWhenOrdering(): void
{
$urls = ['a', 'z', 'c', 'b'];
$urls = ['https://a', 'https://z', 'https://c', 'https://b'];
foreach ($urls as $url) {
$this->getEntityManager()->persist(ShortUrl::withLongUrl($url));
}
@@ -159,37 +159,37 @@ class ShortUrlListRepositoryTest extends DatabaseTestCase
);
self::assertCount(count($urls), $result);
self::assertEquals('a', $result[0]->getLongUrl());
self::assertEquals('b', $result[1]->getLongUrl());
self::assertEquals('c', $result[2]->getLongUrl());
self::assertEquals('z', $result[3]->getLongUrl());
self::assertEquals('https://a', $result[0]->getLongUrl());
self::assertEquals('https://b', $result[1]->getLongUrl());
self::assertEquals('https://c', $result[2]->getLongUrl());
self::assertEquals('https://z', $result[3]->getLongUrl());
}
#[Test]
public function findListReturnsOnlyThoseWithMatchingTags(): void
{
$shortUrl1 = ShortUrl::create(ShortUrlCreation::fromRawData([
'longUrl' => 'foo1',
'longUrl' => 'https://foo1',
'tags' => ['foo', 'bar'],
]), $this->relationResolver);
$this->getEntityManager()->persist($shortUrl1);
$shortUrl2 = ShortUrl::create(ShortUrlCreation::fromRawData([
'longUrl' => 'foo2',
'longUrl' => 'https://foo2',
'tags' => ['foo', 'baz'],
]), $this->relationResolver);
$this->getEntityManager()->persist($shortUrl2);
$shortUrl3 = ShortUrl::create(ShortUrlCreation::fromRawData([
'longUrl' => 'foo3',
'longUrl' => 'https://foo3',
'tags' => ['foo'],
]), $this->relationResolver);
$this->getEntityManager()->persist($shortUrl3);
$shortUrl4 = ShortUrl::create(ShortUrlCreation::fromRawData([
'longUrl' => 'foo4',
'longUrl' => 'https://foo4',
'tags' => ['bar', 'baz'],
]), $this->relationResolver);
$this->getEntityManager()->persist($shortUrl4);
$shortUrl5 = ShortUrl::create(ShortUrlCreation::fromRawData([
'longUrl' => 'foo5',
'longUrl' => 'https://foo5',
'tags' => ['bar', 'baz'],
]), $this->relationResolver);
$this->getEntityManager()->persist($shortUrl5);
@@ -278,17 +278,17 @@ class ShortUrlListRepositoryTest extends DatabaseTestCase
public function findListReturnsOnlyThoseWithMatchingDomains(): void
{
$shortUrl1 = ShortUrl::create(ShortUrlCreation::fromRawData([
'longUrl' => 'foo1',
'longUrl' => 'https://foo1',
'domain' => null,
]), $this->relationResolver);
$this->getEntityManager()->persist($shortUrl1);
$shortUrl2 = ShortUrl::create(ShortUrlCreation::fromRawData([
'longUrl' => 'foo2',
'longUrl' => 'https://foo2',
'domain' => null,
]), $this->relationResolver);
$this->getEntityManager()->persist($shortUrl2);
$shortUrl3 = ShortUrl::create(ShortUrlCreation::fromRawData([
'longUrl' => 'foo3',
'longUrl' => 'https://foo3',
'domain' => 'another.com',
]), $this->relationResolver);
$this->getEntityManager()->persist($shortUrl3);
@@ -314,22 +314,22 @@ class ShortUrlListRepositoryTest extends DatabaseTestCase
public function findListReturnsOnlyThoseWithoutExcludedUrls(): void
{
$shortUrl1 = ShortUrl::create(ShortUrlCreation::fromRawData([
'longUrl' => 'foo1',
'longUrl' => 'https://foo1',
'validUntil' => Chronos::now()->addDays(1)->toAtomString(),
'maxVisits' => 100,
]), $this->relationResolver);
$this->getEntityManager()->persist($shortUrl1);
$shortUrl2 = ShortUrl::create(ShortUrlCreation::fromRawData([
'longUrl' => 'foo2',
'longUrl' => 'https://foo2',
'validUntil' => Chronos::now()->subDays(1)->toAtomString(),
]), $this->relationResolver);
$this->getEntityManager()->persist($shortUrl2);
$shortUrl3 = ShortUrl::create(ShortUrlCreation::fromRawData([
'longUrl' => 'foo3',
'longUrl' => 'https://foo3',
]), $this->relationResolver);
$this->getEntityManager()->persist($shortUrl3);
$shortUrl4 = ShortUrl::create(ShortUrlCreation::fromRawData([
'longUrl' => 'foo4',
'longUrl' => 'https://foo4',
'maxVisits' => 3,
]), $this->relationResolver);
$this->getEntityManager()->persist($shortUrl4);

View File

@@ -34,16 +34,18 @@ class ShortUrlRepositoryTest extends DatabaseTestCase
#[Test]
public function findOneWithDomainFallbackReturnsProperData(): void
{
$regularOne = ShortUrl::create(ShortUrlCreation::fromRawData(['customSlug' => 'Foo', 'longUrl' => 'foo']));
$regularOne = ShortUrl::create(
ShortUrlCreation::fromRawData(['customSlug' => 'Foo', 'longUrl' => 'https://foo']),
);
$this->getEntityManager()->persist($regularOne);
$withDomain = ShortUrl::create(ShortUrlCreation::fromRawData(
['domain' => 'example.com', 'customSlug' => 'domain-short-code', 'longUrl' => 'foo'],
['domain' => 'example.com', 'customSlug' => 'domain-short-code', 'longUrl' => 'https://foo'],
));
$this->getEntityManager()->persist($withDomain);
$withDomainDuplicatingRegular = ShortUrl::create(ShortUrlCreation::fromRawData(
['domain' => 's.test', 'customSlug' => 'Foo', 'longUrl' => 'foo_with_domain'],
['domain' => 's.test', 'customSlug' => 'Foo', 'longUrl' => 'https://foo_with_domain'],
));
$this->getEntityManager()->persist($withDomainDuplicatingRegular);
@@ -102,7 +104,7 @@ class ShortUrlRepositoryTest extends DatabaseTestCase
public function shortCodeIsInUseLooksForShortUrlInProperSetOfTables(): void
{
$shortUrlWithoutDomain = ShortUrl::create(
ShortUrlCreation::fromRawData(['customSlug' => 'my-cool-slug', 'longUrl' => 'foo']),
ShortUrlCreation::fromRawData(['customSlug' => 'my-cool-slug', 'longUrl' => 'https://foo']),
);
$this->getEntityManager()->persist($shortUrlWithoutDomain);
@@ -396,7 +398,7 @@ class ShortUrlRepositoryTest extends DatabaseTestCase
public function importedShortUrlsAreFoundWhenExpected(): void
{
$buildImported = static fn (string $shortCode, ?string $domain = null) =>
new ImportedShlinkUrl(ImportSource::BITLY, 'foo', [], Chronos::now(), $domain, $shortCode, null);
new ImportedShlinkUrl(ImportSource::BITLY, 'https://foo', [], Chronos::now(), $domain, $shortCode, null);
$shortUrlWithoutDomain = ShortUrl::fromImport($buildImported('my-cool-slug'), true);
$this->getEntityManager()->persist($shortUrlWithoutDomain);

View File

@@ -74,7 +74,7 @@ class TagRepositoryTest extends DatabaseTestCase
[$firstUrlTags] = array_chunk($names, 3);
$secondUrlTags = [$names[0]];
$metaWithTags = static fn (array $tags, ?ApiKey $apiKey) => ShortUrlCreation::fromRawData(
['longUrl' => 'longUrl', 'tags' => $tags, 'apiKey' => $apiKey],
['longUrl' => 'https://longUrl', 'tags' => $tags, 'apiKey' => $apiKey],
);
$shortUrl = ShortUrl::create($metaWithTags($firstUrlTags, $apiKey), $this->relationResolver);
@@ -241,14 +241,14 @@ class TagRepositoryTest extends DatabaseTestCase
[$firstUrlTags, $secondUrlTags] = array_chunk($names, 3);
$shortUrl = ShortUrl::create(
ShortUrlCreation::fromRawData(['apiKey' => $authorApiKey, 'longUrl' => 'longUrl', 'tags' => $firstUrlTags]),
ShortUrlCreation::fromRawData(['apiKey' => $authorApiKey, 'longUrl' => 'https://longUrl', 'tags' => $firstUrlTags]),
$this->relationResolver,
);
$this->getEntityManager()->persist($shortUrl);
$shortUrl2 = ShortUrl::create(
ShortUrlCreation::fromRawData(
['domain' => $domain->authority, 'longUrl' => 'longUrl', 'tags' => $secondUrlTags],
['domain' => $domain->authority, 'longUrl' => 'https://longUrl', 'tags' => $secondUrlTags],
),
$this->relationResolver,
);

View File

@@ -266,7 +266,7 @@ class VisitRepositoryTest extends DatabaseTestCase
$this->getEntityManager()->persist($apiKey1);
$shortUrl = ShortUrl::create(
ShortUrlCreation::fromRawData(
['apiKey' => $apiKey1, 'domain' => $domain->authority, 'longUrl' => 'longUrl'],
['apiKey' => $apiKey1, 'domain' => $domain->authority, 'longUrl' => 'https://longUrl'],
),
$this->relationResolver,
);
@@ -275,13 +275,15 @@ class VisitRepositoryTest extends DatabaseTestCase
$apiKey2 = ApiKey::fromMeta(ApiKeyMeta::withRoles(RoleDefinition::forAuthoredShortUrls()));
$this->getEntityManager()->persist($apiKey2);
$shortUrl2 = ShortUrl::create(ShortUrlCreation::fromRawData(['apiKey' => $apiKey2, 'longUrl' => 'longUrl']));
$shortUrl2 = ShortUrl::create(
ShortUrlCreation::fromRawData(['apiKey' => $apiKey2, 'longUrl' => 'https://longUrl']),
);
$this->getEntityManager()->persist($shortUrl2);
$this->createVisitsForShortUrl($shortUrl2, 5);
$shortUrl3 = ShortUrl::create(
ShortUrlCreation::fromRawData(
['apiKey' => $apiKey2, 'domain' => $domain->authority, 'longUrl' => 'longUrl'],
['apiKey' => $apiKey2, 'domain' => $domain->authority, 'longUrl' => 'https://longUrl'],
),
$this->relationResolver,
);
@@ -320,7 +322,7 @@ class VisitRepositoryTest extends DatabaseTestCase
#[Test]
public function findOrphanVisitsReturnsExpectedResult(): void
{
$shortUrl = ShortUrl::create(ShortUrlCreation::fromRawData(['longUrl' => 'longUrl']));
$shortUrl = ShortUrl::create(ShortUrlCreation::fromRawData(['longUrl' => 'https://longUrl']));
$this->getEntityManager()->persist($shortUrl);
$this->createVisitsForShortUrl($shortUrl, 7);