From f08951a9b99a735f53af35806a2c678157eb1e5f Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Wed, 21 Feb 2024 19:24:30 +0100 Subject: [PATCH] Add unit test for short URL path prefix --- .../test/ShortUrl/Entity/ShortUrlTest.php | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/module/Core/test/ShortUrl/Entity/ShortUrlTest.php b/module/Core/test/ShortUrl/Entity/ShortUrlTest.php index 0a898399..eb89df5c 100644 --- a/module/Core/test/ShortUrl/Entity/ShortUrlTest.php +++ b/module/Core/test/ShortUrl/Entity/ShortUrlTest.php @@ -7,6 +7,7 @@ namespace ShlinkioTest\Shlink\Core\ShortUrl\Entity; use Cake\Chronos\Chronos; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Test; +use PHPUnit\Framework\Attributes\TestWith; use PHPUnit\Framework\TestCase; use Shlinkio\Shlink\Core\Exception\ShortCodeCannotBeRegeneratedException; use Shlinkio\Shlink\Core\Model\DeviceType; @@ -91,6 +92,27 @@ class ShortUrlTest extends TestCase yield from array_map(fn (int $value) => [$value, $value], range(4, 10)); } + #[Test] + #[TestWith([null, '', 5])] + #[TestWith(['foo bar/', 'foo-bar-', 13])] + public function shortCodesHaveExpectedPrefix( + ?string $pathPrefix, + string $expectedPrefix, + int $expectedShortCodeLength, + ): void { + $shortUrl = ShortUrl::create(ShortUrlCreation::fromRawData([ + 'longUrl' => 'https://longUrl', + ShortUrlInputFilter::SHORT_CODE_LENGTH => 5, + ShortUrlInputFilter::PATH_PREFIX => $pathPrefix, + ])); + $shortCode = $shortUrl->getShortCode(); + + if (strlen($expectedPrefix) > 0) { + self::assertStringStartsWith($expectedPrefix, $shortCode); + } + self::assertEquals($expectedShortCodeLength, strlen($shortCode)); + } + #[Test] public function deviceLongUrlsAreUpdated(): void {