Passed
Push — master ( 989399...355206 )
by Julien
03:02
created

RestClient::testGet()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 74
Code Lines 55

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 55
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 74
rs 8.9818

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Mapado\RestClientSdk\Tests\Units;
6
7
use atoum;
8
use GuzzleHttp\Client as HttpClient;
9
use GuzzleHttp\Handler\MockHandler;
10
use GuzzleHttp\HandlerStack;
11
use GuzzleHttp\Middleware;
12
use GuzzleHttp\Psr7\Response;
13
use Psr\Http\Message\RequestInterface;
14
use Psr\Http\Message\ResponseInterface;
15
16
class RestClient extends atoum
17
{
18
    /**
19
     * testGet
20
     */
21
    public function testGet()
22
    {
23
        $mock = new MockHandler(
24
            [
25
                new Response(
26
                    200,
27
                    [
28
                        'Content-Type' => 'application/ld+json',
29
                    ],
30
                    file_get_contents(__DIR__ . '/../data/ticketing.list.no_result.json')
31
                ),
32
                new Response(404),
33
                new Response(502),
34
                new Response(
35
                    201,
36
                    []
37
                ),
38
                new Response(403),
39
            ]
40
        );
41
42
        $historyContainer = [];
43
        $history = Middleware::history($historyContainer);
44
        $handler = HandlerStack::create($mock);
45
        $handler->push($history);
46
47
        $this
48
            ->given($http = new HttpClient(['handler' => $handler]))
49
                ->and($this->newTestedInstance($http))
50
            ->then
51
                ->array($this->testedInstance->get('/no-error'))
52
                ->string(array_pop($historyContainer)['request']->getUri()->getPath())
53
                    ->isEqualTo('/no-error')
54
55
            ->then
56
                ->variable($this->testedInstance->get('/not-found'))
57
                    ->isNull()
58
59
            ->then($testedInstance = $this->testedInstance)
60
                ->exception(function () use ($testedInstance) {
61
                    $testedInstance->get('/error');
62
                })
63
                    ->isInstanceOf('Mapado\RestClientSdk\Exception\RestException')
64
                    ->hasMessage('Error while getting resource')
65
                    ->hasCode(1)
66
                ->and
67
                    ->object($this->exception->getResponse())
68
                        ->isInstanceOf(ResponseInterface::class)
69
                ->and
70
                    ->object($request = $this->exception->getRequest())
71
                        ->isInstanceOf(RequestInterface::class)
72
                    ->string($request->getUri()->getPath())
73
                        ->isEqualTo('/error')
74
75
            // test binary get
76
            ->then
77
                ->object($this->testedInstance->get('/not-json'))
78
                    ->isInstanceOf('\GuzzleHttp\Psr7\Response')
79
80
            ->then($testedInstance = $this->testedInstance)
81
                ->exception(function () use ($testedInstance) {
82
                    $testedInstance->get('/403');
83
                })
84
                    ->isInstanceOf('Mapado\RestClientSdk\Exception\RestException')
85
                    ->hasMessage('Error while getting resource')
86
                    ->hasCode(7)
87
                ->and
88
                    ->object($this->exception->getResponse())
89
                        ->isInstanceOf(ResponseInterface::class)
90
                ->and
91
                    ->object($request = $this->exception->getRequest())
92
                        ->isInstanceOf(RequestInterface::class)
93
                    ->string($request->getUri()->getPath())
94
                        ->isEqualTo('/403')
95
           ;
96
    }
97
98
    /**
99
     * testDelete
100
     */
101
    public function testDelete()
102
    {
103
        $mock = new MockHandler(
104
            [
105
                new Response(204),
106
                new Response(404),
107
                new Response(500),
108
            ]
109
        );
110
111
        $handler = HandlerStack::create($mock);
112
113
        $this
114
            ->given($http = new HttpClient(['handler' => $handler]))
115
                ->and($this->newTestedInstance($http))
116
            ->then
117
                ->variable($this->testedInstance->delete('/no-error'))
118
                    ->isNull()
119
120
            ->then
121
                ->variable($this->testedInstance->delete('/not-found'))
122
                    ->isNull()
123
124
            ->then($testedInstance = $this->testedInstance)
125
                ->exception(function () use ($testedInstance) {
126
                    $testedInstance->delete('/error');
127
                })
128
                    ->isInstanceOf('Mapado\RestClientSdk\Exception\RestException')
129
                    ->hasMessage('Error while deleting resource')
130
                    ->hasCode(2)
131
           ;
132
    }
133
134
    /**
135
     * testPost
136
     */
137
    public function testPost()
138
    {
139
        $mock = new MockHandler(
140
            [
141
                new Response(
142
                    201,
143
                    [
144
                        'Content-Type' => 'application/ld+json',
145
                    ],
146
                    file_get_contents(__DIR__ . '/../data/ticketing.created.json')
147
                ),
148
                new Response(400),
149
                new Response(500),
150
            ]
151
        );
152
153
        $handler = HandlerStack::create($mock);
154
155
        $this
156
            ->given($http = new HttpClient(['handler' => $handler]))
157
                ->and($this->newTestedInstance($http))
158
                ->and($params = ['activityUuid' => '63d108be-629c-11e5-b5ce-f153631dac50'])
159
            ->then
160
                ->array(
161
                    $data = $this->testedInstance->post('/no-error', $params)
162
                )
163
                ->then
164
                    ->string($data['activityUuid'])
165
                        ->isEqualTo('63d108be-629c-11e5-b5ce-f153631dac50')
166
            ->then($testedInstance = $this->testedInstance)
167
                ->exception(function () use ($testedInstance, $params) {
168
                    $testedInstance->post('/not-found', $params);
169
                })
170
                    ->isInstanceOf('Mapado\RestClientSdk\Exception\RestClientException')
171
                    ->hasMessage('Cannot create resource')
172
                    ->hasCode(3)
173
174
            ->then
175
                ->exception(function () use ($testedInstance, $params) {
176
                    $testedInstance->post('/error', $params);
177
                })
178
                    ->isInstanceOf('Mapado\RestClientSdk\Exception\RestException')
179
                    ->hasMessage('Error while posting resource')
180
                    ->hasCode(4)
181
        ;
182
    }
183
184
    /**
185
     * testPut
186
     */
187
    public function testPut()
188
    {
189
        $mock = new MockHandler(
190
            [
191
                new Response(
192
                    200,
193
                    [
194
                        'Content-Type' => 'application/ld+json',
195
                    ],
196
                    file_get_contents(__DIR__ . '/../data/ticketing.updated.json')
197
                ),
198
                new Response(404),
199
                new Response(500),
200
            ]
201
        );
202
203
        $handler = HandlerStack::create($mock);
204
205
        $this
206
            ->given($http = new HttpClient(['handler' => $handler]))
207
                ->and($this->newTestedInstance($http))
208
                ->and($params = ['activityUuid' => 'a9e82f60-629e-11e5-8903-0d978bd11e5d'])
209
            ->then
210
                ->array(
211
                    $data = $this->testedInstance->put('/no-error', $params)
212
                )
213
                ->then
214
                    ->string($data['activityUuid'])
215
                        ->isEqualTo('a9e82f60-629e-11e5-8903-0d978bd11e5d')
216
            ->then($testedInstance = $this->testedInstance)
217
                ->exception(function () use ($testedInstance, $params) {
218
                    $testedInstance->put('/not-found', $params);
219
                })
220
                    ->isInstanceOf('Mapado\RestClientSdk\Exception\RestClientException')
221
                    ->hasMessage('Cannot update resource')
222
                    ->hasCode(5)
223
224
            ->then
225
                ->exception(function () use ($testedInstance, $params) {
226
                    $testedInstance->put('/error', $params);
227
                })
228
                    ->isInstanceOf('Mapado\RestClientSdk\Exception\RestException')
229
                    ->hasMessage('Error while puting resource')
230
                    ->hasCode(6)
231
        ;
232
    }
233
234
    public function testHistory()
235
    {
236
        $mock = new MockHandler(
237
            [
238
                new Response(
239
                    200,
240
                    [],
241
                    file_get_contents(__DIR__ . '/../data/ticketing.list.no_result.json')
242
                ),
243
                new Response(
244
                    200,
245
                    [],
246
                    file_get_contents(__DIR__ . '/../data/ticketing.list.no_result.json')
247
                ),
248
                new Response(404),
249
                new Response(502),
250
            ]
251
        );
252
253
        $handler = HandlerStack::create($mock);
254
255
        $this
256
            ->given($http = new HttpClient(['handler' => $handler]))
257
                ->and($this->newTestedInstance($http))
258
            ->then
259
                ->boolean($this->testedInstance->isHistoryLogged())
260
                    ->isFalse()
261
262
            ->then
263
                ->array($this->testedInstance->getRequestHistory())
264
                    ->isEmpty()
265
266
            ->if($this->testedInstance->get('/'))
267
            ->then
268
                ->array($this->testedInstance->getRequestHistory())
269
                    ->isEmpty()
270
271
            ->then
272
                ->if($this->testedInstance->setLogHistory(true))
273
                    ->boolean($this->testedInstance->isHistoryLogged())
274
                        ->isTrue()
275
276
            ->if($this->testedInstance->get('/'))
277
            ->then
278
                ->array($this->testedInstance->getRequestHistory())
279
                    ->isNotEmpty()
280
                    ->size->isEqualTo(1)
281
282
            ->if($this->testedInstance->get('/'))
283
            ->then
284
                ->array($this->testedInstance->getRequestHistory())
285
                    ->isNotEmpty()
286
                    ->size->isEqualTo(2)
287
288
            ->if($testedInstance = $this->testedInstance)
289
            ->and
290
                ->exception(function () use ($testedInstance) {
291
                    $testedInstance->get('/');
292
                })
293
            ->then
294
                ->array($this->testedInstance->getRequestHistory())
295
                    ->size->isEqualTo(3)
296
        ;
297
    }
298
299
    public function testHttpHeaders()
300
    {
301
        $mock = new MockHandler(
302
            [
303
                new Response(
304
                    200,
305
                    ['Content-Type' => 'application/ld+json'],
306
                    file_get_contents(__DIR__ . '/../data/ticketing.list.no_result.json')
307
                ),
308
309
                new Response(
310
                    200,
311
                    ['Content-Type' => 'application/ld+json'],
312
                    file_get_contents(__DIR__ . '/../data/ticketing.list.no_result.json')
313
                ),
314
            ]
315
        );
316
317
        $historyContainer = [];
318
        $history = Middleware::history($historyContainer);
319
        $handler = HandlerStack::create($mock);
320
        $handler->push($history);
321
322
        $this
323
            // no headers
324
            ->given($http = new HttpClient(['handler' => $handler]))
325
                ->and($this->newTestedInstance(
326
                    $http
327
                ))
328
            ->then($this->testedInstance->get('/no-error'))
329
                ->and($headers = array_pop($historyContainer)['request']->getHeaders())
330
            ->array($headers)
331
                ->notHasKey('Accept-Language')
332
333
            // with headers
334
            ->given($http = new HttpClient(['handler' => $handler, 'headers' => ['Accept-Language' => 'fr']]))
335
                ->and($this->newTestedInstance(
336
                    $http
337
                ))
338
            ->then($this->testedInstance->get('/no-error'))
339
                ->and($headers = array_pop($historyContainer)['request']->getHeaders())
340
            ->array($headers)
341
                ->hasKey('Accept-Language')
342
        ;
343
    }
344
}
345