2018-09-15 04:54:58 -05:00
|
|
|
<?php
|
2019-10-05 10:26:10 -05:00
|
|
|
|
2018-09-15 04:54:58 -05:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace ShlinkioTest\Shlink\Core\Exception;
|
|
|
|
|
2023-02-09 13:42:18 -06:00
|
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
|
|
|
use PHPUnit\Framework\Attributes\Test;
|
2018-09-15 04:54:58 -05:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Shlinkio\Shlink\Core\Exception\DeleteShortUrlException;
|
2022-09-23 11:05:17 -05:00
|
|
|
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlIdentifier;
|
2019-02-26 15:56:43 -06:00
|
|
|
|
2023-11-29 05:34:13 -06:00
|
|
|
use function array_map;
|
2018-10-28 02:24:06 -05:00
|
|
|
use function range;
|
2019-11-27 13:18:36 -06:00
|
|
|
use function Shlinkio\Shlink\Core\generateRandomShortCode;
|
2019-02-17 13:28:34 -06:00
|
|
|
use function sprintf;
|
2018-09-15 04:54:58 -05:00
|
|
|
|
|
|
|
class DeleteShortUrlExceptionTest extends TestCase
|
|
|
|
{
|
2023-02-09 13:42:18 -06:00
|
|
|
#[Test, DataProvider('provideThresholds')]
|
2018-09-15 04:54:58 -05:00
|
|
|
public function fromVisitsThresholdGeneratesMessageProperly(
|
|
|
|
int $threshold,
|
|
|
|
string $shortCode,
|
2021-05-23 05:31:10 -05:00
|
|
|
string $expectedMessage,
|
2019-02-17 13:28:34 -06:00
|
|
|
): void {
|
2021-11-30 14:37:35 -06:00
|
|
|
$e = DeleteShortUrlException::fromVisitsThreshold(
|
|
|
|
$threshold,
|
|
|
|
ShortUrlIdentifier::fromShortCodeAndDomain($shortCode),
|
|
|
|
);
|
2019-02-17 13:28:34 -06:00
|
|
|
|
2020-10-03 17:35:14 -05:00
|
|
|
self::assertEquals($threshold, $e->getVisitsThreshold());
|
|
|
|
self::assertEquals($expectedMessage, $e->getMessage());
|
|
|
|
self::assertEquals($expectedMessage, $e->getDetail());
|
|
|
|
self::assertEquals([
|
2019-12-01 03:14:29 -06:00
|
|
|
'shortCode' => $shortCode,
|
|
|
|
'threshold' => $threshold,
|
|
|
|
], $e->getAdditionalData());
|
2020-10-03 17:35:14 -05:00
|
|
|
self::assertEquals('Cannot delete short URL', $e->getTitle());
|
2022-08-13 10:15:04 -05:00
|
|
|
self::assertEquals('https://shlink.io/api/error/invalid-short-url-deletion', $e->getType());
|
2020-10-03 17:35:14 -05:00
|
|
|
self::assertEquals(422, $e->getStatus());
|
2018-09-15 04:54:58 -05:00
|
|
|
}
|
|
|
|
|
2023-02-09 02:32:38 -06:00
|
|
|
public static function provideThresholds(): array
|
2018-09-15 04:54:58 -05:00
|
|
|
{
|
2023-11-29 05:34:13 -06:00
|
|
|
return array_map(function (int $number) {
|
2019-11-27 13:18:36 -06:00
|
|
|
return [$number, $shortCode = generateRandomShortCode(6), sprintf(
|
2021-11-30 14:37:35 -06:00
|
|
|
'Impossible to delete short URL with short code "%s", since it has more than "%s" visits.',
|
2019-02-17 13:28:34 -06:00
|
|
|
$shortCode,
|
2020-01-01 13:48:31 -06:00
|
|
|
$number,
|
2019-02-17 13:28:34 -06:00
|
|
|
)];
|
2023-11-29 05:34:13 -06:00
|
|
|
}, range(5, 50, 5));
|
2018-09-15 04:54:58 -05:00
|
|
|
}
|
2021-11-30 14:37:35 -06:00
|
|
|
|
2023-02-09 13:42:18 -06:00
|
|
|
#[Test]
|
2021-11-30 14:37:35 -06:00
|
|
|
public function domainIsPartOfAdditionalWhenProvidedInIdentifier(): void
|
|
|
|
{
|
|
|
|
$e = DeleteShortUrlException::fromVisitsThreshold(
|
|
|
|
10,
|
2023-01-19 02:05:52 -06:00
|
|
|
ShortUrlIdentifier::fromShortCodeAndDomain('abc123', 's.test'),
|
2021-11-30 14:37:35 -06:00
|
|
|
);
|
2023-01-19 02:05:52 -06:00
|
|
|
$expectedMessage = 'Impossible to delete short URL with short code "abc123" for domain "s.test", since it '
|
2021-11-30 14:37:35 -06:00
|
|
|
. 'has more than "10" visits.';
|
|
|
|
|
|
|
|
self::assertEquals([
|
|
|
|
'shortCode' => 'abc123',
|
2023-01-19 02:05:52 -06:00
|
|
|
'domain' => 's.test',
|
2021-11-30 14:37:35 -06:00
|
|
|
'threshold' => 10,
|
|
|
|
], $e->getAdditionalData());
|
|
|
|
self::assertEquals($expectedMessage, $e->getMessage());
|
|
|
|
self::assertEquals($expectedMessage, $e->getDetail());
|
|
|
|
}
|
2018-09-15 04:54:58 -05:00
|
|
|
}
|