Passed
Pull Request — master (#331)
by Alejandro
05:25
created

failResponseIsReturnedWhenConnectionFails()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 0
dl 0
loc 17
rs 9.8666
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace ShlinkioTest\Shlink\Rest\Action;
5
6
use Doctrine\DBAL\Connection;
7
use Exception;
8
use PHPUnit\Framework\TestCase;
9
use Prophecy\Prophecy\ObjectProphecy;
10
use Shlinkio\Shlink\Core\Options\AppOptions;
11
use Shlinkio\Shlink\Rest\Action\HealthAction;
12
use Zend\Diactoros\Response\JsonResponse;
13
use Zend\Diactoros\ServerRequest;
14
15
class HealthActionTest extends TestCase
16
{
17
    /** @var HealthAction */
18
    private $action;
19
    /** @var ObjectProphecy */
20
    private $conn;
21
22
    public function setUp()
23
    {
24
        $this->conn = $this->prophesize(Connection::class);
25
        $this->action = new HealthAction($this->conn->reveal(), new AppOptions(['version' => '1.2.3']));
26
    }
27
28
    /**
29
     * @test
30
     */
31
    public function passResponseIsReturnedWhenConnectionSucceeds()
32
    {
33
        $ping = $this->conn->ping()->willReturn(true);
34
35
        /** @var JsonResponse $resp */
36
        $resp = $this->action->handle(new ServerRequest());
37
        $payload = $resp->getPayload();
38
39
        $this->assertEquals(200, $resp->getStatusCode());
40
        $this->assertEquals('pass', $payload['status']);
41
        $this->assertEquals('1.2.3', $payload['version']);
42
        $this->assertEquals([
43
            'about' => 'https://shlink.io',
44
            'project' => 'https://github.com/shlinkio/shlink',
45
        ], $payload['links']);
46
        $this->assertEquals('application/health+json', $resp->getHeaderLine('Content-type'));
47
        $ping->shouldHaveBeenCalledOnce();
48
    }
49
50
    /**
51
     * @test
52
     */
53
    public function failResponseIsReturnedWhenConnectionFails()
54
    {
55
        $ping = $this->conn->ping()->willReturn(false);
56
57
        /** @var JsonResponse $resp */
58
        $resp = $this->action->handle(new ServerRequest());
59
        $payload = $resp->getPayload();
60
61
        $this->assertEquals(503, $resp->getStatusCode());
62
        $this->assertEquals('fail', $payload['status']);
63
        $this->assertEquals('1.2.3', $payload['version']);
64
        $this->assertEquals([
65
            'about' => 'https://shlink.io',
66
            'project' => 'https://github.com/shlinkio/shlink',
67
        ], $payload['links']);
68
        $this->assertEquals('application/health+json', $resp->getHeaderLine('Content-type'));
69
        $ping->shouldHaveBeenCalledOnce();
70
    }
71
72
    /**
73
     * @test
74
     */
75
    public function failResponseIsReturnedWhenConnectionThrowsException()
76
    {
77
        $ping = $this->conn->ping()->willThrow(Exception::class);
78
79
        /** @var JsonResponse $resp */
80
        $resp = $this->action->handle(new ServerRequest());
81
        $payload = $resp->getPayload();
82
83
        $this->assertEquals(503, $resp->getStatusCode());
84
        $this->assertEquals('fail', $payload['status']);
85
        $this->assertEquals('1.2.3', $payload['version']);
86
        $this->assertEquals([
87
            'about' => 'https://shlink.io',
88
            'project' => 'https://github.com/shlinkio/shlink',
89
        ], $payload['links']);
90
        $this->assertEquals('application/health+json', $resp->getHeaderLine('Content-type'));
91
        $ping->shouldHaveBeenCalledOnce();
92
    }
93
}
94