|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace ShlinkioTest\Shlink\Core\Exception; |
|
6
|
|
|
|
|
7
|
|
|
use Exception; |
|
8
|
|
|
use LogicException; |
|
9
|
|
|
use PHPUnit\Framework\TestCase; |
|
10
|
|
|
use Shlinkio\Shlink\Core\Exception\IpCannotBeLocatedException; |
|
11
|
|
|
use Shlinkio\Shlink\Core\Exception\RuntimeException; |
|
12
|
|
|
use Throwable; |
|
13
|
|
|
|
|
14
|
|
|
class IpCannotBeLocatedExceptionTest extends TestCase |
|
15
|
|
|
{ |
|
16
|
|
|
/** @test */ |
|
17
|
|
|
public function forEmptyAddressInitializesException(): void |
|
18
|
|
|
{ |
|
19
|
|
|
$e = IpCannotBeLocatedException::forEmptyAddress(); |
|
20
|
|
|
|
|
21
|
|
|
$this->assertTrue($e->isNonLocatableAddress()); |
|
22
|
|
|
$this->assertEquals('Ignored visit with no IP address', $e->getMessage()); |
|
23
|
|
|
$this->assertEquals(0, $e->getCode()); |
|
24
|
|
|
$this->assertNull($e->getPrevious()); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** @test */ |
|
28
|
|
|
public function forLocalhostInitializesException(): void |
|
29
|
|
|
{ |
|
30
|
|
|
$e = IpCannotBeLocatedException::forLocalhost(); |
|
31
|
|
|
|
|
32
|
|
|
$this->assertTrue($e->isNonLocatableAddress()); |
|
33
|
|
|
$this->assertEquals('Ignored localhost address', $e->getMessage()); |
|
34
|
|
|
$this->assertEquals(0, $e->getCode()); |
|
35
|
|
|
$this->assertNull($e->getPrevious()); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @test |
|
40
|
|
|
* @dataProvider provideErrors |
|
41
|
|
|
*/ |
|
42
|
|
|
public function forErrorInitializesException(Throwable $prev): void |
|
43
|
|
|
{ |
|
44
|
|
|
$e = IpCannotBeLocatedException::forError($prev); |
|
45
|
|
|
|
|
46
|
|
|
$this->assertFalse($e->isNonLocatableAddress()); |
|
47
|
|
|
$this->assertEquals('An error occurred while locating IP', $e->getMessage()); |
|
48
|
|
|
$this->assertEquals($prev->getCode(), $e->getCode()); |
|
49
|
|
|
$this->assertSame($prev, $e->getPrevious()); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function provideErrors(): iterable |
|
53
|
|
|
{ |
|
54
|
|
|
yield 'Simple exception with positive code' => [new Exception('Some message', 100)]; |
|
55
|
|
|
yield 'Runtime exception with negative code' => [new RuntimeException('Something went wrong', -50)]; |
|
56
|
|
|
yield 'Logic exception with default code' => [new LogicException('Conditions unmet')]; |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|