Completed
Pull Request — master (#402)
by Alejandro
05:51
created

GeolocationDbUpdateFailedExceptionTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 27
dl 0
loc 66
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A constructCreatesExceptionWithDefaultArgs() 0 8 1
A provideConstructorArgs() 0 5 1
A provideCreateArgs() 0 6 1
A createBuildsException() 0 11 1
A provideOlderDbExists() 0 4 1
A constructCreatesException() 0 8 1
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