2018-09-15 18:57:12 +03:00
|
|
|
<?php
|
2019-10-05 18:26:10 +03:00
|
|
|
|
2018-09-15 18:57:12 +03:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2018-09-16 19:36:02 +03:00
|
|
|
namespace ShlinkioTest\Shlink\CLI\Command\ShortUrl;
|
2018-09-15 18:57:12 +03:00
|
|
|
|
2023-02-09 22:42:18 +03:00
|
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
|
|
|
use PHPUnit\Framework\Attributes\Test;
|
2022-10-22 14:36:33 +03:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2018-09-15 18:57:12 +03:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2018-09-16 19:36:02 +03:00
|
|
|
use Shlinkio\Shlink\CLI\Command\ShortUrl\DeleteShortUrlCommand;
|
2018-09-15 18:57:12 +03:00
|
|
|
use Shlinkio\Shlink\Core\Exception;
|
2022-09-23 19:42:38 +03:00
|
|
|
use Shlinkio\Shlink\Core\ShortUrl\DeleteShortUrlServiceInterface;
|
2022-09-23 19:05:17 +03:00
|
|
|
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlIdentifier;
|
2023-06-18 11:51:59 +03:00
|
|
|
use ShlinkioTest\Shlink\CLI\Util\CliTestUtils;
|
2018-09-15 18:57:12 +03:00
|
|
|
use Symfony\Component\Console\Tester\CommandTester;
|
2019-02-27 00:56:43 +03:00
|
|
|
|
2018-10-28 10:24:06 +03:00
|
|
|
use function sprintf;
|
2018-09-15 18:57:12 +03:00
|
|
|
|
2019-08-01 20:49:54 +03:00
|
|
|
use const PHP_EOL;
|
|
|
|
|
2019-02-16 23:24:32 +03:00
|
|
|
class DeleteShortUrlCommandTest extends TestCase
|
2018-09-15 18:57:12 +03:00
|
|
|
{
|
2019-12-30 00:27:00 +03:00
|
|
|
private CommandTester $commandTester;
|
2022-10-24 20:53:13 +03:00
|
|
|
private MockObject & DeleteShortUrlServiceInterface $service;
|
2018-09-15 18:57:12 +03:00
|
|
|
|
2022-09-11 13:02:49 +03:00
|
|
|
protected function setUp(): void
|
2018-09-15 18:57:12 +03:00
|
|
|
{
|
2022-10-22 14:36:33 +03:00
|
|
|
$this->service = $this->createMock(DeleteShortUrlServiceInterface::class);
|
2023-06-18 11:51:59 +03:00
|
|
|
$this->commandTester = CliTestUtils::testerForCommand(new DeleteShortUrlCommand($this->service));
|
2018-09-15 18:57:12 +03:00
|
|
|
}
|
|
|
|
|
2023-02-09 22:42:18 +03:00
|
|
|
#[Test]
|
2019-02-16 23:24:32 +03:00
|
|
|
public function successMessageIsPrintedIfUrlIsProperlyDeleted(): void
|
2018-09-15 18:57:12 +03:00
|
|
|
{
|
|
|
|
$shortCode = 'abc123';
|
2022-10-22 14:36:33 +03:00
|
|
|
$this->service->expects($this->once())->method('deleteByShortCode')->with(
|
2022-10-23 19:15:57 +03:00
|
|
|
ShortUrlIdentifier::fromShortCodeAndDomain($shortCode),
|
2022-10-22 14:36:33 +03:00
|
|
|
$this->isFalse(),
|
|
|
|
);
|
2018-09-15 18:57:12 +03:00
|
|
|
|
|
|
|
$this->commandTester->execute(['shortCode' => $shortCode]);
|
|
|
|
$output = $this->commandTester->getDisplay();
|
|
|
|
|
2020-10-04 01:35:14 +03:00
|
|
|
self::assertStringContainsString(
|
2019-02-16 12:53:45 +03:00
|
|
|
sprintf('Short URL with short code "%s" successfully deleted.', $shortCode),
|
2020-01-01 22:48:31 +03:00
|
|
|
$output,
|
2019-02-16 12:53:45 +03:00
|
|
|
);
|
2018-09-15 18:57:12 +03:00
|
|
|
}
|
|
|
|
|
2023-02-09 22:42:18 +03:00
|
|
|
#[Test]
|
2019-02-16 23:24:32 +03:00
|
|
|
public function invalidShortCodePrintsMessage(): void
|
2018-09-15 18:57:12 +03:00
|
|
|
{
|
|
|
|
$shortCode = 'abc123';
|
2022-04-23 15:00:47 +03:00
|
|
|
$identifier = ShortUrlIdentifier::fromShortCodeAndDomain($shortCode);
|
2022-10-22 14:36:33 +03:00
|
|
|
$this->service->expects($this->once())->method('deleteByShortCode')->with(
|
2022-10-23 19:15:57 +03:00
|
|
|
$identifier,
|
2022-10-22 14:36:33 +03:00
|
|
|
$this->isFalse(),
|
|
|
|
)->willThrowException(Exception\ShortUrlNotFoundException::fromNotFound($identifier));
|
2018-09-15 18:57:12 +03:00
|
|
|
|
|
|
|
$this->commandTester->execute(['shortCode' => $shortCode]);
|
|
|
|
$output = $this->commandTester->getDisplay();
|
|
|
|
|
2020-10-04 01:35:14 +03:00
|
|
|
self::assertStringContainsString(sprintf('No URL found with short code "%s"', $shortCode), $output);
|
2018-09-15 18:57:12 +03:00
|
|
|
}
|
|
|
|
|
2023-02-09 22:42:18 +03:00
|
|
|
#[Test, DataProvider('provideRetryDeleteAnswers')]
|
2019-02-16 23:24:32 +03:00
|
|
|
public function deleteIsRetriedWhenThresholdIsReachedAndQuestionIsAccepted(
|
|
|
|
array $retryAnswer,
|
|
|
|
int $expectedDeleteCalls,
|
2021-05-23 13:31:10 +03:00
|
|
|
string $expectedMessage,
|
2019-02-16 23:24:32 +03:00
|
|
|
): void {
|
2018-09-15 18:57:12 +03:00
|
|
|
$shortCode = 'abc123';
|
2022-04-23 15:00:47 +03:00
|
|
|
$identifier = ShortUrlIdentifier::fromShortCodeAndDomain($shortCode);
|
2022-10-22 14:36:33 +03:00
|
|
|
$this->service->expects($this->exactly($expectedDeleteCalls))->method('deleteByShortCode')->with(
|
2022-10-23 19:15:57 +03:00
|
|
|
$identifier,
|
2022-10-22 14:36:33 +03:00
|
|
|
$this->isType('bool'),
|
|
|
|
)->willReturnCallback(function ($_, bool $ignoreThreshold) use ($shortCode): void {
|
|
|
|
if (!$ignoreThreshold) {
|
|
|
|
throw Exception\DeleteShortUrlException::fromVisitsThreshold(
|
|
|
|
10,
|
|
|
|
ShortUrlIdentifier::fromShortCodeAndDomain($shortCode),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
2019-02-16 23:24:32 +03:00
|
|
|
$this->commandTester->setInputs($retryAnswer);
|
2018-09-15 18:57:12 +03:00
|
|
|
|
|
|
|
$this->commandTester->execute(['shortCode' => $shortCode]);
|
|
|
|
$output = $this->commandTester->getDisplay();
|
|
|
|
|
2020-10-04 01:35:14 +03:00
|
|
|
self::assertStringContainsString(sprintf(
|
2021-11-30 23:37:35 +03:00
|
|
|
'Impossible to delete short URL with short code "%s", since it has more than "10" visits.',
|
2020-01-01 22:48:31 +03:00
|
|
|
$shortCode,
|
2018-09-15 18:57:12 +03:00
|
|
|
), $output);
|
2020-10-04 01:35:14 +03:00
|
|
|
self::assertStringContainsString($expectedMessage, $output);
|
2018-09-15 18:57:12 +03:00
|
|
|
}
|
|
|
|
|
2023-02-09 11:32:38 +03:00
|
|
|
public static function provideRetryDeleteAnswers(): iterable
|
2019-02-16 23:24:32 +03:00
|
|
|
{
|
|
|
|
yield 'answering yes to retry' => [['yes'], 2, 'Short URL with short code "abc123" successfully deleted.'];
|
|
|
|
yield 'answering no to retry' => [['no'], 1, 'Short URL was not deleted.'];
|
|
|
|
yield 'answering default to retry' => [[PHP_EOL], 1, 'Short URL was not deleted.'];
|
|
|
|
}
|
|
|
|
|
2023-02-09 22:42:18 +03:00
|
|
|
#[Test]
|
2019-02-16 23:24:32 +03:00
|
|
|
public function deleteIsNotRetriedWhenThresholdIsReachedAndQuestionIsDeclined(): void
|
2018-09-15 18:57:12 +03:00
|
|
|
{
|
|
|
|
$shortCode = 'abc123';
|
2022-10-22 14:36:33 +03:00
|
|
|
$this->service->expects($this->once())->method('deleteByShortCode')->with(
|
2022-10-23 19:15:57 +03:00
|
|
|
ShortUrlIdentifier::fromShortCodeAndDomain($shortCode),
|
2022-10-22 14:36:33 +03:00
|
|
|
$this->isFalse(),
|
|
|
|
)->willThrowException(Exception\DeleteShortUrlException::fromVisitsThreshold(
|
2022-04-23 15:00:47 +03:00
|
|
|
10,
|
|
|
|
ShortUrlIdentifier::fromShortCodeAndDomain($shortCode),
|
|
|
|
));
|
2018-09-15 18:57:12 +03:00
|
|
|
$this->commandTester->setInputs(['no']);
|
|
|
|
|
|
|
|
$this->commandTester->execute(['shortCode' => $shortCode]);
|
|
|
|
$output = $this->commandTester->getDisplay();
|
|
|
|
|
2020-10-04 01:35:14 +03:00
|
|
|
self::assertStringContainsString(sprintf(
|
2021-11-30 23:37:35 +03:00
|
|
|
'Impossible to delete short URL with short code "%s", since it has more than "10" visits.',
|
2020-01-01 22:48:31 +03:00
|
|
|
$shortCode,
|
2018-09-15 18:57:12 +03:00
|
|
|
), $output);
|
2020-10-04 01:35:14 +03:00
|
|
|
self::assertStringContainsString('Short URL was not deleted.', $output);
|
2018-09-15 18:57:12 +03:00
|
|
|
}
|
|
|
|
}
|