|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace ShlinkioTest\Shlink\Core\Exception; |
|
5
|
|
|
|
|
6
|
|
|
use Exception; |
|
7
|
|
|
use LogicException; |
|
8
|
|
|
use PHPUnit\Framework\TestCase; |
|
9
|
|
|
use Shlinkio\Shlink\Core\Exception\IpCannotBeLocatedException; |
|
10
|
|
|
use Shlinkio\Shlink\Core\Exception\RuntimeException; |
|
11
|
|
|
use Throwable; |
|
12
|
|
|
use function count; |
|
13
|
|
|
use function func_get_args; |
|
14
|
|
|
use function random_int; |
|
15
|
|
|
|
|
16
|
|
|
class IpCannotBeLocatedExceptionTest extends TestCase |
|
17
|
|
|
{ |
|
18
|
|
|
/** @test */ |
|
19
|
|
|
public function forEmptyAddressInitializesException(): void |
|
20
|
|
|
{ |
|
21
|
|
|
$e = IpCannotBeLocatedException::forEmptyAddress(); |
|
22
|
|
|
|
|
23
|
|
|
$this->assertTrue($e->isNonLocatableAddress()); |
|
24
|
|
|
$this->assertEquals('Ignored visit with no IP address', $e->getMessage()); |
|
25
|
|
|
$this->assertEquals(0, $e->getCode()); |
|
26
|
|
|
$this->assertNull($e->getPrevious()); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** @test */ |
|
30
|
|
|
public function forLocalhostInitializesException(): void |
|
31
|
|
|
{ |
|
32
|
|
|
$e = IpCannotBeLocatedException::forLocalhost(); |
|
33
|
|
|
|
|
34
|
|
|
$this->assertTrue($e->isNonLocatableAddress()); |
|
35
|
|
|
$this->assertEquals('Ignored localhost address', $e->getMessage()); |
|
36
|
|
|
$this->assertEquals(0, $e->getCode()); |
|
37
|
|
|
$this->assertNull($e->getPrevious()); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @test |
|
42
|
|
|
* @dataProvider provideErrors |
|
43
|
|
|
*/ |
|
44
|
|
|
public function forErrorInitializesException(Throwable $prev): void |
|
45
|
|
|
{ |
|
46
|
|
|
$e = IpCannotBeLocatedException::forError($prev); |
|
47
|
|
|
|
|
48
|
|
|
$this->assertFalse($e->isNonLocatableAddress()); |
|
49
|
|
|
$this->assertEquals('An error occurred while locating IP', $e->getMessage()); |
|
50
|
|
|
$this->assertEquals($prev->getCode(), $e->getCode()); |
|
51
|
|
|
$this->assertSame($prev, $e->getPrevious()); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function provideErrors(): iterable |
|
55
|
|
|
{ |
|
56
|
|
|
yield 'Simple exception with positive code' => [new Exception('Some message', 100)]; |
|
57
|
|
|
yield 'Runtime exception with negative code' => [new RuntimeException('Something went wrong', -50)]; |
|
58
|
|
|
yield 'Logic exception with default code' => [new LogicException('Conditions unmet')]; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @test |
|
63
|
|
|
* @dataProvider provideConstructorArgs |
|
64
|
|
|
*/ |
|
65
|
|
|
public function constructorInitializesException(): void |
|
66
|
|
|
{ |
|
67
|
|
|
$args = func_get_args(); |
|
68
|
|
|
[$isNonLocatableAddress, $message] = $args; |
|
69
|
|
|
$code = $args[2] ?? 0; |
|
70
|
|
|
$prev = $args[3] ?? null; |
|
71
|
|
|
|
|
72
|
|
|
switch (count($args)) { |
|
73
|
|
|
case 2: |
|
74
|
|
|
$e = new IpCannotBeLocatedException($isNonLocatableAddress, $message); |
|
75
|
|
|
break; |
|
76
|
|
|
case 3: |
|
77
|
|
|
$e = new IpCannotBeLocatedException($isNonLocatableAddress, $message, $code); |
|
78
|
|
|
break; |
|
79
|
|
|
default: |
|
80
|
|
|
$e = new IpCannotBeLocatedException($isNonLocatableAddress, $message, $code, $prev); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$this->assertEquals($isNonLocatableAddress, $e->isNonLocatableAddress()); |
|
84
|
|
|
$this->assertEquals($message, $e->getMessage()); |
|
85
|
|
|
$this->assertEquals($code, $e->getCode()); |
|
86
|
|
|
$this->assertEquals($prev, $e->getPrevious()); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function provideConstructorArgs(): iterable |
|
90
|
|
|
{ |
|
91
|
|
|
yield 'without default args' => [true, 'Message']; |
|
92
|
|
|
yield 'without prev' => [true, 'Message', random_int(1, 100)]; |
|
93
|
|
|
yield 'without all args' => [false, 'Foo', random_int(1, 100), new RuntimeException('Foo')]; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|