1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yproximite\WannaSpeakBundle\Tests; |
6
|
|
|
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
use Symfony\Component\HttpClient\Response\MockResponse; |
9
|
|
|
use Yproximite\WannaSpeakBundle\Exception\Api\AuthFailedException; |
10
|
|
|
use Yproximite\WannaSpeakBundle\Exception\Api\BadAccountException; |
11
|
|
|
use Yproximite\WannaSpeakBundle\Exception\Api\CantUseDidAsDestinationException; |
12
|
|
|
use Yproximite\WannaSpeakBundle\Exception\Api\DidAlreadyReservedException; |
13
|
|
|
use Yproximite\WannaSpeakBundle\Exception\Api\DidNotExistsOrNotOwnedException; |
14
|
|
|
use Yproximite\WannaSpeakBundle\Exception\Api\MethodNotImplementedException; |
15
|
|
|
use Yproximite\WannaSpeakBundle\Exception\Api\MissingArgumentsException; |
16
|
|
|
use Yproximite\WannaSpeakBundle\Exception\Api\NoDidAvailableForRegionException; |
17
|
|
|
use Yproximite\WannaSpeakBundle\Exception\Api\SoundNameAlreadyExistsException; |
18
|
|
|
use Yproximite\WannaSpeakBundle\Exception\Api\TimeErrorException; |
19
|
|
|
use Yproximite\WannaSpeakBundle\Exception\Api\UnknownApiException; |
20
|
|
|
use Yproximite\WannaSpeakBundle\Exception\Api\UnknownException; |
21
|
|
|
use Yproximite\WannaSpeakBundle\Exception\Api\UnknownMethodException; |
22
|
|
|
use Yproximite\WannaSpeakBundle\Exception\TestModeException; |
23
|
|
|
|
24
|
|
|
class HttpClientTest extends TestCase |
25
|
|
|
{ |
26
|
|
|
use HttpClientTestTrait; |
27
|
|
|
|
28
|
|
|
public function testRequestInTestMode(): void |
29
|
|
|
{ |
30
|
|
|
$this->expectException(TestModeException::class); |
31
|
|
|
|
32
|
|
|
$client = $this->createHttpClient(null, true); |
33
|
|
|
|
34
|
|
|
$client->request('the api', 'the method'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function testRequestWithoutError(): void |
38
|
|
|
{ |
39
|
|
|
$client = $this->createHttpClient(new MockResponse( |
40
|
|
|
(string) json_encode([ |
41
|
|
|
'error' => null, |
42
|
|
|
]) |
43
|
|
|
)); |
44
|
|
|
|
45
|
|
|
$response = $client->request('the api', 'the method'); |
46
|
|
|
|
47
|
|
|
static::assertSame(['error' => null], $response->toArray()); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* This test is maybe useless, because WannaSpeak does not return a response with nb "200" when using JSON format... |
52
|
|
|
*/ |
53
|
|
|
public function testRequestWithCode200(): void |
54
|
|
|
{ |
55
|
|
|
$client = $this->createHttpClient(new MockResponse( |
56
|
|
|
(string) json_encode([ |
57
|
|
|
'error' => [ |
58
|
|
|
'nb' => 200, |
59
|
|
|
], |
60
|
|
|
]) |
61
|
|
|
)); |
62
|
|
|
|
63
|
|
|
$response = $client->request('the api', 'the method'); |
64
|
|
|
|
65
|
|
|
static::assertSame(['error' => ['nb' => 200]], $response->toArray()); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
View Code Duplication |
public function testRequestWithCode401(): void |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
$this->expectException(AuthFailedException::class); |
71
|
|
|
|
72
|
|
|
$client = $this->createHttpClient(new MockResponse( |
73
|
|
|
(string) json_encode([ |
74
|
|
|
'error' => [ |
75
|
|
|
'nb' => 401, |
76
|
|
|
'txt' => 'Auth Failed', |
77
|
|
|
], |
78
|
|
|
]) |
79
|
|
|
)); |
80
|
|
|
|
81
|
|
|
$client->request('the api', 'the method'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
View Code Duplication |
public function testRequestWithCode401SoundNameAlreadyExists(): void |
|
|
|
|
85
|
|
|
{ |
86
|
|
|
$this->expectException(SoundNameAlreadyExistsException::class); |
87
|
|
|
|
88
|
|
|
$client = $this->createHttpClient(new MockResponse( |
89
|
|
|
(string) json_encode([ |
90
|
|
|
'error' => [ |
91
|
|
|
'nb' => 401, |
92
|
|
|
'txt' => 'Name already Exists', |
93
|
|
|
], |
94
|
|
|
]) |
95
|
|
|
)); |
96
|
|
|
|
97
|
|
|
$client->request('the api', 'the method'); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
View Code Duplication |
public function testRequestWithCode402TimeError(): void |
|
|
|
|
101
|
|
|
{ |
102
|
|
|
$this->expectException(TimeErrorException::class); |
103
|
|
|
|
104
|
|
|
$client = $this->createHttpClient(new MockResponse( |
105
|
|
|
(string) json_encode([ |
106
|
|
|
'error' => [ |
107
|
|
|
'nb' => 402, |
108
|
|
|
'txt' => 'Time error, more than UnixTimestamp NOW() 1618306219 +- 10s', |
109
|
|
|
], |
110
|
|
|
]) |
111
|
|
|
)); |
112
|
|
|
|
113
|
|
|
$client->request('the api', 'the method'); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
View Code Duplication |
public function testRequestWithCode403(): void |
|
|
|
|
117
|
|
|
{ |
118
|
|
|
$this->expectException(BadAccountException::class); |
119
|
|
|
|
120
|
|
|
$client = $this->createHttpClient(new MockResponse( |
121
|
|
|
(string) json_encode([ |
122
|
|
|
'error' => [ |
123
|
|
|
'nb' => 403, |
124
|
|
|
'txt' => 'Bad account ID or not Call tracking enabled', |
125
|
|
|
], |
126
|
|
|
]) |
127
|
|
|
)); |
128
|
|
|
|
129
|
|
|
$client->request('the api', 'the method'); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
View Code Duplication |
public function testRequestWithCode404(): void |
|
|
|
|
133
|
|
|
{ |
134
|
|
|
$this->expectException(UnknownMethodException::class); |
135
|
|
|
|
136
|
|
|
$client = $this->createHttpClient(new MockResponse( |
137
|
|
|
(string) json_encode([ |
138
|
|
|
'error' => [ |
139
|
|
|
'nb' => 404, |
140
|
|
|
'txt' => 'Unknown method', |
141
|
|
|
], |
142
|
|
|
]) |
143
|
|
|
)); |
144
|
|
|
|
145
|
|
|
$client->request('the api', 'the method'); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
View Code Duplication |
public function testRequestWithCode405(): void |
|
|
|
|
149
|
|
|
{ |
150
|
|
|
$this->expectException(MethodNotImplementedException::class); |
151
|
|
|
|
152
|
|
|
$client = $this->createHttpClient(new MockResponse( |
153
|
|
|
(string) json_encode([ |
154
|
|
|
'error' => [ |
155
|
|
|
'nb' => 405, |
156
|
|
|
'txt' => 'Method not yet implemented', |
157
|
|
|
], |
158
|
|
|
]) |
159
|
|
|
)); |
160
|
|
|
|
161
|
|
|
$client->request('the api', 'the method'); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
View Code Duplication |
public function testRequestWithCode406(): void |
|
|
|
|
165
|
|
|
{ |
166
|
|
|
$this->expectException(NoDidAvailableForRegionException::class); |
167
|
|
|
|
168
|
|
|
$client = $this->createHttpClient(new MockResponse( |
169
|
|
|
(string) json_encode([ |
170
|
|
|
'error' => [ |
171
|
|
|
'nb' => 406, |
172
|
|
|
'txt' => 'NO DID AVAILABLE FOR THAT REGION', |
173
|
|
|
], |
174
|
|
|
]) |
175
|
|
|
)); |
176
|
|
|
|
177
|
|
|
$client->request('the api', 'the method'); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
View Code Duplication |
public function testRequestWithCode407DidAlreadyReserved(): void |
|
|
|
|
181
|
|
|
{ |
182
|
|
|
$this->expectException(DidAlreadyReservedException::class); |
183
|
|
|
|
184
|
|
|
$client = $this->createHttpClient(new MockResponse( |
185
|
|
|
(string) json_encode([ |
186
|
|
|
'error' => [ |
187
|
|
|
'nb' => 407, |
188
|
|
|
'txt' => 'DID already reserved or not tested', |
189
|
|
|
], |
190
|
|
|
]) |
191
|
|
|
)); |
192
|
|
|
|
193
|
|
|
$client->request('the api', 'the method'); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
View Code Duplication |
public function testRequestWithCode407DidNotExists(): void |
|
|
|
|
197
|
|
|
{ |
198
|
|
|
$this->expectException(DidNotExistsOrNotOwnedException::class); |
199
|
|
|
|
200
|
|
|
$client = $this->createHttpClient(new MockResponse( |
201
|
|
|
(string) json_encode([ |
202
|
|
|
'error' => [ |
203
|
|
|
'nb' => 407, |
204
|
|
|
'txt' => 'DID not exists or not owned', |
205
|
|
|
], |
206
|
|
|
]) |
207
|
|
|
)); |
208
|
|
|
|
209
|
|
|
$client->request('the api', 'the method'); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
View Code Duplication |
public function testRequestWithCode407Unknown(): void |
|
|
|
|
213
|
|
|
{ |
214
|
|
|
$this->expectException(UnknownException::class); |
215
|
|
|
|
216
|
|
|
$client = $this->createHttpClient(new MockResponse( |
217
|
|
|
(string) json_encode([ |
218
|
|
|
'error' => [ |
219
|
|
|
'nb' => 407, |
220
|
|
|
'txt' => '???', |
221
|
|
|
], |
222
|
|
|
]) |
223
|
|
|
)); |
224
|
|
|
|
225
|
|
|
$client->request('the api', 'the method'); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
View Code Duplication |
public function testRequestWithCode410(): void |
|
|
|
|
229
|
|
|
{ |
230
|
|
|
$this->expectException(CantUseDidAsDestinationException::class); |
231
|
|
|
|
232
|
|
|
$client = $this->createHttpClient(new MockResponse( |
233
|
|
|
(string) json_encode([ |
234
|
|
|
'error' => [ |
235
|
|
|
'nb' => 410, |
236
|
|
|
'txt' => "can't use DID as destination", |
237
|
|
|
], |
238
|
|
|
]) |
239
|
|
|
)); |
240
|
|
|
|
241
|
|
|
$client->request('the api', 'the method'); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
View Code Duplication |
public function testRequestWithCode500(): void |
|
|
|
|
245
|
|
|
{ |
246
|
|
|
$this->expectException(MissingArgumentsException::class); |
247
|
|
|
|
248
|
|
|
$client = $this->createHttpClient(new MockResponse( |
249
|
|
|
(string) json_encode([ |
250
|
|
|
'error' => [ |
251
|
|
|
'nb' => 500, |
252
|
|
|
'txt' => 'Missing arguments (...)', |
253
|
|
|
], |
254
|
|
|
]) |
255
|
|
|
)); |
256
|
|
|
|
257
|
|
|
$client->request('the api', 'the method'); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
View Code Duplication |
public function testRequestWithCode501(): void |
|
|
|
|
261
|
|
|
{ |
262
|
|
|
$this->expectException(UnknownApiException::class); |
263
|
|
|
|
264
|
|
|
$client = $this->createHttpClient(new MockResponse( |
265
|
|
|
(string) json_encode([ |
266
|
|
|
'error' => [ |
267
|
|
|
'nb' => 501, |
268
|
|
|
'txt' => 'Unknown API', |
269
|
|
|
], |
270
|
|
|
]) |
271
|
|
|
)); |
272
|
|
|
|
273
|
|
|
$client->request('the api', 'the method'); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
View Code Duplication |
public function testRequestWithUnknownCode(): void |
|
|
|
|
277
|
|
|
{ |
278
|
|
|
$this->expectException(UnknownException::class); |
279
|
|
|
|
280
|
|
|
$client = $this->createHttpClient(new MockResponse( |
281
|
|
|
(string) json_encode([ |
282
|
|
|
'error' => [ |
283
|
|
|
'nb' => 999, |
284
|
|
|
'txt' => '???', |
285
|
|
|
], |
286
|
|
|
]) |
287
|
|
|
)); |
288
|
|
|
|
289
|
|
|
$client->request('the api', 'the method'); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
public function testRequestWithDifferentTypesOfArguments(): void |
293
|
|
|
{ |
294
|
|
|
$fieldsAllFound = false; |
295
|
|
|
|
296
|
|
|
$client = $this->createHttpClient(static function (string $method, string $url, array $options) use (&$fieldsAllFound): MockResponse { |
297
|
|
|
static::assertSame('9999999999', $options['query']['id']); |
298
|
|
|
static::assertRegExp('#^\d{10}-[a-z0-9]{32}$#', $options['query']['key']); |
299
|
|
|
static::assertSame('the api', $options['query']['api']); |
300
|
|
|
static::assertSame('the method', $options['query']['method']); |
301
|
|
|
static::assertSame('The name', $options['query']['query_name']); |
302
|
|
|
static::assertSame('12345', $options['query']['query_tag1']); |
303
|
|
|
static::assertSame('1', $options['query']['query_foo1']); |
304
|
|
|
static::assertSame('0', $options['query']['query_foo2']); |
305
|
|
|
|
306
|
|
|
$body = ''; |
307
|
|
|
while ('' !== $part = $options['body']()) { |
308
|
|
|
$body .= $part; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
static::assertRegExp("/name=\"body_name\"[\r\n]+The name[\r\n]+--/", $body); |
312
|
|
|
static::assertRegExp("/name=\"body_tag1\"[\r\n]+12345[\r\n]+--/", $body); |
313
|
|
|
static::assertRegExp("/name=\"body_foo1\"[\r\n]+1[\r\n]+--/", $body); |
314
|
|
|
static::assertRegExp("/name=\"body_foo2\"[\r\n]+0[\r\n]+--/", $body); |
315
|
|
|
|
316
|
|
|
$fieldsAllFound = true; |
317
|
|
|
|
318
|
|
|
return new MockResponse( |
319
|
|
|
(string) json_encode([ |
320
|
|
|
'error' => null, |
321
|
|
|
]) |
322
|
|
|
); |
323
|
|
|
}); |
324
|
|
|
|
325
|
|
|
$client->request( |
326
|
|
|
'the api', |
327
|
|
|
'the method', |
328
|
|
|
[ |
329
|
|
|
'query_name' => 'The name', |
330
|
|
|
'query_tag1' => 12345, |
331
|
|
|
'query_foo1' => true, |
332
|
|
|
'query_foo2' => false, |
333
|
|
|
], |
334
|
|
|
[ |
335
|
|
|
'body_name' => 'The name', |
336
|
|
|
'body_tag1' => 12345, |
337
|
|
|
'body_foo1' => true, |
338
|
|
|
'body_foo2' => false, |
339
|
|
|
] |
340
|
|
|
); |
341
|
|
|
|
342
|
|
|
static::assertTrue($fieldsAllFound, 'Fields "name", "tag1", "foo1", and "foo2" were not all founds in the request body.'); |
343
|
|
|
} |
344
|
|
|
} |
345
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.