Passed
Push — master ( 12824e...e9c458 )
by Darío
03:48 queued 01:49
created

AbstractClientTest::itCanParseAResponseToJson()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace EasyHttp\LayerContracts\Tests\Unit;
4
5
use EasyHttp\LayerContracts\Contracts\HttpClientResponse;
6
use EasyHttp\LayerContracts\Exceptions\HttpClientException;
7
use EasyHttp\LayerContracts\Exceptions\ImpossibleToParseJsonException;
8
use EasyHttp\LayerContracts\Tests\Unit\Example\SomeClient;
9
use PHPUnit\Framework\TestCase;
10
11
class AbstractClientTest extends TestCase
12
{
13
    /**
14
     * @test
15
     */
16
    public function itExecutesARequest(): HttpClientResponse
17
    {
18
        $client = new SomeClient();
19
20
        $response = $client->call('GET', 'http://example.com/api');
21
22
        $this->assertSame(200, $response->getStatusCode());
23
        $this->assertSame('{"key":"value"}', $response->getBody());
24
        $this->assertSame(['Server' => 'Apache/2.4.38 (Debian)'], $response->getHeaders());
25
26
        return $response;
27
    }
28
29
    /**
30
     * @test
31
     * @depends itExecutesARequest
32
     * @param HttpClientResponse $response
33
     * @throws ImpossibleToParseJsonException
34
     */
35
    public function itCanParseAResponseToJson(HttpClientResponse $response)
36
    {
37
        $this->assertSame(['key' => 'value'], $response->parseJson());
38
    }
39
40
    /**
41
     * @test
42
     */
43
    public function itPreparesARequestForExecution()
44
    {
45
        $client = new SomeClient();
46
        $client->prepareRequest('GET', 'http://example.com/api');
47
48
        $this->assertSame('GET', $client->getRequest()->getMethod());
49
        $this->assertSame('http://example.com/api', $client->getRequest()->getUri());
50
    }
51
52
    /**
53
     * @test
54
     */
55
    public function itExecutesAPreparedRequest()
56
    {
57
        $client = new SomeClient();
58
        $client->prepareRequest('GET', 'http://example.com/api');
59
60
        $response = $client->execute();
61
62
        $this->assertSame(200, $response->getStatusCode());
63
        $this->assertSame(['key' => 'value'], $response->parseJson());
64
        $this->assertSame(['Server' => 'Apache/2.4.38 (Debian)'], $response->getHeaders());
65
    }
66
67
    /**
68
     * @test
69
     */
70
    public function itReuseTheAdapterForEachRequest()
71
    {
72
        $client = new SomeClient();
73
74
        $client->call('GET', 'http://example.com/api');
75
        $client->call('GET', 'http://example.com/api');
76
        $client->call('GET', 'http://example.com/api');
77
78
        $this->assertSame(1, $client->getAdapterCounter());
79
    }
80
81
    /**
82
     * @test
83
     */
84
    public function itSetsHandlers()
85
    {
86
        $client = new SomeClient();
87
88
        $client->withHandler(
89
            function () {
90
                return [
91
                    'status' => 500,
92
                    'headers' => ['Server' => 'Apache/2.4.38 (Ubuntu)'],
93
                    'body' => '{"message":"Server Error"}',
94
                ];
95
            }
96
        );
97
98
        $response = $client->call('GET', 'some uri');
99
100
        $this->assertSame(500, $response->getStatusCode());
101
        $this->assertSame(['message' => 'Server Error'], $response->parseJson());
102
        $this->assertSame(['Server' => 'Apache/2.4.38 (Ubuntu)'], $response->getHeaders());
103
    }
104
105
    /**
106
     * @test
107
     */
108
    public function itFlushTheAdapterAfterSetsHandler()
109
    {
110
        $client = new SomeClient();
111
112
        $liveResponse = $client->call('GET', 'http://example.com/api');
113
114
        $client->withHandler(
115
            function () {
116
                return [
117
                    'status' => 500,
118
                    'headers' => ['Server' => 'Apache/2.4 (Ubuntu)'],
119
                    'body' => 'Server Error',
120
                ];
121
            }
122
        );
123
124
        $mockedResponse = $client->call('GET', 'some uri');
125
126
        $this->assertSame(200, $liveResponse->getStatusCode());
127
        $this->assertSame(['key' => 'value'], $liveResponse->parseJson());
128
        $this->assertSame(['Server' => 'Apache/2.4.38 (Debian)'], $liveResponse->getHeaders());
129
        $this->assertSame(500, $mockedResponse->getStatusCode());
130
        $this->assertSame(['Server' => 'Apache/2.4 (Ubuntu)'], $mockedResponse->getHeaders());
131
    }
132
133
    /**
134
     * This test is just a mock!. The responsibility for throwing this exception lies
135
     * with the library who is implementing this contracts!
136
     *
137
     * @test
138
     */
139
    public function itThrowsClientExceptionWhenFails()
140
    {
141
        $this->expectException(HttpClientException::class);
142
143
        $client = new SomeClient();
144
145
        $client->withHandler(
146
            function () {
147
                throw new HttpClientException('Bad request exception');
148
            }
149
        );
150
151
        $client->call('GET', 'some uri');
152
    }
153
154
    public function itThrowsNotParsedExceptionWhenInvalidJsonIsFound()
155
    {
156
        $this->expectException(ImpossibleToParseJsonException::class);
157
158
        $client = new SomeClient();
159
160
        $client->withHandler(
161
            function () {
162
                return 'Server Error';
163
            }
164
        );
165
166
        $response = $client->call('GET', 'some uri');
167
        $response->parseJson();
168
    }
169
}
170