Total Complexity | 143 |
Total Lines | 2020 |
Duplicated Lines | 0 % |
Changes | 38 | ||
Bugs | 1 | Features | 3 |
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 |
||
184 | { |
||
185 | $this->assertEquals(UserBean::class, $this->tdbmService->getBeanClassName('users')); |
||
186 | |||
187 | // Let's create another TDBMService to test the cache. |
||
188 | $configuration = new Configuration('TheCodingMachine\\TDBM\\Test\\Dao\\Bean', 'TheCodingMachine\\TDBM\\Test\\Dao', self::getConnection(), $this->getNamingStrategy(), $this->getCache(), null, null, [$this->getDummyGeneratorListener()]); |
||
189 | $configuration->setPathFinder(new PathFinder(null, dirname(__DIR__, 4))); |
||
190 | $newTdbmService = new TDBMService($configuration); |
||
191 | $this->assertEquals(UserBean::class, $newTdbmService->getBeanClassName('users')); |
||
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 |
||
254 | { |
||
255 | $countryDao = new CountryDao($this->tdbmService); |
||
256 | $userDao = new UserDao($this->tdbmService); |
||
257 | $userBean = new UserBean('John Doe', '[email protected]', $countryDao->getById(2), 'john.doe'); |
||
258 | $userBean->setOrder(1); // Let's set a "protected keyword" column. |
||
259 | |||
260 | $userDao->save($userBean); |
||
261 | |||
262 | $this->assertNull($userBean->getManager()); |
||
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 |
||
600 | { |
||
601 | $roleDao = new RoleDao($this->tdbmService); |
||
602 | $userDao = new UserDao($this->tdbmService); |
||
603 | $user = $userDao->getById(1); |
||
604 | |||
605 | $roles = $user->getRoles(); |
||
606 | foreach ($roles as $role) { |
||
607 | if ($role->getName() === 'my new role') { |
||
608 | $selectedRole = $role; |
||
609 | break; |
||
610 | } |
||
611 | } |
||
612 | $this->assertNotNull($selectedRole); |
||
613 | |||
614 | $this->assertCount(2, $user->getRoles()); |
||
615 | |||
616 | // Expected: relationship removed! |
||
617 | $roleDao->delete($selectedRole); |
||
618 | |||
619 | $this->assertCount(1, $user->getRoles()); |
||
620 | } |
||
621 | |||
622 | /** |
||
623 | * Step 9: Let's test the setXXX method. |
||
624 | * |
||
625 | * @depends testJointureSave8 |
||
626 | */ |
||
627 | public function testJointureSave9(): void |
||
628 | { |
||
629 | $roleDao = new RoleDao($this->tdbmService); |
||
630 | $userDao = new UserDao($this->tdbmService); |
||
631 | $user = $userDao->getById(1); |
||
632 | |||
633 | // At this point, user 1 is linked to role 1. |
||
634 | // Let's bind it to role 2. |
||
635 | $user->setRoles([$roleDao->getById(2)]); |
||
636 | $userDao->save($user); |
||
637 | $this->assertTrue($user->hasRole($roleDao->getById(2))); |
||
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 | |||
650 | $roles = $user->getRoles(); |
||
651 | |||
652 | $this->assertCount(1, $roles); |
||
653 | $this->assertEquals(2, $roles[0]->getId()); |
||
654 | } |
||
655 | |||
656 | /** |
||
657 | * @depends testDaoGeneration |
||
658 | */ |
||
659 | public function testFindOrderBy(): void |
||
660 | { |
||
661 | $userDao = new TestUserDao($this->tdbmService); |
||
662 | $users = $userDao->getUsersByAlphabeticalOrder(); |
||
663 | |||
664 | $this->assertCount(6, $users); |
||
665 | $this->assertEquals('bill.shakespeare', $users[0]->getLogin()); |
||
666 | $this->assertEquals('jean.dupont', $users[1]->getLogin()); |
||
667 | |||
668 | $users = $userDao->getUsersByCountryOrder(); |
||
669 | $this->assertCount(6, $users); |
||
670 | $countryName1 = $users[0]->getCountry()->getLabel(); |
||
671 | for ($i = 1; $i < 6; $i++) { |
||
672 | $countryName2 = $users[$i]->getCountry()->getLabel(); |
||
673 | $this->assertLessThanOrEqual(0, strcmp($countryName1, $countryName2)); |
||
674 | $countryName1 = $countryName2; |
||
675 | } |
||
676 | } |
||
677 | |||
678 | /** |
||
679 | * @depends testDaoGeneration |
||
680 | */ |
||
681 | public function testFindFromSqlOrderBy(): void |
||
682 | { |
||
683 | $userDao = new TestUserDao($this->tdbmService); |
||
684 | $users = $userDao->getUsersFromSqlByAlphabeticalOrder(); |
||
685 | |||
686 | $this->assertCount(6, $users); |
||
687 | $this->assertEquals('bill.shakespeare', $users[0]->getLogin()); |
||
688 | $this->assertEquals('jean.dupont', $users[1]->getLogin()); |
||
689 | |||
690 | $users = $userDao->getUsersFromSqlByCountryOrder(); |
||
691 | $this->assertCount(6, $users); |
||
692 | $countryName1 = $users[0]->getCountry()->getLabel(); |
||
693 | for ($i = 1; $i < 6; $i++) { |
||
694 | $countryName2 = $users[$i]->getCountry()->getLabel(); |
||
695 | $this->assertLessThanOrEqual(0, strcmp($countryName1, $countryName2)); |
||
696 | $countryName1 = $countryName2; |
||
697 | } |
||
698 | } |
||
699 | |||
700 | /** |
||
701 | * @depends testDaoGeneration |
||
702 | */ |
||
703 | public function testFindFromSqlOrderByOnInheritedBean(): void |
||
704 | { |
||
705 | $articleDao = new TestArticleDao($this->tdbmService); |
||
706 | $articles = $articleDao->getArticlesByUserLogin(); |
||
707 | |||
708 | foreach ($articles as $article) { |
||
709 | var_dump($article); |
||
710 | } |
||
711 | $this->assertCount(0, $articles); |
||
712 | } |
||
713 | |||
714 | |||
715 | /** |
||
716 | * @depends testDaoGeneration |
||
717 | */ |
||
718 | public function testFindFromSqlOrderByJoinRole(): void |
||
719 | { |
||
720 | $roleDao = new TestRoleDao($this->tdbmService); |
||
721 | $roles = $roleDao->getRolesByRightCanSing('roles.name DESC'); |
||
722 | |||
723 | $this->assertCount(2, $roles); |
||
724 | $this->assertEquals('Singers', $roles[0]->getName()); |
||
725 | $this->assertEquals('Admins', $roles[1]->getName()); |
||
726 | } |
||
727 | |||
728 | /** |
||
729 | * @depends testDaoGeneration |
||
730 | */ |
||
731 | public function testFindFromRawSqlOrderByUserCount(): void |
||
732 | { |
||
733 | $countryDao = new TestCountryDao($this->tdbmService); |
||
734 | $countries = $countryDao->getCountriesByUserCount(); |
||
735 | |||
736 | $this->assertCount(4, $countries); |
||
737 | for ($i = 1; $i < count($countries); $i++) { |
||
738 | $this->assertLessThanOrEqual($countries[$i - 1]->getUsers()->count(), $countries[$i]->getUsers()->count()); |
||
739 | } |
||
740 | } |
||
741 | |||
742 | /** |
||
743 | * @depends testDaoGeneration |
||
744 | */ |
||
745 | public function testFindFromRawSqlWithUnion(): void |
||
746 | { |
||
747 | $countryDao = new TestCountryDao($this->tdbmService); |
||
748 | $countries = $countryDao->getCountriesUsingUnion(); |
||
749 | |||
750 | $this->assertCount(2, $countries); |
||
751 | $this->assertEquals(1, $countries[0]->getId()); |
||
752 | } |
||
753 | |||
754 | /** |
||
755 | * @depends testDaoGeneration |
||
756 | */ |
||
757 | public function testFindFromRawSqlWithSimpleQuery(): void |
||
758 | { |
||
759 | $countryDao = new TestCountryDao($this->tdbmService); |
||
760 | $countries = $countryDao->getCountriesUsingSimpleQuery(); |
||
761 | |||
762 | $this->assertCount(1, $countries); |
||
763 | $this->assertEquals(1, $countries[0]->getId()); |
||
764 | } |
||
765 | |||
766 | /** |
||
767 | * @depends testDaoGeneration |
||
768 | */ |
||
769 | public function testFindFromRawSqlWithDistinctQuery(): void |
||
770 | { |
||
771 | $countryDao = new TestCountryDao($this->tdbmService); |
||
772 | $countries = $countryDao->getCountriesUsingDistinctQuery(); |
||
773 | |||
774 | $this->assertCount(1, $countries); |
||
775 | $this->assertEquals(2, $countries[0]->getId()); |
||
776 | } |
||
777 | |||
778 | /** |
||
779 | * @depends testDaoGeneration |
||
780 | */ |
||
781 | public function testFindFilters(): void |
||
788 | } |
||
789 | |||
790 | /** |
||
791 | * @depends testDaoGeneration |
||
792 | */ |
||
793 | public function testFindMode(): void |
||
794 | { |
||
795 | $userDao = new TestUserDao($this->tdbmService); |
||
796 | $users = $userDao->getUsersByLoginStartingWith('bill', TDBMService::MODE_CURSOR); |
||
797 | |||
798 | $this->expectException('TheCodingMachine\TDBM\TDBMException'); |
||
799 | $users[0]; |
||
800 | } |
||
801 | |||
802 | /** |
||
803 | * @depends testDaoGeneration |
||
804 | */ |
||
805 | public function testFindAll(): void |
||
806 | { |
||
807 | $userDao = new TestUserDao($this->tdbmService); |
||
808 | $users = $userDao->findAll(); |
||
809 | |||
810 | $this->assertCount(6, $users); |
||
811 | } |
||
812 | |||
813 | /** |
||
814 | * @depends testDaoGeneration |
||
815 | */ |
||
816 | public function testFindOne(): void |
||
817 | { |
||
818 | $userDao = new TestUserDao($this->tdbmService); |
||
819 | $user = $userDao->getUserByLogin('bill.shakespeare'); |
||
820 | |||
821 | $this->assertEquals('bill.shakespeare', $user->getLogin()); |
||
822 | } |
||
823 | |||
824 | /** |
||
825 | * @depends testDaoGeneration |
||
826 | */ |
||
827 | public function testJsonEncodeBean(): void |
||
849 | } |
||
850 | |||
851 | /** |
||
852 | * @depends testDaoGeneration |
||
853 | */ |
||
854 | public function testNullableForeignKey(): void |
||
855 | { |
||
856 | $userDao = new TestUserDao($this->tdbmService); |
||
857 | $user = $userDao->getUserByLogin('john.smith'); |
||
858 | |||
859 | $this->assertNull($user->getManager()); |
||
860 | |||
861 | $jsonEncoded = json_encode($user); |
||
862 | $userDecoded = json_decode($jsonEncoded, true); |
||
863 | |||
864 | $this->assertNull($userDecoded['manager']); |
||
865 | } |
||
866 | |||
867 | /** |
||
868 | * Test that setting (and saving) objects' references (foreign keys relations) to null is working. |
||
869 | * |
||
870 | * @depends testDaoGeneration |
||
871 | */ |
||
872 | public function testSetToNullForeignKey(): void |
||
873 | { |
||
874 | $userDao = new TestUserDao($this->tdbmService); |
||
875 | $user = $userDao->getUserByLogin('john.smith'); |
||
876 | $manager = $userDao->getUserByLogin('jean.dupont'); |
||
877 | |||
878 | $user->setManager($manager); |
||
879 | $userDao->save($user); |
||
880 | |||
881 | $user->setManager(null); |
||
882 | $userDao->save($user); |
||
883 | $this->assertNull($user->getManager()); |
||
884 | } |
||
885 | |||
886 | /** |
||
887 | * @depends testDaoGeneration |
||
888 | */ |
||
889 | public function testQueryOnWrongTableName(): void |
||
896 | } |
||
897 | |||
898 | /** |
||
899 | * @depends testDaoGeneration |
||
900 | */ |
||
901 | /*public function testQueryNullForeignKey(): void |
||
902 | { |
||
903 | $userDao = new TestUserDao($this->tdbmService); |
||
904 | $users = $userDao->getUsersByManagerId(null); |
||
905 | $this->assertCount(3, $users); |
||
906 | }*/ |
||
907 | |||
908 | /** |
||
909 | * @depends testDaoGeneration |
||
910 | */ |
||
911 | public function testInnerJsonEncode(): void |
||
912 | { |
||
913 | $userDao = new TestUserDao($this->tdbmService); |
||
914 | $user = $userDao->getUserByLogin('bill.shakespeare'); |
||
915 | |||
916 | $jsonEncoded = json_encode(['user' => $user]); |
||
917 | $msgDecoded = json_decode($jsonEncoded, true); |
||
918 | |||
919 | $this->assertEquals('bill.shakespeare', $msgDecoded['user']['login']); |
||
920 | } |
||
921 | |||
922 | /** |
||
923 | * @depends testDaoGeneration |
||
924 | */ |
||
925 | public function testArrayJsonEncode(): void |
||
926 | { |
||
927 | $userDao = new TestUserDao($this->tdbmService); |
||
928 | $users = $userDao->getUsersByLoginStartingWith('bill'); |
||
929 | |||
930 | $jsonEncoded = json_encode($users); |
||
931 | $msgDecoded = json_decode($jsonEncoded, true); |
||
932 | |||
933 | $this->assertCount(1, $msgDecoded); |
||
934 | } |
||
935 | |||
936 | /** |
||
937 | * @depends testDaoGeneration |
||
938 | */ |
||
939 | public function testCursorJsonEncode(): void |
||
948 | } |
||
949 | |||
950 | /** |
||
951 | * @depends testDaoGeneration |
||
952 | */ |
||
953 | public function testPageJsonEncode(): void |
||
954 | { |
||
955 | $userDao = new TestUserDao($this->tdbmService); |
||
956 | $users = $userDao->getUsersByLoginStartingWith('bill'); |
||
962 | } |
||
963 | |||
964 | /** |
||
965 | * @depends testDaoGeneration |
||
966 | */ |
||
967 | public function testInnerResultIteratorCountAfterFetch(): void |
||
968 | { |
||
969 | $userDao = new TestUserDao($this->tdbmService); |
||
970 | $users = $userDao->getUsersByLoginStartingWith('j')->take(0, 4); |
||
971 | $users->toArray(); // We force to fetch |
||
972 | $this->assertEquals(3, $users->count()); |
||
973 | } |
||
974 | |||
975 | /** |
||
976 | * @depends testDaoGeneration |
||
977 | */ |
||
978 | public function testFirst(): void |
||
979 | { |
||
980 | $userDao = new TestUserDao($this->tdbmService); |
||
981 | $users = $userDao->getUsersByLoginStartingWith('bill'); |
||
982 | |||
983 | $bill = $users->first(); |
||
984 | $this->assertEquals('bill.shakespeare', $bill->getLogin()); |
||
985 | } |
||
986 | |||
987 | /** |
||
988 | * @depends testDaoGeneration |
||
989 | */ |
||
990 | public function testFirstNull(): void |
||
991 | { |
||
992 | $userDao = new TestUserDao($this->tdbmService); |
||
993 | $users = $userDao->getUsersByLoginStartingWith('mike'); |
||
994 | |||
995 | $user = $users->first(); |
||
996 | $this->assertNull($user); |
||
997 | } |
||
998 | |||
999 | /** |
||
1000 | * @depends testDaoGeneration |
||
1001 | */ |
||
1002 | public function testCloneBeanAttachedBean(): void |
||
1003 | { |
||
1004 | $userDao = new TestUserDao($this->tdbmService); |
||
1005 | $user = $userDao->getUserByLogin('bill.shakespeare'); |
||
1006 | $this->assertEquals(4, $user->getId()); |
||
1007 | $user2 = clone $user; |
||
1008 | $this->assertNull($user2->getId()); |
||
1009 | $this->assertEquals('bill.shakespeare', $user2->getLogin()); |
||
1010 | $this->assertEquals('Bill Shakespeare', $user2->getName()); |
||
1011 | $this->assertEquals('UK', $user2->getCountry()->getLabel()); |
||
1012 | |||
1013 | // MANY 2 MANY must be duplicated |
||
1014 | $this->assertEquals('Writers', $user2->getRoles()[0]->getName()); |
||
1015 | |||
1016 | // Let's test saving this clone |
||
1017 | $user2->setLogin('william.shakespeare'); |
||
1018 | $userDao->save($user2); |
||
1019 | |||
1020 | $user3 = $userDao->getUserByLogin('william.shakespeare'); |
||
1021 | $this->assertTrue($user3 === $user2); |
||
1022 | $userDao->delete($user3); |
||
1023 | |||
1024 | // Finally, let's test the origin user still exists! |
||
1025 | $user4 = $userDao->getUserByLogin('bill.shakespeare'); |
||
1026 | $this->assertEquals('bill.shakespeare', $user4->getLogin()); |
||
1027 | } |
||
1028 | |||
1029 | /** |
||
1030 | * @depends testDaoGeneration |
||
1031 | */ |
||
1032 | public function testCloneNewBean(): void |
||
1033 | { |
||
1034 | $countryDao = new CountryDao($this->tdbmService); |
||
1035 | $roleDao = new RoleDao($this->tdbmService); |
||
1036 | $role = $roleDao->getById(1); |
||
1037 | |||
1038 | $userBean = new UserBean('John Doe', '[email protected]', $countryDao->getById(2), 'john.doe'); |
||
1039 | $userBean->addRole($role); |
||
1040 | |||
1041 | $user2 = clone $userBean; |
||
1042 | |||
1043 | $this->assertNull($user2->getId()); |
||
1044 | $this->assertEquals('john.doe', $user2->getLogin()); |
||
1045 | $this->assertEquals('John Doe', $user2->getName()); |
||
1046 | $this->assertEquals('UK', $user2->getCountry()->getLabel()); |
||
1047 | |||
1048 | // MANY 2 MANY must be duplicated |
||
1049 | $this->assertEquals($role->getName(), $user2->getRoles()[0]->getName()); |
||
1050 | } |
||
1051 | |||
1052 | /** |
||
1053 | * @depends testDaoGeneration |
||
1054 | */ |
||
1055 | public function testCascadeDelete(): void |
||
1056 | { |
||
1057 | $userDao = new TestUserDao($this->tdbmService); |
||
1058 | $countryDao = new CountryDao($this->tdbmService); |
||
1059 | |||
1060 | $spain = new CountryBean('Spain'); |
||
1061 | $sanchez = new UserBean('Manuel Sanchez', '[email protected]', $spain, 'manuel.sanchez'); |
||
1062 | |||
1063 | $countryDao->save($spain); |
||
1064 | $userDao->save($sanchez); |
||
1065 | |||
1066 | $speedy2 = $userDao->getUserByLogin('manuel.sanchez'); |
||
1067 | $this->assertTrue($sanchez === $speedy2); |
||
1068 | |||
1069 | $exceptionTriggered = false; |
||
1070 | try { |
||
1071 | $countryDao->delete($spain); |
||
1072 | } catch (ForeignKeyConstraintViolationException $e) { |
||
1073 | $exceptionTriggered = true; |
||
1074 | } |
||
1075 | $this->assertTrue($exceptionTriggered); |
||
1076 | |||
1077 | $countryDao->delete($spain, true); |
||
1078 | |||
1079 | // Let's check that speed gonzalez was removed. |
||
1080 | $speedy3 = $userDao->getUserByLogin('manuel.sanchez'); |
||
1081 | $this->assertNull($speedy3); |
||
1082 | } |
||
1083 | |||
1084 | /** |
||
1085 | * @depends testDaoGeneration |
||
1086 | */ |
||
1087 | public function testDiscardChanges(): void |
||
1088 | { |
||
1089 | $contactDao = new ContactDao($this->tdbmService); |
||
1090 | $contactBean = $contactDao->getById(1); |
||
1091 | |||
1092 | $oldName = $contactBean->getName(); |
||
1093 | |||
1094 | $contactBean->setName('MyNewName'); |
||
1095 | |||
1096 | $contactBean->discardChanges(); |
||
1097 | |||
1098 | $this->assertEquals($oldName, $contactBean->getName()); |
||
1099 | } |
||
1100 | |||
1101 | /** |
||
1102 | * @depends testDaoGeneration |
||
1103 | */ |
||
1104 | public function testDiscardChangesOnNewBeanFails(): void |
||
1109 | } |
||
1110 | |||
1111 | /** |
||
1112 | * @depends testDaoGeneration |
||
1113 | */ |
||
1114 | public function testDiscardChangesOnDeletedBeanFails(): void |
||
1115 | { |
||
1116 | $userDao = new TestUserDao($this->tdbmService); |
||
1117 | $countryDao = new CountryDao($this->tdbmService); |
||
1118 | |||
1119 | $sanchez = new UserBean('Manuel Sanchez', '[email protected]', $countryDao->getById(1), 'manuel.sanchez'); |
||
1120 | |||
1121 | $userDao->save($sanchez); |
||
1122 | |||
1123 | $userDao->delete($sanchez); |
||
1124 | |||
1125 | $this->expectException('TheCodingMachine\TDBM\TDBMException'); |
||
1126 | // Cannot discard changes on a bean that is already deleted. |
||
1127 | $sanchez->discardChanges(); |
||
1128 | } |
||
1129 | |||
1130 | /** |
||
1131 | * @depends testDaoGeneration |
||
1132 | */ |
||
1133 | public function testUniqueIndexBasedSearch(): void |
||
1134 | { |
||
1135 | $userDao = new UserDao($this->tdbmService); |
||
1136 | $user = $userDao->findOneByLogin('bill.shakespeare'); |
||
1137 | |||
1138 | $this->assertEquals('bill.shakespeare', $user->getLogin()); |
||
1139 | $this->assertEquals('Bill Shakespeare', $user->getName()); |
||
1140 | } |
||
1141 | |||
1142 | /** |
||
1143 | * @depends testDaoGeneration |
||
1144 | */ |
||
1145 | public function testFindOneByRetunsNull(): void |
||
1146 | { |
||
1147 | // Let's assert that the findOneBy... methods can return null. |
||
1148 | $userDao = new UserDao($this->tdbmService); |
||
1149 | $userBean = $userDao->findOneByLogin('not_exist'); |
||
1150 | |||
1151 | $this->assertNull($userBean); |
||
1152 | } |
||
1153 | |||
1154 | /** |
||
1155 | * @depends testDaoGeneration |
||
1156 | */ |
||
1157 | public function testMultiColumnsIndexBasedSearch(): void |
||
1158 | { |
||
1159 | $countryDao = new CountryDao($this->tdbmService); |
||
1160 | $userDao = new UserDao($this->tdbmService); |
||
1161 | $users = $userDao->findByStatusAndCountry('on', $countryDao->getById(1)); |
||
1162 | |||
1163 | $this->assertEquals('jean.dupont', $users[0]->getLogin()); |
||
1164 | } |
||
1165 | |||
1166 | /** |
||
1167 | * @depends testDaoGeneration |
||
1168 | */ |
||
1169 | public function testPartialMultiColumnsIndexBasedSearch(): void |
||
1170 | { |
||
1171 | $userDao = new UserDao($this->tdbmService); |
||
1172 | $users = $userDao->findByStatusAndCountry('on'); |
||
1173 | |||
1174 | $this->assertCount(2, $users); |
||
1175 | } |
||
1176 | |||
1177 | /** |
||
1178 | * @depends testDaoGeneration |
||
1179 | */ |
||
1180 | public function testCreationInNullableDate(): void |
||
1181 | { |
||
1182 | $roleDao = new RoleDao($this->tdbmService); |
||
1183 | |||
1184 | $role = new RoleBean('newbee'); |
||
1185 | $roleDao->save($role); |
||
1186 | |||
1187 | $this->assertNull($role->getCreatedAt()); |
||
1188 | } |
||
1189 | |||
1190 | /** |
||
1191 | * @depends testDaoGeneration |
||
1192 | */ |
||
1193 | public function testUpdateInNullableDate(): void |
||
1194 | { |
||
1195 | $roleDao = new RoleDao($this->tdbmService); |
||
1196 | |||
1197 | $role = new RoleBean('newbee'); |
||
1198 | $roleDao->save($role); |
||
1199 | |||
1200 | $role->setCreatedAt(null); |
||
1201 | $roleDao->save($role); |
||
1202 | $this->assertNull($role->getCreatedAt()); |
||
1203 | } |
||
1204 | |||
1205 | /** |
||
1206 | * @depends testDaoGeneration |
||
1207 | */ |
||
1208 | public function testFindFromSql(): void |
||
1209 | { |
||
1210 | $roleDao = new TestRoleDao($this->tdbmService); |
||
1211 | |||
1212 | $roles = $roleDao->getRolesByRightCanSing(); |
||
1213 | $this->assertCount(2, $roles); |
||
1214 | $this->assertInstanceOf(RoleBean::class, $roles[0]); |
||
1215 | } |
||
1216 | |||
1217 | /** |
||
1218 | * @depends testDaoGeneration |
||
1219 | */ |
||
1220 | public function testFindOneFromSql(): void |
||
1221 | { |
||
1222 | $roleDao = new TestRoleDao($this->tdbmService); |
||
1223 | |||
1224 | $role = $roleDao->getRoleByRightCanSingAndNameSinger(); |
||
1225 | $this->assertInstanceOf(RoleBean::class, $role); |
||
1226 | } |
||
1227 | |||
1228 | /** |
||
1229 | * @depends testDaoGeneration |
||
1230 | */ |
||
1231 | public function testCreateEmptyExtendedBean(): void |
||
1232 | { |
||
1233 | // This test cases checks issue https://github.com/thecodingmachine/database.tdbm/issues/92 |
||
1234 | |||
1235 | $dogDao = new DogDao($this->tdbmService); |
||
1236 | |||
1237 | // We are not filling no field that is part of dog table. |
||
1238 | $dog = new DogBean('Youki'); |
||
1239 | $dog->setOrder(1); |
||
1240 | |||
1241 | $dogDao->save($dog); |
||
1242 | $this->assertNull($dog->getRace()); |
||
1243 | } |
||
1244 | |||
1245 | /** |
||
1246 | * @depends testCreateEmptyExtendedBean |
||
1247 | */ |
||
1248 | public function testFetchEmptyExtendedBean(): void |
||
1249 | { |
||
1250 | // This test cases checks issue https://github.com/thecodingmachine/database.tdbm/issues/92 |
||
1251 | |||
1252 | $animalDao = new AnimalDao($this->tdbmService); |
||
1253 | |||
1254 | // We are not filling no field that is part of dog table. |
||
1255 | $animalBean = $animalDao->getById(1); |
||
1256 | |||
1257 | $this->assertInstanceOf(DogBean::class, $animalBean); |
||
1258 | } |
||
1259 | |||
1260 | /** |
||
1261 | * @depends testDaoGeneration |
||
1262 | */ |
||
1263 | public function testTwoBranchesHierarchy(): void |
||
1264 | { |
||
1265 | // This test cases checks issue https://github.com/thecodingmachine/mouf/issues/131 |
||
1266 | |||
1267 | $catDao = new CatDao($this->tdbmService); |
||
1268 | |||
1269 | // We are not filling no field that is part of dog table. |
||
1270 | $cat = new CatBean('Mew'); |
||
1271 | $cat->setOrder(2); |
||
1272 | |||
1273 | $catDao->save($cat); |
||
1274 | $this->assertNotNull($cat->getId()); |
||
1275 | } |
||
1276 | |||
1277 | /** |
||
1278 | * @depends testTwoBranchesHierarchy |
||
1279 | */ |
||
1280 | public function testFetchTwoBranchesHierarchy(): void |
||
1281 | { |
||
1282 | // This test cases checks issue https://github.com/thecodingmachine/mouf/issues/131 |
||
1283 | |||
1284 | $animalDao = new AnimalDao($this->tdbmService); |
||
1285 | |||
1286 | $animalBean = $animalDao->getById(2); |
||
1287 | |||
1288 | $this->assertInstanceOf(CatBean::class, $animalBean); |
||
1289 | /* @var $animalBean CatBean */ |
||
1290 | $animalBean->setCutenessLevel(999); |
||
1291 | $animalBean->setUppercaseColumn('foobar'); |
||
1292 | |||
1293 | $animalDao->save($animalBean); |
||
1294 | } |
||
1295 | |||
1296 | /** |
||
1297 | * @depends testDaoGeneration |
||
1298 | */ |
||
1299 | public function testExceptionOnGetById(): void |
||
1300 | { |
||
1301 | $countryDao = new CountryDao($this->tdbmService); |
||
1302 | $this->expectException(\TypeError::class); |
||
1303 | $countryDao->getById(null); |
||
1304 | } |
||
1305 | |||
1306 | /** |
||
1307 | * @depends testDaoGeneration |
||
1308 | */ |
||
1309 | public function testDisconnectedManyToOne(): void |
||
1310 | { |
||
1311 | // This test cases checks issue https://github.com/thecodingmachine/database.tdbm/issues/99 |
||
1312 | |||
1313 | $country = new CountryBean('Spain'); |
||
1314 | |||
1315 | $user = new UserBean('John Doe', '[email protected]', $country, 'john.doe'); |
||
1316 | |||
1317 | $this->assertCount(1, $country->getUsers()); |
||
1318 | $this->assertSame($user, $country->getUsers()[0]); |
||
1319 | } |
||
1320 | |||
1321 | /** |
||
1322 | * @depends testDaoGeneration |
||
1323 | */ |
||
1324 | public function testOrderByExternalCol(): void |
||
1325 | { |
||
1326 | // This test cases checks issue https://github.com/thecodingmachine/database.tdbm/issues/106 |
||
1327 | |||
1328 | $userDao = new TestUserDao($this->tdbmService); |
||
1329 | $users = $userDao->getUsersByCountryName(); |
||
1330 | |||
1331 | $this->assertEquals('UK', $users[0]->getCountry()->getLabel()); |
||
1332 | } |
||
1333 | |||
1334 | /** |
||
1335 | * @depends testDaoGeneration |
||
1336 | */ |
||
1337 | public function testResultIteratorSort(): void |
||
1338 | { |
||
1339 | $userDao = new UserDao($this->tdbmService); |
||
1340 | $users = $userDao->findAll()->withOrder('country.label DESC'); |
||
1341 | |||
1342 | $this->assertEquals('UK', $users[0]->getCountry()->getLabel()); |
||
1343 | |||
1344 | $users = $users->withOrder('country.label ASC'); |
||
1345 | $this->assertEquals('France', $users[0]->getCountry()->getLabel()); |
||
1346 | } |
||
1347 | |||
1348 | /** |
||
1349 | * @depends testDaoGeneration |
||
1350 | */ |
||
1351 | public function testResultIteratorWithParameters(): void |
||
1352 | { |
||
1353 | $userDao = new TestUserDao($this->tdbmService); |
||
1354 | $users = $userDao->getUsersByLoginStartingWith()->withParameters(['login' => 'bill%']); |
||
1355 | $this->assertEquals('bill.shakespeare', $users[0]->getLogin()); |
||
1356 | |||
1357 | $users = $users->withParameters(['login' => 'jean%']); |
||
1358 | $this->assertEquals('jean.dupont', $users[0]->getLogin()); |
||
1359 | } |
||
1360 | |||
1361 | /** |
||
1362 | * @depends testDaoGeneration |
||
1363 | */ |
||
1364 | public function testOrderByExpression(): void |
||
1365 | { |
||
1366 | $userDao = new TestUserDao($this->tdbmService); |
||
1367 | $users = $userDao->getUsersByReversedCountryName(); |
||
1368 | |||
1369 | $this->assertEquals('Jamaica', $users[0]->getCountry()->getLabel()); |
||
1370 | } |
||
1371 | |||
1372 | /** |
||
1373 | * @depends testDaoGeneration |
||
1374 | */ |
||
1375 | public function testOrderByException(): void |
||
1376 | { |
||
1377 | $userDao = new TestUserDao($this->tdbmService); |
||
1378 | $users = $userDao->getUsersByInvalidOrderBy(); |
||
1379 | $this->expectException(TDBMInvalidArgumentException::class); |
||
1380 | $user = $users[0]; |
||
1381 | } |
||
1382 | |||
1383 | /** |
||
1384 | * @depends testDaoGeneration |
||
1385 | */ |
||
1386 | public function testOrderByProtectedColumn(): void |
||
1387 | { |
||
1388 | $animalDao = new AnimalDao($this->tdbmService); |
||
1389 | $animals = $animalDao->findAll(); |
||
1390 | $animals = $animals->withOrder('`order` ASC'); |
||
1391 | |||
1392 | $this->assertInstanceOf(DogBean::class, $animals[0]); |
||
1393 | $this->assertInstanceOf(CatBean::class, $animals[1]); |
||
1394 | |||
1395 | $animals = $animals->withOrder('`order` DESC'); |
||
1396 | |||
1397 | $this->assertInstanceOf(CatBean::class, $animals[0]); |
||
1398 | $this->assertInstanceOf(DogBean::class, $animals[1]); |
||
1399 | } |
||
1400 | |||
1401 | /** |
||
1402 | * @depends testDaoGeneration |
||
1403 | */ |
||
1404 | public function testGetOnAllNullableValues(): void |
||
1405 | { |
||
1406 | // Tests that a get performed on a column that has only nullable fields succeeds. |
||
1407 | $allNullable = new AllNullableBean(); |
||
1408 | $this->assertNull($allNullable->getId()); |
||
1409 | $this->assertNull($allNullable->getLabel()); |
||
1410 | $this->assertNull($allNullable->getCountry()); |
||
1411 | } |
||
1412 | |||
1413 | /** |
||
1414 | * @depends testDaoGeneration |
||
1415 | */ |
||
1416 | public function testExceptionOnMultipleInheritance(): void |
||
1417 | { |
||
1418 | $connection = self::getConnection(); |
||
1419 | self::insert($connection, 'animal', [ |
||
1420 | 'id' => 99, 'name' => 'Snoofield', |
||
1421 | ]); |
||
1422 | self::insert($connection, 'dog', [ |
||
1423 | 'id' => 99, 'race' => 'dog', |
||
1424 | ]); |
||
1425 | self::insert($connection, 'cat', [ |
||
1426 | 'id' => 99, 'cuteness_level' => 0, |
||
1427 | ]); |
||
1428 | |||
1429 | $catched = false; |
||
1430 | try { |
||
1431 | $animalDao = new AnimalDao($this->tdbmService); |
||
1432 | $animalDao->getById(99); |
||
1433 | } catch (TDBMInheritanceException $e) { |
||
1434 | $catched = true; |
||
1435 | } |
||
1436 | $this->assertTrue($catched, 'Exception TDBMInheritanceException was not caught'); |
||
1437 | |||
1438 | self::delete($connection, 'cat', ['id' => 99]); |
||
1439 | self::delete($connection, 'dog', ['id' => 99]); |
||
1440 | self::delete($connection, 'animal', ['id' => 99]); |
||
1441 | } |
||
1442 | |||
1443 | /** |
||
1444 | * @depends testDaoGeneration |
||
1445 | */ |
||
1446 | public function testReferenceNotSaved(): void |
||
1447 | { |
||
1448 | $boatDao = new BoatDao($this->tdbmService); |
||
1449 | |||
1450 | $country = new CountryBean('Atlantis'); |
||
1451 | $boat = new BoatBean($country, 'Titanic'); |
||
1452 | |||
1453 | $boatDao->save($boat); |
||
1454 | $this->assertNotNull($country->getId()); |
||
1455 | } |
||
1456 | |||
1457 | /** |
||
1458 | * @depends testReferenceNotSaved |
||
1459 | */ |
||
1460 | public function testUniqueIndexOnForeignKeyThenScalar(): void |
||
1461 | { |
||
1462 | $boatDao = new BoatDao($this->tdbmService); |
||
1463 | $countryDao = new CountryDao($this->tdbmService); |
||
1464 | |||
1465 | $countryBean = $countryDao->findOneByLabel('Atlantis'); |
||
1466 | $boatBean = $boatDao->findOneByAnchorageCountryAndName($countryBean, 'Titanic'); |
||
1467 | |||
1468 | $this->assertNotNull($boatBean); |
||
1469 | } |
||
1470 | |||
1471 | /** |
||
1472 | * @depends testDaoGeneration |
||
1473 | */ |
||
1474 | public function testReferenceDeleted(): void |
||
1475 | { |
||
1476 | $countryDao = new CountryDao($this->tdbmService); |
||
1477 | $boatDao = new BoatDao($this->tdbmService); |
||
1478 | |||
1479 | $country = new CountryBean('Bikini Bottom'); |
||
1480 | $countryDao->save($country); |
||
1481 | |||
1482 | $boat = new BoatBean($country, 'Squirrel boat'); |
||
1483 | $countryDao->delete($country); |
||
1484 | |||
1485 | $this->expectException(TDBMMissingReferenceException::class); |
||
1486 | $boatDao->save($boat); |
||
1487 | } |
||
1488 | |||
1489 | /** |
||
1490 | * @depends testDaoGeneration |
||
1491 | */ |
||
1492 | public function testCyclicReferenceWithInheritance(): void |
||
1493 | { |
||
1494 | $userDao = new UserDao($this->tdbmService); |
||
1495 | |||
1496 | $country = new CountryBean('Norrisland'); |
||
1497 | $user = new UserBean('Chuck Norris', '[email protected]', $country, 'chuck.norris'); |
||
1498 | |||
1499 | $user->setManager($user); |
||
1500 | |||
1501 | $this->expectException(TDBMCyclicReferenceException::class); |
||
1502 | $userDao->save($user); |
||
1503 | } |
||
1504 | |||
1505 | /** |
||
1506 | * @depends testDaoGeneration |
||
1507 | */ |
||
1508 | public function testCyclicReference(): void |
||
1509 | { |
||
1510 | $categoryDao = new CategoryDao($this->tdbmService); |
||
1511 | |||
1512 | $category = new CategoryBean('Root'); |
||
1513 | |||
1514 | $category->setParent($category); |
||
1515 | |||
1516 | $this->expectException(TDBMCyclicReferenceException::class); |
||
1517 | $categoryDao->save($category); |
||
1518 | } |
||
1519 | |||
1520 | /** |
||
1521 | * @depends testDaoGeneration |
||
1522 | */ |
||
1523 | public function testCorrectTypeForPrimaryKeyAfterSave(): void |
||
1524 | { |
||
1525 | // PosqtgreSQL does not particularly like empty inserts (i.e.: "INSERT INTO all_nullable () VALUES ()" ) |
||
1526 | $this->onlyMySql(); |
||
1527 | |||
1528 | $allNullableDao = new AllNullableDao($this->tdbmService); |
||
1529 | $allNullable = new AllNullableBean(); |
||
1530 | $allNullableDao->save($allNullable); |
||
1531 | $id = $allNullable->getId(); |
||
1532 | |||
1533 | $this->assertTrue(is_int($id)); |
||
1534 | } |
||
1535 | |||
1536 | /** |
||
1537 | * @depends testDaoGeneration |
||
1538 | */ |
||
1539 | public function testPSR2Compliance(): void |
||
1540 | { |
||
1541 | $process = new Process('vendor/bin/php-cs-fixer fix src/Test/ --dry-run --diff --rules=@PSR2'); |
||
1542 | $process->run(); |
||
1543 | |||
1544 | // executes after the command finishes |
||
1545 | if (!$process->isSuccessful()) { |
||
1546 | echo $process->getOutput(); |
||
1547 | $this->fail('Generated code is not PSR-2 compliant'); |
||
1548 | } |
||
1549 | $this->assertTrue($process->isSuccessful()); |
||
1550 | } |
||
1551 | |||
1552 | /** |
||
1553 | * @depends testDaoGeneration |
||
1554 | */ |
||
1555 | public function testFindOneByGeneration(): void |
||
1556 | { |
||
1557 | $reflectionMethod = new \ReflectionMethod(UserBaseDao::class, 'findOneByLogin'); |
||
1558 | $parameters = $reflectionMethod->getParameters(); |
||
1559 | |||
1560 | $this->assertCount(2, $parameters); |
||
1561 | $this->assertSame('login', $parameters[0]->getName()); |
||
1562 | $this->assertSame('additionalTablesFetch', $parameters[1]->getName()); |
||
1563 | } |
||
1564 | |||
1565 | /** |
||
1566 | * @depends testDaoGeneration |
||
1567 | */ |
||
1568 | public function testUuid(): void |
||
1569 | { |
||
1570 | $article = new ArticleBean('content'); |
||
1571 | $this->assertSame('content', $article->getContent()); |
||
1572 | $this->assertNotEmpty($article->getId()); |
||
1573 | $uuid = Uuid::fromString($article->getId()); |
||
1574 | $this->assertSame(1, $uuid->getVersion()); |
||
1575 | } |
||
1576 | |||
1577 | /** |
||
1578 | * @depends testDaoGeneration |
||
1579 | */ |
||
1580 | public function testUuidv4(): void |
||
1581 | { |
||
1582 | $article = new Article2Bean('content'); |
||
1583 | $this->assertSame('content', $article->getContent()); |
||
1584 | $this->assertNotEmpty($article->getId()); |
||
1585 | $uuid = Uuid::fromString($article->getId()); |
||
1586 | $this->assertSame(4, $uuid->getVersion()); |
||
1587 | } |
||
1588 | |||
1589 | /** |
||
1590 | * @depends testDaoGeneration |
||
1591 | */ |
||
1592 | public function testTypeHintedConstructors(): void |
||
1593 | { |
||
1594 | $userBaseBeanReflectionConstructor = new \ReflectionMethod(UserBaseBean::class, '__construct'); |
||
1595 | $nameParam = $userBaseBeanReflectionConstructor->getParameters()[0]; |
||
1596 | |||
1597 | $this->assertSame('string', (string)$nameParam->getType()); |
||
1598 | } |
||
1599 | |||
1600 | /** |
||
1601 | * @depends testDaoGeneration |
||
1602 | */ |
||
1603 | public function testSaveTransaction(): void |
||
1604 | { |
||
1605 | $countryDao = new CountryDao($this->tdbmService); |
||
1606 | |||
1607 | $boatDao = new BoatDao($this->tdbmService); |
||
1608 | $boatBean = $boatDao->getById(1); |
||
1609 | $boatBean->setName('Bismark'); |
||
1610 | |||
1611 | $boatBean->getCountry(); |
||
1612 | |||
1613 | // Let's insert a row without telling TDBM to trigger an error! |
||
1614 | self::insert($this->getConnection(), 'sailed_countries', [ |
||
1615 | 'boat_id' => 1, |
||
1616 | 'country_id' => 2 |
||
1617 | ]); |
||
1618 | |||
1619 | $boatBean->addCountry($countryDao->getById(2)); |
||
1620 | |||
1621 | $this->expectException(UniqueConstraintViolationException::class); |
||
1622 | |||
1623 | $boatDao->save($boatBean); |
||
1624 | } |
||
1625 | |||
1626 | /** |
||
1627 | * @depends testSaveTransaction |
||
1628 | */ |
||
1629 | public function testSaveTransaction2(): void |
||
1630 | { |
||
1631 | $boatDao = new BoatDao($this->tdbmService); |
||
1632 | $boatBean = $boatDao->getById(1); |
||
1633 | |||
1634 | // The name should not have been saved because the transaction of the previous test should have rollbacked. |
||
1635 | $this->assertNotSame('Bismark', $boatBean->getName()); |
||
1636 | } |
||
1637 | |||
1638 | /** |
||
1639 | * @depends testDaoGeneration |
||
1640 | */ |
||
1641 | public function testForeignKeyPointingToNonPrimaryKey(): void |
||
1642 | { |
||
1643 | $dao = new RefNoPrimKeyDao($this->tdbmService); |
||
1644 | $bean = $dao->getById(1); |
||
1645 | |||
1646 | $this->assertSame('foo', $bean->getFrom()->getTo()); |
||
1647 | |||
1648 | $newBean = new RefNoPrimKeyBean($bean, 'baz'); |
||
1649 | $dao->save($newBean); |
||
1650 | $this->assertSame('foo', $newBean->getFrom()->getTo()); |
||
1651 | |||
1652 | $resultSet = $bean->getRefNoPrimKey(); |
||
1653 | $this->assertCount(2, $resultSet); |
||
1654 | } |
||
1655 | |||
1656 | /** |
||
1657 | * @depends testDaoGeneration |
||
1658 | */ |
||
1659 | public function testCloningUuidBean(): void |
||
1666 | } |
||
1667 | |||
1668 | /** |
||
1669 | * @depends testDaoGeneration |
||
1670 | */ |
||
1671 | public function testRecursiveSave(): void |
||
1672 | { |
||
1673 | $categoryDao = new CategoryDao($this->tdbmService); |
||
1674 | |||
1675 | $root1 = new CategoryBean('Root1'); |
||
1676 | $categoryDao->save($root1); |
||
1677 | // Root 2 is not saved yet. |
||
1678 | $root2 = new CategoryBean('Root2'); |
||
1679 | $intermediate = new CategoryBean('Intermediate'); |
||
1680 | $categoryDao->save($intermediate); |
||
1681 | |||
1682 | // Let's switch the parent to a bean in detached state. |
||
1683 | $intermediate->setParent($root2); |
||
1684 | |||
1685 | // Now, let's save a new category that references the leaf category. |
||
1686 | $leaf = new CategoryBean('Leaf'); |
||
1687 | $leaf->setParent($intermediate); |
||
1688 | $categoryDao->save($leaf); |
||
1689 | $this->assertNull($root2->getId()); |
||
1690 | } |
||
1691 | |||
1692 | /** |
||
1693 | * @depends testDaoGeneration |
||
1694 | */ |
||
1695 | public function testBlob(): void |
||
1696 | { |
||
1697 | $fp = fopen(__FILE__, 'r'); |
||
1698 | $file = new FileBean($fp); |
||
1699 | |||
1700 | $fileDao = new FileDao($this->tdbmService); |
||
1701 | |||
1702 | $fileDao->save($file); |
||
1703 | |||
1704 | $loadedFile = $fileDao->getById($file->getId()); |
||
1705 | |||
1706 | $resource = $loadedFile->getFile(); |
||
1707 | $result = fseek($resource, 0); |
||
1708 | $this->assertSame(0, $result); |
||
1709 | $this->assertInternalType('resource', $resource); |
||
1710 | $firstLine = fgets($resource); |
||
1711 | $this->assertSame("<?php\n", $firstLine); |
||
1712 | } |
||
1713 | |||
1714 | /** |
||
1715 | * @depends testBlob |
||
1716 | */ |
||
1717 | public function testReadBlob(): void |
||
1718 | { |
||
1719 | $fileDao = new FileDao($this->tdbmService); |
||
1720 | $loadedFile = $fileDao->getById(1); |
||
1721 | |||
1722 | $resource = $loadedFile->getFile(); |
||
1723 | $this->assertInternalType('resource', $resource); |
||
1724 | $firstLine = fgets($resource); |
||
1725 | $this->assertSame("<?php\n", $firstLine); |
||
1726 | |||
1727 | stream_get_contents($resource); |
||
1728 | |||
1729 | $loadedFile->setId($loadedFile->getId()); |
||
1730 | |||
1731 | $fileDao->save($loadedFile); |
||
1732 | } |
||
1733 | |||
1734 | /** |
||
1735 | * @depends testReadBlob |
||
1736 | */ |
||
1737 | public function testReadAndSaveBlob(): void |
||
1738 | { |
||
1739 | $fileDao = new FileDao($this->tdbmService); |
||
1740 | $loadedFile = $fileDao->getById(1); |
||
1741 | |||
1742 | $resource = $loadedFile->getFile(); |
||
1743 | |||
1744 | $firstLine = fgets($resource); |
||
1745 | $this->assertSame("<?php\n", $firstLine); |
||
1746 | } |
||
1747 | |||
1748 | /** |
||
1749 | * @depends testReadBlob |
||
1750 | */ |
||
1751 | public function testProtectedGetterSetter(): void |
||
1752 | { |
||
1753 | $md5Getter = new ReflectionMethod(FileBaseBean::class, 'getMd5'); |
||
1754 | $md5Setter = new ReflectionMethod(FileBaseBean::class, 'setMd5'); |
||
1755 | |||
1756 | $this->assertTrue($md5Getter->isProtected()); |
||
1757 | $this->assertTrue($md5Setter->isProtected()); |
||
1758 | |||
1759 | $md5Getter2 = new ReflectionMethod(FileBaseBean::class, 'getArticle'); |
||
1760 | $md5Setter2 = new ReflectionMethod(FileBaseBean::class, 'setArticle'); |
||
1761 | |||
1762 | $this->assertTrue($md5Getter2->isProtected()); |
||
1763 | $this->assertTrue($md5Setter2->isProtected()); |
||
1764 | |||
1765 | $oneToManyGetter = new ReflectionMethod(ArticleBaseBean::class, 'getFiles'); |
||
1766 | $this->assertTrue($oneToManyGetter->isProtected()); |
||
1767 | |||
1768 | $fileDao = new FileDao($this->tdbmService); |
||
1769 | $loadedFile = $fileDao->getById(1); |
||
1770 | |||
1771 | // The md5 and article columns are not JSON serialized |
||
1772 | $this->assertSame([ |
||
1773 | 'id' => 1, |
||
1774 | ], $loadedFile->jsonSerialize()); |
||
1775 | } |
||
1776 | |||
1777 | /** |
||
1778 | * @depends testDaoGeneration |
||
1779 | */ |
||
1780 | public function testBlobResourceException(): void |
||
1781 | { |
||
1782 | $this->expectException(TDBMInvalidArgumentException::class); |
||
1783 | $this->expectExceptionMessage('Invalid argument passed to \'TheCodingMachine\\TDBM\\Test\\Dao\\Bean\\Generated\\FileBaseBean::setFile\'. Expecting a resource. Got a string.'); |
||
1784 | new FileBean('foobar'); |
||
1785 | } |
||
1786 | |||
1787 | /** |
||
1788 | * @depends testDaoGeneration |
||
1789 | */ |
||
1790 | public function testFilterBag(): void |
||
1791 | { |
||
1792 | $userDao = new TestUserDao($this->tdbmService); |
||
1793 | $countryDao = new CountryDao($this->tdbmService); |
||
1794 | |||
1795 | $country = $countryDao->getById(2); |
||
1796 | |||
1797 | // Let's test filter bags by bean and filter bag with many values. |
||
1798 | $users = $userDao->getUsersByComplexFilterBag($country, ['John Doe', 'Jane Doe']); |
||
1799 | |||
1800 | $this->assertCount(1, $users); |
||
1801 | $this->assertSame('John Doe', $users[0]->getName()); |
||
1802 | } |
||
1803 | |||
1804 | /** |
||
1805 | * @depends testDaoGeneration |
||
1806 | */ |
||
1807 | public function testDecimalIsMappedToString(): void |
||
1808 | { |
||
1809 | $reflectionClass = new \ReflectionClass(BoatBaseBean::class); |
||
1810 | $this->assertSame('string', (string) $reflectionClass->getMethod('getLength')->getReturnType()); |
||
1811 | } |
||
1812 | |||
1813 | /** |
||
1814 | * @depends testDaoGeneration |
||
1815 | */ |
||
1816 | public function testInsertMultiPrimaryKeysBean(): void |
||
1817 | { |
||
1818 | $countryDao = new CountryDao($this->tdbmService); |
||
1819 | |||
1820 | $country = $countryDao->getById(1); |
||
1821 | |||
1822 | $stateDao = new StateDao($this->tdbmService); |
||
1823 | $state = new StateBean($country, 'IDF', 'Ile de France'); |
||
1824 | $stateDao->save($state); |
||
1825 | |||
1826 | $this->assertSame($state, $stateDao->findAll()[0]); |
||
1827 | } |
||
1828 | |||
1829 | /** |
||
1830 | * @depends testInsertMultiPrimaryKeysBean |
||
1831 | */ |
||
1832 | public function testDeleteMultiPrimaryKeysBean(): void |
||
1833 | { |
||
1834 | $stateDao = new StateDao($this->tdbmService); |
||
1835 | |||
1836 | $state = $stateDao->findAll()[0]; |
||
1837 | $stateDao->delete($state); |
||
1838 | $this->assertCount(0, $stateDao->findAll()); |
||
1839 | } |
||
1840 | |||
1841 | /** |
||
1842 | * @depends testDaoGeneration |
||
1843 | */ |
||
1844 | public function testCompositePrimaryKeyGetter(): void |
||
1845 | { |
||
1846 | $stateDao = new StateDao($this->tdbmService); |
||
1847 | $country = new CountryBean('USA'); |
||
1848 | $stateBean = new StateBean($country, 'CA', 'California'); |
||
1849 | $stateDao->save($stateBean); |
||
1850 | $this->assertSame($stateBean, $stateDao->getById($country->getId(), 'CA')); |
||
1851 | } |
||
1852 | |||
1853 | /** |
||
1854 | * @depends testDaoGeneration |
||
1855 | */ |
||
1856 | public function testSortOnInheritedTable(): void |
||
1857 | { |
||
1858 | $animalDao = new AnimalDao($this->tdbmService); |
||
1859 | |||
1860 | // Let's insert an animal that is nothing. |
||
1861 | $animal = new AnimalBean('Mickey'); |
||
1862 | $animalDao->save($animal); |
||
1863 | |||
1864 | $animals = $animalDao->findAll()->withOrder('dog.race ASC'); |
||
1865 | |||
1866 | $animalsArr = $animals->toArray(); |
||
1867 | $this->assertCount(3, $animalsArr); |
||
1868 | } |
||
1869 | |||
1870 | /** |
||
1871 | * @depends testDaoGeneration |
||
1872 | */ |
||
1873 | public function testJsonKey(): void |
||
1874 | { |
||
1875 | $node = new NodeBean('foo.html'); |
||
1876 | $json = $node->jsonSerialize(); |
||
1877 | self::assertTrue(isset($json['basename'])); |
||
1878 | self::assertEquals('foo.html', $json['basename']); |
||
1879 | } |
||
1880 | |||
1881 | /** |
||
1882 | * @depends testDaoGeneration |
||
1883 | */ |
||
1884 | public function testJsonIgnore(): void |
||
1885 | { |
||
1886 | $nodeDao = new NodeDao($this->tdbmService); |
||
1887 | $index = $nodeDao->getById(6); |
||
1888 | $json = $index->jsonSerialize(); |
||
1889 | // Ignored scalar 'id' |
||
1890 | self::assertTrue(!isset($json['id'])); |
||
1891 | // Ignored object 'root' |
||
1892 | self::assertTrue(!isset($json['root'])); |
||
1893 | self::assertTrue(isset($json['guests'])); |
||
1894 | self::assertTrue(!empty($json['guests'])); |
||
1895 | $account = $index->getAccounts()[0]; |
||
1896 | $json = $account->jsonSerialize(); |
||
1897 | // Ignored array 'nodes' (from nodes_users table) |
||
1898 | self::assertTrue(!isset($json['nodes'])); |
||
1899 | } |
||
1900 | |||
1901 | /** |
||
1902 | * @depends testDaoGeneration |
||
1903 | */ |
||
1904 | public function testJsonInclude(): void |
||
1905 | { |
||
1906 | $nodeDao = new NodeDao($this->tdbmService); |
||
1907 | $index = $nodeDao->getById(6); |
||
1908 | $json = $index->jsonSerialize(); |
||
1909 | // Whole chain of parents should be serialized |
||
1910 | self::assertTrue(isset($json['parent'])); |
||
1911 | self::assertTrue(isset($json['parent']['parent'])); |
||
1912 | self::assertTrue(isset($json['parent']['parent']['parent'])); |
||
1913 | self::assertEquals('/', $json['parent']['parent']['parent']['basename']); |
||
1914 | } |
||
1915 | |||
1916 | /** |
||
1917 | * @depends testDaoGeneration |
||
1918 | */ |
||
1919 | public function testJsonRecursive(): void |
||
1920 | { |
||
1921 | $nodeDao = new NodeDao($this->tdbmService); |
||
1922 | $index = $nodeDao->getById(8); |
||
1923 | $json = $index->jsonSerialize(); |
||
1924 | // Original chain of aliases is recursively serialized, ... |
||
1925 | self::assertTrue(isset($json['alias'])); |
||
1926 | self::assertTrue(isset($json['alias']['alias'])); |
||
1927 | self::assertEquals('index.html', $json['alias']['alias']['basename']); |
||
1928 | // ... each alias even serializes its parents, ... |
||
1929 | self::assertTrue(isset($json['alias']['alias']['parent']['parent'])); |
||
1930 | // ... however, parents aliases chains are not serialized, as parents are serialized with $stopRecursion=true |
||
1931 | self::assertTrue(!isset($json['alias']['alias']['parent']['parent']['alias'])); |
||
1932 | self::assertNotNull($index->getAlias()->getAlias()->getParent()->getParent()->getAlias()); |
||
1933 | } |
||
1934 | |||
1935 | /** |
||
1936 | * @depends testDaoGeneration |
||
1937 | */ |
||
1938 | public function testJsonFormat(): void |
||
1939 | { |
||
1940 | $nodeDao = new NodeDao($this->tdbmService); |
||
1941 | $index = $nodeDao->getById(6); |
||
1942 | $json = $index->jsonSerialize(); |
||
1943 | self::assertTrue(isset($json['size'])); |
||
1944 | self::assertEquals('512 o', $json['size']); |
||
1945 | self::assertEquals('42.50g', $json['weight']); |
||
1946 | self::assertEquals($index->getCreatedAt()->format('Y-m-d'), $json['createdAt']); |
||
1947 | self::assertEquals($index->getOwner()->getName(), $json['owner']); |
||
1948 | self::assertEquals($index->getAccounts()[1]->getName(), $json['guests'][1]); |
||
1949 | self::assertTrue(isset($json['entries'])); |
||
1950 | self::assertEquals('Hello, World', $json['entries'][1]); |
||
1951 | $www = $index->getParent(); |
||
1952 | $json = $www->jsonSerialize(); |
||
1953 | self::assertEquals('0 o', $json['size']); |
||
1954 | self::assertNull($json['weight']); |
||
1955 | self::assertNull($json['owner']); |
||
1956 | } |
||
1957 | |||
1958 | /** |
||
1959 | * @depends testDaoGeneration |
||
1960 | */ |
||
1961 | public function testJsonCollection(): void |
||
1962 | { |
||
1963 | $artists = new ArtistDao($this->tdbmService); |
||
1964 | $pinkFloyd = $artists->getById(1); |
||
1965 | $animals = $pinkFloyd->getAlbums()[0]; |
||
1966 | $json = $pinkFloyd->jsonSerialize(); |
||
1967 | // Collection name properly handled ('discography' instead of default 'albums') |
||
1968 | self::assertTrue(isset($json['discography'])); |
||
1969 | self::assertEquals($animals->getTitle(), $json['discography'][0]['title']); |
||
1970 | // Make sure top object is not its own grandfather |
||
1971 | self::assertTrue(!isset($json['discography'][0]['artist'])); |
||
1972 | $json = $animals->jsonSerialize(); |
||
1973 | // Nevertheless, artist should be serialized in album as top object... |
||
1974 | self::assertTrue(isset($json['artist'])); |
||
1975 | // ... as should be tracks... |
||
1976 | self::assertTrue(isset($json['tracks'][0])); |
||
1977 | self::assertEquals('Pigs on the Wing 1', $json['tracks'][0]['title']); |
||
1978 | // ... and, ultimately, list of featuring artists, since feat is included |
||
1979 | self::assertTrue(isset($json['tracks'][0]['feat'][0])); |
||
1980 | self::assertEquals('Roger Waters', $json['tracks'][0]['feat'][0]['name']); |
||
1981 | } |
||
1982 | |||
1983 | /** |
||
1984 | * @depends testDaoGeneration |
||
1985 | */ |
||
1986 | public function testAddInterfaceAnnotation(): void |
||
1987 | { |
||
1988 | if (!$this->tdbmService->getConnection()->getDatabasePlatform() instanceof MySqlPlatform) { |
||
1989 | // See https://github.com/doctrine/dbal/pull/3512 |
||
1990 | $this->markTestSkipped('Only MySQL supports table level comments'); |
||
1991 | } |
||
1992 | |||
1993 | $refClass = new ReflectionClass(UserBaseBean::class); |
||
1994 | $this->assertTrue($refClass->implementsInterface(TestUserInterface::class)); |
||
1995 | } |
||
1996 | |||
1997 | /** |
||
1998 | * @depends testDaoGeneration |
||
1999 | */ |
||
2000 | public function testAddInterfaceOnDaoAnnotation(): void |
||
2001 | { |
||
2002 | if (!$this->tdbmService->getConnection()->getDatabasePlatform() instanceof MySqlPlatform) { |
||
2003 | // See https://github.com/doctrine/dbal/pull/3512 |
||
2004 | $this->markTestSkipped('Only MySQL supports table level comments'); |
||
2005 | } |
||
2006 | |||
2007 | $refClass = new ReflectionClass(UserBaseDao::class); |
||
2008 | $this->assertTrue($refClass->implementsInterface(TestUserDaoInterface::class)); |
||
2009 | } |
||
2010 | |||
2011 | /** |
||
2012 | * @depends testDaoGeneration |
||
2013 | */ |
||
2014 | public function testTrait(): void |
||
2015 | { |
||
2016 | if (!$this->tdbmService->getConnection()->getDatabasePlatform() instanceof MySqlPlatform) { |
||
2017 | // See https://github.com/doctrine/dbal/pull/3512 |
||
2018 | $this->markTestSkipped('Only MySQL supports table level comments'); |
||
2019 | } |
||
2020 | |||
2021 | $userDao = new UserDao($this->tdbmService); |
||
2022 | $userBean = $userDao->getById(1); |
||
2023 | |||
2024 | $this->assertSame('TestOtherUserTrait', $userBean->method1()); |
||
2025 | $this->assertSame('TestUserTrait', $userBean->method1renamed()); |
||
2026 | |||
2027 | $refClass = new ReflectionClass(UserBaseDao::class); |
||
2028 | $this->assertTrue($refClass->hasMethod('findNothing')); |
||
2029 | } |
||
2030 | |||
2031 | /** |
||
2032 | * @depends testDaoGeneration |
||
2033 | */ |
||
2034 | public function testNonInstantiableAbstractDaosAndBeans(): void |
||
2035 | { |
||
2036 | $refClass = new ReflectionClass(UserBaseDao::class); |
||
2037 | $this->assertFalse($refClass->isInstantiable()); |
||
2038 | |||
2039 | $refClass = new ReflectionClass(UserBaseBean::class); |
||
2040 | $this->assertFalse($refClass->isInstantiable()); |
||
2041 | } |
||
2042 | |||
2043 | /** |
||
2044 | * @depends testDaoGeneration |
||
2045 | */ |
||
2046 | public function testCanNullifyBlob(): void |
||
2047 | { |
||
2048 | $article = new ArticleBean('content'); |
||
2049 | $fp = fopen(__FILE__, 'r'); |
||
2050 | $article->setAttachment($fp); |
||
2051 | $article->setAttachment(null); |
||
2052 | $this->assertNull($article->getAttachment(null)); |
||
2053 | fclose($fp); |
||
2054 | } |
||
2055 | |||
2056 | /** |
||
2057 | * @depends testDaoGeneration |
||
2058 | */ |
||
2059 | public function testOptionnalParametersCanBeNullInFindOneBy() |
||
2060 | { |
||
2061 | $albumDao = new AlbumDao($this->tdbmService); |
||
2062 | $artist = new ArtistBean('Marcel'); |
||
2063 | |||
2064 | $albumDao->findOneByArtistAndNode($artist, null); |
||
2065 | $this->assertEquals(1, 1); |
||
2066 | } |
||
2067 | |||
2068 | /** |
||
2069 | * @depends testDaoGeneration |
||
2070 | */ |
||
2071 | public function testRequiredParametersCannotBeNullInFindOneBy() |
||
2081 | } |
||
2082 | |||
2083 | /** |
||
2084 | * @depends testDaoGeneration |
||
2085 | */ |
||
2086 | public function testLazyLoad(): void |
||
2087 | { |
||
2088 | $roleDao = new RoleDao($this->tdbmService); |
||
2089 | $roleBean = $roleDao->getById(1, true); |
||
2090 | |||
2091 | $this->assertSame(TDBMObjectStateEnum::STATE_NOT_LOADED, $roleBean->_getDbRows()['roles']->_getStatus()); |
||
2092 | $roleBean->getId(); |
||
2093 | $this->assertSame(TDBMObjectStateEnum::STATE_NOT_LOADED, $roleBean->_getDbRows()['roles']->_getStatus()); |
||
2094 | } |
||
2095 | |||
2096 | /** |
||
2097 | * @depends testDaoGeneration |
||
2098 | */ |
||
2099 | public function testOneToOneInverseRelationGetter(): void |
||
2100 | { |
||
2101 | $objectBaseDao = new ObjectBaseDao($this->tdbmService); |
||
2102 | $objectInheritedDao = new ObjectInheritedDao($this->tdbmService); |
||
2103 | $objectBase = new ObjectBaseBean('label'); |
||
2104 | $objectBaseDao->save($objectBase); |
||
2105 | $this->assertNull($objectBase->getObjectInherited()); |
||
2106 | $objectInherited = new ObjectInheritedBean($objectBase); |
||
2107 | $objectInheritedDao->save($objectInherited); |
||
2108 | $this->assertSame($objectInherited, $objectBase->getObjectInherited()); |
||
2109 | $this->assertEquals(1, $objectBase->jsonSerialize()['objectInherited']['id']); |
||
2110 | } |
||
2111 | } |
||
2112 |
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