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