diff --git a/module/CLI/src/Exception/ExceptionInterface.php b/module/CLI/src/Exception/ExceptionInterface.php new file mode 100644 index 00000000..2ff19593 --- /dev/null +++ b/module/CLI/src/Exception/ExceptionInterface.php @@ -0,0 +1,10 @@ +olderDbExists = $olderDbExists; + parent::__construct($message, $code, $previous); + } + + public static function create(bool $olderDbExists, Throwable $prev = null): self + { + return new self( + $olderDbExists, + 'An error occurred while updating geolocation database, and an older version could not be found', + 0, + $prev + ); + } + + public function olderDbExists(): bool + { + return $this->olderDbExists; + } +} diff --git a/module/CLI/test/Exception/GeolocationDbUpdateFailedExceptionTest.php b/module/CLI/test/Exception/GeolocationDbUpdateFailedExceptionTest.php new file mode 100644 index 00000000..b8202967 --- /dev/null +++ b/module/CLI/test/Exception/GeolocationDbUpdateFailedExceptionTest.php @@ -0,0 +1,79 @@ +assertEquals($olderDbExists, $e->olderDbExists()); + $this->assertEquals('', $e->getMessage()); + $this->assertEquals(0, $e->getCode()); + $this->assertNull($e->getPrevious()); + } + + public function provideOlderDbExists(): iterable + { + yield 'with older DB' => [true]; + yield 'without older DB' => [false]; + } + + /** + * @test + * @dataProvider provideConstructorArgs + */ + public function constructCreatesException(bool $olderDbExists, string $message, int $code, ?Throwable $prev): void + { + $e = new GeolocationDbUpdateFailedException($olderDbExists, $message, $code, $prev); + + $this->assertEquals($olderDbExists, $e->olderDbExists()); + $this->assertEquals($message, $e->getMessage()); + $this->assertEquals($code, $e->getCode()); + $this->assertEquals($prev, $e->getPrevious()); + } + + public function provideConstructorArgs(): iterable + { + yield [true, 'This is a nice error message', 99, new Exception('prev')]; + yield [false, 'Another message', 0, new RuntimeException('prev')]; + yield [true, 'An yet another message', -50, null]; + } + + /** + * @test + * @dataProvider provideCreateArgs + */ + public function createBuildsException(bool $olderDbExists, ?Throwable $prev): void + { + $e = GeolocationDbUpdateFailedException::create($olderDbExists, $prev); + + $this->assertEquals($olderDbExists, $e->olderDbExists()); + $this->assertEquals( + 'An error occurred while updating geolocation database, and an older version could not be found', + $e->getMessage() + ); + $this->assertEquals(0, $e->getCode()); + $this->assertEquals($prev, $e->getPrevious()); + } + + public function provideCreateArgs(): iterable + { + yield 'older DB and no prev' => [true, null]; + yield 'older DB and prev' => [true, new RuntimeException('prev')]; + yield 'no older DB and no prev' => [false, null]; + yield 'no older DB and prev' => [false, new Exception('prev')]; + } +}