Updated tests to fit current implementations

This commit is contained in:
Alejandro Celaya 2019-07-23 22:04:01 +02:00
parent 999beef349
commit 173bfbd300
3 changed files with 41 additions and 15 deletions

View file

@ -46,6 +46,9 @@ class GeolocationDbUpdater implements GeolocationDbUpdaterInterface
} }
} }
/**
* @throws GeolocationDbUpdateFailedException
*/
private function downloadIfNeeded(?callable $mustBeUpdated, ?callable $handleProgress): void private function downloadIfNeeded(?callable $mustBeUpdated, ?callable $handleProgress): void
{ {
if (! $this->dbUpdater->databaseFileExists()) { if (! $this->dbUpdater->databaseFileExists()) {
@ -59,21 +62,11 @@ class GeolocationDbUpdater implements GeolocationDbUpdaterInterface
} }
} }
private function buildIsTooOld(int $buildTimestamp): bool
{
$buildDate = Chronos::createFromTimestamp($buildTimestamp);
$now = Chronos::now();
return $now->gt($buildDate->addDays(35));
}
/** /**
* @throws GeolocationDbUpdateFailedException * @throws GeolocationDbUpdateFailedException
*/ */
private function downloadNewDb( private function downloadNewDb(bool $olderDbExists, ?callable $mustBeUpdated, ?callable $handleProgress): void
bool $olderDbExists, {
callable $mustBeUpdated = null,
callable $handleProgress = null
): void {
if ($mustBeUpdated !== null) { if ($mustBeUpdated !== null) {
$mustBeUpdated($olderDbExists); $mustBeUpdated($olderDbExists);
} }
@ -84,4 +77,11 @@ class GeolocationDbUpdater implements GeolocationDbUpdaterInterface
throw GeolocationDbUpdateFailedException::create($olderDbExists, $e); throw GeolocationDbUpdateFailedException::create($olderDbExists, $e);
} }
} }
private function buildIsTooOld(int $buildTimestamp): bool
{
$buildDate = Chronos::createFromTimestamp($buildTimestamp);
$now = Chronos::now();
return $now->gt($buildDate->addDays(35));
}
} }

View file

@ -58,8 +58,10 @@ class GeolocationDbUpdaterTest extends TestCase
$mustBeUpdated = function () { $mustBeUpdated = function () {
$this->assertTrue(true); $this->assertTrue(true);
}; };
$getMeta = $this->geoLiteDbReader->metadata()->willThrow(InvalidArgumentException::class);
$prev = new RuntimeException(''); $prev = new RuntimeException('');
$fileExists = $this->dbUpdater->databaseFileExists()->willReturn(false);
$getMeta = $this->geoLiteDbReader->metadata();
$download = $this->dbUpdater->downloadFreshCopy(null)->willThrow($prev); $download = $this->dbUpdater->downloadFreshCopy(null)->willThrow($prev);
try { try {
@ -72,7 +74,8 @@ class GeolocationDbUpdaterTest extends TestCase
$this->assertFalse($e->olderDbExists()); $this->assertFalse($e->olderDbExists());
} }
$getMeta->shouldHaveBeenCalledOnce(); $fileExists->shouldHaveBeenCalledOnce();
$getMeta->shouldNotHaveBeenCalled();
$download->shouldHaveBeenCalledOnce(); $download->shouldHaveBeenCalledOnce();
} }
@ -82,6 +85,7 @@ class GeolocationDbUpdaterTest extends TestCase
*/ */
public function exceptionIsThrownWhenOlderDbIsTooOldAndDownloadFails(int $days): void public function exceptionIsThrownWhenOlderDbIsTooOldAndDownloadFails(int $days): void
{ {
$fileExists = $this->dbUpdater->databaseFileExists()->willReturn(true);
$getMeta = $this->geoLiteDbReader->metadata()->willReturn(new Metadata([ $getMeta = $this->geoLiteDbReader->metadata()->willReturn(new Metadata([
'binary_format_major_version' => '', 'binary_format_major_version' => '',
'binary_format_minor_version' => '', 'binary_format_minor_version' => '',
@ -106,6 +110,7 @@ class GeolocationDbUpdaterTest extends TestCase
$this->assertTrue($e->olderDbExists()); $this->assertTrue($e->olderDbExists());
} }
$fileExists->shouldHaveBeenCalledOnce();
$getMeta->shouldHaveBeenCalledOnce(); $getMeta->shouldHaveBeenCalledOnce();
$download->shouldHaveBeenCalledOnce(); $download->shouldHaveBeenCalledOnce();
} }
@ -124,6 +129,7 @@ class GeolocationDbUpdaterTest extends TestCase
*/ */
public function databaseIsNotUpdatedIfItIsYoungerThanOneWeek(int $days): void public function databaseIsNotUpdatedIfItIsYoungerThanOneWeek(int $days): void
{ {
$fileExists = $this->dbUpdater->databaseFileExists()->willReturn(true);
$getMeta = $this->geoLiteDbReader->metadata()->willReturn(new Metadata([ $getMeta = $this->geoLiteDbReader->metadata()->willReturn(new Metadata([
'binary_format_major_version' => '', 'binary_format_major_version' => '',
'binary_format_minor_version' => '', 'binary_format_minor_version' => '',
@ -140,6 +146,7 @@ class GeolocationDbUpdaterTest extends TestCase
$this->geolocationDbUpdater->checkDbUpdate(); $this->geolocationDbUpdater->checkDbUpdate();
$fileExists->shouldHaveBeenCalledOnce();
$getMeta->shouldHaveBeenCalledOnce(); $getMeta->shouldHaveBeenCalledOnce();
$download->shouldNotHaveBeenCalled(); $download->shouldNotHaveBeenCalled();
} }

View file

@ -32,7 +32,7 @@ class DbUpdaterTest extends TestCase
$this->filesystem = $this->prophesize(Filesystem::class); $this->filesystem = $this->prophesize(Filesystem::class);
$this->options = new GeoLite2Options([ $this->options = new GeoLite2Options([
'temp_dir' => __DIR__ . '/../../../test-resources', 'temp_dir' => __DIR__ . '/../../../test-resources',
'db_location' => '', 'db_location' => 'db_location',
'download_from' => '', 'download_from' => '',
]); ]);
@ -110,4 +110,23 @@ class DbUpdaterTest extends TestCase
$copy->shouldHaveBeenCalledOnce(); $copy->shouldHaveBeenCalledOnce();
$remove->shouldHaveBeenCalledOnce(); $remove->shouldHaveBeenCalledOnce();
} }
/**
* @test
* @dataProvider provideExists
*/
public function databaseFileExistsChecksIfTheFilesExistsInTheFilesystem(bool $expected): void
{
$exists = $this->filesystem->exists('db_location')->willReturn($expected);
$result = $this->dbUpdater->databaseFileExists();
$this->assertEquals($expected, $result);
$exists->shouldHaveBeenCalledOnce();
}
public function provideExists(): iterable
{
return [[true], [false]];
}
} }