From 173bfbd300e9b621aff157a58b2e67fc989f6925 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Tue, 23 Jul 2019 22:04:01 +0200 Subject: [PATCH] Updated tests to fit current implementations --- module/CLI/src/Util/GeolocationDbUpdater.php | 24 +++++++++---------- .../test/Util/GeolocationDbUpdaterTest.php | 11 +++++++-- .../IpGeolocation/GeoLite2/DbUpdaterTest.php | 21 +++++++++++++++- 3 files changed, 41 insertions(+), 15 deletions(-) diff --git a/module/CLI/src/Util/GeolocationDbUpdater.php b/module/CLI/src/Util/GeolocationDbUpdater.php index 4f7881a1..e7ea60b9 100644 --- a/module/CLI/src/Util/GeolocationDbUpdater.php +++ b/module/CLI/src/Util/GeolocationDbUpdater.php @@ -46,6 +46,9 @@ class GeolocationDbUpdater implements GeolocationDbUpdaterInterface } } + /** + * @throws GeolocationDbUpdateFailedException + */ private function downloadIfNeeded(?callable $mustBeUpdated, ?callable $handleProgress): void { 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 */ - private function downloadNewDb( - bool $olderDbExists, - callable $mustBeUpdated = null, - callable $handleProgress = null - ): void { + private function downloadNewDb(bool $olderDbExists, ?callable $mustBeUpdated, ?callable $handleProgress): void + { if ($mustBeUpdated !== null) { $mustBeUpdated($olderDbExists); } @@ -84,4 +77,11 @@ class GeolocationDbUpdater implements GeolocationDbUpdaterInterface throw GeolocationDbUpdateFailedException::create($olderDbExists, $e); } } + + private function buildIsTooOld(int $buildTimestamp): bool + { + $buildDate = Chronos::createFromTimestamp($buildTimestamp); + $now = Chronos::now(); + return $now->gt($buildDate->addDays(35)); + } } diff --git a/module/CLI/test/Util/GeolocationDbUpdaterTest.php b/module/CLI/test/Util/GeolocationDbUpdaterTest.php index e12dcfd9..9e06c907 100644 --- a/module/CLI/test/Util/GeolocationDbUpdaterTest.php +++ b/module/CLI/test/Util/GeolocationDbUpdaterTest.php @@ -58,8 +58,10 @@ class GeolocationDbUpdaterTest extends TestCase $mustBeUpdated = function () { $this->assertTrue(true); }; - $getMeta = $this->geoLiteDbReader->metadata()->willThrow(InvalidArgumentException::class); $prev = new RuntimeException(''); + + $fileExists = $this->dbUpdater->databaseFileExists()->willReturn(false); + $getMeta = $this->geoLiteDbReader->metadata(); $download = $this->dbUpdater->downloadFreshCopy(null)->willThrow($prev); try { @@ -72,7 +74,8 @@ class GeolocationDbUpdaterTest extends TestCase $this->assertFalse($e->olderDbExists()); } - $getMeta->shouldHaveBeenCalledOnce(); + $fileExists->shouldHaveBeenCalledOnce(); + $getMeta->shouldNotHaveBeenCalled(); $download->shouldHaveBeenCalledOnce(); } @@ -82,6 +85,7 @@ class GeolocationDbUpdaterTest extends TestCase */ public function exceptionIsThrownWhenOlderDbIsTooOldAndDownloadFails(int $days): void { + $fileExists = $this->dbUpdater->databaseFileExists()->willReturn(true); $getMeta = $this->geoLiteDbReader->metadata()->willReturn(new Metadata([ 'binary_format_major_version' => '', 'binary_format_minor_version' => '', @@ -106,6 +110,7 @@ class GeolocationDbUpdaterTest extends TestCase $this->assertTrue($e->olderDbExists()); } + $fileExists->shouldHaveBeenCalledOnce(); $getMeta->shouldHaveBeenCalledOnce(); $download->shouldHaveBeenCalledOnce(); } @@ -124,6 +129,7 @@ class GeolocationDbUpdaterTest extends TestCase */ public function databaseIsNotUpdatedIfItIsYoungerThanOneWeek(int $days): void { + $fileExists = $this->dbUpdater->databaseFileExists()->willReturn(true); $getMeta = $this->geoLiteDbReader->metadata()->willReturn(new Metadata([ 'binary_format_major_version' => '', 'binary_format_minor_version' => '', @@ -140,6 +146,7 @@ class GeolocationDbUpdaterTest extends TestCase $this->geolocationDbUpdater->checkDbUpdate(); + $fileExists->shouldHaveBeenCalledOnce(); $getMeta->shouldHaveBeenCalledOnce(); $download->shouldNotHaveBeenCalled(); } diff --git a/module/Common/test/IpGeolocation/GeoLite2/DbUpdaterTest.php b/module/Common/test/IpGeolocation/GeoLite2/DbUpdaterTest.php index d2c9cc5e..f897d9e1 100644 --- a/module/Common/test/IpGeolocation/GeoLite2/DbUpdaterTest.php +++ b/module/Common/test/IpGeolocation/GeoLite2/DbUpdaterTest.php @@ -32,7 +32,7 @@ class DbUpdaterTest extends TestCase $this->filesystem = $this->prophesize(Filesystem::class); $this->options = new GeoLite2Options([ 'temp_dir' => __DIR__ . '/../../../test-resources', - 'db_location' => '', + 'db_location' => 'db_location', 'download_from' => '', ]); @@ -110,4 +110,23 @@ class DbUpdaterTest extends TestCase $copy->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]]; + } }