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
|
|
|
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Prophecy\Argument;
|
|
|
|
use Prophecy\Prophecy\ObjectProphecy;
|
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;
|
|
|
|
use Shlinkio\Shlink\Core\Service\ShortUrl\DeleteShortUrlServiceInterface;
|
|
|
|
use Symfony\Component\Console\Application;
|
|
|
|
use Symfony\Component\Console\Tester\CommandTester;
|
2019-02-27 00:56:43 +03:00
|
|
|
|
2018-10-28 10:24:06 +03:00
|
|
|
use function array_pop;
|
|
|
|
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
|
|
|
{
|
2018-11-20 21:30:27 +03:00
|
|
|
/** @var CommandTester */
|
2018-09-15 18:57:12 +03:00
|
|
|
private $commandTester;
|
2018-11-20 21:30:27 +03:00
|
|
|
/** @var ObjectProphecy */
|
2018-09-15 18:57:12 +03:00
|
|
|
private $service;
|
|
|
|
|
2019-02-16 12:53:45 +03:00
|
|
|
public function setUp(): void
|
2018-09-15 18:57:12 +03:00
|
|
|
{
|
|
|
|
$this->service = $this->prophesize(DeleteShortUrlServiceInterface::class);
|
|
|
|
|
2018-11-18 18:02:52 +03:00
|
|
|
$command = new DeleteShortUrlCommand($this->service->reveal());
|
2018-09-15 18:57:12 +03:00
|
|
|
$app = new Application();
|
|
|
|
$app->add($command);
|
|
|
|
|
|
|
|
$this->commandTester = new CommandTester($command);
|
|
|
|
}
|
|
|
|
|
2019-02-16 23:24:32 +03:00
|
|
|
/** @test */
|
|
|
|
public function successMessageIsPrintedIfUrlIsProperlyDeleted(): void
|
2018-09-15 18:57:12 +03:00
|
|
|
{
|
|
|
|
$shortCode = 'abc123';
|
|
|
|
$deleteByShortCode = $this->service->deleteByShortCode($shortCode, false)->will(function () {
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->commandTester->execute(['shortCode' => $shortCode]);
|
|
|
|
$output = $this->commandTester->getDisplay();
|
|
|
|
|
2019-02-16 12:53:45 +03:00
|
|
|
$this->assertStringContainsString(
|
|
|
|
sprintf('Short URL with short code "%s" successfully deleted.', $shortCode),
|
|
|
|
$output
|
|
|
|
);
|
2018-11-11 15:18:21 +03:00
|
|
|
$deleteByShortCode->shouldHaveBeenCalledOnce();
|
2018-09-15 18:57:12 +03:00
|
|
|
}
|
|
|
|
|
2019-02-16 23:24:32 +03:00
|
|
|
/** @test */
|
|
|
|
public function invalidShortCodePrintsMessage(): void
|
2018-09-15 18:57:12 +03:00
|
|
|
{
|
|
|
|
$shortCode = 'abc123';
|
|
|
|
$deleteByShortCode = $this->service->deleteByShortCode($shortCode, false)->willThrow(
|
2019-11-25 01:11:25 +03:00
|
|
|
Exception\ShortUrlNotFoundException::class
|
2018-09-15 18:57:12 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->commandTester->execute(['shortCode' => $shortCode]);
|
|
|
|
$output = $this->commandTester->getDisplay();
|
|
|
|
|
2019-02-16 12:53:45 +03:00
|
|
|
$this->assertStringContainsString(sprintf('Provided short code "%s" could not be found.', $shortCode), $output);
|
2018-11-11 15:18:21 +03:00
|
|
|
$deleteByShortCode->shouldHaveBeenCalledOnce();
|
2018-09-15 18:57:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
2019-02-16 23:24:32 +03:00
|
|
|
* @dataProvider provideRetryDeleteAnswers
|
2018-09-15 18:57:12 +03:00
|
|
|
*/
|
2019-02-16 23:24:32 +03:00
|
|
|
public function deleteIsRetriedWhenThresholdIsReachedAndQuestionIsAccepted(
|
|
|
|
array $retryAnswer,
|
|
|
|
int $expectedDeleteCalls,
|
|
|
|
string $expectedMessage
|
|
|
|
): void {
|
2018-09-15 18:57:12 +03:00
|
|
|
$shortCode = 'abc123';
|
|
|
|
$deleteByShortCode = $this->service->deleteByShortCode($shortCode, Argument::type('bool'))->will(
|
|
|
|
function (array $args) {
|
2018-10-28 10:24:06 +03:00
|
|
|
$ignoreThreshold = array_pop($args);
|
2018-09-15 18:57:12 +03:00
|
|
|
|
|
|
|
if (!$ignoreThreshold) {
|
2019-11-27 22:18:36 +03:00
|
|
|
throw Exception\DeleteShortUrlException::fromVisitsThreshold(10, '');
|
2018-09-15 18:57:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
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();
|
|
|
|
|
2019-02-16 12:53:45 +03:00
|
|
|
$this->assertStringContainsString(sprintf(
|
2018-09-15 18:57:12 +03:00
|
|
|
'It was not possible to delete the short URL with short code "%s" because it has more than 10 visits.',
|
|
|
|
$shortCode
|
|
|
|
), $output);
|
2019-02-16 23:24:32 +03:00
|
|
|
$this->assertStringContainsString($expectedMessage, $output);
|
|
|
|
$deleteByShortCode->shouldHaveBeenCalledTimes($expectedDeleteCalls);
|
2018-09-15 18:57:12 +03:00
|
|
|
}
|
|
|
|
|
2019-02-16 23:24:32 +03:00
|
|
|
public function provideRetryDeleteAnswers(): iterable
|
|
|
|
{
|
|
|
|
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.'];
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @test */
|
|
|
|
public function deleteIsNotRetriedWhenThresholdIsReachedAndQuestionIsDeclined(): void
|
2018-09-15 18:57:12 +03:00
|
|
|
{
|
|
|
|
$shortCode = 'abc123';
|
|
|
|
$deleteByShortCode = $this->service->deleteByShortCode($shortCode, false)->willThrow(
|
2019-11-27 22:18:36 +03:00
|
|
|
Exception\DeleteShortUrlException::fromVisitsThreshold(10, '')
|
2018-09-15 18:57:12 +03:00
|
|
|
);
|
|
|
|
$this->commandTester->setInputs(['no']);
|
|
|
|
|
|
|
|
$this->commandTester->execute(['shortCode' => $shortCode]);
|
|
|
|
$output = $this->commandTester->getDisplay();
|
|
|
|
|
2019-02-16 12:53:45 +03:00
|
|
|
$this->assertStringContainsString(sprintf(
|
2018-09-15 18:57:12 +03:00
|
|
|
'It was not possible to delete the short URL with short code "%s" because it has more than 10 visits.',
|
|
|
|
$shortCode
|
|
|
|
), $output);
|
2019-02-16 12:53:45 +03:00
|
|
|
$this->assertStringContainsString('Short URL was not deleted.', $output);
|
2018-11-11 15:18:21 +03:00
|
|
|
$deleteByShortCode->shouldHaveBeenCalledOnce();
|
2018-09-15 18:57:12 +03:00
|
|
|
}
|
|
|
|
}
|