|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace ShlinkioTest\Shlink\CLI\Exception; |
|
5
|
|
|
|
|
6
|
|
|
use Exception; |
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
|
8
|
|
|
use RuntimeException; |
|
9
|
|
|
use Shlinkio\Shlink\CLI\Exception\GeolocationDbUpdateFailedException; |
|
10
|
|
|
use Throwable; |
|
11
|
|
|
|
|
12
|
|
|
class GeolocationDbUpdateFailedExceptionTest extends TestCase |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @test |
|
16
|
|
|
* @dataProvider provideOlderDbExists |
|
17
|
|
|
*/ |
|
18
|
|
|
public function constructCreatesExceptionWithDefaultArgs(bool $olderDbExists): void |
|
19
|
|
|
{ |
|
20
|
|
|
$e = new GeolocationDbUpdateFailedException($olderDbExists); |
|
21
|
|
|
|
|
22
|
|
|
$this->assertEquals($olderDbExists, $e->olderDbExists()); |
|
23
|
|
|
$this->assertEquals('', $e->getMessage()); |
|
24
|
|
|
$this->assertEquals(0, $e->getCode()); |
|
25
|
|
|
$this->assertNull($e->getPrevious()); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function provideOlderDbExists(): iterable |
|
29
|
|
|
{ |
|
30
|
|
|
yield 'with older DB' => [true]; |
|
31
|
|
|
yield 'without older DB' => [false]; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @test |
|
36
|
|
|
* @dataProvider provideConstructorArgs |
|
37
|
|
|
*/ |
|
38
|
|
|
public function constructCreatesException(bool $olderDbExists, string $message, int $code, ?Throwable $prev): void |
|
39
|
|
|
{ |
|
40
|
|
|
$e = new GeolocationDbUpdateFailedException($olderDbExists, $message, $code, $prev); |
|
41
|
|
|
|
|
42
|
|
|
$this->assertEquals($olderDbExists, $e->olderDbExists()); |
|
43
|
|
|
$this->assertEquals($message, $e->getMessage()); |
|
44
|
|
|
$this->assertEquals($code, $e->getCode()); |
|
45
|
|
|
$this->assertEquals($prev, $e->getPrevious()); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function provideConstructorArgs(): iterable |
|
49
|
|
|
{ |
|
50
|
|
|
yield [true, 'This is a nice error message', 99, new Exception('prev')]; |
|
51
|
|
|
yield [false, 'Another message', 0, new RuntimeException('prev')]; |
|
52
|
|
|
yield [true, 'An yet another message', -50, null]; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @test |
|
57
|
|
|
* @dataProvider provideCreateArgs |
|
58
|
|
|
*/ |
|
59
|
|
|
public function createBuildsException(bool $olderDbExists, ?Throwable $prev): void |
|
60
|
|
|
{ |
|
61
|
|
|
$e = GeolocationDbUpdateFailedException::create($olderDbExists, $prev); |
|
62
|
|
|
|
|
63
|
|
|
$this->assertEquals($olderDbExists, $e->olderDbExists()); |
|
64
|
|
|
$this->assertEquals( |
|
65
|
|
|
'An error occurred while updating geolocation database, and an older version could not be found', |
|
66
|
|
|
$e->getMessage() |
|
67
|
|
|
); |
|
68
|
|
|
$this->assertEquals(0, $e->getCode()); |
|
69
|
|
|
$this->assertEquals($prev, $e->getPrevious()); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function provideCreateArgs(): iterable |
|
73
|
|
|
{ |
|
74
|
|
|
yield 'older DB and no prev' => [true, null]; |
|
75
|
|
|
yield 'older DB and prev' => [true, new RuntimeException('prev')]; |
|
76
|
|
|
yield 'no older DB and no prev' => [false, null]; |
|
77
|
|
|
yield 'no older DB and prev' => [false, new Exception('prev')]; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|