Total Complexity | 151 |
Total Lines | 2100 |
Duplicated Lines | 0 % |
Changes | 49 | ||
Bugs | 2 | Features | 4 |
Complex classes like TDBMDaoGeneratorTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use TDBMDaoGeneratorTest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
90 | class TDBMDaoGeneratorTest extends TDBMAbstractServiceTest |
||
91 | { |
||
92 | /** @var TDBMDaoGenerator $tdbmDaoGenerator */ |
||
93 | protected $tdbmDaoGenerator; |
||
94 | |||
95 | private $rootPath; |
||
96 | |||
97 | protected function setUp(): void |
||
105 | //$this->tdbmDaoGenerator->setComposerFile($this->rootPath.'composer.json'); |
||
106 | } |
||
107 | |||
108 | public function testDaoGeneration(): void |
||
109 | { |
||
110 | // Remove all previously generated files. |
||
111 | $this->recursiveDelete($this->rootPath . 'src/Test/Dao/'); |
||
112 | mkdir($this->rootPath . 'src/Test/Dao/Generated', 0755, true); |
||
113 | // Let's generate a dummy file to see it is indeed removed. |
||
114 | $dummyFile = $this->rootPath . 'src/Test/Dao/Generated/foobar.php'; |
||
115 | touch($dummyFile); |
||
116 | $this->assertFileExists($dummyFile); |
||
117 | |||
118 | $this->tdbmDaoGenerator->generateAllDaosAndBeans(); |
||
119 | |||
120 | $this->assertFileNotExists($dummyFile); |
||
121 | |||
122 | // Let's require all files to check they are valid PHP! |
||
123 | // Test the daoFactory |
||
124 | require_once $this->rootPath . 'src/Test/Dao/Generated/DaoFactory.php'; |
||
125 | // Test the others |
||
126 | |||
127 | $beanDescriptors = $this->getDummyGeneratorListener()->getBeanDescriptors(); |
||
128 | |||
129 | foreach ($beanDescriptors as $beanDescriptor) { |
||
130 | $daoName = $beanDescriptor->getDaoClassName(); |
||
131 | $daoBaseName = $beanDescriptor->getBaseDaoClassName(); |
||
132 | $beanName = $beanDescriptor->getBeanClassName(); |
||
133 | $baseBeanName = $beanDescriptor->getBaseBeanClassName(); |
||
134 | require_once $this->rootPath . 'src/Test/Dao/Bean/Generated/' . $baseBeanName . '.php'; |
||
135 | require_once $this->rootPath . 'src/Test/Dao/Bean/' . $beanName . '.php'; |
||
136 | require_once $this->rootPath . 'src/Test/Dao/Generated/' . $daoBaseName . '.php'; |
||
137 | require_once $this->rootPath . 'src/Test/Dao/' . $daoName . '.php'; |
||
138 | } |
||
139 | |||
140 | // Check that pivot tables do not generate DAOs or beans. |
||
141 | $this->assertFalse(class_exists('TheCodingMachine\\TDBM\\Test\\Dao\\RolesRightDao')); |
||
142 | } |
||
143 | |||
144 | public function testGenerationException(): void |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * Delete a file or recursively delete a directory. |
||
161 | * |
||
162 | * @param string $str Path to file or directory |
||
163 | * @return bool |
||
164 | */ |
||
165 | private function recursiveDelete(string $str): bool |
||
166 | { |
||
167 | if (is_file($str)) { |
||
168 | return @unlink($str); |
||
169 | } elseif (is_dir($str)) { |
||
170 | $scan = glob(rtrim($str, '/') . '/*'); |
||
171 | foreach ($scan as $index => $path) { |
||
172 | $this->recursiveDelete($path); |
||
173 | } |
||
174 | |||
175 | return @rmdir($str); |
||
176 | } |
||
177 | return false; |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * @depends testDaoGeneration |
||
182 | */ |
||
183 | public function testGetBeanClassName(): void |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * @depends testDaoGeneration |
||
196 | */ |
||
197 | public function testGeneratedGetById(): void |
||
198 | { |
||
199 | $contactDao = new ContactDao($this->tdbmService); |
||
200 | $contactBean = $contactDao->getById(1); |
||
201 | $this->assertEquals(1, $contactBean->getId()); |
||
202 | $this->assertInstanceOf('\\DateTimeInterface', $contactBean->getCreatedAt()); |
||
203 | |||
204 | // FIXME: Question: que faire du paramètre stockage "UTC"???? |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * @depends testDaoGeneration |
||
209 | */ |
||
210 | public function testGeneratedGetByIdLazyLoaded(): void |
||
211 | { |
||
212 | $roleDao = new RoleDao($this->tdbmService); |
||
213 | $roleBean = $roleDao->getById(1, true); |
||
214 | $this->assertEquals(1, $roleBean->getId()); |
||
215 | $this->assertInstanceOf('\\DateTimeInterface', $roleBean->getCreatedAt()); |
||
216 | |||
217 | $roleBean2 = $roleDao->getById(1, true); |
||
218 | $this->assertTrue($roleBean === $roleBean2); |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * @depends testDaoGeneration |
||
223 | */ |
||
224 | public function testDefaultValueOnNewBean(): void |
||
225 | { |
||
226 | $roleBean = new RoleBean('my_role'); |
||
227 | $this->assertEquals(1, $roleBean->getStatus()); |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * @depends testDaoGeneration |
||
232 | */ |
||
233 | public function testForeignKeyInBean(): void |
||
234 | { |
||
235 | $userDao = new UserDao($this->tdbmService); |
||
236 | $userBean = $userDao->getById(1); |
||
237 | $country = $userBean->getCountry(); |
||
238 | |||
239 | $this->assertEquals('UK', $country->getLabel()); |
||
240 | |||
241 | $userBean2 = $userDao->getById(1); |
||
242 | $this->assertTrue($userBean === $userBean2); |
||
243 | |||
244 | $contactDao = new ContactDao($this->tdbmService); |
||
245 | $contactBean = $contactDao->getById(1); |
||
246 | |||
247 | $this->assertTrue($userBean === $contactBean); |
||
248 | } |
||
249 | |||
250 | /** |
||
251 | * @depends testDaoGeneration |
||
252 | */ |
||
253 | public function testNewBeans(): void |
||
263 | } |
||
264 | |||
265 | /** |
||
266 | * @depends testDaoGeneration |
||
267 | */ |
||
268 | public function testDateTimeImmutableGetter(): void |
||
269 | { |
||
270 | $userDao = new UserDao($this->tdbmService); |
||
271 | $user = $userDao->getById(1); |
||
272 | |||
273 | $this->assertInstanceOf('\DateTimeImmutable', $user->getCreatedAt()); |
||
274 | } |
||
275 | |||
276 | /** |
||
277 | * @depends testDaoGeneration |
||
278 | */ |
||
279 | public function testAssigningNewBeans(): void |
||
280 | { |
||
281 | $userDao = new UserDao($this->tdbmService); |
||
282 | $countryBean = new CountryBean('Mexico'); |
||
283 | $userBean = new UserBean('Speedy Gonzalez', '[email protected]', $countryBean, 'speedy.gonzalez'); |
||
284 | $this->assertEquals($countryBean, $userBean->getCountry()); |
||
285 | |||
286 | $userDao->save($userBean); |
||
287 | } |
||
288 | |||
289 | /** |
||
290 | * @depends testAssigningNewBeans |
||
291 | */ |
||
292 | public function testUpdatingProtectedColumn(): void |
||
293 | { |
||
294 | $userDao = new UserDao($this->tdbmService); |
||
295 | $userBean = $userDao->findOneByLogin('speedy.gonzalez'); |
||
296 | $userBean->setOrder(2); |
||
297 | $userDao->save($userBean); |
||
298 | $this->assertSame(2, $userBean->getOrder()); |
||
299 | } |
||
300 | |||
301 | /** |
||
302 | * @depends testDaoGeneration |
||
303 | */ |
||
304 | public function testAssigningExistingRelationship(): void |
||
305 | { |
||
306 | $userDao = new UserDao($this->tdbmService); |
||
307 | $user = $userDao->getById(1); |
||
308 | $countryDao = new CountryDao($this->tdbmService); |
||
309 | $country = $countryDao->getById(2); |
||
310 | |||
311 | $user->setCountry($country); |
||
312 | $this->assertEquals(TDBMObjectStateEnum::STATE_DIRTY, $user->_getStatus()); |
||
313 | } |
||
314 | |||
315 | /** |
||
316 | * @depends testDaoGeneration |
||
317 | */ |
||
318 | public function testDirectReversedRelationship(): void |
||
319 | { |
||
320 | $countryDao = new CountryDao($this->tdbmService); |
||
321 | $country = $countryDao->getById(1); |
||
322 | $users = $country->getUsers(); |
||
323 | |||
324 | $arr = $users->toArray(); |
||
325 | |||
326 | $this->assertCount(1, $arr); |
||
327 | $this->assertInstanceOf('TheCodingMachine\\TDBM\\Test\\Dao\\Bean\\UserBean', $arr[0]); |
||
328 | $this->assertEquals('jean.dupont', $arr[0]->getLogin()); |
||
329 | |||
330 | $newUser = new UserBean('Speedy Gonzalez', '[email protected]', $country, 'speedy.gonzalez'); |
||
331 | $users = $country->getUsers(); |
||
332 | |||
333 | $arr = $users->toArray(); |
||
334 | |||
335 | $this->assertCount(2, $arr); |
||
336 | $this->assertInstanceOf('TheCodingMachine\\TDBM\\Test\\Dao\\Bean\\UserBean', $arr[1]); |
||
337 | $this->assertEquals('speedy.gonzalez', $arr[1]->getLogin()); |
||
338 | } |
||
339 | |||
340 | /** |
||
341 | * @depends testDaoGeneration |
||
342 | */ |
||
343 | public function testDeleteInDirectReversedRelationship(): void |
||
344 | { |
||
345 | $countryDao = new CountryDao($this->tdbmService); |
||
346 | $country = $countryDao->getById(1); |
||
347 | |||
348 | $userDao = new UserDao($this->tdbmService); |
||
349 | $newUser = new UserBean('John Snow', '[email protected]', $country, 'john.snow'); |
||
350 | $userDao->save($newUser); |
||
351 | |||
352 | $users = $country->getUsers(); |
||
353 | |||
354 | $arr = $users->toArray(); |
||
355 | |||
356 | $this->assertCount(2, $arr); |
||
357 | |||
358 | $userDao->delete($arr[1]); |
||
359 | |||
360 | $users = $country->getUsers(); |
||
361 | $arr = $users->toArray(); |
||
362 | $this->assertCount(1, $arr); |
||
363 | } |
||
364 | |||
365 | /** |
||
366 | * @depends testDaoGeneration |
||
367 | */ |
||
368 | public function testJointureGetters(): void |
||
369 | { |
||
370 | $roleDao = new RoleDao($this->tdbmService); |
||
371 | $role = $roleDao->getById(1); |
||
372 | $users = $role->getUsers(); |
||
373 | |||
374 | $this->assertCount(2, $users); |
||
375 | $this->assertInstanceOf('TheCodingMachine\\TDBM\\Test\\Dao\\Bean\\UserBean', $users[0]); |
||
376 | |||
377 | $rights = $role->getRights(); |
||
378 | |||
379 | $this->assertCount(2, $rights); |
||
380 | $this->assertInstanceOf('TheCodingMachine\\TDBM\\Test\\Dao\\Bean\\RightBean', $rights[0]); |
||
381 | } |
||
382 | |||
383 | /** |
||
384 | * @depends testDaoGeneration |
||
385 | */ |
||
386 | public function testNestedIterationOnAlterableResultIterator(): void |
||
387 | { |
||
388 | $countryDao = new CountryDao($this->tdbmService); |
||
389 | $country = $countryDao->getById(2); |
||
390 | |||
391 | $count = 0; |
||
392 | // Let's perform 2 nested calls to check that iterators do not melt. |
||
393 | foreach ($country->getUsers() as $user) { |
||
394 | foreach ($country->getUsers() as $user2) { |
||
395 | $count++; |
||
396 | } |
||
397 | } |
||
398 | // There are 3 users linked to country 2. |
||
399 | $this->assertSame(9, $count); |
||
400 | } |
||
401 | |||
402 | /** |
||
403 | * @depends testDaoGeneration |
||
404 | */ |
||
405 | public function testNewBeanConstructor(): void |
||
406 | { |
||
407 | $role = new RoleBean('Newrole'); |
||
408 | $this->assertEquals(TDBMObjectStateEnum::STATE_DETACHED, $role->_getStatus()); |
||
409 | } |
||
410 | |||
411 | /** |
||
412 | * @depends testDaoGeneration |
||
413 | */ |
||
414 | public function testJointureAdderOnNewBean(): void |
||
415 | { |
||
416 | $countryDao = new CountryDao($this->tdbmService); |
||
417 | $country = $countryDao->getById(1); |
||
418 | $user = new UserBean('Speedy Gonzalez', '[email protected]', $country, 'speedy.gonzalez'); |
||
419 | $role = new RoleBean('Mouse'); |
||
420 | $user->addRole($role); |
||
421 | $roles = $user->getRoles(); |
||
422 | $this->assertCount(1, $roles); |
||
423 | $role = $roles[0]; |
||
424 | $this->assertInstanceOf('TheCodingMachine\\TDBM\\Test\\Dao\\Bean\\RoleBean', $role); |
||
425 | $users = $role->getUsers(); |
||
426 | $this->assertCount(1, $users); |
||
427 | $this->assertEquals($user, $users[0]); |
||
428 | |||
429 | $role->removeUser($user); |
||
430 | $this->assertCount(0, $role->getUsers()); |
||
431 | $this->assertCount(0, $user->getRoles()); |
||
432 | } |
||
433 | |||
434 | /** |
||
435 | * @depends testDaoGeneration |
||
436 | */ |
||
437 | public function testJointureDeleteBeforeGetters(): void |
||
438 | { |
||
439 | $roleDao = new RoleDao($this->tdbmService); |
||
440 | $userDao = new UserDao($this->tdbmService); |
||
441 | $role = $roleDao->getById(1); |
||
442 | $user = $userDao->getById(1); |
||
443 | |||
444 | // We call removeUser BEFORE calling getUsers |
||
445 | // This should work as expected. |
||
446 | $role->removeUser($user); |
||
447 | $users = $role->getUsers(); |
||
448 | |||
449 | $this->assertCount(1, $users); |
||
450 | } |
||
451 | |||
452 | /** |
||
453 | * @depends testDaoGeneration |
||
454 | */ |
||
455 | public function testJointureMultiAdd(): void |
||
456 | { |
||
457 | $countryDao = new CountryDao($this->tdbmService); |
||
458 | $country = $countryDao->getById(1); |
||
459 | $user = new UserBean('Speedy Gonzalez', '[email protected]', $country, 'speedy.gonzalez'); |
||
460 | $role = new RoleBean('Mouse'); |
||
461 | $user->addRole($role); |
||
462 | $role->addUser($user); |
||
463 | $user->addRole($role); |
||
464 | |||
465 | $this->assertCount(1, $user->getRoles()); |
||
466 | } |
||
467 | |||
468 | /** |
||
469 | * Step 1: we remove the role 1 from user 1 but save role 1. |
||
470 | * Expected result: no save done. |
||
471 | * |
||
472 | * @depends testDaoGeneration |
||
473 | */ |
||
474 | public function testJointureSave1(): void |
||
475 | { |
||
476 | $roleDao = new RoleDao($this->tdbmService); |
||
477 | $role = $roleDao->getById(1); |
||
478 | $userDao = new UserDao($this->tdbmService); |
||
479 | $user = $userDao->getById(1); |
||
480 | |||
481 | $this->assertTrue($user->hasRole($role)); |
||
482 | $this->assertTrue($role->hasUser($user)); |
||
483 | $user->removeRole($role); |
||
484 | $this->assertFalse($user->hasRole($role)); |
||
485 | $this->assertFalse($role->hasUser($user)); |
||
486 | $roleDao->save($role); |
||
487 | |||
488 | $this->assertEquals(TDBMObjectStateEnum::STATE_DIRTY, $user->_getStatus()); |
||
489 | $this->assertEquals(TDBMObjectStateEnum::STATE_LOADED, $role->_getStatus()); |
||
490 | } |
||
491 | |||
492 | /** |
||
493 | * Step 2: we check that nothing was saved |
||
494 | * Expected result: no save done. |
||
495 | * |
||
496 | * @depends testJointureSave1 |
||
497 | */ |
||
498 | public function testJointureSave2(): void |
||
499 | { |
||
500 | $roleDao = new RoleDao($this->tdbmService); |
||
501 | $role = $roleDao->getById(1); |
||
502 | $this->assertCount(2, $role->getUsers()); |
||
503 | } |
||
504 | |||
505 | /** |
||
506 | * Step 3: we remove the role 1 from user 1 and save user 1. |
||
507 | * Expected result: save done. |
||
508 | * |
||
509 | * @depends testJointureSave2 |
||
510 | */ |
||
511 | public function testJointureSave3(): void |
||
512 | { |
||
513 | $roleDao = new RoleDao($this->tdbmService); |
||
514 | $role = $roleDao->getById(1); |
||
515 | $userDao = new UserDao($this->tdbmService); |
||
516 | $user = $userDao->getById(1); |
||
517 | |||
518 | $this->assertCount(1, $user->getRoles()); |
||
519 | $user->removeRole($role); |
||
520 | $this->assertCount(0, $user->getRoles()); |
||
521 | $userDao->save($user); |
||
522 | $this->assertCount(0, $user->getRoles()); |
||
523 | } |
||
524 | |||
525 | /** |
||
526 | * Step 4: we check that save was done |
||
527 | * Expected result: save done. |
||
528 | * |
||
529 | * @depends testJointureSave3 |
||
530 | */ |
||
531 | public function testJointureSave4(): void |
||
532 | { |
||
533 | $roleDao = new RoleDao($this->tdbmService); |
||
534 | $role = $roleDao->getById(1); |
||
535 | $this->assertCount(1, $role->getUsers()); |
||
536 | $userDao = new UserDao($this->tdbmService); |
||
537 | $user = $userDao->getById(1); |
||
538 | $this->assertCount(0, $user->getRoles()); |
||
539 | } |
||
540 | |||
541 | /** |
||
542 | * Step 5: we add the role 1 from user 1 and save user 1. |
||
543 | * Expected result: save done. |
||
544 | * |
||
545 | * @depends testJointureSave4 |
||
546 | */ |
||
547 | public function testJointureSave5(): void |
||
548 | { |
||
549 | $roleDao = new RoleDao($this->tdbmService); |
||
550 | $role = $roleDao->getById(1); |
||
551 | $userDao = new UserDao($this->tdbmService); |
||
552 | $user = $userDao->getById(1); |
||
553 | |||
554 | $user->addRole($role); |
||
555 | $this->assertCount(1, $user->getRoles()); |
||
556 | $userDao->save($user); |
||
557 | } |
||
558 | |||
559 | /** |
||
560 | * Step 6: we check that save was done |
||
561 | * Expected result: save done. |
||
562 | * |
||
563 | * @depends testJointureSave5 |
||
564 | */ |
||
565 | public function testJointureSave6(): void |
||
566 | { |
||
567 | $roleDao = new RoleDao($this->tdbmService); |
||
568 | $role = $roleDao->getById(1); |
||
569 | $this->assertCount(2, $role->getUsers()); |
||
570 | $userDao = new UserDao($this->tdbmService); |
||
571 | $user = $userDao->getById(1); |
||
572 | $this->assertCount(1, $user->getRoles()); |
||
573 | } |
||
574 | |||
575 | /** |
||
576 | * Step 7: we add a new role to user 1 and save user 1. |
||
577 | * Expected result: save done, including the new role. |
||
578 | * |
||
579 | * @depends testJointureSave6 |
||
580 | */ |
||
581 | public function testJointureSave7(): void |
||
582 | { |
||
583 | $role = new RoleBean('my new role'); |
||
584 | $userDao = new UserDao($this->tdbmService); |
||
585 | $user = $userDao->getById(1); |
||
586 | |||
587 | $user->addRole($role); |
||
588 | $userDao->save($user); |
||
589 | |||
590 | $this->assertEquals(TDBMObjectStateEnum::STATE_LOADED, $role->_getStatus()); |
||
591 | } |
||
592 | |||
593 | /** |
||
594 | * Step 8: we check that save was done |
||
595 | * Expected result: save done. |
||
596 | * |
||
597 | * @depends testJointureSave7 |
||
598 | */ |
||
599 | public function testJointureSave8(): void |
||
620 | } |
||
621 | |||
622 | /** |
||
623 | * Step 9: Let's test the setXXX method. |
||
624 | * |
||
625 | * @depends testJointureSave8 |
||
626 | */ |
||
627 | public function testJointureSave9(): void |
||
638 | } |
||
639 | |||
640 | /** |
||
641 | * Step 10: Let's check results of 9. |
||
642 | * |
||
643 | * @depends testJointureSave9 |
||
644 | */ |
||
645 | public function testJointureSave10(): void |
||
646 | { |
||
647 | $userDao = new UserDao($this->tdbmService); |
||
648 | $user = $userDao->getById(1); |
||
649 | |||
654 | } |
||
655 | |||
656 | /** |
||
657 | * Test jointure in a parent table in an inheritance relationship |
||
658 | * |
||
659 | * @depends testDaoGeneration |
||
660 | */ |
||
661 | public function testJointureInParentTable(): void |
||
662 | { |
||
663 | $userDao = new UserDao($this->tdbmService); |
||
664 | $user = $userDao->getById(1); |
||
665 | |||
666 | $boats = $user->getBoats(); |
||
667 | |||
668 | $this->assertCount(1, $boats); |
||
669 | $this->assertEquals(1, $boats[0]->getId()); |
||
670 | } |
||
671 | |||
672 | /** |
||
673 | * @depends testDaoGeneration |
||
674 | */ |
||
675 | public function testFindOrderBy(): void |
||
691 | } |
||
692 | } |
||
693 | |||
694 | /** |
||
695 | * @depends testDaoGeneration |
||
696 | */ |
||
697 | public function testFindFromSqlOrderBy(): void |
||
698 | { |
||
699 | $userDao = new TestUserDao($this->tdbmService); |
||
700 | $users = $userDao->getUsersFromSqlByAlphabeticalOrder(); |
||
701 | |||
702 | $this->assertCount(6, $users); |
||
703 | $this->assertEquals('bill.shakespeare', $users[0]->getLogin()); |
||
704 | $this->assertEquals('jean.dupont', $users[1]->getLogin()); |
||
705 | |||
706 | $users = $userDao->getUsersFromSqlByCountryOrder(); |
||
707 | $this->assertCount(6, $users); |
||
708 | $countryName1 = $users[0]->getCountry()->getLabel(); |
||
709 | for ($i = 1; $i < 6; $i++) { |
||
710 | $countryName2 = $users[$i]->getCountry()->getLabel(); |
||
711 | $this->assertLessThanOrEqual(0, strcmp($countryName1, $countryName2)); |
||
712 | $countryName1 = $countryName2; |
||
713 | } |
||
714 | } |
||
715 | |||
716 | /** |
||
717 | * @depends testDaoGeneration |
||
718 | */ |
||
719 | public function testFindFromSqlOrderByOnInheritedBean(): void |
||
728 | } |
||
729 | |||
730 | |||
731 | /** |
||
732 | * @depends testDaoGeneration |
||
733 | */ |
||
734 | public function testFindFromSqlOrderByJoinRole(): void |
||
742 | } |
||
743 | |||
744 | /** |
||
745 | * @depends testDaoGeneration |
||
746 | */ |
||
747 | public function testFindFromRawSqlOrderByUserCount(): void |
||
748 | { |
||
749 | $countryDao = new TestCountryDao($this->tdbmService); |
||
750 | $countries = $countryDao->getCountriesByUserCount(); |
||
751 | |||
752 | $nbCountries = 4; |
||
753 | $this->assertCount($nbCountries, $countries); |
||
754 | for ($i = 1; $i < $nbCountries; $i++) { |
||
755 | $this->assertLessThanOrEqual($countries[$i - 1]->getUsers()->count(), $countries[$i]->getUsers()->count()); |
||
756 | } |
||
757 | } |
||
758 | |||
759 | /** |
||
760 | * @depends testDaoGeneration |
||
761 | */ |
||
762 | public function testFindFromRawSqlWithUnion(): void |
||
763 | { |
||
764 | $countryDao = new TestCountryDao($this->tdbmService); |
||
765 | $countries = $countryDao->getCountriesUsingUnion(); |
||
766 | |||
767 | $this->assertCount(2, $countries); |
||
768 | $this->assertEquals(1, $countries[0]->getId()); |
||
769 | } |
||
770 | |||
771 | /** |
||
772 | * @depends testDaoGeneration |
||
773 | */ |
||
774 | public function testFindFromRawSqlWithSimpleQuery(): void |
||
775 | { |
||
776 | $countryDao = new TestCountryDao($this->tdbmService); |
||
777 | $countries = $countryDao->getCountriesUsingSimpleQuery(); |
||
778 | |||
779 | $this->assertCount(1, $countries); |
||
780 | $this->assertEquals(1, $countries[0]->getId()); |
||
781 | } |
||
782 | |||
783 | /** |
||
784 | * @depends testDaoGeneration |
||
785 | */ |
||
786 | public function testFindFromRawSqlWithDistinctQuery(): void |
||
787 | { |
||
788 | $countryDao = new TestCountryDao($this->tdbmService); |
||
789 | $countries = $countryDao->getCountriesUsingDistinctQuery(); |
||
790 | |||
791 | $this->assertCount(1, $countries); |
||
792 | $this->assertEquals(2, $countries[0]->getId()); |
||
793 | } |
||
794 | |||
795 | /** |
||
796 | * @depends testDaoGeneration |
||
797 | */ |
||
798 | public function testFindFilters(): void |
||
799 | { |
||
800 | $userDao = new TestUserDao($this->tdbmService); |
||
801 | $users = $userDao->getUsersByLoginStartingWith('bill'); |
||
802 | |||
803 | $this->assertCount(1, $users); |
||
804 | $this->assertEquals('bill.shakespeare', $users[0]->getLogin()); |
||
805 | } |
||
806 | |||
807 | /** |
||
808 | * @depends testDaoGeneration |
||
809 | */ |
||
810 | public function testFindMode(): void |
||
811 | { |
||
812 | $userDao = new TestUserDao($this->tdbmService); |
||
813 | $users = $userDao->getUsersByLoginStartingWith('bill', TDBMService::MODE_CURSOR); |
||
814 | |||
815 | $this->expectException('TheCodingMachine\TDBM\TDBMException'); |
||
816 | $users[0]; |
||
817 | } |
||
818 | |||
819 | /** |
||
820 | * @depends testDaoGeneration |
||
821 | */ |
||
822 | public function testFindAll(): void |
||
823 | { |
||
824 | $userDao = new TestUserDao($this->tdbmService); |
||
825 | $users = $userDao->findAll(); |
||
826 | |||
827 | $this->assertCount(6, $users); |
||
828 | } |
||
829 | |||
830 | /** |
||
831 | * @depends testDaoGeneration |
||
832 | */ |
||
833 | public function testFindOne(): void |
||
834 | { |
||
835 | $userDao = new TestUserDao($this->tdbmService); |
||
836 | $user = $userDao->getUserByLogin('bill.shakespeare'); |
||
837 | |||
838 | $this->assertEquals('bill.shakespeare', $user->getLogin()); |
||
839 | } |
||
840 | |||
841 | /** |
||
842 | * @depends testDaoGeneration |
||
843 | */ |
||
844 | public function testJsonEncodeBean(): void |
||
845 | { |
||
846 | $userDao = new TestUserDao($this->tdbmService); |
||
847 | $user = $userDao->getUserByLogin('bill.shakespeare'); |
||
848 | |||
849 | $jsonEncoded = json_encode($user); |
||
850 | $userDecoded = json_decode($jsonEncoded, true); |
||
851 | |||
852 | $this->assertEquals('bill.shakespeare', $userDecoded['login']); |
||
853 | |||
854 | // test serialization of dates. |
||
855 | $this->assertTrue(is_string($userDecoded['createdAt'])); |
||
856 | $this->assertEquals('2015-10-24', (new \DateTimeImmutable($userDecoded['createdAt']))->format('Y-m-d')); |
||
857 | $this->assertNull($userDecoded['modifiedAt']); |
||
858 | |||
859 | // testing many to 1 relationships |
||
860 | $this->assertEquals('UK', $userDecoded['country']['label']); |
||
861 | |||
862 | // testing many to many relationships |
||
863 | $this->assertCount(1, $userDecoded['roles']); |
||
864 | $this->assertArrayNotHasKey('users', $userDecoded['roles'][0]); |
||
865 | $this->assertArrayNotHasKey('rights', $userDecoded['roles'][0]); |
||
866 | } |
||
867 | |||
868 | /** |
||
869 | * @depends testDaoGeneration |
||
870 | */ |
||
871 | public function testNullableForeignKey(): void |
||
872 | { |
||
873 | $userDao = new TestUserDao($this->tdbmService); |
||
874 | $user = $userDao->getUserByLogin('john.smith'); |
||
875 | |||
876 | $this->assertNull($user->getManager()); |
||
877 | |||
878 | $jsonEncoded = json_encode($user); |
||
879 | $userDecoded = json_decode($jsonEncoded, true); |
||
880 | |||
881 | $this->assertNull($userDecoded['manager']); |
||
882 | } |
||
883 | |||
884 | /** |
||
885 | * Test that setting (and saving) objects' references (foreign keys relations) to null is working. |
||
886 | * |
||
887 | * @depends testDaoGeneration |
||
888 | */ |
||
889 | public function testSetToNullForeignKey(): void |
||
890 | { |
||
891 | $userDao = new TestUserDao($this->tdbmService); |
||
892 | $user = $userDao->getUserByLogin('john.smith'); |
||
893 | $manager = $userDao->getUserByLogin('jean.dupont'); |
||
894 | |||
895 | $user->setManager($manager); |
||
896 | $userDao->save($user); |
||
897 | |||
898 | $user->setManager(null); |
||
899 | $userDao->save($user); |
||
900 | $this->assertNull($user->getManager()); |
||
901 | } |
||
902 | |||
903 | /** |
||
904 | * @depends testDaoGeneration |
||
905 | */ |
||
906 | public function testQueryOnWrongTableName(): void |
||
907 | { |
||
908 | $userDao = new TestUserDao($this->tdbmService); |
||
909 | $users = $userDao->getUsersWrongTableName(); |
||
910 | $this->expectException('Mouf\Database\SchemaAnalyzer\SchemaAnalyzerTableNotFoundException'); |
||
911 | $this->expectExceptionMessage('Could not find table \'contacts\'. Did you mean \'contact\'?'); |
||
912 | $users->count(); |
||
913 | } |
||
914 | |||
915 | /** |
||
916 | * @depends testDaoGeneration |
||
917 | */ |
||
918 | /*public function testQueryNullForeignKey(): void |
||
919 | { |
||
920 | $userDao = new TestUserDao($this->tdbmService); |
||
921 | $users = $userDao->getUsersByManagerId(null); |
||
922 | $this->assertCount(3, $users); |
||
923 | }*/ |
||
924 | |||
925 | /** |
||
926 | * @depends testDaoGeneration |
||
927 | */ |
||
928 | public function testInnerJsonEncode(): void |
||
929 | { |
||
930 | $userDao = new TestUserDao($this->tdbmService); |
||
931 | $user = $userDao->getUserByLogin('bill.shakespeare'); |
||
932 | |||
933 | $jsonEncoded = json_encode(['user' => $user]); |
||
934 | $msgDecoded = json_decode($jsonEncoded, true); |
||
935 | |||
936 | $this->assertEquals('bill.shakespeare', $msgDecoded['user']['login']); |
||
937 | } |
||
938 | |||
939 | /** |
||
940 | * @depends testDaoGeneration |
||
941 | */ |
||
942 | public function testArrayJsonEncode(): void |
||
943 | { |
||
944 | $userDao = new TestUserDao($this->tdbmService); |
||
945 | $users = $userDao->getUsersByLoginStartingWith('bill'); |
||
946 | |||
947 | $jsonEncoded = json_encode($users); |
||
948 | $msgDecoded = json_decode($jsonEncoded, true); |
||
949 | |||
950 | $this->assertCount(1, $msgDecoded); |
||
951 | } |
||
952 | |||
953 | /** |
||
954 | * @depends testDaoGeneration |
||
955 | */ |
||
956 | public function testCursorJsonEncode(): void |
||
957 | { |
||
958 | $userDao = new TestUserDao($this->tdbmService); |
||
959 | $users = $userDao->getUsersByLoginStartingWith('bill', TDBMService::MODE_CURSOR); |
||
960 | |||
961 | $jsonEncoded = json_encode($users); |
||
962 | $msgDecoded = json_decode($jsonEncoded, true); |
||
963 | |||
964 | $this->assertCount(1, $msgDecoded); |
||
965 | } |
||
966 | |||
967 | /** |
||
968 | * @depends testDaoGeneration |
||
969 | */ |
||
970 | public function testPageJsonEncode(): void |
||
971 | { |
||
972 | $userDao = new TestUserDao($this->tdbmService); |
||
973 | $users = $userDao->getUsersByLoginStartingWith('bill'); |
||
974 | |||
975 | $jsonEncoded = json_encode($users->take(0, 1)); |
||
976 | $msgDecoded = json_decode($jsonEncoded, true); |
||
977 | |||
978 | $this->assertCount(1, $msgDecoded); |
||
979 | } |
||
980 | |||
981 | /** |
||
982 | * @depends testDaoGeneration |
||
983 | */ |
||
984 | public function testInnerResultIteratorCountAfterFetch(): void |
||
985 | { |
||
986 | $userDao = new TestUserDao($this->tdbmService); |
||
987 | $users = $userDao->getUsersByLoginStartingWith('j')->take(0, 4); |
||
988 | $users->toArray(); // We force to fetch |
||
989 | $this->assertEquals(3, $users->count()); |
||
990 | } |
||
991 | |||
992 | /** |
||
993 | * @depends testDaoGeneration |
||
994 | */ |
||
995 | public function testFirst(): void |
||
996 | { |
||
997 | $userDao = new TestUserDao($this->tdbmService); |
||
998 | $users = $userDao->getUsersByLoginStartingWith('bill'); |
||
999 | |||
1000 | $bill = $users->first(); |
||
1001 | $this->assertEquals('bill.shakespeare', $bill->getLogin()); |
||
1002 | } |
||
1003 | |||
1004 | /** |
||
1005 | * @depends testDaoGeneration |
||
1006 | */ |
||
1007 | public function testFirstNull(): void |
||
1008 | { |
||
1009 | $userDao = new TestUserDao($this->tdbmService); |
||
1010 | $users = $userDao->getUsersByLoginStartingWith('mike'); |
||
1011 | |||
1012 | $user = $users->first(); |
||
1013 | $this->assertNull($user); |
||
1014 | } |
||
1015 | |||
1016 | /** |
||
1017 | * @depends testDaoGeneration |
||
1018 | */ |
||
1019 | public function testCloneBeanAttachedBean(): void |
||
1020 | { |
||
1021 | $userDao = new TestUserDao($this->tdbmService); |
||
1022 | $user = $userDao->getUserByLogin('bill.shakespeare'); |
||
1023 | $this->assertEquals(4, $user->getId()); |
||
1024 | $user2 = clone $user; |
||
1025 | $this->assertNull($user2->getId()); |
||
1026 | $this->assertEquals('bill.shakespeare', $user2->getLogin()); |
||
1027 | $this->assertEquals('Bill Shakespeare', $user2->getName()); |
||
1028 | $this->assertEquals('UK', $user2->getCountry()->getLabel()); |
||
1029 | |||
1030 | // MANY 2 MANY must be duplicated |
||
1031 | $this->assertEquals('Writers', $user2->getRoles()[0]->getName()); |
||
1032 | |||
1033 | // Let's test saving this clone |
||
1034 | $user2->setLogin('william.shakespeare'); |
||
1035 | $userDao->save($user2); |
||
1036 | |||
1037 | $user3 = $userDao->getUserByLogin('william.shakespeare'); |
||
1038 | $this->assertTrue($user3 === $user2); |
||
1039 | $userDao->delete($user3); |
||
1040 | |||
1041 | // Finally, let's test the origin user still exists! |
||
1042 | $user4 = $userDao->getUserByLogin('bill.shakespeare'); |
||
1043 | $this->assertEquals('bill.shakespeare', $user4->getLogin()); |
||
1044 | } |
||
1045 | |||
1046 | /** |
||
1047 | * @depends testDaoGeneration |
||
1048 | */ |
||
1049 | public function testCloneNewBean(): void |
||
1050 | { |
||
1051 | $countryDao = new CountryDao($this->tdbmService); |
||
1052 | $roleDao = new RoleDao($this->tdbmService); |
||
1053 | $role = $roleDao->getById(1); |
||
1054 | |||
1055 | $userBean = new UserBean('John Doe', '[email protected]', $countryDao->getById(2), 'john.doe'); |
||
1056 | $userBean->addRole($role); |
||
1057 | |||
1058 | $user2 = clone $userBean; |
||
1059 | |||
1060 | $this->assertNull($user2->getId()); |
||
1061 | $this->assertEquals('john.doe', $user2->getLogin()); |
||
1062 | $this->assertEquals('John Doe', $user2->getName()); |
||
1063 | $this->assertEquals('UK', $user2->getCountry()->getLabel()); |
||
1064 | |||
1065 | // MANY 2 MANY must be duplicated |
||
1066 | $this->assertEquals($role->getName(), $user2->getRoles()[0]->getName()); |
||
1067 | } |
||
1068 | |||
1069 | /** |
||
1070 | * @depends testDaoGeneration |
||
1071 | */ |
||
1072 | public function testCascadeDelete(): void |
||
1073 | { |
||
1074 | $userDao = new TestUserDao($this->tdbmService); |
||
1075 | $countryDao = new CountryDao($this->tdbmService); |
||
1076 | |||
1077 | $spain = new CountryBean('Spain'); |
||
1078 | $sanchez = new UserBean('Manuel Sanchez', '[email protected]', $spain, 'manuel.sanchez'); |
||
1079 | |||
1080 | $countryDao->save($spain); |
||
1081 | $userDao->save($sanchez); |
||
1082 | |||
1083 | $speedy2 = $userDao->getUserByLogin('manuel.sanchez'); |
||
1084 | $this->assertTrue($sanchez === $speedy2); |
||
1085 | |||
1086 | $exceptionTriggered = false; |
||
1087 | try { |
||
1088 | $countryDao->delete($spain); |
||
1089 | } catch (ForeignKeyConstraintViolationException $e) { |
||
1090 | $exceptionTriggered = true; |
||
1091 | } |
||
1092 | $this->assertTrue($exceptionTriggered); |
||
1093 | |||
1094 | $countryDao->delete($spain, true); |
||
1095 | |||
1096 | // Let's check that speed gonzalez was removed. |
||
1097 | $speedy3 = $userDao->getUserByLogin('manuel.sanchez'); |
||
1098 | $this->assertNull($speedy3); |
||
1099 | } |
||
1100 | |||
1101 | /** |
||
1102 | * @depends testDaoGeneration |
||
1103 | */ |
||
1104 | public function testDiscardChanges(): void |
||
1105 | { |
||
1106 | $contactDao = new ContactDao($this->tdbmService); |
||
1107 | $contactBean = $contactDao->getById(1); |
||
1108 | |||
1109 | $oldName = $contactBean->getName(); |
||
1110 | |||
1111 | $contactBean->setName('MyNewName'); |
||
1112 | |||
1113 | $contactBean->discardChanges(); |
||
1114 | |||
1115 | $this->assertEquals($oldName, $contactBean->getName()); |
||
1116 | } |
||
1117 | |||
1118 | /** |
||
1119 | * @depends testDaoGeneration |
||
1120 | */ |
||
1121 | public function testDiscardChangesOnNewBeanFails(): void |
||
1126 | } |
||
1127 | |||
1128 | /** |
||
1129 | * @depends testDaoGeneration |
||
1130 | */ |
||
1131 | public function testDiscardChangesOnDeletedBeanFails(): void |
||
1132 | { |
||
1133 | $userDao = new TestUserDao($this->tdbmService); |
||
1134 | $countryDao = new CountryDao($this->tdbmService); |
||
1135 | |||
1136 | $sanchez = new UserBean('Manuel Sanchez', '[email protected]', $countryDao->getById(1), 'manuel.sanchez'); |
||
1137 | |||
1138 | $userDao->save($sanchez); |
||
1139 | |||
1140 | $userDao->delete($sanchez); |
||
1141 | |||
1142 | $this->expectException('TheCodingMachine\TDBM\TDBMException'); |
||
1143 | // Cannot discard changes on a bean that is already deleted. |
||
1144 | $sanchez->discardChanges(); |
||
1145 | } |
||
1146 | |||
1147 | /** |
||
1148 | * @depends testDaoGeneration |
||
1149 | */ |
||
1150 | public function testUniqueIndexBasedSearch(): void |
||
1151 | { |
||
1152 | $userDao = new UserDao($this->tdbmService); |
||
1153 | $user = $userDao->findOneByLogin('bill.shakespeare'); |
||
1154 | |||
1155 | $this->assertEquals('bill.shakespeare', $user->getLogin()); |
||
1156 | $this->assertEquals('Bill Shakespeare', $user->getName()); |
||
1157 | } |
||
1158 | |||
1159 | /** |
||
1160 | * @depends testDaoGeneration |
||
1161 | */ |
||
1162 | public function testFindOneByRetunsNull(): void |
||
1163 | { |
||
1164 | // Let's assert that the findOneBy... methods can return null. |
||
1165 | $userDao = new UserDao($this->tdbmService); |
||
1166 | $userBean = $userDao->findOneByLogin('not_exist'); |
||
1167 | |||
1168 | $this->assertNull($userBean); |
||
1169 | } |
||
1170 | |||
1171 | /** |
||
1172 | * @depends testDaoGeneration |
||
1173 | */ |
||
1174 | public function testMultiColumnsIndexBasedSearch(): void |
||
1175 | { |
||
1176 | $countryDao = new CountryDao($this->tdbmService); |
||
1177 | $userDao = new UserDao($this->tdbmService); |
||
1178 | $users = $userDao->findByStatusAndCountry('on', $countryDao->getById(1)); |
||
1179 | |||
1180 | $this->assertEquals('jean.dupont', $users[0]->getLogin()); |
||
1181 | } |
||
1182 | |||
1183 | /** |
||
1184 | * @depends testDaoGeneration |
||
1185 | */ |
||
1186 | public function testPartialMultiColumnsIndexBasedSearch(): void |
||
1187 | { |
||
1188 | $userDao = new UserDao($this->tdbmService); |
||
1189 | $users = $userDao->findByStatusAndCountry('on'); |
||
1190 | |||
1191 | $this->assertCount(2, $users); |
||
1192 | } |
||
1193 | |||
1194 | /** |
||
1195 | * @depends testDaoGeneration |
||
1196 | */ |
||
1197 | public function testCreationInNullableDate(): void |
||
1198 | { |
||
1199 | $roleDao = new RoleDao($this->tdbmService); |
||
1200 | |||
1201 | $role = new RoleBean('newbee'); |
||
1202 | $roleDao->save($role); |
||
1203 | |||
1204 | $this->assertNull($role->getCreatedAt()); |
||
1205 | } |
||
1206 | |||
1207 | /** |
||
1208 | * @depends testDaoGeneration |
||
1209 | */ |
||
1210 | public function testUpdateInNullableDate(): void |
||
1211 | { |
||
1212 | $roleDao = new RoleDao($this->tdbmService); |
||
1213 | |||
1214 | $role = new RoleBean('newbee'); |
||
1215 | $roleDao->save($role); |
||
1216 | |||
1217 | $role->setCreatedAt(null); |
||
1218 | $roleDao->save($role); |
||
1219 | $this->assertNull($role->getCreatedAt()); |
||
1220 | } |
||
1221 | |||
1222 | /** |
||
1223 | * @depends testDaoGeneration |
||
1224 | */ |
||
1225 | public function testFindFromSql(): void |
||
1226 | { |
||
1227 | $roleDao = new TestRoleDao($this->tdbmService); |
||
1228 | |||
1229 | $roles = $roleDao->getRolesByRightCanSing(); |
||
1230 | $this->assertCount(2, $roles); |
||
1231 | $this->assertInstanceOf(RoleBean::class, $roles[0]); |
||
1232 | } |
||
1233 | |||
1234 | /** |
||
1235 | * @depends testDaoGeneration |
||
1236 | */ |
||
1237 | public function testFindOneFromSql(): void |
||
1238 | { |
||
1239 | $roleDao = new TestRoleDao($this->tdbmService); |
||
1240 | |||
1241 | $role = $roleDao->getRoleByRightCanSingAndNameSinger(); |
||
1242 | $this->assertInstanceOf(RoleBean::class, $role); |
||
1243 | } |
||
1244 | |||
1245 | /** |
||
1246 | * @depends testDaoGeneration |
||
1247 | */ |
||
1248 | public function testCreateEmptyExtendedBean(): void |
||
1249 | { |
||
1250 | // This test cases checks issue https://github.com/thecodingmachine/database.tdbm/issues/92 |
||
1251 | |||
1252 | $dogDao = new DogDao($this->tdbmService); |
||
1253 | |||
1254 | // We are not filling no field that is part of dog table. |
||
1255 | $dog = new DogBean('Youki'); |
||
1256 | $dog->setOrder(1); |
||
1257 | |||
1258 | $dogDao->save($dog); |
||
1259 | $this->assertNull($dog->getRace()); |
||
1260 | } |
||
1261 | |||
1262 | /** |
||
1263 | * @depends testCreateEmptyExtendedBean |
||
1264 | */ |
||
1265 | public function testFetchEmptyExtendedBean(): void |
||
1266 | { |
||
1267 | // This test cases checks issue https://github.com/thecodingmachine/database.tdbm/issues/92 |
||
1268 | |||
1269 | $animalDao = new AnimalDao($this->tdbmService); |
||
1270 | |||
1271 | // We are not filling no field that is part of dog table. |
||
1272 | $animalBean = $animalDao->getById(1); |
||
1273 | |||
1274 | $this->assertInstanceOf(DogBean::class, $animalBean); |
||
1275 | } |
||
1276 | |||
1277 | /** |
||
1278 | * @depends testDaoGeneration |
||
1279 | */ |
||
1280 | public function testTwoBranchesHierarchy(): void |
||
1281 | { |
||
1282 | // This test cases checks issue https://github.com/thecodingmachine/mouf/issues/131 |
||
1283 | |||
1284 | $catDao = new CatDao($this->tdbmService); |
||
1285 | |||
1286 | // We are not filling no field that is part of dog table. |
||
1287 | $cat = new CatBean('Mew'); |
||
1288 | $cat->setOrder(2); |
||
1289 | |||
1290 | $catDao->save($cat); |
||
1291 | $this->assertNotNull($cat->getId()); |
||
1292 | } |
||
1293 | |||
1294 | /** |
||
1295 | * @depends testTwoBranchesHierarchy |
||
1296 | */ |
||
1297 | public function testFetchTwoBranchesHierarchy(): void |
||
1298 | { |
||
1299 | // This test cases checks issue https://github.com/thecodingmachine/mouf/issues/131 |
||
1300 | |||
1301 | $animalDao = new AnimalDao($this->tdbmService); |
||
1302 | |||
1303 | $animalBean = $animalDao->getById(2); |
||
1304 | |||
1305 | $this->assertInstanceOf(CatBean::class, $animalBean); |
||
1306 | /* @var $animalBean CatBean */ |
||
1307 | $animalBean->setCutenessLevel(999); |
||
1308 | $animalBean->setUppercaseColumn('foobar'); |
||
1309 | |||
1310 | $animalDao->save($animalBean); |
||
1311 | } |
||
1312 | |||
1313 | /** |
||
1314 | * @depends testDaoGeneration |
||
1315 | */ |
||
1316 | public function testExceptionOnGetById(): void |
||
1317 | { |
||
1318 | $countryDao = new CountryDao($this->tdbmService); |
||
1319 | $this->expectException(\TypeError::class); |
||
1320 | $countryDao->getById(null); |
||
1321 | } |
||
1322 | |||
1323 | /** |
||
1324 | * @depends testDaoGeneration |
||
1325 | */ |
||
1326 | public function testDisconnectedManyToOne(): void |
||
1327 | { |
||
1328 | // This test cases checks issue https://github.com/thecodingmachine/database.tdbm/issues/99 |
||
1329 | |||
1330 | $country = new CountryBean('Spain'); |
||
1331 | |||
1332 | $user = new UserBean('John Doe', '[email protected]', $country, 'john.doe'); |
||
1333 | |||
1334 | $this->assertCount(1, $country->getUsers()); |
||
1335 | $this->assertSame($user, $country->getUsers()[0]); |
||
1336 | } |
||
1337 | |||
1338 | /** |
||
1339 | * @depends testDaoGeneration |
||
1340 | */ |
||
1341 | public function testOrderByExternalCol(): void |
||
1342 | { |
||
1343 | // This test cases checks issue https://github.com/thecodingmachine/database.tdbm/issues/106 |
||
1344 | |||
1345 | $userDao = new TestUserDao($this->tdbmService); |
||
1346 | $users = $userDao->getUsersByCountryName(); |
||
1347 | |||
1348 | $this->assertEquals('UK', $users[0]->getCountry()->getLabel()); |
||
1349 | } |
||
1350 | |||
1351 | /** |
||
1352 | * @depends testDaoGeneration |
||
1353 | */ |
||
1354 | public function testResultIteratorSort(): void |
||
1355 | { |
||
1356 | $userDao = new UserDao($this->tdbmService); |
||
1357 | $users = $userDao->findAll()->withOrder('country.label DESC'); |
||
1358 | |||
1359 | $this->assertEquals('UK', $users[0]->getCountry()->getLabel()); |
||
1360 | |||
1361 | $users = $users->withOrder('country.label ASC'); |
||
1362 | $this->assertEquals('France', $users[0]->getCountry()->getLabel()); |
||
1363 | } |
||
1364 | |||
1365 | /** |
||
1366 | * @depends testDaoGeneration |
||
1367 | */ |
||
1368 | public function testResultIteratorWithParameters(): void |
||
1369 | { |
||
1370 | $userDao = new TestUserDao($this->tdbmService); |
||
1371 | $users = $userDao->getUsersByLoginStartingWith()->withParameters(['login' => 'bill%']); |
||
1372 | $this->assertEquals('bill.shakespeare', $users[0]->getLogin()); |
||
1373 | |||
1374 | $users = $users->withParameters(['login' => 'jean%']); |
||
1375 | $this->assertEquals('jean.dupont', $users[0]->getLogin()); |
||
1376 | } |
||
1377 | |||
1378 | /** |
||
1379 | * @depends testDaoGeneration |
||
1380 | */ |
||
1381 | public function testOrderByExpression(): void |
||
1382 | { |
||
1383 | $userDao = new TestUserDao($this->tdbmService); |
||
1384 | $users = $userDao->getUsersByReversedCountryName(); |
||
1385 | |||
1386 | $this->assertEquals('Jamaica', $users[0]->getCountry()->getLabel()); |
||
1387 | } |
||
1388 | |||
1389 | /** |
||
1390 | * @depends testDaoGeneration |
||
1391 | */ |
||
1392 | public function testOrderByException(): void |
||
1393 | { |
||
1394 | $userDao = new TestUserDao($this->tdbmService); |
||
1395 | $users = $userDao->getUsersByInvalidOrderBy(); |
||
1396 | $this->expectException(TDBMInvalidArgumentException::class); |
||
1397 | $user = $users[0]; |
||
1398 | } |
||
1399 | |||
1400 | /** |
||
1401 | * @depends testDaoGeneration |
||
1402 | */ |
||
1403 | public function testOrderByProtectedColumn(): void |
||
1404 | { |
||
1405 | $animalDao = new AnimalDao($this->tdbmService); |
||
1406 | $animals = $animalDao->findAll(); |
||
1407 | $animals = $animals->withOrder('`order` ASC'); |
||
1408 | |||
1409 | $this->assertInstanceOf(DogBean::class, $animals[0]); |
||
1410 | $this->assertInstanceOf(CatBean::class, $animals[1]); |
||
1411 | |||
1412 | $animals = $animals->withOrder('`order` DESC'); |
||
1413 | |||
1414 | $this->assertInstanceOf(CatBean::class, $animals[0]); |
||
1415 | $this->assertInstanceOf(DogBean::class, $animals[1]); |
||
1416 | } |
||
1417 | |||
1418 | /** |
||
1419 | * @depends testDaoGeneration |
||
1420 | */ |
||
1421 | public function testGetOnAllNullableValues(): void |
||
1422 | { |
||
1423 | // Tests that a get performed on a column that has only nullable fields succeeds. |
||
1424 | $allNullable = new AllNullableBean(); |
||
1425 | $this->assertNull($allNullable->getId()); |
||
1426 | $this->assertNull($allNullable->getLabel()); |
||
1427 | $this->assertNull($allNullable->getCountry()); |
||
1428 | } |
||
1429 | |||
1430 | /** |
||
1431 | * @depends testDaoGeneration |
||
1432 | */ |
||
1433 | public function testExceptionOnMultipleInheritance(): void |
||
1434 | { |
||
1435 | $connection = self::getConnection(); |
||
1436 | self::insert($connection, 'animal', [ |
||
1437 | 'id' => 99, 'name' => 'Snoofield', |
||
1438 | ]); |
||
1439 | self::insert($connection, 'dog', [ |
||
1440 | 'id' => 99, 'race' => 'dog', |
||
1441 | ]); |
||
1442 | self::insert($connection, 'cat', [ |
||
1443 | 'id' => 99, 'cuteness_level' => 0, |
||
1444 | ]); |
||
1445 | |||
1446 | $catched = false; |
||
1447 | try { |
||
1448 | $animalDao = new AnimalDao($this->tdbmService); |
||
1449 | $animalDao->getById(99); |
||
1450 | } catch (TDBMInheritanceException $e) { |
||
1451 | $catched = true; |
||
1452 | } |
||
1453 | $this->assertTrue($catched, 'Exception TDBMInheritanceException was not caught'); |
||
1454 | |||
1455 | self::delete($connection, 'cat', ['id' => 99]); |
||
1456 | self::delete($connection, 'dog', ['id' => 99]); |
||
1457 | self::delete($connection, 'animal', ['id' => 99]); |
||
1458 | } |
||
1459 | |||
1460 | /** |
||
1461 | * @depends testDaoGeneration |
||
1462 | */ |
||
1463 | public function testReferenceNotSaved(): void |
||
1464 | { |
||
1465 | $boatDao = new BoatDao($this->tdbmService); |
||
1466 | |||
1467 | $country = new CountryBean('Atlantis'); |
||
1468 | $boat = new BoatBean($country, 'Titanic'); |
||
1469 | |||
1470 | $boatDao->save($boat); |
||
1471 | $this->assertNotNull($country->getId()); |
||
1472 | } |
||
1473 | |||
1474 | /** |
||
1475 | * @depends testReferenceNotSaved |
||
1476 | */ |
||
1477 | public function testUniqueIndexOnForeignKeyThenScalar(): void |
||
1478 | { |
||
1479 | $boatDao = new BoatDao($this->tdbmService); |
||
1480 | $countryDao = new CountryDao($this->tdbmService); |
||
1481 | |||
1482 | $countryBean = $countryDao->findOneByLabel('Atlantis'); |
||
1483 | $boatBean = $boatDao->findOneByAnchorageCountryAndName($countryBean, 'Titanic'); |
||
1484 | |||
1485 | $this->assertNotNull($boatBean); |
||
1486 | } |
||
1487 | |||
1488 | /** |
||
1489 | * @depends testDaoGeneration |
||
1490 | */ |
||
1491 | public function testReferenceDeleted(): void |
||
1492 | { |
||
1493 | $countryDao = new CountryDao($this->tdbmService); |
||
1494 | $boatDao = new BoatDao($this->tdbmService); |
||
1495 | |||
1496 | $country = new CountryBean('Bikini Bottom'); |
||
1497 | $countryDao->save($country); |
||
1498 | |||
1499 | $boat = new BoatBean($country, 'Squirrel boat'); |
||
1500 | $countryDao->delete($country); |
||
1501 | |||
1502 | $this->expectException(TDBMMissingReferenceException::class); |
||
1503 | $boatDao->save($boat); |
||
1504 | } |
||
1505 | |||
1506 | /** |
||
1507 | * @depends testDaoGeneration |
||
1508 | */ |
||
1509 | public function testCyclicReferenceWithInheritance(): void |
||
1510 | { |
||
1511 | $userDao = new UserDao($this->tdbmService); |
||
1512 | |||
1513 | $country = new CountryBean('Norrisland'); |
||
1514 | $user = new UserBean('Chuck Norris', '[email protected]', $country, 'chuck.norris'); |
||
1515 | |||
1516 | $user->setManager($user); |
||
1517 | |||
1518 | $this->expectException(TDBMCyclicReferenceException::class); |
||
1519 | $userDao->save($user); |
||
1520 | } |
||
1521 | |||
1522 | /** |
||
1523 | * @depends testDaoGeneration |
||
1524 | */ |
||
1525 | public function testCyclicReference(): void |
||
1526 | { |
||
1527 | $categoryDao = new CategoryDao($this->tdbmService); |
||
1528 | |||
1529 | $category = new CategoryBean('Root'); |
||
1530 | |||
1531 | $category->setParent($category); |
||
1532 | |||
1533 | $this->expectException(TDBMCyclicReferenceException::class); |
||
1534 | $categoryDao->save($category); |
||
1535 | } |
||
1536 | |||
1537 | /** |
||
1538 | * @depends testDaoGeneration |
||
1539 | */ |
||
1540 | public function testCorrectTypeForPrimaryKeyAfterSave(): void |
||
1541 | { |
||
1542 | // PosqtgreSQL does not particularly like empty inserts (i.e.: "INSERT INTO all_nullable () VALUES ()" ) |
||
1543 | $this->onlyMySql(); |
||
1544 | |||
1545 | $allNullableDao = new AllNullableDao($this->tdbmService); |
||
1546 | $allNullable = new AllNullableBean(); |
||
1547 | $allNullableDao->save($allNullable); |
||
1548 | $id = $allNullable->getId(); |
||
1549 | |||
1550 | $this->assertTrue(is_int($id)); |
||
1551 | } |
||
1552 | |||
1553 | /** |
||
1554 | * @depends testDaoGeneration |
||
1555 | */ |
||
1556 | public function testPSR2Compliance(): void |
||
1557 | { |
||
1558 | $process = new Process(['vendor/bin/php-cs-fixer', 'fix', 'src/Test/', '--dry-run', '--diff', '--rules=@PSR2']); |
||
1559 | $process->run(); |
||
1560 | |||
1561 | // executes after the command finishes |
||
1562 | if (!$process->isSuccessful()) { |
||
1563 | echo $process->getOutput(); |
||
1564 | $this->fail('Generated code is not PSR-2 compliant'); |
||
1565 | } |
||
1566 | $this->assertTrue($process->isSuccessful()); |
||
1567 | } |
||
1568 | |||
1569 | /** |
||
1570 | * @depends testDaoGeneration |
||
1571 | */ |
||
1572 | public function testFindOneByGeneration(): void |
||
1573 | { |
||
1574 | $reflectionMethod = new \ReflectionMethod(UserBaseDao::class, 'findOneByLogin'); |
||
1575 | $parameters = $reflectionMethod->getParameters(); |
||
1576 | |||
1577 | $this->assertCount(2, $parameters); |
||
1578 | $this->assertSame('login', $parameters[0]->getName()); |
||
1579 | $this->assertSame('additionalTablesFetch', $parameters[1]->getName()); |
||
1580 | } |
||
1581 | |||
1582 | /** |
||
1583 | * @depends testDaoGeneration |
||
1584 | */ |
||
1585 | public function testUuid(): void |
||
1586 | { |
||
1587 | $article = new ArticleBean('content'); |
||
1588 | $this->assertSame('content', $article->getContent()); |
||
1589 | $this->assertNotEmpty($article->getId()); |
||
1590 | $uuid = Uuid::fromString($article->getId()); |
||
1591 | $this->assertSame(1, $uuid->getVersion()); |
||
1592 | } |
||
1593 | |||
1594 | /** |
||
1595 | * @depends testDaoGeneration |
||
1596 | */ |
||
1597 | public function testUuidv4(): void |
||
1598 | { |
||
1599 | $article = new Article2Bean('content'); |
||
1600 | $this->assertSame('content', $article->getContent()); |
||
1601 | $this->assertNotEmpty($article->getId()); |
||
1602 | $uuid = Uuid::fromString($article->getId()); |
||
1603 | $this->assertSame(4, $uuid->getVersion()); |
||
1604 | } |
||
1605 | |||
1606 | /** |
||
1607 | * @depends testDaoGeneration |
||
1608 | */ |
||
1609 | public function testTypeHintedConstructors(): void |
||
1610 | { |
||
1611 | $userBaseBeanReflectionConstructor = new \ReflectionMethod(UserBaseBean::class, '__construct'); |
||
1612 | $nameParam = $userBaseBeanReflectionConstructor->getParameters()[0]; |
||
1613 | |||
1614 | $this->assertSame('string', (string)$nameParam->getType()); |
||
1615 | } |
||
1616 | |||
1617 | /** |
||
1618 | * @depends testDaoGeneration |
||
1619 | */ |
||
1620 | public function testSaveTransaction(): void |
||
1621 | { |
||
1622 | $countryDao = new CountryDao($this->tdbmService); |
||
1623 | |||
1624 | $boatDao = new BoatDao($this->tdbmService); |
||
1625 | $boatBean = $boatDao->getById(1); |
||
1626 | $boatBean->setName('Bismark'); |
||
1627 | |||
1628 | $boatBean->getCountry(); |
||
1629 | |||
1630 | // Let's insert a row without telling TDBM to trigger an error! |
||
1631 | self::insert($this->getConnection(), 'sailed_countries', [ |
||
1632 | 'boat_id' => 1, |
||
1633 | 'country_id' => 2 |
||
1634 | ]); |
||
1635 | |||
1636 | $boatBean->addCountry($countryDao->getById(2)); |
||
1637 | |||
1638 | $this->expectException(UniqueConstraintViolationException::class); |
||
1639 | |||
1640 | $boatDao->save($boatBean); |
||
1641 | } |
||
1642 | |||
1643 | /** |
||
1644 | * @depends testSaveTransaction |
||
1645 | */ |
||
1646 | public function testSaveTransaction2(): void |
||
1647 | { |
||
1648 | $boatDao = new BoatDao($this->tdbmService); |
||
1649 | $boatBean = $boatDao->getById(1); |
||
1650 | |||
1651 | // The name should not have been saved because the transaction of the previous test should have rollbacked. |
||
1652 | $this->assertNotSame('Bismark', $boatBean->getName()); |
||
1653 | } |
||
1654 | |||
1655 | /** |
||
1656 | * @depends testDaoGeneration |
||
1657 | */ |
||
1658 | public function testForeignKeyPointingToNonPrimaryKey(): void |
||
1659 | { |
||
1660 | $dao = new RefNoPrimKeyDao($this->tdbmService); |
||
1661 | $bean = $dao->getById(1); |
||
1662 | |||
1663 | $this->assertSame('foo', $bean->getFrom()->getTo()); |
||
1664 | |||
1665 | $newBean = new RefNoPrimKeyBean($bean, 'baz'); |
||
1666 | $dao->save($newBean); |
||
1667 | $this->assertSame('foo', $newBean->getFrom()->getTo()); |
||
1668 | |||
1669 | $resultSet = $bean->getRefNoPrimKey(); |
||
1670 | $this->assertCount(2, $resultSet); |
||
1671 | } |
||
1672 | |||
1673 | /** |
||
1674 | * @depends testDaoGeneration |
||
1675 | */ |
||
1676 | public function testCloningUuidBean(): void |
||
1683 | } |
||
1684 | |||
1685 | /** |
||
1686 | * @depends testDaoGeneration |
||
1687 | */ |
||
1688 | public function testRecursiveSave(): void |
||
1689 | { |
||
1690 | $categoryDao = new CategoryDao($this->tdbmService); |
||
1691 | |||
1692 | $root1 = new CategoryBean('Root1'); |
||
1693 | $categoryDao->save($root1); |
||
1694 | // Root 2 is not saved yet. |
||
1695 | $root2 = new CategoryBean('Root2'); |
||
1696 | $intermediate = new CategoryBean('Intermediate'); |
||
1697 | $categoryDao->save($intermediate); |
||
1698 | |||
1699 | // Let's switch the parent to a bean in detached state. |
||
1700 | $intermediate->setParent($root2); |
||
1701 | |||
1702 | // Now, let's save a new category that references the leaf category. |
||
1703 | $leaf = new CategoryBean('Leaf'); |
||
1704 | $leaf->setParent($intermediate); |
||
1705 | $categoryDao->save($leaf); |
||
1706 | $this->assertNull($root2->getId()); |
||
1707 | } |
||
1708 | |||
1709 | /** |
||
1710 | * @depends testDaoGeneration |
||
1711 | */ |
||
1712 | public function testBlob(): void |
||
1713 | { |
||
1714 | $fp = fopen(__FILE__, 'r'); |
||
1715 | $file = new FileBean($fp); |
||
1716 | |||
1717 | $fileDao = new FileDao($this->tdbmService); |
||
1718 | |||
1719 | $fileDao->save($file); |
||
1720 | |||
1721 | $loadedFile = $fileDao->getById($file->getId()); |
||
1722 | |||
1723 | $resource = $loadedFile->getFile(); |
||
1724 | $result = fseek($resource, 0); |
||
1725 | $this->assertSame(0, $result); |
||
1726 | $this->assertInternalType('resource', $resource); |
||
1727 | $firstLine = fgets($resource); |
||
1728 | $this->assertSame("<?php\n", $firstLine); |
||
1729 | } |
||
1730 | |||
1731 | /** |
||
1732 | * @depends testBlob |
||
1733 | */ |
||
1734 | public function testReadBlob(): void |
||
1735 | { |
||
1736 | $fileDao = new FileDao($this->tdbmService); |
||
1737 | $loadedFile = $fileDao->getById(1); |
||
1738 | |||
1739 | $resource = $loadedFile->getFile(); |
||
1740 | $this->assertInternalType('resource', $resource); |
||
1741 | $firstLine = fgets($resource); |
||
1742 | $this->assertSame("<?php\n", $firstLine); |
||
1743 | |||
1744 | stream_get_contents($resource); |
||
1745 | |||
1746 | $loadedFile->setId($loadedFile->getId()); |
||
1747 | |||
1748 | $fileDao->save($loadedFile); |
||
1749 | } |
||
1750 | |||
1751 | /** |
||
1752 | * @depends testReadBlob |
||
1753 | */ |
||
1754 | public function testReadAndSaveBlob(): void |
||
1755 | { |
||
1756 | $fileDao = new FileDao($this->tdbmService); |
||
1757 | $loadedFile = $fileDao->getById(1); |
||
1758 | |||
1759 | $resource = $loadedFile->getFile(); |
||
1760 | |||
1761 | $firstLine = fgets($resource); |
||
1762 | $this->assertSame("<?php\n", $firstLine); |
||
1763 | } |
||
1764 | |||
1765 | /** |
||
1766 | * @depends testReadBlob |
||
1767 | */ |
||
1768 | public function testProtectedGetterSetter(): void |
||
1769 | { |
||
1770 | $md5Getter = new ReflectionMethod(FileBaseBean::class, 'getMd5'); |
||
1771 | $md5Setter = new ReflectionMethod(FileBaseBean::class, 'setMd5'); |
||
1772 | |||
1773 | $this->assertTrue($md5Getter->isProtected()); |
||
1774 | $this->assertTrue($md5Setter->isProtected()); |
||
1775 | |||
1776 | $md5Getter2 = new ReflectionMethod(FileBaseBean::class, 'getArticle'); |
||
1777 | $md5Setter2 = new ReflectionMethod(FileBaseBean::class, 'setArticle'); |
||
1778 | |||
1779 | $this->assertTrue($md5Getter2->isProtected()); |
||
1780 | $this->assertTrue($md5Setter2->isProtected()); |
||
1781 | |||
1782 | $oneToManyGetter = new ReflectionMethod(ArticleBaseBean::class, 'getFiles'); |
||
1783 | $this->assertTrue($oneToManyGetter->isProtected()); |
||
1784 | |||
1785 | $fileDao = new FileDao($this->tdbmService); |
||
1786 | $loadedFile = $fileDao->getById(1); |
||
1787 | |||
1788 | // The md5 and article columns are not JSON serialized |
||
1789 | $this->assertSame([ |
||
1790 | 'id' => 1, |
||
1791 | ], $loadedFile->jsonSerialize()); |
||
1792 | } |
||
1793 | |||
1794 | /** |
||
1795 | * @depends testDaoGeneration |
||
1796 | */ |
||
1797 | public function testBlobResourceException(): void |
||
1798 | { |
||
1799 | $this->expectException(TDBMInvalidArgumentException::class); |
||
1800 | $this->expectExceptionMessage('Invalid argument passed to \'TheCodingMachine\\TDBM\\Test\\Dao\\Bean\\Generated\\FileBaseBean::setFile\'. Expecting a resource. Got a string.'); |
||
1801 | new FileBean('foobar'); |
||
1802 | } |
||
1803 | |||
1804 | /** |
||
1805 | * @depends testDaoGeneration |
||
1806 | */ |
||
1807 | public function testFilterBag(): void |
||
1808 | { |
||
1809 | $userDao = new TestUserDao($this->tdbmService); |
||
1810 | $countryDao = new CountryDao($this->tdbmService); |
||
1811 | |||
1812 | $country = $countryDao->getById(2); |
||
1813 | |||
1814 | // Let's test filter bags by bean and filter bag with many values. |
||
1815 | $users = $userDao->getUsersByComplexFilterBag($country, ['John Doe', 'Jane Doe']); |
||
1816 | |||
1817 | $this->assertCount(1, $users); |
||
1818 | $this->assertSame('John Doe', $users[0]->getName()); |
||
1819 | } |
||
1820 | |||
1821 | /** |
||
1822 | * @depends testDaoGeneration |
||
1823 | */ |
||
1824 | public function testDecimalIsMappedToString(): void |
||
1825 | { |
||
1826 | $reflectionClass = new \ReflectionClass(BoatBaseBean::class); |
||
1827 | $this->assertSame('string', (string) $reflectionClass->getMethod('getLength')->getReturnType()); |
||
1828 | } |
||
1829 | |||
1830 | /** |
||
1831 | * @depends testDaoGeneration |
||
1832 | */ |
||
1833 | public function testInsertMultiPrimaryKeysBean(): void |
||
1834 | { |
||
1835 | $countryDao = new CountryDao($this->tdbmService); |
||
1836 | |||
1837 | $country = $countryDao->getById(1); |
||
1838 | |||
1839 | $stateDao = new StateDao($this->tdbmService); |
||
1840 | $state = new StateBean($country, 'IDF', 'Ile de France'); |
||
1841 | $stateDao->save($state); |
||
1842 | |||
1843 | $this->assertSame($state, $stateDao->findAll()[0]); |
||
1844 | } |
||
1845 | |||
1846 | /** |
||
1847 | * @depends testInsertMultiPrimaryKeysBean |
||
1848 | */ |
||
1849 | public function testDeleteMultiPrimaryKeysBean(): void |
||
1850 | { |
||
1851 | $stateDao = new StateDao($this->tdbmService); |
||
1852 | |||
1853 | $state = $stateDao->findAll()[0]; |
||
1854 | $stateDao->delete($state); |
||
1855 | $this->assertCount(0, $stateDao->findAll()); |
||
1856 | } |
||
1857 | |||
1858 | /** |
||
1859 | * @depends testDaoGeneration |
||
1860 | */ |
||
1861 | public function testCompositePrimaryKeyGetter(): void |
||
1862 | { |
||
1863 | $stateDao = new StateDao($this->tdbmService); |
||
1864 | $country = new CountryBean('USA'); |
||
1865 | $stateBean = new StateBean($country, 'CA', 'California'); |
||
1866 | $stateDao->save($stateBean); |
||
1867 | $this->assertSame($stateBean, $stateDao->getById($country->getId(), 'CA')); |
||
1868 | } |
||
1869 | |||
1870 | /** |
||
1871 | * @depends testDaoGeneration |
||
1872 | */ |
||
1873 | public function testSortOnInheritedTable(): void |
||
1874 | { |
||
1875 | $animalDao = new AnimalDao($this->tdbmService); |
||
1876 | |||
1877 | // Let's insert an animal that is nothing. |
||
1878 | $animal = new AnimalBean('Mickey'); |
||
1879 | $animalDao->save($animal); |
||
1880 | |||
1881 | $animals = $animalDao->findAll()->withOrder('dog.race ASC'); |
||
1882 | |||
1883 | $animalsArr = $animals->toArray(); |
||
1884 | $this->assertCount(3, $animalsArr); |
||
1885 | } |
||
1886 | |||
1887 | /** |
||
1888 | * @depends testDaoGeneration |
||
1889 | */ |
||
1890 | public function testJsonKey(): void |
||
1891 | { |
||
1892 | $node = new NodeBean('foo.html'); |
||
1893 | $json = $node->jsonSerialize(); |
||
1894 | self::assertTrue(isset($json['basename'])); |
||
1895 | self::assertEquals('foo.html', $json['basename']); |
||
1896 | } |
||
1897 | |||
1898 | /** |
||
1899 | * @depends testDaoGeneration |
||
1900 | */ |
||
1901 | public function testJsonIgnore(): void |
||
1902 | { |
||
1903 | $nodeDao = new NodeDao($this->tdbmService); |
||
1904 | $index = $nodeDao->getById(6); |
||
1905 | $json = $index->jsonSerialize(); |
||
1906 | // Ignored scalar 'id' |
||
1907 | self::assertTrue(!isset($json['id'])); |
||
1908 | // Ignored object 'root' |
||
1909 | self::assertTrue(!isset($json['root'])); |
||
1910 | self::assertTrue(isset($json['guests'])); |
||
1911 | self::assertTrue(!empty($json['guests'])); |
||
1912 | $account = $index->getAccounts()[0]; |
||
1913 | $json = $account->jsonSerialize(); |
||
1914 | // Ignored array 'nodes' (from nodes_users table) |
||
1915 | self::assertTrue(!isset($json['nodes'])); |
||
1916 | } |
||
1917 | |||
1918 | /** |
||
1919 | * @depends testDaoGeneration |
||
1920 | */ |
||
1921 | public function testJsonInclude(): void |
||
1922 | { |
||
1923 | $nodeDao = new NodeDao($this->tdbmService); |
||
1924 | $index = $nodeDao->getById(6); |
||
1925 | $json = $index->jsonSerialize(); |
||
1926 | // Whole chain of parents should be serialized |
||
1927 | self::assertTrue(isset($json['parent'])); |
||
1928 | self::assertTrue(isset($json['parent']['parent'])); |
||
1929 | self::assertTrue(isset($json['parent']['parent']['parent'])); |
||
1930 | self::assertEquals('/', $json['parent']['parent']['parent']['basename']); |
||
1931 | } |
||
1932 | |||
1933 | /** |
||
1934 | * @depends testDaoGeneration |
||
1935 | */ |
||
1936 | public function testJsonRecursive(): void |
||
1937 | { |
||
1938 | $nodeDao = new NodeDao($this->tdbmService); |
||
1939 | $index = $nodeDao->getById(8); |
||
1940 | $json = $index->jsonSerialize(); |
||
1941 | // Original chain of aliases is recursively serialized, ... |
||
1942 | self::assertTrue(isset($json['alias'])); |
||
1943 | self::assertTrue(isset($json['alias']['alias'])); |
||
1944 | self::assertEquals('index.html', $json['alias']['alias']['basename']); |
||
1945 | // ... each alias even serializes its parents, ... |
||
1946 | self::assertTrue(isset($json['alias']['alias']['parent']['parent'])); |
||
1947 | // ... however, parents aliases chains have just their foreign key (id), as parents are serialized with $stopRecursion=true |
||
1948 | self::assertEquals(3, $json['alias']['alias']['parent']['parent']['alias']['id']); |
||
1949 | self::assertCount(1, $json['alias']['alias']['parent']['parent']['alias']); |
||
1950 | } |
||
1951 | |||
1952 | /** |
||
1953 | * @depends testDaoGeneration |
||
1954 | */ |
||
1955 | public function testJsonFormat(): void |
||
1956 | { |
||
1957 | $nodeDao = new NodeDao($this->tdbmService); |
||
1958 | $index = $nodeDao->getById(6); |
||
1959 | $json = $index->jsonSerialize(); |
||
1960 | self::assertTrue(isset($json['size'])); |
||
1961 | self::assertEquals('512 o', $json['size']); |
||
1962 | self::assertEquals('42.50g', $json['weight']); |
||
1963 | self::assertEquals($index->getCreatedAt()->format('Y-m-d'), $json['createdAt']); |
||
1964 | self::assertEquals($index->getOwner()->getName(), $json['owner']); |
||
1965 | self::assertEquals($index->getAccounts()[1]->getName(), $json['guests'][1]); |
||
1966 | self::assertTrue(isset($json['entries'])); |
||
1967 | self::assertEquals('Hello, World', $json['entries'][1]); |
||
1968 | $www = $index->getParent(); |
||
1969 | $json = $www->jsonSerialize(); |
||
1970 | self::assertEquals('0 o', $json['size']); |
||
1971 | self::assertNull($json['weight']); |
||
1972 | self::assertNull($json['owner']); |
||
1973 | } |
||
1974 | |||
1975 | /** |
||
1976 | * @depends testDaoGeneration |
||
1977 | */ |
||
1978 | public function testJsonCollection(): void |
||
1979 | { |
||
1980 | $artists = new ArtistDao($this->tdbmService); |
||
1981 | $pinkFloyd = $artists->getById(1); |
||
1982 | $animals = $pinkFloyd->getAlbums()[0]; |
||
1983 | $json = $pinkFloyd->jsonSerialize(); |
||
1984 | // Collection name properly handled ('discography' instead of default 'albums') |
||
1985 | self::assertTrue(isset($json['discography'])); |
||
1986 | self::assertEquals($animals->getTitle(), $json['discography'][0]['title']); |
||
1987 | // Make sure top object have just its primary key |
||
1988 | self::assertEquals(1, $json['discography'][0]['artist']['id']); |
||
1989 | self::assertCount(1, $json['discography'][0]['artist']); |
||
1990 | $json = $animals->jsonSerialize(); |
||
1991 | // Nevertheless, artist should be serialized in album as top object... |
||
1992 | self::assertTrue(isset($json['artist'])); |
||
1993 | // ... as should be tracks... |
||
1994 | self::assertTrue(isset($json['tracks'][0])); |
||
1995 | self::assertEquals('Pigs on the Wing 1', $json['tracks'][0]['title']); |
||
1996 | // ... and, ultimately, list of featuring artists, since feat is included |
||
1997 | self::assertTrue(isset($json['tracks'][0]['feat'][0])); |
||
1998 | self::assertEquals('Roger Waters', $json['tracks'][0]['feat'][0]['name']); |
||
1999 | } |
||
2000 | |||
2001 | public function testFloydHasNoParent(): void |
||
2002 | { |
||
2003 | $artists = new ArtistDao($this->tdbmService); |
||
2004 | $pinkFloyd = $artists->getById(1); |
||
2005 | $parents = $pinkFloyd->getParents(); |
||
2006 | |||
2007 | $this->assertEquals([], $parents); |
||
2008 | } |
||
2009 | |||
2010 | public function testFloydHasOneChild(): void |
||
2011 | { |
||
2012 | $artists = new ArtistDao($this->tdbmService); |
||
2013 | $pinkFloyd = $artists->getById(1); |
||
2014 | $children = $pinkFloyd->getChildrenByArtistsRelations(); |
||
2015 | |||
2016 | $this->assertEquals(1, count($children)); |
||
2017 | $this->assertEquals(2, $children[0]->getId()); |
||
2018 | } |
||
2019 | |||
2020 | /** |
||
2021 | * @depends testDaoGeneration |
||
2022 | */ |
||
2023 | public function testAddInterfaceAnnotation(): void |
||
2024 | { |
||
2025 | if (!$this->tdbmService->getConnection()->getDatabasePlatform() instanceof MySqlPlatform) { |
||
2026 | // See https://github.com/doctrine/dbal/pull/3512 |
||
2027 | $this->markTestSkipped('Only MySQL supports table level comments'); |
||
2028 | } |
||
2029 | |||
2030 | $refClass = new ReflectionClass(UserBaseBean::class); |
||
2031 | $this->assertTrue($refClass->implementsInterface(TestUserInterface::class)); |
||
2032 | } |
||
2033 | |||
2034 | /** |
||
2035 | * @depends testDaoGeneration |
||
2036 | */ |
||
2037 | public function testAddInterfaceOnDaoAnnotation(): void |
||
2038 | { |
||
2039 | if (!$this->tdbmService->getConnection()->getDatabasePlatform() instanceof MySqlPlatform) { |
||
2040 | // See https://github.com/doctrine/dbal/pull/3512 |
||
2041 | $this->markTestSkipped('Only MySQL supports table level comments'); |
||
2042 | } |
||
2043 | |||
2044 | $refClass = new ReflectionClass(UserBaseDao::class); |
||
2045 | $this->assertTrue($refClass->implementsInterface(TestUserDaoInterface::class)); |
||
2046 | } |
||
2047 | |||
2048 | /** |
||
2049 | * @depends testDaoGeneration |
||
2050 | */ |
||
2051 | public function testTrait(): void |
||
2052 | { |
||
2053 | if (!$this->tdbmService->getConnection()->getDatabasePlatform() instanceof MySqlPlatform) { |
||
2054 | // See https://github.com/doctrine/dbal/pull/3512 |
||
2055 | $this->markTestSkipped('Only MySQL supports table level comments'); |
||
2056 | } |
||
2057 | |||
2058 | $userDao = new UserDao($this->tdbmService); |
||
2059 | $userBean = $userDao->getById(1); |
||
2060 | |||
2061 | $this->assertSame('TestOtherUserTrait', $userBean->method1()); |
||
2062 | $this->assertSame('TestUserTrait', $userBean->method1renamed()); |
||
2063 | |||
2064 | $refClass = new ReflectionClass(UserBaseDao::class); |
||
2065 | $this->assertTrue($refClass->hasMethod('findNothing')); |
||
2066 | } |
||
2067 | |||
2068 | /** |
||
2069 | * @depends testDaoGeneration |
||
2070 | */ |
||
2071 | public function testNonInstantiableAbstractDaosAndBeans(): void |
||
2072 | { |
||
2073 | $refClass = new ReflectionClass(UserBaseDao::class); |
||
2074 | $this->assertFalse($refClass->isInstantiable()); |
||
2075 | |||
2076 | $refClass = new ReflectionClass(UserBaseBean::class); |
||
2077 | $this->assertFalse($refClass->isInstantiable()); |
||
2078 | } |
||
2079 | |||
2080 | /** |
||
2081 | * @depends testDaoGeneration |
||
2082 | */ |
||
2083 | public function testCanNullifyBlob(): void |
||
2084 | { |
||
2085 | $article = new ArticleBean('content'); |
||
2086 | $fp = fopen(__FILE__, 'r'); |
||
2087 | $article->setAttachment($fp); |
||
2088 | $article->setAttachment(null); |
||
2089 | $this->assertNull($article->getAttachment(null)); |
||
2090 | fclose($fp); |
||
2091 | } |
||
2092 | |||
2093 | /** |
||
2094 | * @depends testDaoGeneration |
||
2095 | */ |
||
2096 | public function testOptionnalParametersCanBeNullInFindOneBy() |
||
2097 | { |
||
2098 | $albumDao = new AlbumDao($this->tdbmService); |
||
2099 | $artist = new ArtistBean('Marcel'); |
||
2100 | |||
2101 | $albumDao->findOneByArtistAndNode($artist, null); |
||
2102 | $this->assertEquals(1, 1); |
||
2103 | } |
||
2104 | |||
2105 | /** |
||
2106 | * @depends testDaoGeneration |
||
2107 | */ |
||
2108 | public function testRequiredParametersCannotBeNullInFindOneBy() |
||
2118 | } |
||
2119 | |||
2120 | /** |
||
2121 | * @depends testDaoGeneration |
||
2122 | */ |
||
2123 | public function testLazyLoad(): void |
||
2124 | { |
||
2125 | $roleDao = new RoleDao($this->tdbmService); |
||
2126 | $roleBean = $roleDao->getById(1, true); |
||
2127 | |||
2128 | $this->assertSame(TDBMObjectStateEnum::STATE_NOT_LOADED, $roleBean->_getDbRows()['roles']->_getStatus()); |
||
2129 | $roleBean->getId(); |
||
2130 | $this->assertSame(TDBMObjectStateEnum::STATE_NOT_LOADED, $roleBean->_getDbRows()['roles']->_getStatus()); |
||
2131 | } |
||
2132 | |||
2133 | /** |
||
2134 | * @depends testDaoGeneration |
||
2135 | */ |
||
2136 | public function testOneToOneInverseRelationGetter(): void |
||
2137 | { |
||
2138 | $objectBaseDao = new BaseObjectDao($this->tdbmService); |
||
2139 | $objectInheritedDao = new InheritedObjectDao($this->tdbmService); |
||
2140 | $objectBase = new BaseObjectBean('label'); |
||
2141 | $objectBaseDao->save($objectBase); |
||
2142 | $this->assertNull($objectBase->getInheritedObject()); |
||
2143 | $objectInherited = new InheritedObjectBean($objectBase); |
||
2144 | $objectInheritedDao->save($objectInherited); |
||
2145 | $this->assertSame($objectInherited, $objectBase->getInheritedObject()); |
||
2146 | $this->assertEquals(1, $objectBase->jsonSerialize()['inheritedObject']['id']); |
||
2147 | } |
||
2148 | |||
2149 | public function testLazyStopRecursion(): void |
||
2150 | { |
||
2151 | $albumDao = new AlbumDao($this->tdbmService); |
||
2152 | $albumBean = $albumDao->getById(1); |
||
2156 | } |
||
2157 | |||
2158 | public function testLazyStopRecursionOnCompositeForeignKey(): void |
||
2159 | { |
||
2160 | $compositeFkSourceDao = new CompositeFkSourceDao($this->tdbmService); |
||
2161 | $compositeFkSourceBean = $compositeFkSourceDao->getById(1); |
||
2162 | $json = $compositeFkSourceBean->jsonSerialize(true); |
||
2163 | $this->assertEquals(1, $json['compositeFkTarget']['id1']); |
||
2164 | $this->assertEquals(1, $json['compositeFkTarget']['id2']); |
||
2165 | } |
||
2166 | |||
2167 | public function testMethodNameConflictsBetweenRegularAndAutoPivotProperties(): void |
||
2168 | { |
||
2169 | $artist = new ArtistBean('Super'); |
||
2170 | $artist->getChildren(); // regular property |
||
2171 | $artist->getChildrenByArtistId(); // one-to-may relationship |
||
2172 | $artist->getChildrenByArtistsRelations(); // auto-pivot relationship |
||
2173 | $this->assertEquals(1, 1); |
||
2174 | } |
||
2175 | |||
2176 | public function testFindByDateTime(): void |
||
2177 | { |
||
2178 | $personDao = new PersonDao($this->tdbmService); |
||
2179 | $personDao->findByModifiedAt(new \DateTimeImmutable())->count(); |
||
2180 | $this->assertTrue(true); |
||
2181 | } |
||
2182 | |||
2183 | public function testFindFromRawSQLOnInheritance(): void |
||
2184 | { |
||
2185 | $dao = new TestPersonDao($this->tdbmService); |
||
2186 | $objects = $dao->testFindFromRawSQLONInherited(); |
||
2187 | |||
2188 | $this->assertNotNull($objects->first()); |
||
2189 | $this->assertNotEquals(0, $objects->count()); |
||
2190 | } |
||
2191 | } |
||
2192 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths