diff --git a/module/CLI/src/Command/Visit/DownloadGeoLiteDbCommand.php b/module/CLI/src/Command/Visit/DownloadGeoLiteDbCommand.php index 5f52c3b7..3d76663a 100644 --- a/module/CLI/src/Command/Visit/DownloadGeoLiteDbCommand.php +++ b/module/CLI/src/Command/Visit/DownloadGeoLiteDbCommand.php @@ -51,11 +51,11 @@ class DownloadGeoLiteDbCommand extends Command $this->progressBar->setProgress($downloaded); }); - if ($this->progressBar !== null) { + if ($this->progressBar === null) { + $io->info('GeoLite2 db file is up to date.'); + } else { $this->progressBar->finish(); $io->success('GeoLite2 db file properly downloaded.'); - } else { - $io->info('GeoLite2 db file is up to date.'); } return ExitCodes::EXIT_SUCCESS; diff --git a/module/CLI/test/CliTestUtilsTrait.php b/module/CLI/test/CliTestUtilsTrait.php index b5d81a76..412131dc 100644 --- a/module/CLI/test/CliTestUtilsTrait.php +++ b/module/CLI/test/CliTestUtilsTrait.php @@ -9,6 +9,7 @@ use Prophecy\PhpUnit\ProphecyTrait; use Prophecy\Prophecy\ObjectProphecy; use Symfony\Component\Console\Application; use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Tester\CommandTester; trait CliTestUtilsTrait { @@ -29,4 +30,15 @@ trait CliTestUtilsTrait return $command; } + + private function testerForCommand(Command $mainCommand, Command ...$extraCommands): CommandTester + { + $app = new Application(); + $app->add($mainCommand); + foreach ($extraCommands as $command) { + $app->add($command); + } + + return new CommandTester($mainCommand); + } } diff --git a/module/CLI/test/Command/Visit/DownloadGeoLiteDbCommandTest.php b/module/CLI/test/Command/Visit/DownloadGeoLiteDbCommandTest.php new file mode 100644 index 00000000..7ead517d --- /dev/null +++ b/module/CLI/test/Command/Visit/DownloadGeoLiteDbCommandTest.php @@ -0,0 +1,107 @@ +dbUpdater = $this->prophesize(GeolocationDbUpdaterInterface::class); + $this->commandTester = $this->testerForCommand(new DownloadGeoLiteDbCommand($this->dbUpdater->reveal())); + } + + /** + * @test + * @dataProvider provideFailureParams + */ + public function showsProperMessageWhenGeoLiteUpdateFails( + bool $olderDbExists, + string $expectedMessage, + int $expectedExitCode + ): void { + $checkDbUpdate = $this->dbUpdater->checkDbUpdate(Argument::cetera())->will( + function (array $args) use ($olderDbExists): void { + [$beforeDownload, $handleProgress] = $args; + + $beforeDownload($olderDbExists); + $handleProgress(100, 50); + + throw $olderDbExists + ? GeolocationDbUpdateFailedException::withOlderDb() + : GeolocationDbUpdateFailedException::withoutOlderDb(); + }, + ); + + $this->commandTester->execute([]); + $output = $this->commandTester->getDisplay(); + $exitCode = $this->commandTester->getStatusCode(); + + self::assertStringContainsString( + sprintf('%s GeoLite2 db file...', $olderDbExists ? 'Updating' : 'Downloading'), + $output, + ); + self::assertStringContainsString($expectedMessage, $output); + self::assertSame($expectedExitCode, $exitCode); + $checkDbUpdate->shouldHaveBeenCalledOnce(); + } + + public function provideFailureParams(): iterable + { + yield 'existing db' => [ + true, + '[WARNING] GeoLite2 db file update failed. Visits will continue to be located', + ExitCodes::EXIT_WARNING, + ]; + yield 'not existing db' => [ + false, + '[ERROR] GeoLite2 db file download failed. It will not be possible to locate', + ExitCodes::EXIT_FAILURE, + ]; + } + + /** + * @test + * @dataProvider provideSuccessParams + */ + public function printsExpectedMessageWhenNoErrorOccurs(callable $checkUpdateBehavior, string $expectedMessage): void + { + $checkDbUpdate = $this->dbUpdater->checkDbUpdate(Argument::cetera())->will($checkUpdateBehavior); + + $this->commandTester->execute([]); + $output = $this->commandTester->getDisplay(); + $exitCode = $this->commandTester->getStatusCode(); + + self::assertStringContainsString($expectedMessage, $output); + self::assertSame(ExitCodes::EXIT_SUCCESS, $exitCode); + $checkDbUpdate->shouldHaveBeenCalledOnce(); + } + + public function provideSuccessParams(): iterable + { + yield 'up to date db' => [function (): void { + }, '[INFO] GeoLite2 db file is up to date.']; + yield 'outdated db' => [function (array $args): void { + [$beforeDownload] = $args; + $beforeDownload(true); + }, '[OK] GeoLite2 db file properly downloaded.']; + } +} diff --git a/module/CLI/test/Command/Visit/LocateVisitsCommandTest.php b/module/CLI/test/Command/Visit/LocateVisitsCommandTest.php index e5632034..e7ccc9d2 100644 --- a/module/CLI/test/Command/Visit/LocateVisitsCommandTest.php +++ b/module/CLI/test/Command/Visit/LocateVisitsCommandTest.php @@ -216,45 +216,6 @@ class LocateVisitsCommandTest extends TestCase $this->visitService->locateUnlocatedVisits(Argument::cetera())->shouldNotHaveBeenCalled(); } -// /** -// * @test -// * @dataProvider provideParams -// */ -// public function showsProperMessageWhenGeoLiteUpdateFails(bool $olderDbExists, string $expectedMessage): void -// { -// $locateVisits = $this->visitService->locateUnlocatedVisits(Argument::cetera())->will(function (): void { -// }); -// $checkDbUpdate = $this->dbUpdater->checkDbUpdate(Argument::cetera())->will( -// function (array $args) use ($olderDbExists): void { -// [$mustBeUpdated, $handleProgress] = $args; -// -// $mustBeUpdated($olderDbExists); -// $handleProgress(100, 50); -// -// throw $olderDbExists -// ? GeolocationDbUpdateFailedException::withOlderDb() -// : GeolocationDbUpdateFailedException::withoutOlderDb(); -// }, -// ); -// -// $this->commandTester->execute([]); -// $output = $this->commandTester->getDisplay(); -// -// self::assertStringContainsString( -// sprintf('%s GeoLite2 db file...', $olderDbExists ? 'Updating' : 'Downloading'), -// $output, -// ); -// self::assertStringContainsString($expectedMessage, $output); -// $locateVisits->shouldHaveBeenCalledTimes((int) $olderDbExists); -// $checkDbUpdate->shouldHaveBeenCalledOnce(); -// } -// -// public function provideParams(): iterable -// { -// yield [true, '[Warning] GeoLite2 database update failed. Proceeding with old version.']; -// yield [false, 'GeoLite2 database download failed. It is not possible to locate visits.']; -// } - /** @test */ public function providingAllFlagOnItsOwnDisplaysNotice(): void {