shlink/module/CLI/test/Command/ShortUrl/DeleteShortUrlCommandTest.php

130 lines
4.7 KiB
PHP
Raw Normal View History

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);
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;
use Shlinkio\Shlink\CLI\Command\ShortUrl\DeleteShortUrlCommand;
2018-09-15 18:57:12 +03:00
use Shlinkio\Shlink\Core\Exception;
use Shlinkio\Shlink\Core\Model\ShortUrlIdentifier;
2018-09-15 18:57:12 +03:00
use Shlinkio\Shlink\Core\Service\ShortUrl\DeleteShortUrlServiceInterface;
use ShlinkioTest\Shlink\CLI\CliTestUtilsTrait;
2018-09-15 18:57:12 +03:00
use Symfony\Component\Console\Tester\CommandTester;
use function array_pop;
use function sprintf;
2018-09-15 18:57:12 +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
{
use CliTestUtilsTrait;
2020-11-02 13:50:19 +03:00
2019-12-30 00:27:00 +03:00
private CommandTester $commandTester;
private ObjectProphecy $service;
2018-09-15 18:57:12 +03:00
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);
$this->commandTester = $this->testerForCommand(new DeleteShortUrlCommand($this->service->reveal()));
2018-09-15 18:57:12 +03:00
}
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(new ShortUrlIdentifier($shortCode), false)->will(
function (): void {
},
);
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-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';
$identifier = new ShortUrlIdentifier($shortCode);
$deleteByShortCode = $this->service->deleteByShortCode($identifier, false)->willThrow(
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-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,
2019-02-16 23:24:32 +03:00
): void {
2018-09-15 18:57:12 +03:00
$shortCode = 'abc123';
$identifier = new ShortUrlIdentifier($shortCode);
$deleteByShortCode = $this->service->deleteByShortCode($identifier, Argument::type('bool'))->will(
2020-01-01 22:48:31 +03:00
function (array $args) use ($shortCode): void {
$ignoreThreshold = array_pop($args);
2018-09-15 18:57:12 +03:00
if (!$ignoreThreshold) {
throw Exception\DeleteShortUrlException::fromVisitsThreshold(10, $shortCode);
2018-09-15 18:57:12 +03:00
}
2020-01-01 22:48:31 +03:00
},
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();
2020-10-04 01:35:14 +03:00
self::assertStringContainsString(sprintf(
'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);
2019-02-16 23:24:32 +03:00
$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(new ShortUrlIdentifier($shortCode), false)->willThrow(
2020-01-01 22:48:31 +03:00
Exception\DeleteShortUrlException::fromVisitsThreshold(10, $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(
'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-11-11 15:18:21 +03:00
$deleteByShortCode->shouldHaveBeenCalledOnce();
2018-09-15 18:57:12 +03:00
}
}