mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-06 15:23:12 +08:00
Drastically improved performance when creating new short URLs with findIfExists by moving logic to DB query
This commit is contained in:
@@ -75,38 +75,4 @@ class ShortUrlTest extends TestCase
|
||||
yield [null, DEFAULT_SHORT_CODES_LENGTH];
|
||||
yield from map(range(4, 10), fn (int $value) => [$value, $value]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @dataProvider provideCriteriaToMatch
|
||||
*/
|
||||
public function criteriaIsMatchedWhenDatesMatch(ShortUrl $shortUrl, ShortUrlMeta $meta, bool $expected): void
|
||||
{
|
||||
$this->assertEquals($expected, $shortUrl->matchesCriteria($meta, []));
|
||||
}
|
||||
|
||||
public function provideCriteriaToMatch(): iterable
|
||||
{
|
||||
$start = Chronos::parse('2020-03-05 20:18:30');
|
||||
$end = Chronos::parse('2021-03-05 20:18:30');
|
||||
|
||||
yield [new ShortUrl('foo'), ShortUrlMeta::fromRawData(['validSince' => $start]), false];
|
||||
yield [new ShortUrl('foo'), ShortUrlMeta::fromRawData(['validUntil' => $end]), false];
|
||||
yield [new ShortUrl('foo'), ShortUrlMeta::fromRawData(['validSince' => $start, 'validUntil' => $end]), false];
|
||||
yield [
|
||||
new ShortUrl('foo', ShortUrlMeta::fromRawData(['validSince' => $start])),
|
||||
ShortUrlMeta::fromRawData(['validSince' => $start]),
|
||||
true,
|
||||
];
|
||||
yield [
|
||||
new ShortUrl('foo', ShortUrlMeta::fromRawData(['validUntil' => $end])),
|
||||
ShortUrlMeta::fromRawData(['validUntil' => $end]),
|
||||
true,
|
||||
];
|
||||
yield [
|
||||
new ShortUrl('foo', ShortUrlMeta::fromRawData(['validUntil' => $end, 'validSince' => $start])),
|
||||
ShortUrlMeta::fromRawData(['validUntil' => $end, 'validSince' => $start]),
|
||||
true,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -147,7 +147,7 @@ class UrlShortenerTest extends TestCase
|
||||
ShortUrl $expected
|
||||
): void {
|
||||
$repo = $this->prophesize(ShortUrlRepository::class);
|
||||
$findExisting = $repo->findBy(Argument::any())->willReturn([$expected]);
|
||||
$findExisting = $repo->findOneMatching(Argument::cetera())->willReturn($expected);
|
||||
$getRepo = $this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal());
|
||||
|
||||
$result = $this->urlShortener->urlToShortCode($url, $tags, $meta);
|
||||
@@ -211,33 +211,4 @@ class UrlShortenerTest extends TestCase
|
||||
])))->setTags(new ArrayCollection([new Tag('foo'), new Tag('bar'), new Tag('baz')])),
|
||||
];
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function properExistingShortUrlIsReturnedWhenMultipleMatch(): void
|
||||
{
|
||||
$url = 'http://foo.com';
|
||||
$tags = ['baz', 'foo', 'bar'];
|
||||
$meta = ShortUrlMeta::fromRawData([
|
||||
'findIfExists' => true,
|
||||
'validUntil' => Chronos::parse('2017-01-01'),
|
||||
'maxVisits' => 4,
|
||||
]);
|
||||
$tagsCollection = new ArrayCollection(array_map(fn (string $tag) => new Tag($tag), $tags));
|
||||
$expected = (new ShortUrl($url, $meta))->setTags($tagsCollection);
|
||||
|
||||
$repo = $this->prophesize(ShortUrlRepository::class);
|
||||
$findExisting = $repo->findBy(Argument::any())->willReturn([
|
||||
new ShortUrl($url),
|
||||
new ShortUrl($url, $meta),
|
||||
$expected,
|
||||
(new ShortUrl($url))->setTags($tagsCollection),
|
||||
]);
|
||||
$getRepo = $this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal());
|
||||
|
||||
$result = $this->urlShortener->urlToShortCode($url, $tags, $meta);
|
||||
|
||||
$this->assertSame($expected, $result);
|
||||
$findExisting->shouldHaveBeenCalledOnce();
|
||||
$getRepo->shouldHaveBeenCalledOnce();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user