Completed
Pull Request — develop (#578)
by Alejandro
08:17
created

IpCannotBeLocatedExceptionTest::constructorInitializesException()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 22
rs 9.7
cc 3
nc 3
nop 0
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