Completed
Pull Request — master (#29)
by Hugo
01:27
created

testRequestWithDifferentTypesOfArguments()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 9.392
c 0
b 0
f 0
cc 2
nc 1
nop 0
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\UnknownApiException;
19
use Yproximite\WannaSpeakBundle\Exception\Api\UnknownException;
20
use Yproximite\WannaSpeakBundle\Exception\Api\UnknownMethodException;
21
use Yproximite\WannaSpeakBundle\Exception\TestModeException;
22
23
class HttpClientTest extends TestCase
24
{
25
    use HttpClientTestTrait;
26
27
    public function testRequestInTestMode(): void
28
    {
29
        $this->expectException(TestModeException::class);
30
31
        $client = $this->createHttpClient(null, true);
32
33
        $client->request('the api', 'the method');
34
    }
35
36
    public function testRequestWithoutError(): void
37
    {
38
        $client = $this->createHttpClient(new MockResponse(
39
            (string) json_encode([
40
                'error' => null,
41
            ])
42
        ));
43
44
        $response = $client->request('the api', 'the method');
45
46
        static::assertSame(['error' => null], $response->toArray());
47
    }
48
49
    /**
50
     * This test is maybe useless, because WannaSpeak does not return a response with nb "200" when using JSON format...
51
     */
52
    public function testRequestWithCode200(): void
53
    {
54
        $client = $this->createHttpClient(new MockResponse(
55
            (string) json_encode([
56
                'error' => [
57
                    'nb' => 200,
58
                ],
59
            ])
60
        ));
61
62
        $response = $client->request('the api', 'the method');
63
64
        static::assertSame(['error' => ['nb' => 200]], $response->toArray());
65
    }
66
67 View Code Duplication
    public function testRequestWithCode401(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
68
    {
69
        $this->expectException(AuthFailedException::class);
70
71
        $client = $this->createHttpClient(new MockResponse(
72
            (string) json_encode([
73
                'error' => [
74
                    'nb'  => 401,
75
                    'txt' => 'Auth Failed',
76
                ],
77
            ])
78
        ));
79
80
        $client->request('the api', 'the method');
81
    }
82
83 View Code Duplication
    public function testRequestWithCode401SoundNameAlreadyExists(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
84
    {
85
        $this->expectException(SoundNameAlreadyExistsException::class);
86
87
        $client = $this->createHttpClient(new MockResponse(
88
            (string) json_encode([
89
                'error' => [
90
                    'nb'  => 401,
91
                    'txt' => 'Name already Exists',
92
                ],
93
            ])
94
        ));
95
96
        $client->request('the api', 'the method');
97
    }
98
99 View Code Duplication
    public function testRequestWithCode403(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
100
    {
101
        $this->expectException(BadAccountException::class);
102
103
        $client = $this->createHttpClient(new MockResponse(
104
            (string) json_encode([
105
                'error' => [
106
                    'nb'  => 403,
107
                    'txt' => 'Bad account ID or not Call tracking enabled',
108
                ],
109
            ])
110
        ));
111
112
        $client->request('the api', 'the method');
113
    }
114
115 View Code Duplication
    public function testRequestWithCode404(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
116
    {
117
        $this->expectException(UnknownMethodException::class);
118
119
        $client = $this->createHttpClient(new MockResponse(
120
            (string) json_encode([
121
                'error' => [
122
                    'nb'  => 404,
123
                    'txt' => 'Unknown method',
124
                ],
125
            ])
126
        ));
127
128
        $client->request('the api', 'the method');
129
    }
130
131 View Code Duplication
    public function testRequestWithCode405(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
132
    {
133
        $this->expectException(MethodNotImplementedException::class);
134
135
        $client = $this->createHttpClient(new MockResponse(
136
            (string) json_encode([
137
                'error' => [
138
                    'nb'  => 405,
139
                    'txt' => 'Method not yet implemented',
140
                ],
141
            ])
142
        ));
143
144
        $client->request('the api', 'the method');
145
    }
146
147 View Code Duplication
    public function testRequestWithCode406(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
148
    {
149
        $this->expectException(NoDidAvailableForRegionException::class);
150
151
        $client = $this->createHttpClient(new MockResponse(
152
            (string) json_encode([
153
                'error' => [
154
                    'nb'  => 406,
155
                    'txt' => 'NO DID AVAILABLE FOR THAT REGION',
156
                ],
157
            ])
158
        ));
159
160
        $client->request('the api', 'the method');
161
    }
162
163 View Code Duplication
    public function testRequestWithCode407DidAlreadyReserved(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
164
    {
165
        $this->expectException(DidAlreadyReservedException::class);
166
167
        $client = $this->createHttpClient(new MockResponse(
168
            (string) json_encode([
169
                'error' => [
170
                    'nb'  => 407,
171
                    'txt' => 'DID already reserved or not tested',
172
                ],
173
            ])
174
        ));
175
176
        $client->request('the api', 'the method');
177
    }
178
179 View Code Duplication
    public function testRequestWithCode407DidNotExists(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
180
    {
181
        $this->expectException(DidNotExistsOrNotOwnedException::class);
182
183
        $client = $this->createHttpClient(new MockResponse(
184
            (string) json_encode([
185
                'error' => [
186
                    'nb'  => 407,
187
                    'txt' => 'DID not exists or not owned',
188
                ],
189
            ])
190
        ));
191
192
        $client->request('the api', 'the method');
193
    }
194
195 View Code Duplication
    public function testRequestWithCode407Unknown(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
196
    {
197
        $this->expectException(UnknownException::class);
198
199
        $client = $this->createHttpClient(new MockResponse(
200
            (string) json_encode([
201
                'error' => [
202
                    'nb'  => 407,
203
                    'txt' => '???',
204
                ],
205
            ])
206
        ));
207
208
        $client->request('the api', 'the method');
209
    }
210
211 View Code Duplication
    public function testRequestWithCode410(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
212
    {
213
        $this->expectException(CantUseDidAsDestinationException::class);
214
215
        $client = $this->createHttpClient(new MockResponse(
216
            (string) json_encode([
217
                'error' => [
218
                    'nb'  => 410,
219
                    'txt' => "can't use DID as destination",
220
                ],
221
            ])
222
        ));
223
224
        $client->request('the api', 'the method');
225
    }
226
227 View Code Duplication
    public function testRequestWithCode500(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
228
    {
229
        $this->expectException(MissingArgumentsException::class);
230
231
        $client = $this->createHttpClient(new MockResponse(
232
            (string) json_encode([
233
                'error' => [
234
                    'nb'  => 500,
235
                    'txt' => 'Missing arguments (...)',
236
                ],
237
            ])
238
        ));
239
240
        $client->request('the api', 'the method');
241
    }
242
243 View Code Duplication
    public function testRequestWithCode501(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
244
    {
245
        $this->expectException(UnknownApiException::class);
246
247
        $client = $this->createHttpClient(new MockResponse(
248
            (string) json_encode([
249
                'error' => [
250
                    'nb'  => 501,
251
                    'txt' => 'Unknown API',
252
                ],
253
            ])
254
        ));
255
256
        $client->request('the api', 'the method');
257
    }
258
259 View Code Duplication
    public function testRequestWithUnknownCode(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
260
    {
261
        $this->expectException(UnknownException::class);
262
263
        $client = $this->createHttpClient(new MockResponse(
264
            (string) json_encode([
265
                'error' => [
266
                    'nb'  => 999,
267
                    'txt' => '???',
268
                ],
269
            ])
270
        ));
271
272
        $client->request('the api', 'the method');
273
    }
274
275
    public function testRequestWithDifferentTypesOfArguments(): void
276
    {
277
        $fieldsAllFound = false;
278
279
        $client = $this->createHttpClient(static function (string $method, string $url, array $options) use (&$fieldsAllFound): MockResponse {
280
            $body = '';
281
            while ('' !== $part = $options['body']()) {
282
                $body .= $part;
283
            }
284
285
            static::assertRegExp("/name=\"name\"[\r\n]+The name[\r\n]+--/", $body);
286
            static::assertRegExp("/name=\"tag1\"[\r\n]+12345[\r\n]+--/", $body);
287
            static::assertRegExp("/name=\"foo1\"[\r\n]+1[\r\n]+--/", $body);
288
            static::assertRegExp("/name=\"foo2\"[\r\n]+0[\r\n]+--/", $body);
289
290
            $fieldsAllFound = true;
291
292
            return new MockResponse(
293
                (string) json_encode([
294
                    'error' => null,
295
                ])
296
            );
297
        });
298
299
        $client->request('the api', 'the method', [
300
            'name' => 'The name',
301
            'tag1' => 12345,
302
            'foo1' => true,
303
            'foo2' => false,
304
        ]);
305
306
        static::assertTrue($fieldsAllFound, 'Fields "name", "tag1", "foo1", and "foo2" were not all founds in the request body.');
307
    }
308
}
309