1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Tests\Controller; |
4
|
|
|
|
5
|
|
|
use App\Repository\BookRepository; |
6
|
|
|
use Doctrine\Persistence\ManagerRegistry; |
7
|
|
|
use Symfony\Bundle\FrameworkBundle\KernelBrowser; |
8
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
9
|
|
|
use Symfony\Component\HttpFoundation\Request; |
10
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
11
|
|
|
use Symfony\Component\HttpFoundation\Session\Session; |
12
|
|
|
use Symfony\Component\Routing\RouterInterface; |
13
|
|
|
|
14
|
|
|
class BookControllerTest extends WebTestCase |
15
|
|
|
{ |
16
|
|
|
private KernelBrowser $client; |
17
|
|
|
private RouterInterface $router; |
18
|
|
|
private ManagerRegistry $doctrine; |
19
|
|
|
private ?int $createdBookId = null; // Store created book ID |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* setUp |
23
|
|
|
* |
24
|
|
|
* @return void |
25
|
|
|
*/ |
26
|
|
|
protected function setUp(): void |
27
|
|
|
{ |
28
|
|
|
parent::setUp(); |
29
|
|
|
|
30
|
|
|
// Retrieve the client |
31
|
|
|
$this->client = static::createClient(); |
32
|
|
|
|
33
|
|
|
// Retrieve the container |
34
|
|
|
$container = $this->client->getContainer(); |
35
|
|
|
|
36
|
|
|
// Retrieve router service |
37
|
|
|
// @phpstan-ignore-next-line |
38
|
|
|
$this->router = $container->get('router'); |
39
|
|
|
|
40
|
|
|
// Get the ManagerRegistry service from container |
41
|
|
|
// @phpstan-ignore-next-line |
42
|
|
|
$this->doctrine = $container->get('doctrine'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* tearDown |
47
|
|
|
* |
48
|
|
|
* @return void |
49
|
|
|
*/ |
50
|
|
|
protected function tearDown(): void |
51
|
|
|
{ |
52
|
|
|
parent::tearDown(); |
53
|
|
|
|
54
|
|
|
if ($this->createdBookId !== null) { |
55
|
|
|
// Generate delete URL |
56
|
|
|
$deleteUrl = $this->router->generate('book_delete_post', ['id' => $this->createdBookId]); |
57
|
|
|
|
58
|
|
|
// Send delete request |
59
|
|
|
$this->client->request('POST', $deleteUrl, ['id' => $this->createdBookId]); |
60
|
|
|
|
61
|
|
|
// Reset the ID to prevent repeated deletions |
62
|
|
|
$this->createdBookId = null; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* testBookCreatePOST |
68
|
|
|
* |
69
|
|
|
* Test book_create_post route |
70
|
|
|
* |
71
|
|
|
* @return void |
72
|
|
|
*/ |
73
|
|
|
private function testBookCreatePOST(): void |
74
|
|
|
{ |
75
|
|
|
$bookRepository = new BookRepository($this->doctrine); |
76
|
|
|
|
77
|
|
|
// book_create_post |
78
|
|
|
|
79
|
|
|
// Generate URL from route name |
80
|
|
|
$url = $this->router->generate('book_create_post'); |
81
|
|
|
|
82
|
|
|
// Create a mock UploadedFile |
83
|
|
|
$file = $this->createMock(UploadedFile::class); |
84
|
|
|
$file->method('isValid')->willReturn(true); |
85
|
|
|
$file->method('getMimeType')->willReturn('image/jpeg'); |
86
|
|
|
$file->method('getClientOriginalName')->willReturn('test_image.jpg'); |
87
|
|
|
$file->method('guessExtension')->willReturn('jpg'); |
88
|
|
|
|
89
|
|
|
$formData = [ |
90
|
|
|
'id' => 0, |
91
|
|
|
'title' => 'test book', |
92
|
|
|
'isbn' => '0000000000000', |
93
|
|
|
'author' => 'test author', |
94
|
|
|
'img' => $file, |
95
|
|
|
]; |
96
|
|
|
|
97
|
|
|
// Send POST request to the route |
98
|
|
|
$this->client->request('POST', $url, $formData); |
99
|
|
|
|
100
|
|
|
// Assert that response is a redirect (302 status) |
101
|
|
|
$response = $this->client->getResponse(); |
102
|
|
|
$this->assertTrue($response->isRedirect()); |
103
|
|
|
|
104
|
|
|
$data = $bookRepository->readOneBookISBN('0000000000000'); |
105
|
|
|
|
106
|
|
|
$this->assertEquals('test book', $data['book']['title']); |
107
|
|
|
$this->assertEquals('0000000000000', $data['book']['isbn']); |
108
|
|
|
$this->assertEquals('test author', $data['book']['author']); |
109
|
|
|
|
110
|
|
|
// After creating the book, store its ID |
111
|
|
|
$this->createdBookId = $data['book']['id']; |
112
|
|
|
|
113
|
|
|
// Test if title is null |
114
|
|
|
|
115
|
|
|
$formData = [ |
116
|
|
|
'id' => 0, |
117
|
|
|
'title' => '', |
118
|
|
|
'isbn' => '000000000001', |
119
|
|
|
'author' => 'test author', |
120
|
|
|
'img' => $file, |
121
|
|
|
]; |
122
|
|
|
|
123
|
|
|
// Send POST request to the route |
124
|
|
|
$this->client->request('POST', $url, $formData); |
125
|
|
|
|
126
|
|
|
$data = $bookRepository->readOneBookISBN('0000000000001'); |
127
|
|
|
|
128
|
|
|
$this->assertNull($data['book']['author']); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* testBookUpdatePOST |
133
|
|
|
* |
134
|
|
|
* Test book_update_post route |
135
|
|
|
* |
136
|
|
|
* @return void |
137
|
|
|
*/ |
138
|
|
|
private function testBookUpdatePOST(): void |
139
|
|
|
{ |
140
|
|
|
$bookRepository = new BookRepository($this->doctrine); |
141
|
|
|
|
142
|
|
|
// Create a mock UploadedFile |
143
|
|
|
$file = $this->createMock(UploadedFile::class); |
144
|
|
|
$file->method('isValid')->willReturn(true); |
145
|
|
|
$file->method('getMimeType')->willReturn('image/jpeg'); |
146
|
|
|
$file->method('getClientOriginalName')->willReturn('test_image1.jpg'); |
147
|
|
|
$file->method('guessExtension')->willReturn('jpg'); |
148
|
|
|
|
149
|
|
|
$formData = [ |
150
|
|
|
'id' => $this->createdBookId, |
151
|
|
|
'title' => 'test book 2', |
152
|
|
|
'isbn' => '0000000000001', |
153
|
|
|
'author' => 'test author 2', |
154
|
|
|
'img' => $file, |
155
|
|
|
]; |
156
|
|
|
|
157
|
|
|
// Generate URL from route name |
158
|
|
|
$url = $this->router->generate('book_update_post', ['id' => (int) $this->createdBookId]); |
159
|
|
|
|
160
|
|
|
// Send POST request to the route |
161
|
|
|
$this->client->request('POST', $url, $formData); |
162
|
|
|
|
163
|
|
|
// Assert that response is a redirect (302 status) |
164
|
|
|
$response = $this->client->getResponse(); |
165
|
|
|
$this->assertTrue($response->isRedirect()); |
166
|
|
|
|
167
|
|
|
$data = $bookRepository->readOneBook((int) $this->createdBookId); |
168
|
|
|
|
169
|
|
|
$this->assertEquals('test book 2', $data['book']['title']); |
170
|
|
|
|
171
|
|
|
//Test if book don't exist |
172
|
|
|
$data = $bookRepository->readAllBooks(); |
173
|
|
|
|
174
|
|
|
// Extract all existing IDs |
175
|
|
|
$existingIds = array_map(function ($book) { |
176
|
|
|
return $book['id']; |
177
|
|
|
}, $data['books']); |
178
|
|
|
|
179
|
|
|
// Find the smallest missing integer starting from 1 |
180
|
|
|
$missingId = null; |
181
|
|
|
for ($i = 1; ; $i++) { |
182
|
|
|
if (!in_array($i, $existingIds, true)) { |
183
|
|
|
$missingId = $i; |
184
|
|
|
break; |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
$formData = [ |
189
|
|
|
'id' => (int) $missingId, |
190
|
|
|
'title' => 'test book 1', |
191
|
|
|
'isbn' => '0000000000000', |
192
|
|
|
'author' => 'test author 1', |
193
|
|
|
'img' => $file, |
194
|
|
|
]; |
195
|
|
|
|
196
|
|
|
// Generate URL from route name |
197
|
|
|
$url = $this->router->generate('book_update_post', ['id' => (int) $missingId]); |
198
|
|
|
|
199
|
|
|
// Send POST request to the route |
200
|
|
|
$this->client->request('POST', $url, $formData); |
201
|
|
|
|
202
|
|
|
// Assert that response is a redirect (302 status) |
203
|
|
|
$response = $this->client->getResponse(); |
204
|
|
|
$this->assertTrue($response->isRedirect()); |
205
|
|
|
|
206
|
|
|
$data = $bookRepository->readOneBook((int) $missingId); |
207
|
|
|
|
208
|
|
|
$this->assertNull($data['book']['author']); |
209
|
|
|
|
210
|
|
|
// Test if title is null |
211
|
|
|
$formData = [ |
212
|
|
|
'id' => $this->createdBookId, |
213
|
|
|
'title' => '', |
214
|
|
|
'isbn' => '0000000000000', |
215
|
|
|
'author' => 'test author 1', |
216
|
|
|
'img' => $file, |
217
|
|
|
]; |
218
|
|
|
|
219
|
|
|
// Generate URL from route name |
220
|
|
|
$url = $this->router->generate('book_update_post', ['id' => (int) $this->createdBookId]); |
221
|
|
|
|
222
|
|
|
// Send POST request to the route |
223
|
|
|
$this->client->request('POST', $url, $formData); |
224
|
|
|
|
225
|
|
|
// Assert that response is a redirect (302 status) |
226
|
|
|
$response = $this->client->getResponse(); |
227
|
|
|
$this->assertTrue($response->isRedirect()); |
228
|
|
|
|
229
|
|
|
$data = $bookRepository->readOneBook((int) $this->createdBookId); |
230
|
|
|
|
231
|
|
|
$this->assertEquals('test book 2', $data['book']['title']); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* testBookDeletePOST |
236
|
|
|
* |
237
|
|
|
* Test book_delete_post route |
238
|
|
|
* |
239
|
|
|
* @return void |
240
|
|
|
*/ |
241
|
|
|
private function testBookDeletePOST(): void |
242
|
|
|
{ |
243
|
|
|
$formData = [ |
244
|
|
|
'id' => (int) $this->createdBookId, |
245
|
|
|
]; |
246
|
|
|
|
247
|
|
|
//book_delete_post |
248
|
|
|
|
249
|
|
|
// Generate URL from route name |
250
|
|
|
$url = $this->router->generate('book_delete_post', ['id' => (int)$this->createdBookId]); |
251
|
|
|
|
252
|
|
|
// Send POST request to the route |
253
|
|
|
$this->client->request('POST', $url, $formData); |
254
|
|
|
|
255
|
|
|
// Assert that response is a redirect (302 status) |
256
|
|
|
$response = $this->client->getResponse(); |
257
|
|
|
$this->assertTrue($response->isRedirect()); |
258
|
|
|
|
259
|
|
|
// Reset the ID to prevent repeated deletions |
260
|
|
|
$this->createdBookId = null; |
261
|
|
|
|
262
|
|
|
//Test to delete book that don't exists |
263
|
|
|
|
264
|
|
|
// Send POST request to the route |
265
|
|
|
$this->client->request('POST', $url, $formData); |
266
|
|
|
|
267
|
|
|
// Assert that response is a redirect (302 status) |
268
|
|
|
$response = $this->client->getResponse(); |
269
|
|
|
$this->assertTrue($response->isRedirect()); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* testCRUD |
274
|
|
|
* |
275
|
|
|
* @return void |
276
|
|
|
*/ |
277
|
|
|
public function testCRUD(): void |
278
|
|
|
{ |
279
|
|
|
// Test Create Book |
280
|
|
|
$this->testBookCreatePOST(); |
281
|
|
|
|
282
|
|
|
// Test Update Book |
283
|
|
|
$this->testBookUpdatePOST(); |
284
|
|
|
|
285
|
|
|
// Test Delete Book |
286
|
|
|
$this->testBookDeletePOST(); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* testLibrary |
291
|
|
|
* |
292
|
|
|
* Test library route |
293
|
|
|
* |
294
|
|
|
* @return void |
295
|
|
|
*/ |
296
|
|
|
public function testLibrary(): void |
297
|
|
|
{ |
298
|
|
|
//library |
299
|
|
|
|
300
|
|
|
// Generate URL from route name |
301
|
|
|
$url = $this->router->generate('library'); |
302
|
|
|
|
303
|
|
|
// Send a GET request to the route |
304
|
|
|
$crawler = $this->client->request('GET', $url); |
305
|
|
|
|
306
|
|
|
// Assert the response status code |
307
|
|
|
$this->assertEquals(200, $this->client->getResponse()->getStatusCode()); |
308
|
|
|
|
309
|
|
|
// Assert that certain content exists in the response |
310
|
|
|
$this->assertStringContainsString('Book: Library', $crawler->filter('title')->text()); |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* testBookCreateGET |
315
|
|
|
* |
316
|
|
|
* Test book_create_get route |
317
|
|
|
* |
318
|
|
|
* @return void |
319
|
|
|
*/ |
320
|
|
|
public function testBookCreateGET(): void |
321
|
|
|
{ |
322
|
|
|
//book_create_get |
323
|
|
|
|
324
|
|
|
// Generate URL from route name |
325
|
|
|
$url = $this->router->generate('book_create_get'); |
326
|
|
|
|
327
|
|
|
// Send a GET request to the route |
328
|
|
|
$crawler = $this->client->request('GET', $url); |
329
|
|
|
|
330
|
|
|
// Assert the response status code |
331
|
|
|
$this->assertEquals(200, $this->client->getResponse()->getStatusCode()); |
332
|
|
|
|
333
|
|
|
// Assert that certain content exists in the response |
334
|
|
|
$this->assertStringContainsString('Book: Create', $crawler->filter('title')->text()); |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
/** |
338
|
|
|
* testShowAll |
339
|
|
|
* |
340
|
|
|
* Test book_show_all route |
341
|
|
|
* |
342
|
|
|
* @return void |
343
|
|
|
*/ |
344
|
|
|
public function testShowAll(): void |
345
|
|
|
{ |
346
|
|
|
//book_show_all |
347
|
|
|
|
348
|
|
|
// Generate URL from route name |
349
|
|
|
$url = $this->router->generate('book_show_all'); |
350
|
|
|
|
351
|
|
|
// Send a GET request to the route |
352
|
|
|
$crawler = $this->client->request('GET', $url); |
353
|
|
|
|
354
|
|
|
// Assert the response status code |
355
|
|
|
$this->assertEquals(200, $this->client->getResponse()->getStatusCode()); |
356
|
|
|
|
357
|
|
|
// Assert that certain content exists in the response |
358
|
|
|
$this->assertStringContainsString('Book: Show Library', $crawler->filter('title')->text()); |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
/** |
362
|
|
|
* testShowOne |
363
|
|
|
* |
364
|
|
|
* Test book_show_one route |
365
|
|
|
* |
366
|
|
|
* @return void |
367
|
|
|
*/ |
368
|
|
|
public function testShowOne(): void |
369
|
|
|
{ |
370
|
|
|
// Retrieve the container |
371
|
|
|
$container = $this->client->getContainer(); |
372
|
|
|
|
373
|
|
|
// Get the ManagerRegistry service from container |
374
|
|
|
/** @var ManagerRegistry $doctrine */ |
375
|
|
|
$doctrine = $container->get('doctrine'); |
376
|
|
|
|
377
|
|
|
$bookRepository = new BookRepository($doctrine); |
378
|
|
|
|
379
|
|
|
$data = $bookRepository->readAllBooks(); |
380
|
|
|
|
381
|
|
|
//If there is books in the library |
382
|
|
|
$id = null; |
383
|
|
|
|
384
|
|
|
if (count($data['books']) > 0) { |
385
|
|
|
$id = $data['books'][0]['id']; |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
if (null === $id) { |
389
|
|
|
$id = '0'; |
390
|
|
|
} |
391
|
|
|
|
392
|
|
|
//book_show_one |
393
|
|
|
|
394
|
|
|
// Generate URL from route name |
395
|
|
|
$url = $this->router->generate('book_show_one', ['id' => $id]); |
396
|
|
|
|
397
|
|
|
// Send a GET request to the route |
398
|
|
|
$crawler = $this->client->request('GET', $url); |
399
|
|
|
|
400
|
|
|
// Assert the response status code |
401
|
|
|
$this->assertEquals(200, $this->client->getResponse()->getStatusCode()); |
402
|
|
|
|
403
|
|
|
// Assert that certain content exists in the response |
404
|
|
|
$this->assertStringContainsString('Book: Show Book', $crawler->filter('title')->text()); |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
/** |
408
|
|
|
* testShowAllUpdate |
409
|
|
|
* |
410
|
|
|
* Test book_show_all_update route |
411
|
|
|
* |
412
|
|
|
* @return void |
413
|
|
|
*/ |
414
|
|
|
public function testShowAllUpdate(): void |
415
|
|
|
{ |
416
|
|
|
//book_show_all_update |
417
|
|
|
|
418
|
|
|
// Generate URL from route name |
419
|
|
|
$url = $this->router->generate('book_show_all_update'); |
420
|
|
|
|
421
|
|
|
// Send a GET request to the route |
422
|
|
|
$crawler = $this->client->request('GET', $url); |
423
|
|
|
|
424
|
|
|
// Assert the response status code |
425
|
|
|
$this->assertEquals(200, $this->client->getResponse()->getStatusCode()); |
426
|
|
|
|
427
|
|
|
// Assert that certain content exists in the response |
428
|
|
|
$this->assertStringContainsString('Book: Show Library Update', $crawler->filter('title')->text()); |
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
/** |
432
|
|
|
* testBookUpdateGET |
433
|
|
|
* |
434
|
|
|
* Test book_update_get route |
435
|
|
|
* |
436
|
|
|
* @return void |
437
|
|
|
*/ |
438
|
|
|
public function testBookUpdateGET(): void |
439
|
|
|
{ |
440
|
|
|
$bookRepository = new BookRepository($this->doctrine); |
441
|
|
|
|
442
|
|
|
$data = $bookRepository->readAllBooks(); |
443
|
|
|
|
444
|
|
|
//If there is books in the library |
445
|
|
|
$id = null; |
446
|
|
|
|
447
|
|
|
if (count($data['books']) > 0) { |
448
|
|
|
$id = $data['books'][0]['id']; |
449
|
|
|
} |
450
|
|
|
|
451
|
|
|
if (null === $id) { |
452
|
|
|
$id = '0'; |
453
|
|
|
} |
454
|
|
|
|
455
|
|
|
//book_update_get |
456
|
|
|
|
457
|
|
|
// Generate URL from route name |
458
|
|
|
$url = $this->router->generate('book_update_get', ['id' => $id]); |
459
|
|
|
|
460
|
|
|
// Send a GET request to the route |
461
|
|
|
$crawler = $this->client->request('GET', $url); |
462
|
|
|
|
463
|
|
|
// Assert the response status code |
464
|
|
|
$this->assertEquals(200, $this->client->getResponse()->getStatusCode()); |
465
|
|
|
|
466
|
|
|
// Assert that certain content exists in the response |
467
|
|
|
$this->assertStringContainsString('Book: Update book', $crawler->filter('title')->text()); |
468
|
|
|
} |
469
|
|
|
|
470
|
|
|
/** |
471
|
|
|
* testBookShowAllDeleteGET |
472
|
|
|
* |
473
|
|
|
* Test book_show_all_delete_get route |
474
|
|
|
* |
475
|
|
|
* @return void |
476
|
|
|
*/ |
477
|
|
|
public function testBookShowAllDeleteGET(): void |
478
|
|
|
{ |
479
|
|
|
//book_show_all_delete_get |
480
|
|
|
|
481
|
|
|
// Generate URL from route name |
482
|
|
|
$url = $this->router->generate('book_show_all_delete_get'); |
483
|
|
|
|
484
|
|
|
// Send a GET request to the route |
485
|
|
|
$crawler = $this->client->request('GET', $url); |
486
|
|
|
|
487
|
|
|
// Assert the response status code |
488
|
|
|
$this->assertEquals(200, $this->client->getResponse()->getStatusCode()); |
489
|
|
|
|
490
|
|
|
// Assert that certain content exists in the response |
491
|
|
|
$this->assertStringContainsString('Book: Show Library Delete', $crawler->filter('title')->text()); |
492
|
|
|
} |
493
|
|
|
} |
494
|
|
|
|