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