2019-10-11 04:28:53 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace ShlinkioTest\Shlink\Core\Entity;
|
|
|
|
|
2020-10-25 04:26:11 -05:00
|
|
|
use Cake\Chronos\Chronos;
|
2019-10-11 04:28:53 -05:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
|
|
|
use Shlinkio\Shlink\Core\Exception\ShortCodeCannotBeRegeneratedException;
|
|
|
|
use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
|
2021-01-31 00:44:46 -06:00
|
|
|
use Shlinkio\Shlink\Core\Validation\ShortUrlInputFilter;
|
2020-10-25 04:26:11 -05:00
|
|
|
use Shlinkio\Shlink\Importer\Model\ImportedShlinkUrl;
|
2022-08-07 02:36:51 -05:00
|
|
|
use Shlinkio\Shlink\Importer\Sources\ImportSource;
|
2020-10-25 05:16:42 -05:00
|
|
|
|
2020-02-18 11:54:40 -06:00
|
|
|
use function Functional\map;
|
|
|
|
use function range;
|
|
|
|
use function strlen;
|
|
|
|
|
2021-09-26 04:20:29 -05:00
|
|
|
use const Shlinkio\Shlink\DEFAULT_SHORT_CODES_LENGTH;
|
2019-10-11 04:28:53 -05:00
|
|
|
|
|
|
|
class ShortUrlTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideInvalidShortUrls
|
|
|
|
*/
|
|
|
|
public function regenerateShortCodeThrowsExceptionIfStateIsInvalid(
|
|
|
|
ShortUrl $shortUrl,
|
2021-05-23 05:31:10 -05:00
|
|
|
string $expectedMessage,
|
2019-10-11 04:28:53 -05:00
|
|
|
): void {
|
|
|
|
$this->expectException(ShortCodeCannotBeRegeneratedException::class);
|
|
|
|
$this->expectExceptionMessage($expectedMessage);
|
|
|
|
|
|
|
|
$shortUrl->regenerateShortCode();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideInvalidShortUrls(): iterable
|
|
|
|
{
|
|
|
|
yield 'with custom slug' => [
|
2021-01-30 07:18:44 -06:00
|
|
|
ShortUrl::fromMeta(ShortUrlMeta::fromRawData(['customSlug' => 'custom-slug', 'longUrl' => ''])),
|
2019-10-11 04:28:53 -05:00
|
|
|
'The short code cannot be regenerated on ShortUrls where a custom slug was provided.',
|
|
|
|
];
|
|
|
|
yield 'already persisted' => [
|
2021-01-30 07:18:44 -06:00
|
|
|
ShortUrl::createEmpty()->setId('1'),
|
2019-10-11 04:28:53 -05:00
|
|
|
'The short code can be regenerated only on new ShortUrls which have not been persisted yet.',
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-10-25 04:26:11 -05:00
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideValidShortUrls
|
|
|
|
*/
|
|
|
|
public function regenerateShortCodeProperlyChangesTheValueOnValidShortUrls(ShortUrl $shortUrl): void
|
2019-10-11 04:28:53 -05:00
|
|
|
{
|
|
|
|
$firstShortCode = $shortUrl->getShortCode();
|
|
|
|
|
|
|
|
$shortUrl->regenerateShortCode();
|
|
|
|
$secondShortCode = $shortUrl->getShortCode();
|
|
|
|
|
2020-10-03 17:35:14 -05:00
|
|
|
self::assertNotEquals($firstShortCode, $secondShortCode);
|
2019-10-11 04:28:53 -05:00
|
|
|
}
|
2020-02-18 11:54:40 -06:00
|
|
|
|
2020-10-25 04:26:11 -05:00
|
|
|
public function provideValidShortUrls(): iterable
|
|
|
|
{
|
2021-01-30 07:18:44 -06:00
|
|
|
yield 'no custom slug' => [ShortUrl::createEmpty()];
|
2022-08-07 02:36:51 -05:00
|
|
|
yield 'imported with custom slug' => [ShortUrl::fromImport(
|
|
|
|
new ImportedShlinkUrl(ImportSource::BITLY, '', [], Chronos::now(), null, 'custom-slug', null),
|
|
|
|
true,
|
|
|
|
)];
|
2020-10-25 04:26:11 -05:00
|
|
|
}
|
|
|
|
|
2020-02-18 11:54:40 -06:00
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideLengths
|
|
|
|
*/
|
|
|
|
public function shortCodesHaveExpectedLength(?int $length, int $expectedLength): void
|
|
|
|
{
|
2021-01-30 07:18:44 -06:00
|
|
|
$shortUrl = ShortUrl::fromMeta(ShortUrlMeta::fromRawData(
|
2021-01-31 00:44:46 -06:00
|
|
|
[ShortUrlInputFilter::SHORT_CODE_LENGTH => $length, 'longUrl' => ''],
|
2020-02-18 11:54:40 -06:00
|
|
|
));
|
|
|
|
|
2020-10-03 17:35:14 -05:00
|
|
|
self::assertEquals($expectedLength, strlen($shortUrl->getShortCode()));
|
2020-02-18 11:54:40 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
public function provideLengths(): iterable
|
|
|
|
{
|
|
|
|
yield [null, DEFAULT_SHORT_CODES_LENGTH];
|
2020-02-18 11:57:24 -06:00
|
|
|
yield from map(range(4, 10), fn (int $value) => [$value, $value]);
|
2020-02-18 11:54:40 -06:00
|
|
|
}
|
2019-10-11 04:28:53 -05:00
|
|
|
}
|