2018-09-15 04:54:58 -05:00
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace ShlinkioTest\Shlink\Core\Exception;
|
|
|
|
|
|
|
|
use PHPUnit\Framework\TestCase;
|
2019-02-17 13:28:34 -06:00
|
|
|
use Shlinkio\Shlink\Common\Util\StringUtilsTrait;
|
2018-09-15 04:54:58 -05:00
|
|
|
use Shlinkio\Shlink\Core\Exception\DeleteShortUrlException;
|
2019-02-26 15:56:43 -06:00
|
|
|
|
2018-11-02 06:05:01 -05:00
|
|
|
use function Functional\map;
|
2018-10-28 02:24:06 -05:00
|
|
|
use function range;
|
2019-02-17 13:28:34 -06:00
|
|
|
use function sprintf;
|
2018-09-15 04:54:58 -05:00
|
|
|
|
|
|
|
class DeleteShortUrlExceptionTest extends TestCase
|
|
|
|
{
|
2019-02-17 13:28:34 -06:00
|
|
|
use StringUtilsTrait;
|
|
|
|
|
2018-09-15 04:54:58 -05:00
|
|
|
/**
|
|
|
|
* @test
|
2019-02-17 13:28:34 -06:00
|
|
|
* @dataProvider provideThresholds
|
2018-09-15 04:54:58 -05:00
|
|
|
*/
|
|
|
|
public function fromVisitsThresholdGeneratesMessageProperly(
|
|
|
|
int $threshold,
|
|
|
|
string $shortCode,
|
|
|
|
string $expectedMessage
|
2019-02-17 13:28:34 -06:00
|
|
|
): void {
|
2018-09-15 04:54:58 -05:00
|
|
|
$e = DeleteShortUrlException::fromVisitsThreshold($threshold, $shortCode);
|
2019-02-17 13:28:34 -06:00
|
|
|
|
|
|
|
$this->assertEquals($threshold, $e->getVisitsThreshold());
|
2018-09-15 04:54:58 -05:00
|
|
|
$this->assertEquals($expectedMessage, $e->getMessage());
|
2018-11-17 12:23:25 -06:00
|
|
|
$this->assertEquals(0, $e->getCode());
|
2019-02-17 13:28:34 -06:00
|
|
|
$this->assertNull($e->getPrevious());
|
2018-09-15 04:54:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideThresholds
|
|
|
|
*/
|
2019-02-17 13:28:34 -06:00
|
|
|
public function visitsThresholdIsProperlyReturned(int $threshold): void
|
2018-09-15 04:54:58 -05:00
|
|
|
{
|
|
|
|
$e = new DeleteShortUrlException($threshold);
|
2019-02-17 13:28:34 -06:00
|
|
|
|
2018-09-15 04:54:58 -05:00
|
|
|
$this->assertEquals($threshold, $e->getVisitsThreshold());
|
2019-02-17 13:28:34 -06:00
|
|
|
$this->assertEquals('', $e->getMessage());
|
|
|
|
$this->assertEquals(0, $e->getCode());
|
|
|
|
$this->assertNull($e->getPrevious());
|
2018-09-15 04:54:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public function provideThresholds(): array
|
|
|
|
{
|
2018-11-02 06:05:01 -05:00
|
|
|
return map(range(5, 50, 5), function (int $number) {
|
2019-02-17 13:28:34 -06:00
|
|
|
$shortCode = $this->generateRandomString(6);
|
|
|
|
return [$number, $shortCode, sprintf(
|
|
|
|
'Impossible to delete short URL with short code "%s" since it has more than "%s" visits.',
|
|
|
|
$shortCode,
|
|
|
|
$number
|
|
|
|
)];
|
2018-11-02 06:05:01 -05:00
|
|
|
});
|
2018-09-15 04:54:58 -05:00
|
|
|
}
|
|
|
|
}
|