2019-04-14 11:49:54 +03:00
|
|
|
<?php
|
2019-10-05 18:26:10 +03:00
|
|
|
|
2019-04-14 11:49:54 +03:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace ShlinkioTest\Shlink\CLI\Util;
|
|
|
|
|
|
|
|
use Cake\Chronos\Chronos;
|
|
|
|
use GeoIp2\Database\Reader;
|
|
|
|
use MaxMind\Db\Reader\Metadata;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
2019-07-20 13:11:07 +03:00
|
|
|
use Prophecy\Argument;
|
2019-04-14 11:49:54 +03:00
|
|
|
use Prophecy\Prophecy\ObjectProphecy;
|
|
|
|
use Shlinkio\Shlink\CLI\Exception\GeolocationDbUpdateFailedException;
|
|
|
|
use Shlinkio\Shlink\CLI\Util\GeolocationDbUpdater;
|
2019-08-11 00:30:47 +03:00
|
|
|
use Shlinkio\Shlink\IpGeolocation\Exception\RuntimeException;
|
2019-08-10 14:42:37 +03:00
|
|
|
use Shlinkio\Shlink\IpGeolocation\GeoLite2\DbUpdaterInterface;
|
2019-07-20 13:11:07 +03:00
|
|
|
use Symfony\Component\Lock;
|
2019-04-14 11:49:54 +03:00
|
|
|
use Throwable;
|
|
|
|
|
|
|
|
use function Functional\map;
|
|
|
|
use function range;
|
|
|
|
|
|
|
|
class GeolocationDbUpdaterTest extends TestCase
|
|
|
|
{
|
2019-12-30 00:27:00 +03:00
|
|
|
private GeolocationDbUpdater $geolocationDbUpdater;
|
|
|
|
private ObjectProphecy $dbUpdater;
|
|
|
|
private ObjectProphecy $geoLiteDbReader;
|
|
|
|
private ObjectProphecy $locker;
|
|
|
|
private ObjectProphecy $lock;
|
2019-04-14 11:49:54 +03:00
|
|
|
|
|
|
|
public function setUp(): void
|
|
|
|
{
|
|
|
|
$this->dbUpdater = $this->prophesize(DbUpdaterInterface::class);
|
|
|
|
$this->geoLiteDbReader = $this->prophesize(Reader::class);
|
|
|
|
|
2019-11-30 19:59:04 +03:00
|
|
|
$this->locker = $this->prophesize(Lock\LockFactory::class);
|
2019-07-20 13:11:07 +03:00
|
|
|
$this->lock = $this->prophesize(Lock\LockInterface::class);
|
|
|
|
$this->lock->acquire(true)->willReturn(true);
|
2020-01-01 22:48:31 +03:00
|
|
|
$this->lock->release()->will(function (): void {
|
2019-07-20 13:11:07 +03:00
|
|
|
});
|
|
|
|
$this->locker->createLock(Argument::type('string'))->willReturn($this->lock->reveal());
|
|
|
|
|
2019-04-14 11:49:54 +03:00
|
|
|
$this->geolocationDbUpdater = new GeolocationDbUpdater(
|
|
|
|
$this->dbUpdater->reveal(),
|
2019-07-20 13:11:07 +03:00
|
|
|
$this->geoLiteDbReader->reveal(),
|
2020-01-01 22:48:31 +03:00
|
|
|
$this->locker->reveal(),
|
2019-04-14 11:49:54 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @test */
|
|
|
|
public function exceptionIsThrownWhenOlderDbDoesNotExistAndDownloadFails(): void
|
|
|
|
{
|
2019-12-30 01:16:55 +03:00
|
|
|
$mustBeUpdated = fn () => $this->assertTrue(true);
|
2019-04-14 11:49:54 +03:00
|
|
|
$prev = new RuntimeException('');
|
2019-07-23 23:04:01 +03:00
|
|
|
|
|
|
|
$fileExists = $this->dbUpdater->databaseFileExists()->willReturn(false);
|
|
|
|
$getMeta = $this->geoLiteDbReader->metadata();
|
2019-04-14 11:49:54 +03:00
|
|
|
$download = $this->dbUpdater->downloadFreshCopy(null)->willThrow($prev);
|
|
|
|
|
|
|
|
try {
|
2019-04-14 12:19:21 +03:00
|
|
|
$this->geolocationDbUpdater->checkDbUpdate($mustBeUpdated);
|
2019-04-14 11:49:54 +03:00
|
|
|
$this->assertTrue(false); // If this is reached, the test will fail
|
|
|
|
} catch (Throwable $e) {
|
|
|
|
/** @var GeolocationDbUpdateFailedException $e */
|
|
|
|
$this->assertInstanceOf(GeolocationDbUpdateFailedException::class, $e);
|
|
|
|
$this->assertSame($prev, $e->getPrevious());
|
|
|
|
$this->assertFalse($e->olderDbExists());
|
|
|
|
}
|
|
|
|
|
2019-07-23 23:04:01 +03:00
|
|
|
$fileExists->shouldHaveBeenCalledOnce();
|
|
|
|
$getMeta->shouldNotHaveBeenCalled();
|
2019-04-14 11:49:54 +03:00
|
|
|
$download->shouldHaveBeenCalledOnce();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
2019-04-14 14:18:03 +03:00
|
|
|
* @dataProvider provideBigDays
|
2019-04-14 11:49:54 +03:00
|
|
|
*/
|
|
|
|
public function exceptionIsThrownWhenOlderDbIsTooOldAndDownloadFails(int $days): void
|
|
|
|
{
|
2019-07-23 23:04:01 +03:00
|
|
|
$fileExists = $this->dbUpdater->databaseFileExists()->willReturn(true);
|
2019-04-14 11:49:54 +03:00
|
|
|
$getMeta = $this->geoLiteDbReader->metadata()->willReturn(new Metadata([
|
|
|
|
'binary_format_major_version' => '',
|
|
|
|
'binary_format_minor_version' => '',
|
|
|
|
'build_epoch' => Chronos::now()->subDays($days)->getTimestamp(),
|
|
|
|
'database_type' => '',
|
|
|
|
'languages' => '',
|
|
|
|
'description' => '',
|
|
|
|
'ip_version' => '',
|
|
|
|
'node_count' => 1,
|
|
|
|
'record_size' => 4,
|
|
|
|
]));
|
|
|
|
$prev = new RuntimeException('');
|
|
|
|
$download = $this->dbUpdater->downloadFreshCopy(null)->willThrow($prev);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$this->geolocationDbUpdater->checkDbUpdate();
|
|
|
|
$this->assertTrue(false); // If this is reached, the test will fail
|
|
|
|
} catch (Throwable $e) {
|
|
|
|
/** @var GeolocationDbUpdateFailedException $e */
|
|
|
|
$this->assertInstanceOf(GeolocationDbUpdateFailedException::class, $e);
|
|
|
|
$this->assertSame($prev, $e->getPrevious());
|
|
|
|
$this->assertTrue($e->olderDbExists());
|
|
|
|
}
|
|
|
|
|
2019-07-23 23:04:01 +03:00
|
|
|
$fileExists->shouldHaveBeenCalledOnce();
|
2019-04-14 11:49:54 +03:00
|
|
|
$getMeta->shouldHaveBeenCalledOnce();
|
|
|
|
$download->shouldHaveBeenCalledOnce();
|
|
|
|
}
|
|
|
|
|
2019-04-14 14:18:03 +03:00
|
|
|
public function provideBigDays(): iterable
|
2019-04-14 11:49:54 +03:00
|
|
|
{
|
2019-04-14 14:18:03 +03:00
|
|
|
yield [36];
|
|
|
|
yield [50];
|
|
|
|
yield [75];
|
2019-04-14 11:49:54 +03:00
|
|
|
yield [100];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
2019-04-14 14:18:03 +03:00
|
|
|
* @dataProvider provideSmallDays
|
2019-04-14 11:49:54 +03:00
|
|
|
*/
|
|
|
|
public function databaseIsNotUpdatedIfItIsYoungerThanOneWeek(int $days): void
|
|
|
|
{
|
2019-07-23 23:04:01 +03:00
|
|
|
$fileExists = $this->dbUpdater->databaseFileExists()->willReturn(true);
|
2019-04-14 11:49:54 +03:00
|
|
|
$getMeta = $this->geoLiteDbReader->metadata()->willReturn(new Metadata([
|
|
|
|
'binary_format_major_version' => '',
|
|
|
|
'binary_format_minor_version' => '',
|
|
|
|
'build_epoch' => Chronos::now()->subDays($days)->getTimestamp(),
|
|
|
|
'database_type' => '',
|
|
|
|
'languages' => '',
|
|
|
|
'description' => '',
|
|
|
|
'ip_version' => '',
|
|
|
|
'node_count' => 1,
|
|
|
|
'record_size' => 4,
|
|
|
|
]));
|
2020-01-01 22:48:31 +03:00
|
|
|
$download = $this->dbUpdater->downloadFreshCopy(null)->will(function (): void {
|
2019-04-14 11:49:54 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
$this->geolocationDbUpdater->checkDbUpdate();
|
|
|
|
|
2019-07-23 23:04:01 +03:00
|
|
|
$fileExists->shouldHaveBeenCalledOnce();
|
2019-04-14 11:49:54 +03:00
|
|
|
$getMeta->shouldHaveBeenCalledOnce();
|
|
|
|
$download->shouldNotHaveBeenCalled();
|
|
|
|
}
|
|
|
|
|
2019-04-14 14:18:03 +03:00
|
|
|
public function provideSmallDays(): iterable
|
2019-04-14 11:49:54 +03:00
|
|
|
{
|
2019-12-30 01:16:55 +03:00
|
|
|
return map(range(0, 34), fn (int $days) => [$days]);
|
2019-04-14 11:49:54 +03:00
|
|
|
}
|
|
|
|
}
|