Passed
Push — main ( f3cfb1...17781b )
by Emil
04:34
created

JasonControllerTest::testApiLibraryBookISBN()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 60
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 26
nc 8
nop 0
dl 0
loc 60
rs 9.504
c 0
b 0
f 0

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
namespace App\Tests\Controller;
4
5
use App\Repository\BookRepository;
6
use Doctrine\Persistence\ManagerRegistry;
7
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
8
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
9
use Symfony\Component\Routing\RouterInterface;
10
11
class JasonControllerTest extends WebTestCase
12
{
13
    /**
14
     * testApi
15
     *
16
     * Test api route
17
     *
18
     * @return void
19
     */
20
    public function testApi(): void
21
    {
22
        // Create a client to browse the application
23
        $client = static::createClient();
24
25
        // Retrieve router service
26
        /** @var RouterInterface $router */
27
        $router = static::getContainer()->get('router');
28
29
        // Generate URL from route name
30
        $url = $router->generate('api');
31
32
        // Send a GET request to the route
33
        $crawler = $client->request('GET', $url);
34
35
        // $response = $client->getResponse();
36
        // echo $response->getContent();
37
38
        // Assert the response status code
39
        $this->assertEquals(200, $client->getResponse()->getStatusCode());
40
41
        // Assert that certain content exists in the response
42
        $this->assertStringContainsString('MVC: Api', $crawler->filter('title')->text());
43
    }
44
45
    /**
46
     * testApiQuote
47
     *
48
     * Test api/quote route
49
     *
50
     * @return void
51
     */
52
    public function testApiQuote(): void
53
    {
54
        // Create a client to browse the application
55
        $client = static::createClient();
56
57
        // Retrieve router service
58
        /** @var RouterInterface $router */
59
        $router = static::getContainer()->get('router');
60
61
        // Generate URL from route name
62
        $url = $router->generate('api/quote');
63
64
        // Send a GET request to the route
65
        $client->request('GET', $url);
66
67
        // Assert response status
68
        $this->assertEquals(200, $client->getResponse()->getStatusCode());
69
70
        // Get the response content
71
        $content = $client->getResponse()->getContent();
72
73
        $data = null;
74
75
        // Decode JSON
76
        if (is_string($content)) {
77
            $data = json_decode($content, true);
78
        }
79
80
        // Assert that JSON decoding was successful
81
        $this->assertNotNull($data, 'Failed to decode JSON.');
82
        $this->assertIsArray($data, 'Decoded JSON data is not an array.');
83
84
        // Verify that required keys exist
85
        $this->assertArrayHasKey('quote', $data);
86
        $this->assertArrayHasKey('author', $data);
87
        $this->assertArrayHasKey('timestamp', $data);
88
89
        // Assert that 'quote' and 'author' are not empty
90
        $this->assertNotEmpty($data['quote']);
91
        $this->assertNotEmpty($data['author']);
92
    }
93
94
    /**
95
     * testApiDeck
96
     *
97
     * Test api/deck route
98
     *
99
     * @return void
100
     */
101
    public function testApiDeck(): void
102
    {
103
        // Create a client to browse the application
104
        $client = static::createClient();
105
106
        // Retrieve router service
107
        /** @var RouterInterface $router */
108
        $router = static::getContainer()->get('router');
109
110
        // Generate URL from route name
111
        $url = $router->generate('api/deck');
112
113
        // Send a GET request to the route
114
        $client->request('GET', $url);
115
116
        // Assert response status
117
        $this->assertEquals(200, $client->getResponse()->getStatusCode());
118
119
        // Get the response content
120
        $content = $client->getResponse()->getContent();
121
122
        $data = null;
123
124
        // Decode JSON
125
        if (is_string($content)) {
126
            $data = json_decode($content, true);
127
        }
128
129
        // Assert that JSON decoding was successful
130
        $this->assertNotNull($data, 'Failed to decode JSON.');
131
        $this->assertIsArray($data, 'Decoded JSON data is not an array.');
132
133
        // Verify that required keys exist
134
        $this->assertArrayHasKey('deck', $data);
135
136
        // Assert the deck is an array
137
        $this->assertIsArray($data['deck']);
138
    }
139
140
    /**
141
     * testApiDeckShuffle
142
     *
143
     * Test api/deck/shuffle route
144
     *
145
     * @return void
146
     */
147
    public function testApiDeckShuffle(): void
148
    {
149
        // Create a client to browse the application
150
        $client = static::createClient();
151
152
        // Retrieve router service
153
        /** @var RouterInterface $router */
154
        $router = static::getContainer()->get('router');
155
156
        // Generate URL from route name
157
        $url = $router->generate('api/deck/shuffle');
158
159
        // Send a POST request to the route
160
        $client->request('POST', $url);
161
162
        // Get the response content
163
        $content = $client->getResponse()->getContent();
164
165
        $data = null;
166
167
        // Decode JSON
168
        if (is_string($content)) {
169
            $data = json_decode($content, true);
170
        }
171
172
        // Assert that JSON decoding was successful
173
        $this->assertNotNull($data, 'Failed to decode JSON.');
174
        $this->assertIsArray($data, 'Decoded JSON data is not an array.');
175
176
        // Verify that required keys exist
177
        $this->assertArrayHasKey('deck', $data);
178
179
        // Assert the deck is an array
180
        $this->assertIsArray($data['deck']);
181
    }
182
183
    /**
184
     * testApiDeckDraw
185
     *
186
     * Test api/deck/draw route
187
     *
188
     * @return void
189
     */
190
    public function testApiDeckDraw(): void
191
    {
192
        // Create a client to browse the application
193
        $client = static::createClient();
194
195
        // Retrieve router service
196
        /** @var RouterInterface $router */
197
        $router = static::getContainer()->get('router');
198
199
        // Generate URL from route name
200
        $url = $router->generate('api/deck/draw');
201
202
        // Send a POST request to the route
203
        $client->request('POST', $url);
204
205
        // Get the response content
206
        $content = $client->getResponse()->getContent();
207
208
        $data = null;
209
210
        // Decode JSON
211
        if (is_string($content)) {
212
            $data = json_decode($content, true);
213
        }
214
215
        // Assert that JSON decoding was successful
216
        $this->assertNotNull($data, 'Failed to decode JSON.');
217
        $this->assertIsArray($data, 'Decoded JSON data is not an array.');
218
219
        // Verify that required keys exist
220
        $this->assertArrayHasKey('hand', $data);
221
        $this->assertArrayHasKey('deckCount', $data);
222
223
        // Assert the hand is an array
224
        $this->assertIsArray($data['hand']);
225
226
        // Assert the deckCount is not empty
227
        $this->assertNotEmpty($data['deckCount']);
228
    }
229
230
    /**
231
     * testApiDeckDrawMany
232
     *
233
     * Test api/deck/draw/:number route
234
     *
235
     * @return void
236
     */
237
    public function testApiDeckDrawMany(): void
238
    {
239
        // Create a client to browse the application
240
        $client = static::createClient();
241
242
        // Retrieve router service
243
        /** @var RouterInterface $router */
244
        $router = static::getContainer()->get('router');
245
246
        // Generate URL from route name
247
        $url = $router->generate('api/deck/draw/:number', ['num' => 5]);
248
249
        // Send a POST request to the route
250
        $client->request('POST', $url);
251
252
        // Get the response content
253
        $content = $client->getResponse()->getContent();
254
255
        $data = null;
256
257
        // Decode JSON
258
        if (is_string($content)) {
259
            $data = json_decode($content, true);
260
        }
261
262
        // Assert that JSON decoding was successful
263
        $this->assertNotNull($data, 'Failed to decode JSON.');
264
        $this->assertIsArray($data, 'Decoded JSON data is not an array.');
265
266
        // Verify that required keys exist
267
        $this->assertArrayHasKey('hand', $data);
268
        $this->assertArrayHasKey('deckCount', $data);
269
270
        // Assert the hand is an array
271
        $this->assertIsArray($data['hand']);
272
273
        // Assert the deckCount is not empty
274
        $this->assertNotEmpty($data['deckCount']);
275
    }
276
277
    /**
278
     * testApiBlackJack
279
     *
280
     * Test api/game route
281
     *
282
     * @return void
283
     */
284
    public function testApiBlackJack(): void
285
    {
286
        // Create a client to browse the application
287
        $client = static::createClient();
288
289
        // Retrieve router service
290
        /** @var RouterInterface $router */
291
        $router = static::getContainer()->get('router');
292
293
        // Generate URL from route name
294
        $url = $router->generate('api/game');
295
296
        // Send a GET request to the route
297
        $client->request('GET', $url);
298
299
        // Get the response content
300
        $content = $client->getResponse()->getContent();
301
302
        $data = null;
303
304
        // Decode JSON
305
        if (is_string($content)) {
306
            $data = json_decode($content, true);
307
        }
308
309
        // Assert that JSON decoding was successful
310
        $this->assertNotNull($data, 'Failed to decode JSON.');
311
        $this->assertIsArray($data, 'Decoded JSON data is not an array.');
312
313
        // Verify that required keys exist
314
        $this->assertArrayHasKey('numOfPlayers', $data);
315
        $this->assertArrayHasKey('playersCards', $data);
316
        $this->assertArrayHasKey('playersHandValue', $data);
317
        $this->assertArrayHasKey('dealerCards', $data);
318
        $this->assertArrayHasKey('dealerHandValue', $data);
319
        $this->assertArrayHasKey('gameStates', $data);
320
321
        // Assert is an array
322
        $this->assertIsArray($data['playersCards']);
323
        $this->assertIsArray($data['playersCards'][0]);
324
        $this->assertIsArray($data['playersHandValue']);
325
        $this->assertIsArray($data['dealerCards']);
326
        $this->assertIsArray($data['gameStates']);
327
328
        // Assert is not empty
329
        $this->assertNotNull($data['numOfPlayers']);
330
        $this->assertNotEmpty($data['playersCards'][0]);
331
        $this->assertNotNull($data['playersHandValue'][0]);
332
        $this->assertNotEmpty($data['dealerCards']);
333
        $this->assertNotNull($data['dealerHandValue']);
334
        $this->assertNotEmpty($data['gameStates']);
335
        $this->assertNotNull($data['gameStates'][0]);
336
    }
337
338
    /**
339
     * testApiLibraryBooks
340
     *
341
     * Test api/library/books route
342
     *
343
     * @return void
344
     */
345
    public function testApiLibraryBooks(): void
346
    {
347
        // Create a client to browse the application
348
        $client = static::createClient();
349
350
        // Retrieve router service
351
        /** @var RouterInterface $router */
352
        $router = static::getContainer()->get('router');
353
354
        // Generate URL from route name
355
        $url = $router->generate('api/library/books');
356
357
        // Send a GET request to the route
358
        $client->request('GET', $url);
359
360
        // Assert the response status code
361
        $this->assertEquals(200, $client->getResponse()->getStatusCode());
362
363
        // Get the response content
364
        $content = $client->getResponse()->getContent();
365
366
        $data = null;
367
368
        // Decode JSON
369
        if (is_string($content)) {
370
            $data = json_decode($content, true);
371
        }
372
373
        // Assert that JSON decoding was successful
374
        $this->assertNotNull($data, 'Failed to decode JSON.');
375
        $this->assertIsArray($data, 'Decoded JSON data is not an array.');
376
377
        // Verify that required keys exist
378
        $this->assertArrayHasKey('books', $data);
379
    }
380
381
    /**
382
     * testApiLibraryBookISBN
383
     *
384
     * Test api/library/book/isbn route
385
     *
386
     * @return void
387
     */
388
    public function testApiLibraryBookISBN(): void
389
    {
390
        // Create a client to browse the application
391
        $client = static::createClient();
392
393
        // Retrieve router service
394
        /** @var RouterInterface $router */
395
        $router = static::getContainer()->get('router');
396
397
        // Boot the kernel
398
        $kernel = self::bootKernel();
399
400
        // Get the ManagerRegistry service from container
401
        /** @var ManagerRegistry $doctrine */
402
        $doctrine = $kernel->getContainer()->get('doctrine');
403
        $bookRepository = new BookRepository($doctrine);
404
405
        $data = $bookRepository->readAllBooks();
406
407
        //If there is books in the library
408
        $isbn = null;
409
410
        if (count($data['books']) > 0) {
411
            $isbn = $data['books'][0]['isbn'];
412
        }
413
414
        if (null === $isbn) {
415
            $isbn = '0';
416
        }
417
418
        // Generate URL from route name
419
        $url = $router->generate('api/library/book/isbn', ['isbn' => $isbn]);
420
421
        // Send a GET request to the route
422
        $client->request('GET', $url);
423
424
        // Assert the response status code
425
        $this->assertEquals(200, $client->getResponse()->getStatusCode());
426
427
        // Get the response content
428
        $content = $client->getResponse()->getContent();
429
430
        $data = null;
431
432
        // Decode JSON
433
        if (is_string($content)) {
434
            $data = json_decode($content, true);
435
        }
436
437
        // Assert that JSON decoding was successful
438
        $this->assertNotNull($data, 'Failed to decode JSON.');
439
        $this->assertIsArray($data, 'Decoded JSON data is not an array.');
440
441
        // Verify that required keys exist
442
        $this->assertArrayHasKey('book', $data);
443
        $this->assertArrayHasKey('id', $data['book']);
444
        $this->assertArrayHasKey('title', $data['book']);
445
        $this->assertArrayHasKey('isbn', $data['book']);
446
        $this->assertArrayHasKey('author', $data['book']);
447
        $this->assertArrayHasKey('img', $data['book']);
448
    }
449
}
450