1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Tests\Repository; |
4
|
|
|
|
5
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; |
6
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
7
|
|
|
use App\Repository\BookRepository; |
8
|
|
|
use App\Entity\Book; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Test cases for class BookRepository. |
12
|
|
|
*/ |
13
|
|
|
class BookRepositoryTest extends KernelTestCase |
14
|
|
|
{ |
15
|
|
|
private EntityManagerInterface $entityManager; |
16
|
|
|
private BookRepository $repository; |
17
|
|
|
private ?int $createdBookId = null; // Store created book ID |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* setUp |
21
|
|
|
* |
22
|
|
|
* @return void |
23
|
|
|
*/ |
24
|
|
|
protected function setUp(): void |
25
|
|
|
{ |
26
|
|
|
self::bootKernel(); |
27
|
|
|
|
28
|
|
|
$kernel = self::$kernel; |
29
|
|
|
|
30
|
|
|
// Check if kernel is not null |
31
|
|
|
if ($kernel === null) { |
32
|
|
|
throw new \RuntimeException('Kernel is not initialized.'); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
// Access the container |
36
|
|
|
$container = $kernel->getContainer(); |
37
|
|
|
|
38
|
|
|
// Get the Doctrine registry service |
39
|
|
|
$doctrine = $container->get('doctrine'); |
40
|
|
|
|
41
|
|
|
// Get the EntityManager from the container |
42
|
|
|
// @phpstan-ignore-next-line |
43
|
|
|
$this->entityManager = $doctrine->getManager(); |
44
|
|
|
|
45
|
|
|
// Initialize the repository |
46
|
|
|
// @phpstan-ignore-next-line |
47
|
|
|
$this->repository = $this->entityManager->getRepository(Book::class); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* getNonExistantId |
52
|
|
|
* |
53
|
|
|
* @return int |
54
|
|
|
*/ |
55
|
|
|
private function getNonExistantId(): int |
56
|
|
|
{ |
57
|
|
|
// Read all the books |
58
|
|
|
$data = $this->repository->readAllBooks(); |
59
|
|
|
|
60
|
|
|
// Extract all existing IDs |
61
|
|
|
$existingIds = array_map(function ($book) { |
62
|
|
|
return $book['id']; |
63
|
|
|
}, $data['books']); |
64
|
|
|
|
65
|
|
|
// Find the smallest missing integer starting from 1 |
66
|
|
|
$missingId = null; |
67
|
|
|
for ($i = 1; ; $i++) { |
68
|
|
|
if (!in_array($i, $existingIds, true)) { |
69
|
|
|
$missingId = $i; |
70
|
|
|
break; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return (int) $missingId; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* testSaveBook |
79
|
|
|
* |
80
|
|
|
* @return void |
81
|
|
|
*/ |
82
|
|
|
private function testSaveBook(): void |
83
|
|
|
{ |
84
|
|
|
// save a book object |
85
|
|
|
$this->repository->saveBook('Title 1', '1000000000000', 'Author 1', 'Image 1'); |
86
|
|
|
|
87
|
|
|
// Read the book |
88
|
|
|
$data = $this->repository->readOneBookISBN('1000000000000'); |
89
|
|
|
|
90
|
|
|
$this->assertNotNull($data['book']['id']); |
91
|
|
|
$this->assertEquals("Title 1", $data['book']['title']); |
92
|
|
|
$this->assertEquals("1000000000000", $data['book']['isbn']); |
93
|
|
|
$this->assertEquals("Author 1", $data['book']['author']); |
94
|
|
|
$this->assertEquals("Image 1", $data['book']['img']); |
95
|
|
|
|
96
|
|
|
// After creating the book, store its ID |
97
|
|
|
$this->createdBookId = $data['book']['id']; |
98
|
|
|
|
99
|
|
|
// Test title is null |
100
|
|
|
$this->repository->saveBook('', '1000000000001', 'Author 2', 'Image 2'); |
101
|
|
|
|
102
|
|
|
// Read the book |
103
|
|
|
$data = $this->repository->readOneBookISBN('1000000000001'); |
104
|
|
|
|
105
|
|
|
$this->assertEquals(-1, $data['book']['id']); |
106
|
|
|
$this->assertEquals('', $data['book']['title']); |
107
|
|
|
$this->assertEquals("1000000000001", $data['book']['isbn']); |
108
|
|
|
$this->assertNull($data['book']['author']); |
109
|
|
|
$this->assertNull($data['book']['img']); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* testReadAllBooks |
114
|
|
|
* |
115
|
|
|
* @return void |
116
|
|
|
*/ |
117
|
|
|
private function testReadAllBooks(): void |
118
|
|
|
{ |
119
|
|
|
// Read all the books |
120
|
|
|
$data = $this->repository->readAllBooks(); |
121
|
|
|
|
122
|
|
|
$this->assertGreaterThan(0, count($data['books'])); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* testReadOneBookISBN |
127
|
|
|
* |
128
|
|
|
* @return void |
129
|
|
|
*/ |
130
|
|
|
private function testReadOneBookISBN(): void |
131
|
|
|
{ |
132
|
|
|
// Read the book |
133
|
|
|
$data = $this->repository->readOneBookISBN('1000000000000'); |
134
|
|
|
|
135
|
|
|
$this->assertNotNull($data['book']['id']); |
136
|
|
|
$this->assertEquals("Title 1", $data['book']['title']); |
137
|
|
|
$this->assertEquals("1000000000000", $data['book']['isbn']); |
138
|
|
|
$this->assertEquals("Author 1", $data['book']['author']); |
139
|
|
|
$this->assertEquals("Image 1", $data['book']['img']); |
140
|
|
|
|
141
|
|
|
// Test wrong ISBN digit length |
142
|
|
|
|
143
|
|
|
// Read the book |
144
|
|
|
$data = $this->repository->readOneBookISBN('0'); |
145
|
|
|
|
146
|
|
|
$this->assertEquals(-1, $data['book']['id']); |
147
|
|
|
$this->assertEquals('', $data['book']['title']); |
148
|
|
|
$this->assertEquals("0", $data['book']['isbn']); |
149
|
|
|
$this->assertNull($data['book']['author']); |
150
|
|
|
$this->assertNull($data['book']['img']); |
151
|
|
|
|
152
|
|
|
// Test Book not found |
153
|
|
|
|
154
|
|
|
// Read the book |
155
|
|
|
$data = $this->repository->readOneBookISBN('1000000000001'); |
156
|
|
|
|
157
|
|
|
$this->assertEquals(-1, $data['book']['id']); |
158
|
|
|
$this->assertEquals('', $data['book']['title']); |
159
|
|
|
$this->assertEquals("1000000000001", $data['book']['isbn']); |
160
|
|
|
$this->assertNull($data['book']['author']); |
161
|
|
|
$this->assertNull($data['book']['img']); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* testReadOneBook |
166
|
|
|
* |
167
|
|
|
* @return void |
168
|
|
|
*/ |
169
|
|
|
private function testReadOneBook(): void |
170
|
|
|
{ |
171
|
|
|
// Read one book based on id |
172
|
|
|
$data = $this->repository->readOneBook((int) $this->createdBookId); |
173
|
|
|
|
174
|
|
|
$this->assertEquals((int) $this->createdBookId, $data['book']['id']); |
175
|
|
|
$this->assertEquals("Title 1", $data['book']['title']); |
176
|
|
|
$this->assertEquals("1000000000000", $data['book']['isbn']); |
177
|
|
|
$this->assertEquals("Author 1", $data['book']['author']); |
178
|
|
|
$this->assertEquals("Image 1", $data['book']['img']); |
179
|
|
|
|
180
|
|
|
// Test if id is negative |
181
|
|
|
|
182
|
|
|
// Read one book based on id |
183
|
|
|
$data = $this->repository->readOneBook(-1); |
184
|
|
|
|
185
|
|
|
$this->assertEquals(-1, $data['book']['id']); |
186
|
|
|
$this->assertEquals('', $data['book']['title']); |
187
|
|
|
$this->assertNull($data['book']['isbn']); |
188
|
|
|
$this->assertNull($data['book']['author']); |
189
|
|
|
$this->assertNull($data['book']['img']); |
190
|
|
|
|
191
|
|
|
// Test if Book don't exists |
192
|
|
|
|
193
|
|
|
// Get id of Book that don't exist |
194
|
|
|
$missingId = $this->getNonExistantId(); |
195
|
|
|
|
196
|
|
|
// Read one book based on id |
197
|
|
|
$data = $this->repository->readOneBook($missingId); |
198
|
|
|
|
199
|
|
|
$this->assertEquals($missingId, $data['book']['id']); |
200
|
|
|
$this->assertEquals('', $data['book']['title']); |
201
|
|
|
$this->assertNull($data['book']['isbn']); |
202
|
|
|
$this->assertNull($data['book']['author']); |
203
|
|
|
$this->assertNull($data['book']['img']); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* testReturnBook |
208
|
|
|
* |
209
|
|
|
* @return void |
210
|
|
|
*/ |
211
|
|
|
private function testReturnBook(): void |
212
|
|
|
{ |
213
|
|
|
// Return a book object |
214
|
|
|
$book = $this->repository->returnBook('Title 1', '1000000000000', 'Author 1', 'Image 1'); |
215
|
|
|
|
216
|
|
|
$this->assertNull($book->getId()); |
217
|
|
|
$this->assertEquals("Title 1", $book->getTitle()); |
218
|
|
|
$this->assertEquals("1000000000000", $book->getISBN()); |
219
|
|
|
$this->assertEquals("Author 1", $book->getAuthor()); |
220
|
|
|
$this->assertEquals("Image 1", $book->getImg()); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* testUpdateBook |
225
|
|
|
* |
226
|
|
|
* @return void |
227
|
|
|
*/ |
228
|
|
|
private function testUpdateBook(): void |
229
|
|
|
{ |
230
|
|
|
//Update book |
231
|
|
|
$this->repository->updateBook((int) $this->createdBookId, 'Title 2', '1000000000001', 'Author 2', 'Image 2'); |
232
|
|
|
|
233
|
|
|
// Read one book based on id |
234
|
|
|
$data = $this->repository->readOneBook((int) $this->createdBookId); |
235
|
|
|
|
236
|
|
|
$this->assertEquals((int) $this->createdBookId, $data['book']['id']); |
237
|
|
|
$this->assertEquals("Title 2", $data['book']['title']); |
238
|
|
|
$this->assertEquals("1000000000001", $data['book']['isbn']); |
239
|
|
|
$this->assertEquals("Author 2", $data['book']['author']); |
240
|
|
|
$this->assertEquals("Image 2", $data['book']['img']); |
241
|
|
|
|
242
|
|
|
//Test to update book that don't exists |
243
|
|
|
|
244
|
|
|
// Get id of Book that don't exist |
245
|
|
|
$missingId = $this->getNonExistantId(); |
246
|
|
|
|
247
|
|
|
// Read all the books |
248
|
|
|
$data = $this->repository->readAllBooks(); |
249
|
|
|
|
250
|
|
|
$numBooks = count($data['books']); |
251
|
|
|
|
252
|
|
|
//Update book |
253
|
|
|
$this->repository->updateBook($missingId, 'Title 3', '1000000000002', 'Author 3', 'Image 3'); |
254
|
|
|
|
255
|
|
|
// Read all the books |
256
|
|
|
$data = $this->repository->readAllBooks(); |
257
|
|
|
|
258
|
|
|
// If update happen then the book would have been created |
259
|
|
|
$this->assertEquals(count($data['books']), $numBooks); |
260
|
|
|
|
261
|
|
|
// Test empty title |
262
|
|
|
|
263
|
|
|
//Update book |
264
|
|
|
$this->repository->updateBook((int) $this->createdBookId, '', '1000000000002', 'Author 3', 'Image 3'); |
265
|
|
|
|
266
|
|
|
// Read one the books |
267
|
|
|
$data = $this->repository->readOneBook((int) $this->createdBookId); |
268
|
|
|
|
269
|
|
|
$this->assertEquals((int) $this->createdBookId, $data['book']['id']); |
270
|
|
|
$this->assertEquals("Title 2", $data['book']['title']); |
271
|
|
|
$this->assertEquals("1000000000001", $data['book']['isbn']); |
272
|
|
|
$this->assertEquals("Author 2", $data['book']['author']); |
273
|
|
|
$this->assertEquals("Image 2", $data['book']['img']); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* testDeleteBook |
278
|
|
|
* |
279
|
|
|
* @return void |
280
|
|
|
*/ |
281
|
|
|
private function testDeleteBook(): void |
282
|
|
|
{ |
283
|
|
|
// Delete the book |
284
|
|
|
$this->repository->deleteBook((int)$this->createdBookId); |
285
|
|
|
|
286
|
|
|
// Read one the books |
287
|
|
|
$data = $this->repository->readOneBook((int) $this->createdBookId); |
288
|
|
|
|
289
|
|
|
$this->assertEquals((int) $this->createdBookId, $data['book']['id']); |
290
|
|
|
$this->assertEquals('', $data['book']['title']); |
291
|
|
|
$this->assertNull($data['book']['isbn']); |
292
|
|
|
$this->assertNull($data['book']['author']); |
293
|
|
|
$this->assertNull($data['book']['img']); |
294
|
|
|
|
295
|
|
|
// Test if book don't exists |
296
|
|
|
|
297
|
|
|
// Delete the book |
298
|
|
|
$this->repository->deleteBook((int) $this->createdBookId); |
299
|
|
|
|
300
|
|
|
// Reset the ID to prevent repeated deletions |
301
|
|
|
$this->createdBookId = null; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* tearDown |
306
|
|
|
* |
307
|
|
|
* @return void |
308
|
|
|
*/ |
309
|
|
|
protected function tearDown(): void |
310
|
|
|
{ |
311
|
|
|
parent::tearDown(); |
312
|
|
|
|
313
|
|
|
if ($this->createdBookId !== null) { |
314
|
|
|
|
315
|
|
|
// Delete the book |
316
|
|
|
$this->repository->deleteBook($this->createdBookId); |
317
|
|
|
|
318
|
|
|
// Reset the ID to prevent repeated deletions |
319
|
|
|
$this->createdBookId = null; |
320
|
|
|
} |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* testCRUD |
325
|
|
|
* |
326
|
|
|
* @return void |
327
|
|
|
*/ |
328
|
|
|
public function testCRUD(): void |
329
|
|
|
{ |
330
|
|
|
// Test returning a Book Object |
331
|
|
|
$this->testReturnBook(); |
332
|
|
|
|
333
|
|
|
// Test Create Book |
334
|
|
|
$this->testSaveBook(); |
335
|
|
|
|
336
|
|
|
// Test Read Book |
337
|
|
|
$this->testReadAllBooks(); |
338
|
|
|
$this->testReadOneBookISBN(); |
339
|
|
|
$this->testReadOneBook(); |
340
|
|
|
|
341
|
|
|
// Test Update Book |
342
|
|
|
$this->testUpdateBook(); |
343
|
|
|
|
344
|
|
|
// Test Delete Book |
345
|
|
|
$this->testDeleteBook(); |
346
|
|
|
} |
347
|
|
|
} |
348
|
|
|
|