2021-04-08 06:34:14 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace ShlinkioTest\Shlink\CLI\Command\Visit;
|
|
|
|
|
2023-02-09 13:42:18 -06:00
|
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
|
|
|
use PHPUnit\Framework\Attributes\Test;
|
2022-10-22 07:02:38 -05:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2021-04-08 06:34:14 -05:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Shlinkio\Shlink\CLI\Command\Visit\DownloadGeoLiteDbCommand;
|
|
|
|
use Shlinkio\Shlink\CLI\Exception\GeolocationDbUpdateFailedException;
|
2022-09-18 03:01:22 -05:00
|
|
|
use Shlinkio\Shlink\CLI\GeoLite\GeolocationDbUpdaterInterface;
|
2022-09-18 03:31:14 -05:00
|
|
|
use Shlinkio\Shlink\CLI\GeoLite\GeolocationResult;
|
2023-05-15 02:43:05 -05:00
|
|
|
use Shlinkio\Shlink\CLI\Util\ExitCode;
|
2023-06-18 03:51:59 -05:00
|
|
|
use ShlinkioTest\Shlink\CLI\Util\CliTestUtils;
|
2021-04-08 06:34:14 -05:00
|
|
|
use Symfony\Component\Console\Tester\CommandTester;
|
|
|
|
|
|
|
|
use function sprintf;
|
|
|
|
|
|
|
|
class DownloadGeoLiteDbCommandTest extends TestCase
|
|
|
|
{
|
|
|
|
private CommandTester $commandTester;
|
2022-10-24 12:53:13 -05:00
|
|
|
private MockObject & GeolocationDbUpdaterInterface $dbUpdater;
|
2021-04-08 06:34:14 -05:00
|
|
|
|
|
|
|
protected function setUp(): void
|
|
|
|
{
|
2022-10-22 07:02:38 -05:00
|
|
|
$this->dbUpdater = $this->createMock(GeolocationDbUpdaterInterface::class);
|
2023-06-18 03:51:59 -05:00
|
|
|
$this->commandTester = CliTestUtils::testerForCommand(new DownloadGeoLiteDbCommand($this->dbUpdater));
|
2021-04-08 06:34:14 -05:00
|
|
|
}
|
|
|
|
|
2023-02-09 13:42:18 -06:00
|
|
|
#[Test, DataProvider('provideFailureParams')]
|
2021-04-08 06:34:14 -05:00
|
|
|
public function showsProperMessageWhenGeoLiteUpdateFails(
|
|
|
|
bool $olderDbExists,
|
|
|
|
string $expectedMessage,
|
2021-05-23 05:31:10 -05:00
|
|
|
int $expectedExitCode,
|
2021-04-08 06:34:14 -05:00
|
|
|
): void {
|
2022-10-22 07:02:38 -05:00
|
|
|
$this->dbUpdater->expects($this->once())->method('checkDbUpdate')->withAnyParameters()->willReturnCallback(
|
|
|
|
function (callable $beforeDownload, callable $handleProgress) use ($olderDbExists): void {
|
2021-04-08 06:34:14 -05:00
|
|
|
$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);
|
|
|
|
}
|
|
|
|
|
2023-02-09 02:32:38 -06:00
|
|
|
public static function provideFailureParams(): iterable
|
2021-04-08 06:34:14 -05:00
|
|
|
{
|
|
|
|
yield 'existing db' => [
|
|
|
|
true,
|
|
|
|
'[WARNING] GeoLite2 db file update failed. Visits will continue to be located',
|
2023-05-15 02:43:05 -05:00
|
|
|
ExitCode::EXIT_WARNING,
|
2021-04-08 06:34:14 -05:00
|
|
|
];
|
|
|
|
yield 'not existing db' => [
|
|
|
|
false,
|
|
|
|
'[ERROR] GeoLite2 db file download failed. It will not be possible to locate',
|
2023-05-15 02:43:05 -05:00
|
|
|
ExitCode::EXIT_FAILURE,
|
2021-04-08 06:34:14 -05:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2023-11-23 02:31:02 -06:00
|
|
|
#[Test]
|
|
|
|
public function warningIsPrintedWhenLicenseIsMissing(): void
|
|
|
|
{
|
|
|
|
$this->dbUpdater->expects($this->once())->method('checkDbUpdate')->withAnyParameters()->willReturn(
|
|
|
|
GeolocationResult::LICENSE_MISSING,
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->commandTester->execute([]);
|
|
|
|
$output = $this->commandTester->getDisplay();
|
|
|
|
$exitCode = $this->commandTester->getStatusCode();
|
|
|
|
|
|
|
|
self::assertStringContainsString('[WARNING] It was not possible to download GeoLite2 db', $output);
|
|
|
|
self::assertSame(ExitCode::EXIT_WARNING, $exitCode);
|
|
|
|
}
|
|
|
|
|
2023-02-09 13:42:18 -06:00
|
|
|
#[Test, DataProvider('provideSuccessParams')]
|
2021-04-08 06:34:14 -05:00
|
|
|
public function printsExpectedMessageWhenNoErrorOccurs(callable $checkUpdateBehavior, string $expectedMessage): void
|
|
|
|
{
|
2022-10-22 07:02:38 -05:00
|
|
|
$this->dbUpdater->expects($this->once())->method('checkDbUpdate')->withAnyParameters()->willReturnCallback(
|
|
|
|
$checkUpdateBehavior,
|
|
|
|
);
|
2021-04-08 06:34:14 -05:00
|
|
|
|
|
|
|
$this->commandTester->execute([]);
|
|
|
|
$output = $this->commandTester->getDisplay();
|
|
|
|
$exitCode = $this->commandTester->getStatusCode();
|
|
|
|
|
|
|
|
self::assertStringContainsString($expectedMessage, $output);
|
2023-05-15 02:43:05 -05:00
|
|
|
self::assertSame(ExitCode::EXIT_SUCCESS, $exitCode);
|
2021-04-08 06:34:14 -05:00
|
|
|
}
|
|
|
|
|
2023-02-09 02:32:38 -06:00
|
|
|
public static function provideSuccessParams(): iterable
|
2021-04-08 06:34:14 -05:00
|
|
|
{
|
2022-09-18 03:31:14 -05:00
|
|
|
yield 'up to date db' => [fn () => GeolocationResult::CHECK_SKIPPED, '[INFO] GeoLite2 db file is up to date.'];
|
2022-10-22 07:02:38 -05:00
|
|
|
yield 'outdated db' => [function (callable $beforeDownload): GeolocationResult {
|
2021-04-08 06:34:14 -05:00
|
|
|
$beforeDownload(true);
|
2022-09-18 03:31:14 -05:00
|
|
|
return GeolocationResult::DB_CREATED;
|
2021-04-08 06:34:14 -05:00
|
|
|
}, '[OK] GeoLite2 db file properly downloaded.'];
|
|
|
|
}
|
|
|
|
}
|