mirror of
https://github.com/shlinkio/shlink.git
synced 2024-12-22 15:13:59 -06:00
Created exception to handle cases in which downloading a new geolite db fails
This commit is contained in:
parent
feb67e76f0
commit
935562acc9
10
module/CLI/src/Exception/ExceptionInterface.php
Normal file
10
module/CLI/src/Exception/ExceptionInterface.php
Normal file
@ -0,0 +1,10 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\CLI\Exception;
|
||||
|
||||
use Throwable;
|
||||
|
||||
interface ExceptionInterface extends Throwable
|
||||
{
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\CLI\Exception;
|
||||
|
||||
use RuntimeException;
|
||||
use Throwable;
|
||||
|
||||
class GeolocationDbUpdateFailedException extends RuntimeException implements ExceptionInterface
|
||||
{
|
||||
/** @var bool */
|
||||
private $olderDbExists;
|
||||
|
||||
public function __construct(bool $olderDbExists, string $message = '', int $code = 0, Throwable $previous = null)
|
||||
{
|
||||
$this->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;
|
||||
}
|
||||
}
|
@ -0,0 +1,79 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ShlinkioTest\Shlink\CLI\Exception;
|
||||
|
||||
use Exception;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use RuntimeException;
|
||||
use Shlinkio\Shlink\CLI\Exception\GeolocationDbUpdateFailedException;
|
||||
use Throwable;
|
||||
|
||||
class GeolocationDbUpdateFailedExceptionTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @test
|
||||
* @dataProvider provideOlderDbExists
|
||||
*/
|
||||
public function constructCreatesExceptionWithDefaultArgs(bool $olderDbExists): void
|
||||
{
|
||||
$e = new GeolocationDbUpdateFailedException($olderDbExists);
|
||||
|
||||
$this->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')];
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user