Completed
Push — master ( b27094...afbc6d )
by Julien
05:50 queued 01:38
created

RestClient::testHttpHeaders()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 43
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

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