mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-06 23:33:13 +08:00
Created new listener to update the GeoLite db after a visit occurs
This commit is contained in:
@@ -226,67 +226,4 @@ class LocateVisitTest extends TestCase
|
||||
yield 'invalid short url' => [Visit::forInvalidShortUrl(new Visitor('', '', '1.2.3.4', '')), '1.2.3.4'];
|
||||
yield 'regular not found' => [Visit::forRegularNotFound(new Visitor('', '', '1.2.3.4', '')), '1.2.3.4'];
|
||||
}
|
||||
|
||||
// /** @test */
|
||||
// public function errorWhenUpdatingGeoLiteWithExistingCopyLogsWarning(): void
|
||||
// {
|
||||
// $e = GeolocationDbUpdateFailedException::withOlderDb();
|
||||
// $ipAddr = '1.2.3.0';
|
||||
// $visit = Visit::forValidShortUrl(ShortUrl::createEmpty(), new Visitor('', '', $ipAddr, ''));
|
||||
// $location = new Location('', '', '', '', 0.0, 0.0, '');
|
||||
// $event = new UrlVisited('123');
|
||||
//
|
||||
// $findVisit = $this->em->find(Visit::class, '123')->willReturn($visit);
|
||||
// $flush = $this->em->flush()->will(function (): void {
|
||||
// });
|
||||
// $resolveIp = $this->ipLocationResolver->resolveIpLocation($ipAddr)->willReturn($location);
|
||||
// $checkUpdateDb = $this->dbUpdater->checkDbUpdate(Argument::cetera())->willThrow($e);
|
||||
// $dispatch = $this->eventDispatcher->dispatch(new VisitLocated('123'))->will(function (): void {
|
||||
// });
|
||||
//
|
||||
// ($this->locateVisit)($event);
|
||||
//
|
||||
// self::assertEquals($visit->getVisitLocation(), new VisitLocation($location));
|
||||
// $findVisit->shouldHaveBeenCalledOnce();
|
||||
// $flush->shouldHaveBeenCalledOnce();
|
||||
// $resolveIp->shouldHaveBeenCalledOnce();
|
||||
// $checkUpdateDb->shouldHaveBeenCalledOnce();
|
||||
// $this->logger->warning(
|
||||
// 'GeoLite2 database update failed. Proceeding with old version. {e}',
|
||||
// ['e' => $e],
|
||||
// )->shouldHaveBeenCalledOnce();
|
||||
// $dispatch->shouldHaveBeenCalledOnce();
|
||||
// }
|
||||
//
|
||||
// /** @test */
|
||||
// public function errorWhenDownloadingGeoLiteCancelsLocation(): void
|
||||
// {
|
||||
// $e = GeolocationDbUpdateFailedException::withoutOlderDb();
|
||||
// $ipAddr = '1.2.3.0';
|
||||
// $visit = Visit::forValidShortUrl(ShortUrl::createEmpty(), new Visitor('', '', $ipAddr, ''));
|
||||
// $location = new Location('', '', '', '', 0.0, 0.0, '');
|
||||
// $event = new UrlVisited('123');
|
||||
//
|
||||
// $findVisit = $this->em->find(Visit::class, '123')->willReturn($visit);
|
||||
// $flush = $this->em->flush()->will(function (): void {
|
||||
// });
|
||||
// $resolveIp = $this->ipLocationResolver->resolveIpLocation($ipAddr)->willReturn($location);
|
||||
// $checkUpdateDb = $this->dbUpdater->checkDbUpdate(Argument::cetera())->willThrow($e);
|
||||
// $logError = $this->logger->error(
|
||||
// 'GeoLite2 database download failed. It is not possible to locate visit with id {visitId}. {e}',
|
||||
// ['e' => $e, 'visitId' => 123],
|
||||
// );
|
||||
// $dispatch = $this->eventDispatcher->dispatch(new VisitLocated('123'))->will(function (): void {
|
||||
// });
|
||||
//
|
||||
// ($this->locateVisit)($event);
|
||||
//
|
||||
// self::assertNull($visit->getVisitLocation());
|
||||
// $findVisit->shouldHaveBeenCalledOnce();
|
||||
// $flush->shouldNotHaveBeenCalled();
|
||||
// $resolveIp->shouldNotHaveBeenCalled();
|
||||
// $checkUpdateDb->shouldHaveBeenCalledOnce();
|
||||
// $logError->shouldHaveBeenCalledOnce();
|
||||
// $dispatch->shouldHaveBeenCalledOnce();
|
||||
// }
|
||||
}
|
||||
|
||||
114
module/Core/test/EventDispatcher/UpdateGeoLiteDbTest.php
Normal file
114
module/Core/test/EventDispatcher/UpdateGeoLiteDbTest.php
Normal file
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ShlinkioTest\Shlink\Core\EventDispatcher;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Prophecy\Argument;
|
||||
use Prophecy\PhpUnit\ProphecyTrait;
|
||||
use Prophecy\Prophecy\ObjectProphecy;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use RuntimeException;
|
||||
use Shlinkio\Shlink\CLI\Util\GeolocationDbUpdaterInterface;
|
||||
use Shlinkio\Shlink\Core\EventDispatcher\UpdateGeoLiteDb;
|
||||
|
||||
class UpdateGeoLiteDbTest extends TestCase
|
||||
{
|
||||
use ProphecyTrait;
|
||||
|
||||
private UpdateGeoLiteDb $listener;
|
||||
private ObjectProphecy $dbUpdater;
|
||||
private ObjectProphecy $logger;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->dbUpdater = $this->prophesize(GeolocationDbUpdaterInterface::class);
|
||||
$this->logger = $this->prophesize(LoggerInterface::class);
|
||||
|
||||
$this->listener = new UpdateGeoLiteDb($this->dbUpdater->reveal(), $this->logger->reveal());
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function exceptionWhileUpdatingDbLogsError(): void
|
||||
{
|
||||
$e = new RuntimeException();
|
||||
|
||||
$checkDbUpdate = $this->dbUpdater->checkDbUpdate(Argument::cetera())->willThrow($e);
|
||||
$logError = $this->logger->error('GeoLite2 database download failed. {e}', ['e' => $e]);
|
||||
|
||||
($this->listener)();
|
||||
|
||||
$checkDbUpdate->shouldHaveBeenCalledOnce();
|
||||
$logError->shouldHaveBeenCalledOnce();
|
||||
$this->logger->notice(Argument::cetera())->shouldNotHaveBeenCalled();
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @dataProvider provideFlags
|
||||
*/
|
||||
public function noticeMessageIsPrintedWhenFirstCallbackIsInvoked(bool $oldDbExists, string $expectedMessage): void
|
||||
{
|
||||
$checkDbUpdate = $this->dbUpdater->checkDbUpdate(Argument::cetera())->will(
|
||||
function (array $args) use ($oldDbExists): void {
|
||||
[$firstCallback] = $args;
|
||||
$firstCallback($oldDbExists);
|
||||
},
|
||||
);
|
||||
$logNotice = $this->logger->notice($expectedMessage);
|
||||
|
||||
($this->listener)();
|
||||
|
||||
$checkDbUpdate->shouldHaveBeenCalledOnce();
|
||||
$logNotice->shouldHaveBeenCalledOnce();
|
||||
$this->logger->error(Argument::cetera())->shouldNotHaveBeenCalled();
|
||||
}
|
||||
|
||||
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,
|
||||
?string $expectedMessage
|
||||
): void {
|
||||
$checkDbUpdate = $this->dbUpdater->checkDbUpdate(Argument::cetera())->will(
|
||||
function (array $args) use ($total, $downloaded, $oldDbExists): void {
|
||||
[, $secondCallback] = $args;
|
||||
$secondCallback($total, $downloaded, $oldDbExists);
|
||||
},
|
||||
);
|
||||
$logNotice = $this->logger->notice($expectedMessage ?? Argument::cetera());
|
||||
|
||||
($this->listener)();
|
||||
|
||||
if ($expectedMessage !== null) {
|
||||
$logNotice->shouldHaveBeenCalledOnce();
|
||||
} else {
|
||||
$logNotice->shouldNotHaveBeenCalled();
|
||||
}
|
||||
$checkDbUpdate->shouldHaveBeenCalledOnce();
|
||||
$this->logger->error(Argument::cetera())->shouldNotHaveBeenCalled();
|
||||
}
|
||||
|
||||
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'];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user