Add DeleteExpiredShortUrlsCommand test

This commit is contained in:
Alejandro Celaya 2024-04-03 19:18:56 +02:00
parent f2371e8a80
commit 527d28ad81
2 changed files with 95 additions and 0 deletions

View file

@ -11,6 +11,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com), and this
Previously, this was exposed only for orphan visits, since this can be an arbitrary value for those. Previously, this was exposed only for orphan visits, since this can be an arbitrary value for those.
* [#2077](https://github.com/shlinkio/shlink/issues/2077) When sending visits to Matomo, the short URL title is now used as document title in matomo. * [#2077](https://github.com/shlinkio/shlink/issues/2077) When sending visits to Matomo, the short URL title is now used as document title in matomo.
* [#2059](https://github.com/shlinkio/shlink/issues/2059) Add new `short-url:delete-expired` command that can be used to programmatically delete expired short URLs.
Expired short URLs are those that have a `calidUntil` date in the past, or optionally, that have reached the max amount of visits.
This command can be run periodically by those who create many disposable URLs which are valid only for a period of time, and then can be deleted to save space.
### Changed ### Changed
* [#2034](https://github.com/shlinkio/shlink/issues/2034) Modernize entities, using constructor property promotion and readonly wherever possible. * [#2034](https://github.com/shlinkio/shlink/issues/2034) Modernize entities, using constructor property promotion and readonly wherever possible.

View file

@ -0,0 +1,90 @@
<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\CLI\Command\ShortUrl;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\TestWith;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Shlinkio\Shlink\CLI\Command\ShortUrl\DeleteExpiredShortUrlsCommand;
use Shlinkio\Shlink\CLI\Util\ExitCode;
use Shlinkio\Shlink\Core\ShortUrl\DeleteShortUrlServiceInterface;
use Shlinkio\Shlink\Core\ShortUrl\Model\ExpiredShortUrlsConditions;
use ShlinkioTest\Shlink\CLI\Util\CliTestUtils;
use Symfony\Component\Console\Tester\CommandTester;
class DeleteExpiredShortUrlsCommandTest extends TestCase
{
private CommandTester $commandTester;
private MockObject & DeleteShortUrlServiceInterface $service;
protected function setUp(): void
{
$this->service = $this->createMock(DeleteShortUrlServiceInterface::class);
$this->commandTester = CliTestUtils::testerForCommand(new DeleteExpiredShortUrlsCommand($this->service));
}
#[Test]
public function warningIsDisplayedAndExecutionCanBeCancelled(): void
{
$this->service->expects($this->never())->method('countExpiredShortUrls');
$this->service->expects($this->never())->method('deleteExpiredShortUrls');
$this->commandTester->setInputs(['n']);
$this->commandTester->execute([]);
$output = $this->commandTester->getDisplay();
$status = $this->commandTester->getStatusCode();
self::assertStringContainsString('Careful!', $output);
self::assertEquals(ExitCode::EXIT_WARNING, $status);
}
#[Test]
#[TestWith([[], true])]
#[TestWith([['--force' => true], false])]
#[TestWith([['-f' => true], false])]
public function deletionIsExecutedByDefault(array $input, bool $expectsWarning): void
{
$this->service->expects($this->never())->method('countExpiredShortUrls');
$this->service->expects($this->once())->method('deleteExpiredShortUrls')->willReturn(5);
$this->commandTester->setInputs(['y']);
$this->commandTester->execute($input);
$output = $this->commandTester->getDisplay();
$status = $this->commandTester->getStatusCode();
if ($expectsWarning) {
self::assertStringContainsString('Careful!', $output);
} else {
self::assertStringNotContainsString('Careful!', $output);
}
self::assertStringContainsString('5 expired short URLs have been deleted', $output);
self::assertEquals(ExitCode::EXIT_SUCCESS, $status);
}
#[Test]
public function countIsExecutedDuringDryRun(): void
{
$this->service->expects($this->once())->method('countExpiredShortUrls')->willReturn(38);
$this->service->expects($this->never())->method('deleteExpiredShortUrls');
$this->commandTester->execute(['--dry-run' => true]);
$output = $this->commandTester->getDisplay();
$status = $this->commandTester->getStatusCode();
self::assertStringNotContainsString('Careful!', $output);
self::assertStringContainsString('There are 38 expired short URLs matching provided conditions', $output);
self::assertEquals(ExitCode::EXIT_SUCCESS, $status);
}
#[Test]
#[TestWith([[], new ExpiredShortUrlsConditions()])]
#[TestWith([['--evaluate-max-visits' => true], new ExpiredShortUrlsConditions(maxVisitsReached: true)])]
public function providesExpectedConditionsToService(array $extraInput, ExpiredShortUrlsConditions $conditions): void
{
$this->service->expects($this->once())->method('countExpiredShortUrls')->with($conditions)->willReturn(4);
$this->commandTester->execute(['--dry-run' => true, ...$extraInput]);
}
}