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