2021-04-07 09:29:29 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace ShlinkioTest\Shlink\Core\EventDispatcher;
|
|
|
|
|
2022-10-22 02:42:21 -05:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2021-04-07 09:29:29 -05:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2022-09-18 04:17:17 -05:00
|
|
|
use Psr\EventDispatcher\EventDispatcherInterface;
|
2021-04-07 09:29:29 -05:00
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
use RuntimeException;
|
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;
|
2022-09-18 04:17:17 -05:00
|
|
|
use Shlinkio\Shlink\Core\EventDispatcher\Event\GeoLiteDbCreated;
|
2021-04-07 09:29:29 -05:00
|
|
|
use Shlinkio\Shlink\Core\EventDispatcher\UpdateGeoLiteDb;
|
|
|
|
|
2022-09-18 04:17:17 -05:00
|
|
|
use function Functional\map;
|
|
|
|
|
2021-04-07 09:29:29 -05:00
|
|
|
class UpdateGeoLiteDbTest extends TestCase
|
|
|
|
{
|
|
|
|
private UpdateGeoLiteDb $listener;
|
2022-10-22 02:42:21 -05:00
|
|
|
private MockObject $dbUpdater;
|
|
|
|
private MockObject $logger;
|
|
|
|
private MockObject $eventDispatcher;
|
2021-04-07 09:29:29 -05:00
|
|
|
|
|
|
|
protected function setUp(): void
|
|
|
|
{
|
2022-10-22 02:42:21 -05:00
|
|
|
$this->dbUpdater = $this->createMock(GeolocationDbUpdaterInterface::class);
|
|
|
|
$this->logger = $this->createMock(LoggerInterface::class);
|
|
|
|
$this->eventDispatcher = $this->createMock(EventDispatcherInterface::class);
|
|
|
|
|
|
|
|
$this->listener = new UpdateGeoLiteDb($this->dbUpdater, $this->logger, $this->eventDispatcher);
|
2021-04-07 09:29:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @test */
|
|
|
|
public function exceptionWhileUpdatingDbLogsError(): void
|
|
|
|
{
|
|
|
|
$e = new RuntimeException();
|
|
|
|
|
2022-10-22 02:42:21 -05:00
|
|
|
$this->dbUpdater->expects($this->once())->method('checkDbUpdate')->withAnyParameters()->willThrowException($e);
|
|
|
|
$this->logger->expects($this->once())->method('error')->with(
|
2022-10-23 11:15:57 -05:00
|
|
|
'GeoLite2 database download failed. {e}',
|
|
|
|
['e' => $e],
|
2022-10-22 02:42:21 -05:00
|
|
|
);
|
|
|
|
$this->logger->expects($this->never())->method('notice');
|
|
|
|
$this->eventDispatcher->expects($this->never())->method('dispatch');
|
2021-04-07 09:29:29 -05:00
|
|
|
|
|
|
|
($this->listener)();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideFlags
|
|
|
|
*/
|
|
|
|
public function noticeMessageIsPrintedWhenFirstCallbackIsInvoked(bool $oldDbExists, string $expectedMessage): void
|
|
|
|
{
|
2022-10-22 02:42:21 -05:00
|
|
|
$this->dbUpdater->expects($this->once())->method('checkDbUpdate')->withAnyParameters()->willReturnCallback(
|
|
|
|
function (callable $firstCallback) use ($oldDbExists): GeolocationResult {
|
2021-04-07 09:29:29 -05:00
|
|
|
$firstCallback($oldDbExists);
|
2022-09-18 04:17:17 -05:00
|
|
|
return GeolocationResult::DB_IS_UP_TO_DATE;
|
2021-04-07 09:29:29 -05:00
|
|
|
},
|
|
|
|
);
|
2022-10-23 11:15:57 -05:00
|
|
|
$this->logger->expects($this->once())->method('notice')->with($expectedMessage);
|
2022-10-22 02:42:21 -05:00
|
|
|
$this->logger->expects($this->never())->method('error');
|
|
|
|
$this->eventDispatcher->expects($this->never())->method('dispatch');
|
2021-04-07 09:29:29 -05:00
|
|
|
|
|
|
|
($this->listener)();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideFlags(): iterable
|
|
|
|
{
|
|
|
|
yield 'existing old db' => [true, 'Updating GeoLite2 db file...'];
|
|
|
|
yield 'not existing old db' => [false, 'Downloading GeoLite2 db file...'];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideDownloaded
|
|
|
|
*/
|
|
|
|
public function noticeMessageIsPrintedWhenSecondCallbackIsInvoked(
|
|
|
|
int $total,
|
|
|
|
int $downloaded,
|
|
|
|
bool $oldDbExists,
|
2021-05-23 05:31:10 -05:00
|
|
|
?string $expectedMessage,
|
2021-04-07 09:29:29 -05:00
|
|
|
): void {
|
2022-10-22 02:42:21 -05:00
|
|
|
$this->dbUpdater->expects($this->once())->method('checkDbUpdate')->withAnyParameters()->willReturnCallback(
|
|
|
|
function ($_, callable $secondCallback) use ($total, $downloaded, $oldDbExists): GeolocationResult {
|
2021-04-08 07:09:26 -05:00
|
|
|
// Invoke several times to ensure the log is printed only once
|
|
|
|
$secondCallback($total, $downloaded, $oldDbExists);
|
|
|
|
$secondCallback($total, $downloaded, $oldDbExists);
|
2021-04-07 09:29:29 -05:00
|
|
|
$secondCallback($total, $downloaded, $oldDbExists);
|
2022-09-18 03:31:14 -05:00
|
|
|
|
2022-09-18 04:17:17 -05:00
|
|
|
return GeolocationResult::DB_UPDATED;
|
2021-04-07 09:29:29 -05:00
|
|
|
},
|
|
|
|
);
|
2022-10-22 02:42:21 -05:00
|
|
|
$logNoticeExpectation = $expectedMessage !== null ? $this->once() : $this->never();
|
2022-10-23 11:15:57 -05:00
|
|
|
$this->logger->expects($logNoticeExpectation)->method('notice')->with($expectedMessage);
|
2022-10-22 02:42:21 -05:00
|
|
|
$this->logger->expects($this->never())->method('error');
|
|
|
|
$this->eventDispatcher->expects($this->never())->method('dispatch');
|
2021-04-07 09:29:29 -05:00
|
|
|
|
|
|
|
($this->listener)();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideDownloaded(): iterable
|
|
|
|
{
|
|
|
|
yield [100, 0, true, null];
|
|
|
|
yield [100, 0, false, null];
|
|
|
|
yield [100, 99, true, null];
|
|
|
|
yield [100, 99, false, null];
|
|
|
|
yield [100, 100, true, 'Finished updating GeoLite2 db file'];
|
|
|
|
yield [100, 100, false, 'Finished downloading GeoLite2 db file'];
|
|
|
|
yield [100, 101, true, 'Finished updating GeoLite2 db file'];
|
|
|
|
yield [100, 101, false, 'Finished downloading GeoLite2 db file'];
|
|
|
|
}
|
2022-09-18 04:17:17 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideGeolocationResults
|
|
|
|
*/
|
|
|
|
public function dispatchesEventOnlyWhenDbFileHasBeenCreatedForTheFirstTime(
|
|
|
|
GeolocationResult $result,
|
|
|
|
int $expectedDispatches,
|
|
|
|
): void {
|
2022-10-22 02:42:21 -05:00
|
|
|
$this->dbUpdater->expects($this->once())->method('checkDbUpdate')->withAnyParameters()->willReturn($result);
|
|
|
|
$this->eventDispatcher->expects($this->exactly($expectedDispatches))->method('dispatch')->with(
|
2022-10-23 11:15:57 -05:00
|
|
|
new GeoLiteDbCreated(),
|
2022-10-22 02:42:21 -05:00
|
|
|
);
|
2022-09-18 04:17:17 -05:00
|
|
|
|
|
|
|
($this->listener)();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideGeolocationResults(): iterable
|
|
|
|
{
|
|
|
|
return map(GeolocationResult::cases(), static fn (GeolocationResult $value) => [
|
|
|
|
$value,
|
|
|
|
$value === GeolocationResult::DB_CREATED ? 1 : 0,
|
|
|
|
]);
|
|
|
|
}
|
2021-04-07 09:29:29 -05:00
|
|
|
}
|