| Total Complexity | 123 |
| Total Lines | 1730 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
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 |
||
| 69 | class TDBMDaoGeneratorTest extends TDBMAbstractServiceTest |
||
| 70 | { |
||
| 71 | /** @var TDBMDaoGenerator $tdbmDaoGenerator */ |
||
| 72 | protected $tdbmDaoGenerator; |
||
| 73 | |||
| 74 | private $rootPath; |
||
| 75 | |||
| 76 | protected function setUp() |
||
| 77 | { |
||
| 78 | parent::setUp(); |
||
| 79 | $schemaManager = $this->tdbmService->getConnection()->getSchemaManager(); |
||
| 80 | $schemaAnalyzer = new SchemaAnalyzer($schemaManager); |
||
| 81 | $tdbmSchemaAnalyzer = new TDBMSchemaAnalyzer($this->tdbmService->getConnection(), new ArrayCache(), $schemaAnalyzer); |
||
| 82 | $this->tdbmDaoGenerator = new TDBMDaoGenerator($this->getConfiguration(), $tdbmSchemaAnalyzer); |
||
| 83 | $this->rootPath = __DIR__ . '/../'; |
||
| 84 | //$this->tdbmDaoGenerator->setComposerFile($this->rootPath.'composer.json'); |
||
| 85 | } |
||
| 86 | |||
| 87 | public function testDaoGeneration() |
||
| 88 | { |
||
| 89 | // Remove all previously generated files. |
||
| 90 | $this->recursiveDelete($this->rootPath . 'src/Test/Dao/'); |
||
| 91 | mkdir($this->rootPath . 'src/Test/Dao/Generated', 0755, true); |
||
| 92 | // Let's generate a dummy file to see it is indeed removed. |
||
| 93 | $dummyFile = $this->rootPath . 'src/Test/Dao/Generated/foobar.php'; |
||
| 94 | touch($dummyFile); |
||
| 95 | $this->assertFileExists($dummyFile); |
||
| 96 | |||
| 97 | $this->tdbmDaoGenerator->generateAllDaosAndBeans(); |
||
| 98 | |||
| 99 | $this->assertFileNotExists($dummyFile); |
||
| 100 | |||
| 101 | // Let's require all files to check they are valid PHP! |
||
| 102 | // Test the daoFactory |
||
| 103 | require_once $this->rootPath . 'src/Test/Dao/Generated/DaoFactory.php'; |
||
| 104 | // Test the others |
||
| 105 | |||
| 106 | $beanDescriptors = $this->getDummyGeneratorListener()->getBeanDescriptors(); |
||
| 107 | |||
| 108 | foreach ($beanDescriptors as $beanDescriptor) { |
||
| 109 | $daoName = $beanDescriptor->getDaoClassName(); |
||
| 110 | $daoBaseName = $beanDescriptor->getBaseDaoClassName(); |
||
| 111 | $beanName = $beanDescriptor->getBeanClassName(); |
||
| 112 | $baseBeanName = $beanDescriptor->getBaseBeanClassName(); |
||
| 113 | require_once $this->rootPath . 'src/Test/Dao/Bean/Generated/' . $baseBeanName . '.php'; |
||
| 114 | require_once $this->rootPath . 'src/Test/Dao/Bean/' . $beanName . '.php'; |
||
| 115 | require_once $this->rootPath . 'src/Test/Dao/Generated/' . $daoBaseName . '.php'; |
||
| 116 | require_once $this->rootPath . 'src/Test/Dao/' . $daoName . '.php'; |
||
| 117 | } |
||
| 118 | |||
| 119 | // Check that pivot tables do not generate DAOs or beans. |
||
| 120 | $this->assertFalse(class_exists('TheCodingMachine\\TDBM\\Test\\Dao\\RolesRightDao')); |
||
| 121 | } |
||
| 122 | |||
| 123 | public function testGenerationException() |
||
| 124 | { |
||
| 125 | $configuration = new Configuration('UnknownVendor\\Dao', 'UnknownVendor\\Bean', self::getConnection(), $this->getNamingStrategy()); |
||
| 126 | |||
| 127 | $schemaManager = $this->tdbmService->getConnection()->getSchemaManager(); |
||
| 128 | $schemaAnalyzer = new SchemaAnalyzer($schemaManager); |
||
| 129 | $tdbmSchemaAnalyzer = new TDBMSchemaAnalyzer($this->tdbmService->getConnection(), new ArrayCache(), $schemaAnalyzer); |
||
| 130 | $tdbmDaoGenerator = new TDBMDaoGenerator($configuration, $tdbmSchemaAnalyzer); |
||
| 131 | $this->rootPath = __DIR__ . '/../../../../'; |
||
| 132 | //$tdbmDaoGenerator->setComposerFile($this->rootPath.'composer.json'); |
||
| 133 | |||
| 134 | $this->expectException(NoPathFoundException::class); |
||
| 135 | $tdbmDaoGenerator->generateAllDaosAndBeans(); |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Delete a file or recursively delete a directory. |
||
| 140 | * |
||
| 141 | * @param string $str Path to file or directory |
||
| 142 | * @return bool |
||
| 143 | */ |
||
| 144 | private function recursiveDelete(string $str): bool |
||
| 145 | { |
||
| 146 | if (is_file($str)) { |
||
| 147 | return @unlink($str); |
||
| 148 | } elseif (is_dir($str)) { |
||
| 149 | $scan = glob(rtrim($str, '/') . '/*'); |
||
| 150 | foreach ($scan as $index => $path) { |
||
| 151 | $this->recursiveDelete($path); |
||
| 152 | } |
||
| 153 | |||
| 154 | return @rmdir($str); |
||
| 155 | } |
||
| 156 | return false; |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @depends testDaoGeneration |
||
| 161 | */ |
||
| 162 | public function testGetBeanClassName() |
||
| 163 | { |
||
| 164 | $this->assertEquals(UserBean::class, $this->tdbmService->getBeanClassName('users')); |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @depends testDaoGeneration |
||
| 169 | */ |
||
| 170 | public function testGeneratedGetById() |
||
| 171 | { |
||
| 172 | $contactDao = new ContactDao($this->tdbmService); |
||
| 173 | $contactBean = $contactDao->getById(1); |
||
| 174 | $this->assertEquals(1, $contactBean->getId()); |
||
| 175 | $this->assertInstanceOf('\\DateTimeInterface', $contactBean->getCreatedAt()); |
||
| 176 | |||
| 177 | // FIXME: Question: que faire du paramètre stockage "UTC"???? |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @depends testDaoGeneration |
||
| 182 | */ |
||
| 183 | public function testGeneratedGetByIdLazyLoaded() |
||
| 184 | { |
||
| 185 | $roleDao = new RoleDao($this->tdbmService); |
||
| 186 | $roleBean = $roleDao->getById(1, true); |
||
| 187 | $this->assertEquals(1, $roleBean->getId()); |
||
| 188 | $this->assertInstanceOf('\\DateTimeInterface', $roleBean->getCreatedAt()); |
||
| 189 | |||
| 190 | $roleBean2 = $roleDao->getById(1, true); |
||
| 191 | $this->assertTrue($roleBean === $roleBean2); |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @depends testDaoGeneration |
||
| 196 | */ |
||
| 197 | public function testDefaultValueOnNewBean() |
||
| 198 | { |
||
| 199 | $roleBean = new RoleBean('my_role'); |
||
| 200 | $this->assertEquals(1, $roleBean->getStatus()); |
||
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @depends testDaoGeneration |
||
| 205 | */ |
||
| 206 | public function testForeignKeyInBean() |
||
| 207 | { |
||
| 208 | $userDao = new UserDao($this->tdbmService); |
||
| 209 | $userBean = $userDao->getById(1); |
||
| 210 | $country = $userBean->getCountry(); |
||
| 211 | |||
| 212 | $this->assertEquals('UK', $country->getLabel()); |
||
| 213 | |||
| 214 | $userBean2 = $userDao->getById(1); |
||
| 215 | $this->assertTrue($userBean === $userBean2); |
||
| 216 | |||
| 217 | $contactDao = new ContactDao($this->tdbmService); |
||
| 218 | $contactBean = $contactDao->getById(1); |
||
| 219 | |||
| 220 | $this->assertTrue($userBean === $contactBean); |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @depends testDaoGeneration |
||
| 225 | */ |
||
| 226 | public function testNewBeans() |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @depends testDaoGeneration |
||
| 240 | */ |
||
| 241 | public function testDateTimeImmutableGetter() |
||
| 242 | { |
||
| 243 | $userDao = new UserDao($this->tdbmService); |
||
| 244 | $user = $userDao->getById(1); |
||
| 245 | |||
| 246 | $this->assertInstanceOf('\DateTimeImmutable', $user->getCreatedAt()); |
||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @depends testDaoGeneration |
||
| 251 | */ |
||
| 252 | public function testAssigningNewBeans() |
||
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @depends testAssigningNewBeans |
||
| 264 | */ |
||
| 265 | public function testUpdatingProtectedColumn() |
||
| 266 | { |
||
| 267 | $userDao = new UserDao($this->tdbmService); |
||
| 268 | $userBean = $userDao->findOneByLogin('speedy.gonzalez'); |
||
| 269 | $userBean->setOrder(2); |
||
| 270 | $userDao->save($userBean); |
||
| 271 | $this->assertSame(2, $userBean->getOrder()); |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @depends testDaoGeneration |
||
| 276 | */ |
||
| 277 | public function testAssigningExistingRelationship() |
||
| 278 | { |
||
| 279 | $userDao = new UserDao($this->tdbmService); |
||
| 280 | $user = $userDao->getById(1); |
||
| 281 | $countryDao = new CountryDao($this->tdbmService); |
||
| 282 | $country = $countryDao->getById(2); |
||
| 283 | |||
| 284 | $user->setCountry($country); |
||
| 285 | $this->assertEquals(TDBMObjectStateEnum::STATE_DIRTY, $user->_getStatus()); |
||
| 286 | } |
||
| 287 | |||
| 288 | /** |
||
| 289 | * @depends testDaoGeneration |
||
| 290 | */ |
||
| 291 | public function testDirectReversedRelationship() |
||
| 292 | { |
||
| 293 | $countryDao = new CountryDao($this->tdbmService); |
||
| 294 | $country = $countryDao->getById(1); |
||
| 295 | $users = $country->getUsers(); |
||
| 296 | |||
| 297 | $arr = $users->toArray(); |
||
| 298 | |||
| 299 | $this->assertCount(1, $arr); |
||
| 300 | $this->assertInstanceOf('TheCodingMachine\\TDBM\\Test\\Dao\\Bean\\UserBean', $arr[0]); |
||
| 301 | $this->assertEquals('jean.dupont', $arr[0]->getLogin()); |
||
| 302 | |||
| 303 | $newUser = new UserBean('Speedy Gonzalez', '[email protected]', $country, 'speedy.gonzalez'); |
||
| 304 | $users = $country->getUsers(); |
||
| 305 | |||
| 306 | $arr = $users->toArray(); |
||
| 307 | |||
| 308 | $this->assertCount(2, $arr); |
||
| 309 | $this->assertInstanceOf('TheCodingMachine\\TDBM\\Test\\Dao\\Bean\\UserBean', $arr[1]); |
||
| 310 | $this->assertEquals('speedy.gonzalez', $arr[1]->getLogin()); |
||
| 311 | } |
||
| 312 | |||
| 313 | /** |
||
| 314 | * @depends testDaoGeneration |
||
| 315 | */ |
||
| 316 | public function testDeleteInDirectReversedRelationship() |
||
| 317 | { |
||
| 318 | $countryDao = new CountryDao($this->tdbmService); |
||
| 319 | $country = $countryDao->getById(1); |
||
| 320 | |||
| 321 | $userDao = new UserDao($this->tdbmService); |
||
| 322 | $newUser = new UserBean('John Snow', '[email protected]', $country, 'john.snow'); |
||
| 323 | $userDao->save($newUser); |
||
| 324 | |||
| 325 | $users = $country->getUsers(); |
||
| 326 | |||
| 327 | $arr = $users->toArray(); |
||
| 328 | |||
| 329 | $this->assertCount(2, $arr); |
||
| 330 | |||
| 331 | $userDao->delete($arr[1]); |
||
| 332 | |||
| 333 | $users = $country->getUsers(); |
||
| 334 | $arr = $users->toArray(); |
||
| 335 | $this->assertCount(1, $arr); |
||
| 336 | } |
||
| 337 | |||
| 338 | /** |
||
| 339 | * @depends testDaoGeneration |
||
| 340 | */ |
||
| 341 | public function testJointureGetters() |
||
| 342 | { |
||
| 343 | $roleDao = new RoleDao($this->tdbmService); |
||
| 344 | $role = $roleDao->getById(1); |
||
| 345 | $users = $role->getUsers(); |
||
| 346 | |||
| 347 | $this->assertCount(2, $users); |
||
| 348 | $this->assertInstanceOf('TheCodingMachine\\TDBM\\Test\\Dao\\Bean\\UserBean', $users[0]); |
||
| 349 | |||
| 350 | $rights = $role->getRights(); |
||
| 351 | |||
| 352 | $this->assertCount(2, $rights); |
||
| 353 | $this->assertInstanceOf('TheCodingMachine\\TDBM\\Test\\Dao\\Bean\\RightBean', $rights[0]); |
||
| 354 | } |
||
| 355 | |||
| 356 | /** |
||
| 357 | * @depends testDaoGeneration |
||
| 358 | */ |
||
| 359 | public function testNestedIterationOnAlterableResultIterator() |
||
| 360 | { |
||
| 361 | $countryDao = new CountryDao($this->tdbmService); |
||
| 362 | $country = $countryDao->getById(2); |
||
| 363 | |||
| 364 | $count = 0; |
||
| 365 | // Let's perform 2 nested calls to check that iterators do not melt. |
||
| 366 | foreach ($country->getUsers() as $user) { |
||
| 367 | foreach ($country->getUsers() as $user2) { |
||
| 368 | $count++; |
||
| 369 | } |
||
| 370 | } |
||
| 371 | // There are 3 users linked to country 2. |
||
| 372 | $this->assertSame(9, $count); |
||
| 373 | } |
||
| 374 | |||
| 375 | /** |
||
| 376 | * @depends testDaoGeneration |
||
| 377 | */ |
||
| 378 | public function testNewBeanConstructor() |
||
| 379 | { |
||
| 380 | $role = new RoleBean('Newrole'); |
||
| 381 | $this->assertEquals(TDBMObjectStateEnum::STATE_DETACHED, $role->_getStatus()); |
||
| 382 | } |
||
| 383 | |||
| 384 | /** |
||
| 385 | * @depends testDaoGeneration |
||
| 386 | */ |
||
| 387 | public function testJointureAdderOnNewBean() |
||
| 388 | { |
||
| 389 | $countryDao = new CountryDao($this->tdbmService); |
||
| 390 | $country = $countryDao->getById(1); |
||
| 391 | $user = new UserBean('Speedy Gonzalez', '[email protected]', $country, 'speedy.gonzalez'); |
||
| 392 | $role = new RoleBean('Mouse'); |
||
| 393 | $user->addRole($role); |
||
| 394 | $roles = $user->getRoles(); |
||
| 395 | $this->assertCount(1, $roles); |
||
| 396 | $role = $roles[0]; |
||
| 397 | $this->assertInstanceOf('TheCodingMachine\\TDBM\\Test\\Dao\\Bean\\RoleBean', $role); |
||
| 398 | $users = $role->getUsers(); |
||
| 399 | $this->assertCount(1, $users); |
||
| 400 | $this->assertEquals($user, $users[0]); |
||
| 401 | |||
| 402 | $role->removeUser($user); |
||
| 403 | $this->assertCount(0, $role->getUsers()); |
||
| 404 | $this->assertCount(0, $user->getRoles()); |
||
| 405 | } |
||
| 406 | |||
| 407 | /** |
||
| 408 | * @depends testDaoGeneration |
||
| 409 | */ |
||
| 410 | public function testJointureDeleteBeforeGetters() |
||
| 411 | { |
||
| 412 | $roleDao = new RoleDao($this->tdbmService); |
||
| 413 | $userDao = new UserDao($this->tdbmService); |
||
| 414 | $role = $roleDao->getById(1); |
||
| 415 | $user = $userDao->getById(1); |
||
| 416 | |||
| 417 | // We call removeUser BEFORE calling getUsers |
||
| 418 | // This should work as expected. |
||
| 419 | $role->removeUser($user); |
||
| 420 | $users = $role->getUsers(); |
||
| 421 | |||
| 422 | $this->assertCount(1, $users); |
||
| 423 | } |
||
| 424 | |||
| 425 | /** |
||
| 426 | * @depends testDaoGeneration |
||
| 427 | */ |
||
| 428 | public function testJointureMultiAdd() |
||
| 429 | { |
||
| 430 | $countryDao = new CountryDao($this->tdbmService); |
||
| 431 | $country = $countryDao->getById(1); |
||
| 432 | $user = new UserBean('Speedy Gonzalez', '[email protected]', $country, 'speedy.gonzalez'); |
||
| 433 | $role = new RoleBean('Mouse'); |
||
| 434 | $user->addRole($role); |
||
| 435 | $role->addUser($user); |
||
| 436 | $user->addRole($role); |
||
| 437 | |||
| 438 | $this->assertCount(1, $user->getRoles()); |
||
| 439 | } |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Step 1: we remove the role 1 from user 1 but save role 1. |
||
| 443 | * Expected result: no save done. |
||
| 444 | * |
||
| 445 | * @depends testDaoGeneration |
||
| 446 | */ |
||
| 447 | public function testJointureSave1() |
||
| 448 | { |
||
| 449 | $roleDao = new RoleDao($this->tdbmService); |
||
| 450 | $role = $roleDao->getById(1); |
||
| 451 | $userDao = new UserDao($this->tdbmService); |
||
| 452 | $user = $userDao->getById(1); |
||
| 453 | |||
| 454 | $this->assertTrue($user->hasRole($role)); |
||
| 455 | $this->assertTrue($role->hasUser($user)); |
||
| 456 | $user->removeRole($role); |
||
| 457 | $this->assertFalse($user->hasRole($role)); |
||
| 458 | $this->assertFalse($role->hasUser($user)); |
||
| 459 | $roleDao->save($role); |
||
| 460 | |||
| 461 | $this->assertEquals(TDBMObjectStateEnum::STATE_DIRTY, $user->_getStatus()); |
||
| 462 | $this->assertEquals(TDBMObjectStateEnum::STATE_LOADED, $role->_getStatus()); |
||
| 463 | } |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Step 2: we check that nothing was saved |
||
| 467 | * Expected result: no save done. |
||
| 468 | * |
||
| 469 | * @depends testJointureSave1 |
||
| 470 | */ |
||
| 471 | public function testJointureSave2() |
||
| 472 | { |
||
| 473 | $roleDao = new RoleDao($this->tdbmService); |
||
| 474 | $role = $roleDao->getById(1); |
||
| 475 | $this->assertCount(2, $role->getUsers()); |
||
| 476 | } |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Step 3: we remove the role 1 from user 1 and save user 1. |
||
| 480 | * Expected result: save done. |
||
| 481 | * |
||
| 482 | * @depends testJointureSave2 |
||
| 483 | */ |
||
| 484 | public function testJointureSave3() |
||
| 485 | { |
||
| 486 | $roleDao = new RoleDao($this->tdbmService); |
||
| 487 | $role = $roleDao->getById(1); |
||
| 488 | $userDao = new UserDao($this->tdbmService); |
||
| 489 | $user = $userDao->getById(1); |
||
| 490 | |||
| 491 | $this->assertCount(1, $user->getRoles()); |
||
| 492 | $user->removeRole($role); |
||
| 493 | $this->assertCount(0, $user->getRoles()); |
||
| 494 | $userDao->save($user); |
||
| 495 | $this->assertCount(0, $user->getRoles()); |
||
| 496 | } |
||
| 497 | |||
| 498 | /** |
||
| 499 | * Step 4: we check that save was done |
||
| 500 | * Expected result: save done. |
||
| 501 | * |
||
| 502 | * @depends testJointureSave3 |
||
| 503 | */ |
||
| 504 | public function testJointureSave4() |
||
| 505 | { |
||
| 506 | $roleDao = new RoleDao($this->tdbmService); |
||
| 507 | $role = $roleDao->getById(1); |
||
| 508 | $this->assertCount(1, $role->getUsers()); |
||
| 509 | $userDao = new UserDao($this->tdbmService); |
||
| 510 | $user = $userDao->getById(1); |
||
| 511 | $this->assertCount(0, $user->getRoles()); |
||
| 512 | } |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Step 5: we add the role 1 from user 1 and save user 1. |
||
| 516 | * Expected result: save done. |
||
| 517 | * |
||
| 518 | * @depends testJointureSave4 |
||
| 519 | */ |
||
| 520 | public function testJointureSave5() |
||
| 521 | { |
||
| 522 | $roleDao = new RoleDao($this->tdbmService); |
||
| 523 | $role = $roleDao->getById(1); |
||
| 524 | $userDao = new UserDao($this->tdbmService); |
||
| 525 | $user = $userDao->getById(1); |
||
| 526 | |||
| 527 | $user->addRole($role); |
||
| 528 | $this->assertCount(1, $user->getRoles()); |
||
| 529 | $userDao->save($user); |
||
| 530 | } |
||
| 531 | |||
| 532 | /** |
||
| 533 | * Step 6: we check that save was done |
||
| 534 | * Expected result: save done. |
||
| 535 | * |
||
| 536 | * @depends testJointureSave5 |
||
| 537 | */ |
||
| 538 | public function testJointureSave6() |
||
| 539 | { |
||
| 540 | $roleDao = new RoleDao($this->tdbmService); |
||
| 541 | $role = $roleDao->getById(1); |
||
| 542 | $this->assertCount(2, $role->getUsers()); |
||
| 543 | $userDao = new UserDao($this->tdbmService); |
||
| 544 | $user = $userDao->getById(1); |
||
| 545 | $this->assertCount(1, $user->getRoles()); |
||
| 546 | } |
||
| 547 | |||
| 548 | /** |
||
| 549 | * Step 7: we add a new role to user 1 and save user 1. |
||
| 550 | * Expected result: save done, including the new role. |
||
| 551 | * |
||
| 552 | * @depends testJointureSave6 |
||
| 553 | */ |
||
| 554 | public function testJointureSave7() |
||
| 555 | { |
||
| 556 | $role = new RoleBean('my new role'); |
||
| 557 | $userDao = new UserDao($this->tdbmService); |
||
| 558 | $user = $userDao->getById(1); |
||
| 559 | |||
| 560 | $user->addRole($role); |
||
| 561 | $userDao->save($user); |
||
| 562 | |||
| 563 | $this->assertEquals(TDBMObjectStateEnum::STATE_LOADED, $role->_getStatus()); |
||
| 564 | } |
||
| 565 | |||
| 566 | /** |
||
| 567 | * Step 8: we check that save was done |
||
| 568 | * Expected result: save done. |
||
| 569 | * |
||
| 570 | * @depends testJointureSave7 |
||
| 571 | */ |
||
| 572 | public function testJointureSave8() |
||
| 573 | { |
||
| 574 | $roleDao = new RoleDao($this->tdbmService); |
||
| 575 | $userDao = new UserDao($this->tdbmService); |
||
| 576 | $user = $userDao->getById(1); |
||
| 577 | |||
| 578 | $roles = $user->getRoles(); |
||
| 579 | foreach ($roles as $role) { |
||
| 580 | if ($role->getName() === 'my new role') { |
||
| 581 | $selectedRole = $role; |
||
| 582 | break; |
||
| 583 | } |
||
| 584 | } |
||
| 585 | $this->assertNotNull($selectedRole); |
||
| 586 | |||
| 587 | $this->assertCount(2, $user->getRoles()); |
||
| 588 | |||
| 589 | // Expected: relationship removed! |
||
| 590 | $roleDao->delete($selectedRole); |
||
| 591 | |||
| 592 | $this->assertCount(1, $user->getRoles()); |
||
| 593 | } |
||
| 594 | |||
| 595 | /** |
||
| 596 | * Step 9: Let's test the setXXX method. |
||
| 597 | * |
||
| 598 | * @depends testJointureSave8 |
||
| 599 | */ |
||
| 600 | public function testJointureSave9() |
||
| 601 | { |
||
| 602 | $roleDao = new RoleDao($this->tdbmService); |
||
| 603 | $userDao = new UserDao($this->tdbmService); |
||
| 604 | $user = $userDao->getById(1); |
||
| 605 | |||
| 606 | // At this point, user 1 is linked to role 1. |
||
| 607 | // Let's bind it to role 2. |
||
| 608 | $user->setRoles([$roleDao->getById(2)]); |
||
| 609 | $userDao->save($user); |
||
| 610 | $this->assertTrue($user->hasRole($roleDao->getById(2))); |
||
| 611 | } |
||
| 612 | |||
| 613 | /** |
||
| 614 | * Step 10: Let's check results of 9. |
||
| 615 | * |
||
| 616 | * @depends testJointureSave9 |
||
| 617 | */ |
||
| 618 | public function testJointureSave10() |
||
| 619 | { |
||
| 620 | $userDao = new UserDao($this->tdbmService); |
||
| 621 | $user = $userDao->getById(1); |
||
| 622 | |||
| 623 | $roles = $user->getRoles(); |
||
| 624 | |||
| 625 | $this->assertCount(1, $roles); |
||
| 626 | $this->assertEquals(2, $roles[0]->getId()); |
||
| 627 | } |
||
| 628 | |||
| 629 | /** |
||
| 630 | * @depends testDaoGeneration |
||
| 631 | */ |
||
| 632 | public function testFindOrderBy() |
||
| 633 | { |
||
| 634 | $userDao = new TestUserDao($this->tdbmService); |
||
| 635 | $users = $userDao->getUsersByAlphabeticalOrder(); |
||
| 636 | |||
| 637 | $this->assertCount(6, $users); |
||
| 638 | $this->assertEquals('bill.shakespeare', $users[0]->getLogin()); |
||
| 639 | $this->assertEquals('jean.dupont', $users[1]->getLogin()); |
||
| 640 | |||
| 641 | $users = $userDao->getUsersByCountryOrder(); |
||
| 642 | $this->assertCount(6, $users); |
||
| 643 | $countryName1 = $users[0]->getCountry()->getLabel(); |
||
| 644 | for ($i = 1; $i < 6; $i++) { |
||
| 645 | $countryName2 = $users[$i]->getCountry()->getLabel(); |
||
| 646 | $this->assertLessThanOrEqual(0, strcmp($countryName1, $countryName2)); |
||
| 647 | $countryName1 = $countryName2; |
||
| 648 | } |
||
| 649 | } |
||
| 650 | |||
| 651 | /** |
||
| 652 | * @depends testDaoGeneration |
||
| 653 | */ |
||
| 654 | public function testFindFromSqlOrderBy() |
||
| 655 | { |
||
| 656 | $userDao = new TestUserDao($this->tdbmService); |
||
| 657 | $users = $userDao->getUsersFromSqlByAlphabeticalOrder(); |
||
| 658 | |||
| 659 | $this->assertCount(6, $users); |
||
| 660 | $this->assertEquals('bill.shakespeare', $users[0]->getLogin()); |
||
| 661 | $this->assertEquals('jean.dupont', $users[1]->getLogin()); |
||
| 662 | |||
| 663 | $users = $userDao->getUsersFromSqlByCountryOrder(); |
||
| 664 | $this->assertCount(6, $users); |
||
| 665 | $countryName1 = $users[0]->getCountry()->getLabel(); |
||
| 666 | for ($i = 1; $i < 6; $i++) { |
||
| 667 | $countryName2 = $users[$i]->getCountry()->getLabel(); |
||
| 668 | $this->assertLessThanOrEqual(0, strcmp($countryName1, $countryName2)); |
||
| 669 | $countryName1 = $countryName2; |
||
| 670 | } |
||
| 671 | } |
||
| 672 | |||
| 673 | /** |
||
| 674 | * @depends testDaoGeneration |
||
| 675 | */ |
||
| 676 | public function testFindFromSqlOrderByOnInheritedBean() |
||
| 677 | { |
||
| 678 | $articleDao = new TestArticleDao($this->tdbmService); |
||
| 679 | $articles = $articleDao->getArticlesByUserLogin(); |
||
| 680 | |||
| 681 | foreach ($articles as $article) { |
||
| 682 | var_dump($article); |
||
| 683 | } |
||
| 684 | $this->assertCount(0, $articles); |
||
| 685 | } |
||
| 686 | |||
| 687 | |||
| 688 | /** |
||
| 689 | * @depends testDaoGeneration |
||
| 690 | */ |
||
| 691 | public function testFindFromSqlOrderByJoinRole() |
||
| 692 | { |
||
| 693 | $roleDao = new TestRoleDao($this->tdbmService); |
||
| 694 | $roles = $roleDao->getRolesByRightCanSing('roles.name DESC'); |
||
| 695 | |||
| 696 | $this->assertCount(2, $roles); |
||
| 697 | $this->assertEquals('Singers', $roles[0]->getName()); |
||
| 698 | $this->assertEquals('Admins', $roles[1]->getName()); |
||
| 699 | } |
||
| 700 | |||
| 701 | /** |
||
| 702 | * @depends testDaoGeneration |
||
| 703 | */ |
||
| 704 | public function testFindFromRawSqlOrderByUserCount() |
||
| 705 | { |
||
| 706 | $countryDao = new TestCountryDao($this->tdbmService); |
||
| 707 | $countries = $countryDao->getCountriesByUserCount(); |
||
| 708 | |||
| 709 | $this->assertCount(4, $countries); |
||
| 710 | for ($i = 1; $i < count($countries); $i++) { |
||
| 711 | $this->assertLessThanOrEqual($countries[$i - 1]->getUsers()->count(), $countries[$i]->getUsers()->count()); |
||
| 712 | } |
||
| 713 | } |
||
| 714 | |||
| 715 | /** |
||
| 716 | * @depends testDaoGeneration |
||
| 717 | */ |
||
| 718 | public function testFindFromRawSqlWithUnion() |
||
| 719 | { |
||
| 720 | $countryDao = new TestCountryDao($this->tdbmService); |
||
| 721 | $countries = $countryDao->getCountriesUsingUnion(); |
||
| 722 | |||
| 723 | $this->assertCount(2, $countries); |
||
| 724 | $this->assertEquals(1, $countries[0]->getId()); |
||
| 725 | } |
||
| 726 | |||
| 727 | /** |
||
| 728 | * @depends testDaoGeneration |
||
| 729 | */ |
||
| 730 | public function testFindFromRawSqlWithSimpleQuery() |
||
| 731 | { |
||
| 732 | $countryDao = new TestCountryDao($this->tdbmService); |
||
| 733 | $countries = $countryDao->getCountriesUsingSimpleQuery(); |
||
| 734 | |||
| 735 | $this->assertCount(1, $countries); |
||
| 736 | $this->assertEquals(1, $countries[0]->getId()); |
||
| 737 | } |
||
| 738 | |||
| 739 | /** |
||
| 740 | * @depends testDaoGeneration |
||
| 741 | */ |
||
| 742 | public function testFindFromRawSqlWithDistinctQuery() |
||
| 743 | { |
||
| 744 | $countryDao = new TestCountryDao($this->tdbmService); |
||
| 745 | $countries = $countryDao->getCountriesUsingDistinctQuery(); |
||
| 746 | |||
| 747 | $this->assertCount(1, $countries); |
||
| 748 | $this->assertEquals(2, $countries[0]->getId()); |
||
| 749 | } |
||
| 750 | |||
| 751 | /** |
||
| 752 | * @depends testDaoGeneration |
||
| 753 | */ |
||
| 754 | public function testFindFilters() |
||
| 755 | { |
||
| 756 | $userDao = new TestUserDao($this->tdbmService); |
||
| 757 | $users = $userDao->getUsersByLoginStartingWith('bill'); |
||
| 758 | |||
| 759 | $this->assertCount(1, $users); |
||
| 760 | $this->assertEquals('bill.shakespeare', $users[0]->getLogin()); |
||
| 761 | } |
||
| 762 | |||
| 763 | /** |
||
| 764 | * @expectedException \TheCodingMachine\TDBM\TDBMException |
||
| 765 | * @depends testDaoGeneration |
||
| 766 | */ |
||
| 767 | public function testFindMode() |
||
| 768 | { |
||
| 769 | $userDao = new TestUserDao($this->tdbmService); |
||
| 770 | $users = $userDao->getUsersByLoginStartingWith('bill', TDBMService::MODE_CURSOR); |
||
| 771 | |||
| 772 | $users[0]; |
||
| 773 | } |
||
| 774 | |||
| 775 | /** |
||
| 776 | * @depends testDaoGeneration |
||
| 777 | */ |
||
| 778 | public function testFindAll() |
||
| 779 | { |
||
| 780 | $userDao = new TestUserDao($this->tdbmService); |
||
| 781 | $users = $userDao->findAll(); |
||
| 782 | |||
| 783 | $this->assertCount(6, $users); |
||
| 784 | } |
||
| 785 | |||
| 786 | /** |
||
| 787 | * @depends testDaoGeneration |
||
| 788 | */ |
||
| 789 | public function testFindOne() |
||
| 790 | { |
||
| 791 | $userDao = new TestUserDao($this->tdbmService); |
||
| 792 | $user = $userDao->getUserByLogin('bill.shakespeare'); |
||
| 793 | |||
| 794 | $this->assertEquals('bill.shakespeare', $user->getLogin()); |
||
| 795 | } |
||
| 796 | |||
| 797 | /** |
||
| 798 | * @depends testDaoGeneration |
||
| 799 | */ |
||
| 800 | public function testJsonEncodeBean() |
||
| 801 | { |
||
| 802 | $userDao = new TestUserDao($this->tdbmService); |
||
| 803 | $user = $userDao->getUserByLogin('bill.shakespeare'); |
||
| 804 | |||
| 805 | $jsonEncoded = json_encode($user); |
||
| 806 | $userDecoded = json_decode($jsonEncoded, true); |
||
| 807 | |||
| 808 | $this->assertEquals('bill.shakespeare', $userDecoded['login']); |
||
| 809 | |||
| 810 | // test serialization of dates. |
||
| 811 | $this->assertTrue(is_string($userDecoded['createdAt'])); |
||
| 812 | $this->assertEquals('2015-10-24', (new \DateTimeImmutable($userDecoded['createdAt']))->format('Y-m-d')); |
||
| 813 | $this->assertNull($userDecoded['modifiedAt']); |
||
| 814 | |||
| 815 | // testing many to 1 relationships |
||
| 816 | $this->assertEquals('UK', $userDecoded['country']['label']); |
||
| 817 | |||
| 818 | // testing many to many relationships |
||
| 819 | $this->assertCount(1, $userDecoded['roles']); |
||
| 820 | $this->assertArrayNotHasKey('users', $userDecoded['roles'][0]); |
||
| 821 | $this->assertArrayNotHasKey('rights', $userDecoded['roles'][0]); |
||
| 822 | } |
||
| 823 | |||
| 824 | /** |
||
| 825 | * @depends testDaoGeneration |
||
| 826 | */ |
||
| 827 | public function testNullableForeignKey() |
||
| 828 | { |
||
| 829 | $userDao = new TestUserDao($this->tdbmService); |
||
| 830 | $user = $userDao->getUserByLogin('john.smith'); |
||
| 831 | |||
| 832 | $this->assertNull($user->getManager()); |
||
| 833 | |||
| 834 | $jsonEncoded = json_encode($user); |
||
| 835 | $userDecoded = json_decode($jsonEncoded, true); |
||
| 836 | |||
| 837 | $this->assertNull($userDecoded['manager']); |
||
| 838 | } |
||
| 839 | |||
| 840 | /** |
||
| 841 | * Test that setting (and saving) objects' references (foreign keys relations) to null is working. |
||
| 842 | * |
||
| 843 | * @depends testDaoGeneration |
||
| 844 | */ |
||
| 845 | public function testSetToNullForeignKey() |
||
| 846 | { |
||
| 847 | $userDao = new TestUserDao($this->tdbmService); |
||
| 848 | $user = $userDao->getUserByLogin('john.smith'); |
||
| 849 | $manager = $userDao->getUserByLogin('jean.dupont'); |
||
| 850 | |||
| 851 | $user->setManager($manager); |
||
| 852 | $userDao->save($user); |
||
| 853 | |||
| 854 | $user->setManager(null); |
||
| 855 | $userDao->save($user); |
||
| 856 | $this->assertNull($user->getManager()); |
||
| 857 | } |
||
| 858 | |||
| 859 | /** |
||
| 860 | * @depends testDaoGeneration |
||
| 861 | * @expectedException \Mouf\Database\SchemaAnalyzer\SchemaAnalyzerTableNotFoundException |
||
| 862 | * @expectedExceptionMessage Could not find table 'contacts'. Did you mean 'contact'? |
||
| 863 | */ |
||
| 864 | public function testQueryOnWrongTableName() |
||
| 865 | { |
||
| 866 | $userDao = new TestUserDao($this->tdbmService); |
||
| 867 | $users = $userDao->getUsersWrongTableName(); |
||
| 868 | $users->count(); |
||
| 869 | } |
||
| 870 | |||
| 871 | /** |
||
| 872 | * @depends testDaoGeneration |
||
| 873 | */ |
||
| 874 | /*public function testQueryNullForeignKey() |
||
| 875 | { |
||
| 876 | $userDao = new TestUserDao($this->tdbmService); |
||
| 877 | $users = $userDao->getUsersByManagerId(null); |
||
| 878 | $this->assertCount(3, $users); |
||
| 879 | }*/ |
||
| 880 | |||
| 881 | /** |
||
| 882 | * @depends testDaoGeneration |
||
| 883 | */ |
||
| 884 | public function testInnerJsonEncode() |
||
| 885 | { |
||
| 886 | $userDao = new TestUserDao($this->tdbmService); |
||
| 887 | $user = $userDao->getUserByLogin('bill.shakespeare'); |
||
| 888 | |||
| 889 | $jsonEncoded = json_encode(['user' => $user]); |
||
| 890 | $msgDecoded = json_decode($jsonEncoded, true); |
||
| 891 | |||
| 892 | $this->assertEquals('bill.shakespeare', $msgDecoded['user']['login']); |
||
| 893 | } |
||
| 894 | |||
| 895 | /** |
||
| 896 | * @depends testDaoGeneration |
||
| 897 | */ |
||
| 898 | public function testArrayJsonEncode() |
||
| 899 | { |
||
| 900 | $userDao = new TestUserDao($this->tdbmService); |
||
| 901 | $users = $userDao->getUsersByLoginStartingWith('bill'); |
||
| 902 | |||
| 903 | $jsonEncoded = json_encode($users); |
||
| 904 | $msgDecoded = json_decode($jsonEncoded, true); |
||
| 905 | |||
| 906 | $this->assertCount(1, $msgDecoded); |
||
| 907 | } |
||
| 908 | |||
| 909 | /** |
||
| 910 | * @depends testDaoGeneration |
||
| 911 | */ |
||
| 912 | public function testCursorJsonEncode() |
||
| 913 | { |
||
| 914 | $userDao = new TestUserDao($this->tdbmService); |
||
| 915 | $users = $userDao->getUsersByLoginStartingWith('bill', TDBMService::MODE_CURSOR); |
||
| 916 | |||
| 917 | $jsonEncoded = json_encode($users); |
||
| 918 | $msgDecoded = json_decode($jsonEncoded, true); |
||
| 919 | |||
| 920 | $this->assertCount(1, $msgDecoded); |
||
| 921 | } |
||
| 922 | |||
| 923 | /** |
||
| 924 | * @depends testDaoGeneration |
||
| 925 | */ |
||
| 926 | public function testPageJsonEncode() |
||
| 927 | { |
||
| 928 | $userDao = new TestUserDao($this->tdbmService); |
||
| 929 | $users = $userDao->getUsersByLoginStartingWith('bill'); |
||
| 930 | |||
| 931 | $jsonEncoded = json_encode($users->take(0, 1)); |
||
| 932 | $msgDecoded = json_decode($jsonEncoded, true); |
||
| 933 | |||
| 934 | $this->assertCount(1, $msgDecoded); |
||
| 935 | } |
||
| 936 | |||
| 937 | /** |
||
| 938 | * @depends testDaoGeneration |
||
| 939 | */ |
||
| 940 | public function testFirst() |
||
| 941 | { |
||
| 942 | $userDao = new TestUserDao($this->tdbmService); |
||
| 943 | $users = $userDao->getUsersByLoginStartingWith('bill'); |
||
| 944 | |||
| 945 | $bill = $users->first(); |
||
| 946 | $this->assertEquals('bill.shakespeare', $bill->getLogin()); |
||
| 947 | } |
||
| 948 | |||
| 949 | /** |
||
| 950 | * @depends testDaoGeneration |
||
| 951 | */ |
||
| 952 | public function testFirstNull() |
||
| 953 | { |
||
| 954 | $userDao = new TestUserDao($this->tdbmService); |
||
| 955 | $users = $userDao->getUsersByLoginStartingWith('mike'); |
||
| 956 | |||
| 957 | $user = $users->first(); |
||
| 958 | $this->assertNull($user); |
||
| 959 | } |
||
| 960 | |||
| 961 | /** |
||
| 962 | * @depends testDaoGeneration |
||
| 963 | */ |
||
| 964 | public function testCloneBeanAttachedBean() |
||
| 965 | { |
||
| 966 | $userDao = new TestUserDao($this->tdbmService); |
||
| 967 | $user = $userDao->getUserByLogin('bill.shakespeare'); |
||
| 968 | $this->assertEquals(4, $user->getId()); |
||
| 969 | $user2 = clone $user; |
||
| 970 | $this->assertNull($user2->getId()); |
||
| 971 | $this->assertEquals('bill.shakespeare', $user2->getLogin()); |
||
| 972 | $this->assertEquals('Bill Shakespeare', $user2->getName()); |
||
| 973 | $this->assertEquals('UK', $user2->getCountry()->getLabel()); |
||
| 974 | |||
| 975 | // MANY 2 MANY must be duplicated |
||
| 976 | $this->assertEquals('Writers', $user2->getRoles()[0]->getName()); |
||
| 977 | |||
| 978 | // Let's test saving this clone |
||
| 979 | $user2->setLogin('william.shakespeare'); |
||
| 980 | $userDao->save($user2); |
||
| 981 | |||
| 982 | $user3 = $userDao->getUserByLogin('william.shakespeare'); |
||
| 983 | $this->assertTrue($user3 === $user2); |
||
| 984 | $userDao->delete($user3); |
||
| 985 | |||
| 986 | // Finally, let's test the origin user still exists! |
||
| 987 | $user4 = $userDao->getUserByLogin('bill.shakespeare'); |
||
| 988 | $this->assertEquals('bill.shakespeare', $user4->getLogin()); |
||
| 989 | } |
||
| 990 | |||
| 991 | /** |
||
| 992 | * @depends testDaoGeneration |
||
| 993 | */ |
||
| 994 | public function testCloneNewBean() |
||
| 995 | { |
||
| 996 | $countryDao = new CountryDao($this->tdbmService); |
||
| 997 | $roleDao = new RoleDao($this->tdbmService); |
||
| 998 | $role = $roleDao->getById(1); |
||
| 999 | |||
| 1000 | $userBean = new UserBean('John Doe', '[email protected]', $countryDao->getById(2), 'john.doe'); |
||
| 1001 | $userBean->addRole($role); |
||
| 1002 | |||
| 1003 | $user2 = clone $userBean; |
||
| 1004 | |||
| 1005 | $this->assertNull($user2->getId()); |
||
| 1006 | $this->assertEquals('john.doe', $user2->getLogin()); |
||
| 1007 | $this->assertEquals('John Doe', $user2->getName()); |
||
| 1008 | $this->assertEquals('UK', $user2->getCountry()->getLabel()); |
||
| 1009 | |||
| 1010 | // MANY 2 MANY must be duplicated |
||
| 1011 | $this->assertEquals($role->getName(), $user2->getRoles()[0]->getName()); |
||
| 1012 | } |
||
| 1013 | |||
| 1014 | /** |
||
| 1015 | * @depends testDaoGeneration |
||
| 1016 | */ |
||
| 1017 | public function testCascadeDelete() |
||
| 1018 | { |
||
| 1019 | $userDao = new TestUserDao($this->tdbmService); |
||
| 1020 | $countryDao = new CountryDao($this->tdbmService); |
||
| 1021 | |||
| 1022 | $spain = new CountryBean('Spain'); |
||
| 1023 | $sanchez = new UserBean('Manuel Sanchez', '[email protected]', $spain, 'manuel.sanchez'); |
||
| 1024 | |||
| 1025 | $countryDao->save($spain); |
||
| 1026 | $userDao->save($sanchez); |
||
| 1027 | |||
| 1028 | $speedy2 = $userDao->getUserByLogin('manuel.sanchez'); |
||
| 1029 | $this->assertTrue($sanchez === $speedy2); |
||
| 1030 | |||
| 1031 | $exceptionTriggered = false; |
||
| 1032 | try { |
||
| 1033 | $countryDao->delete($spain); |
||
| 1034 | } catch (ForeignKeyConstraintViolationException $e) { |
||
| 1035 | $exceptionTriggered = true; |
||
| 1036 | } |
||
| 1037 | $this->assertTrue($exceptionTriggered); |
||
| 1038 | |||
| 1039 | $countryDao->delete($spain, true); |
||
| 1040 | |||
| 1041 | // Let's check that speed gonzalez was removed. |
||
| 1042 | $speedy3 = $userDao->getUserByLogin('manuel.sanchez'); |
||
| 1043 | $this->assertNull($speedy3); |
||
| 1044 | } |
||
| 1045 | |||
| 1046 | /** |
||
| 1047 | * @depends testDaoGeneration |
||
| 1048 | */ |
||
| 1049 | public function testDiscardChanges() |
||
| 1050 | { |
||
| 1051 | $contactDao = new ContactDao($this->tdbmService); |
||
| 1052 | $contactBean = $contactDao->getById(1); |
||
| 1053 | |||
| 1054 | $oldName = $contactBean->getName(); |
||
| 1055 | |||
| 1056 | $contactBean->setName('MyNewName'); |
||
| 1057 | |||
| 1058 | $contactBean->discardChanges(); |
||
| 1059 | |||
| 1060 | $this->assertEquals($oldName, $contactBean->getName()); |
||
| 1061 | } |
||
| 1062 | |||
| 1063 | /** |
||
| 1064 | * @expectedException \TheCodingMachine\TDBM\TDBMException |
||
| 1065 | * @depends testDaoGeneration |
||
| 1066 | */ |
||
| 1067 | public function testDiscardChangesOnNewBeanFails() |
||
| 1071 | } |
||
| 1072 | |||
| 1073 | /** |
||
| 1074 | * @expectedException \TheCodingMachine\TDBM\TDBMException |
||
| 1075 | * @depends testDaoGeneration |
||
| 1076 | */ |
||
| 1077 | public function testDiscardChangesOnDeletedBeanFails() |
||
| 1078 | { |
||
| 1079 | $userDao = new TestUserDao($this->tdbmService); |
||
| 1080 | $countryDao = new CountryDao($this->tdbmService); |
||
| 1081 | |||
| 1082 | $sanchez = new UserBean('Manuel Sanchez', '[email protected]', $countryDao->getById(1), 'manuel.sanchez'); |
||
| 1083 | |||
| 1084 | $userDao->save($sanchez); |
||
| 1085 | |||
| 1086 | $userDao->delete($sanchez); |
||
| 1087 | |||
| 1088 | // Cannot discard changes on a bean that is already deleted. |
||
| 1089 | $sanchez->discardChanges(); |
||
| 1090 | } |
||
| 1091 | |||
| 1092 | /** |
||
| 1093 | * @depends testDaoGeneration |
||
| 1094 | */ |
||
| 1095 | public function testUniqueIndexBasedSearch() |
||
| 1096 | { |
||
| 1097 | $userDao = new UserDao($this->tdbmService); |
||
| 1098 | $user = $userDao->findOneByLogin('bill.shakespeare'); |
||
| 1099 | |||
| 1100 | $this->assertEquals('bill.shakespeare', $user->getLogin()); |
||
| 1101 | $this->assertEquals('Bill Shakespeare', $user->getName()); |
||
| 1102 | } |
||
| 1103 | |||
| 1104 | /** |
||
| 1105 | * @depends testDaoGeneration |
||
| 1106 | */ |
||
| 1107 | public function testFindOneByRetunsNull() |
||
| 1114 | } |
||
| 1115 | |||
| 1116 | /** |
||
| 1117 | * @depends testDaoGeneration |
||
| 1118 | */ |
||
| 1119 | public function testMultiColumnsIndexBasedSearch() |
||
| 1120 | { |
||
| 1121 | $countryDao = new CountryDao($this->tdbmService); |
||
| 1122 | $userDao = new UserDao($this->tdbmService); |
||
| 1123 | $users = $userDao->findByStatusAndCountry('on', $countryDao->getById(1)); |
||
| 1124 | |||
| 1125 | $this->assertEquals('jean.dupont', $users[0]->getLogin()); |
||
| 1126 | } |
||
| 1127 | |||
| 1128 | /** |
||
| 1129 | * @depends testDaoGeneration |
||
| 1130 | */ |
||
| 1131 | public function testPartialMultiColumnsIndexBasedSearch() |
||
| 1132 | { |
||
| 1133 | $userDao = new UserDao($this->tdbmService); |
||
| 1134 | $users = $userDao->findByStatusAndCountry('on'); |
||
| 1135 | |||
| 1136 | $this->assertCount(2, $users); |
||
| 1137 | } |
||
| 1138 | |||
| 1139 | /** |
||
| 1140 | * @depends testDaoGeneration |
||
| 1141 | */ |
||
| 1142 | public function testCreationInNullableDate() |
||
| 1143 | { |
||
| 1144 | $roleDao = new RoleDao($this->tdbmService); |
||
| 1145 | |||
| 1146 | $role = new RoleBean('newbee'); |
||
| 1147 | $roleDao->save($role); |
||
| 1148 | |||
| 1149 | $this->assertNull($role->getCreatedAt()); |
||
| 1150 | } |
||
| 1151 | |||
| 1152 | /** |
||
| 1153 | * @depends testDaoGeneration |
||
| 1154 | */ |
||
| 1155 | public function testUpdateInNullableDate() |
||
| 1165 | } |
||
| 1166 | |||
| 1167 | /** |
||
| 1168 | * @depends testDaoGeneration |
||
| 1169 | */ |
||
| 1170 | public function testFindFromSql() |
||
| 1171 | { |
||
| 1172 | $roleDao = new TestRoleDao($this->tdbmService); |
||
| 1173 | |||
| 1174 | $roles = $roleDao->getRolesByRightCanSing(); |
||
| 1175 | $this->assertCount(2, $roles); |
||
| 1176 | $this->assertInstanceOf(RoleBean::class, $roles[0]); |
||
| 1177 | } |
||
| 1178 | |||
| 1179 | /** |
||
| 1180 | * @depends testDaoGeneration |
||
| 1181 | */ |
||
| 1182 | public function testFindOneFromSql() |
||
| 1183 | { |
||
| 1184 | $roleDao = new TestRoleDao($this->tdbmService); |
||
| 1185 | |||
| 1186 | $role = $roleDao->getRoleByRightCanSingAndNameSinger(); |
||
| 1187 | $this->assertInstanceOf(RoleBean::class, $role); |
||
| 1188 | } |
||
| 1189 | |||
| 1190 | /** |
||
| 1191 | * @depends testDaoGeneration |
||
| 1192 | */ |
||
| 1193 | public function testCreateEmptyExtendedBean() |
||
| 1194 | { |
||
| 1195 | // This test cases checks issue https://github.com/thecodingmachine/database.tdbm/issues/92 |
||
| 1196 | |||
| 1197 | $dogDao = new DogDao($this->tdbmService); |
||
| 1198 | |||
| 1199 | // We are not filling no field that is part of dog table. |
||
| 1200 | $dog = new DogBean('Youki'); |
||
| 1201 | $dog->setOrder(1); |
||
| 1202 | |||
| 1203 | $dogDao->save($dog); |
||
| 1204 | $this->assertNull($dog->getRace()); |
||
| 1205 | } |
||
| 1206 | |||
| 1207 | /** |
||
| 1208 | * @depends testCreateEmptyExtendedBean |
||
| 1209 | */ |
||
| 1210 | public function testFetchEmptyExtendedBean() |
||
| 1220 | } |
||
| 1221 | |||
| 1222 | /** |
||
| 1223 | * @depends testDaoGeneration |
||
| 1224 | */ |
||
| 1225 | public function testTwoBranchesHierarchy() |
||
| 1226 | { |
||
| 1227 | // This test cases checks issue https://github.com/thecodingmachine/mouf/issues/131 |
||
| 1228 | |||
| 1229 | $catDao = new CatDao($this->tdbmService); |
||
| 1230 | |||
| 1231 | // We are not filling no field that is part of dog table. |
||
| 1232 | $cat = new CatBean('Mew'); |
||
| 1233 | $cat->setOrder(2); |
||
| 1234 | |||
| 1235 | $catDao->save($cat); |
||
| 1236 | $this->assertNotNull($cat->getId()); |
||
| 1237 | } |
||
| 1238 | |||
| 1239 | /** |
||
| 1240 | * @depends testTwoBranchesHierarchy |
||
| 1241 | */ |
||
| 1242 | public function testFetchTwoBranchesHierarchy() |
||
| 1243 | { |
||
| 1244 | // This test cases checks issue https://github.com/thecodingmachine/mouf/issues/131 |
||
| 1245 | |||
| 1246 | $animalDao = new AnimalDao($this->tdbmService); |
||
| 1247 | |||
| 1248 | $animalBean = $animalDao->getById(2); |
||
| 1249 | |||
| 1250 | $this->assertInstanceOf(CatBean::class, $animalBean); |
||
| 1251 | /* @var $animalBean CatBean */ |
||
| 1252 | $animalBean->setCutenessLevel(999); |
||
| 1253 | $animalBean->setUppercaseColumn('foobar'); |
||
| 1254 | |||
| 1255 | $animalDao->save($animalBean); |
||
| 1256 | } |
||
| 1257 | |||
| 1258 | /** |
||
| 1259 | * @depends testDaoGeneration |
||
| 1260 | */ |
||
| 1261 | public function testExceptionOnGetById() |
||
| 1262 | { |
||
| 1263 | $countryDao = new CountryDao($this->tdbmService); |
||
| 1264 | $this->expectException(\TypeError::class); |
||
| 1265 | $countryDao->getById(null); |
||
| 1266 | } |
||
| 1267 | |||
| 1268 | /** |
||
| 1269 | * @depends testDaoGeneration |
||
| 1270 | */ |
||
| 1271 | public function testDisconnectedManyToOne() |
||
| 1272 | { |
||
| 1273 | // This test cases checks issue https://github.com/thecodingmachine/database.tdbm/issues/99 |
||
| 1274 | |||
| 1275 | $country = new CountryBean('Spain'); |
||
| 1276 | |||
| 1277 | $user = new UserBean('John Doe', '[email protected]', $country, 'john.doe'); |
||
| 1278 | |||
| 1279 | $this->assertCount(1, $country->getUsers()); |
||
| 1280 | $this->assertSame($user, $country->getUsers()[0]); |
||
| 1281 | } |
||
| 1282 | |||
| 1283 | /** |
||
| 1284 | * @depends testDaoGeneration |
||
| 1285 | */ |
||
| 1286 | public function testOrderByExternalCol() |
||
| 1287 | { |
||
| 1288 | // This test cases checks issue https://github.com/thecodingmachine/database.tdbm/issues/106 |
||
| 1289 | |||
| 1290 | $userDao = new TestUserDao($this->tdbmService); |
||
| 1291 | $users = $userDao->getUsersByCountryName(); |
||
| 1292 | |||
| 1293 | $this->assertEquals('UK', $users[0]->getCountry()->getLabel()); |
||
| 1294 | } |
||
| 1295 | |||
| 1296 | /** |
||
| 1297 | * @depends testDaoGeneration |
||
| 1298 | */ |
||
| 1299 | public function testResultIteratorSort() |
||
| 1300 | { |
||
| 1301 | $userDao = new UserDao($this->tdbmService); |
||
| 1302 | $users = $userDao->findAll()->withOrder('country.label DESC'); |
||
| 1303 | |||
| 1304 | $this->assertEquals('UK', $users[0]->getCountry()->getLabel()); |
||
| 1305 | |||
| 1306 | $users = $users->withOrder('country.label ASC'); |
||
| 1307 | $this->assertEquals('France', $users[0]->getCountry()->getLabel()); |
||
| 1308 | } |
||
| 1309 | |||
| 1310 | /** |
||
| 1311 | * @depends testDaoGeneration |
||
| 1312 | */ |
||
| 1313 | public function testResultIteratorWithParameters() |
||
| 1314 | { |
||
| 1315 | $userDao = new TestUserDao($this->tdbmService); |
||
| 1316 | $users = $userDao->getUsersByLoginStartingWith()->withParameters(['login' => 'bill%']); |
||
| 1317 | $this->assertEquals('bill.shakespeare', $users[0]->getLogin()); |
||
| 1318 | |||
| 1319 | $users = $users->withParameters(['login' => 'jean%']); |
||
| 1320 | $this->assertEquals('jean.dupont', $users[0]->getLogin()); |
||
| 1321 | } |
||
| 1322 | |||
| 1323 | /** |
||
| 1324 | * @depends testDaoGeneration |
||
| 1325 | */ |
||
| 1326 | public function testOrderByExpression() |
||
| 1327 | { |
||
| 1328 | $userDao = new TestUserDao($this->tdbmService); |
||
| 1329 | $users = $userDao->getUsersByReversedCountryName(); |
||
| 1330 | |||
| 1331 | $this->assertEquals('Jamaica', $users[0]->getCountry()->getLabel()); |
||
| 1332 | } |
||
| 1333 | |||
| 1334 | /** |
||
| 1335 | * @depends testDaoGeneration |
||
| 1336 | */ |
||
| 1337 | public function testOrderByException() |
||
| 1338 | { |
||
| 1339 | $userDao = new TestUserDao($this->tdbmService); |
||
| 1340 | $users = $userDao->getUsersByInvalidOrderBy(); |
||
| 1341 | $this->expectException(TDBMInvalidArgumentException::class); |
||
| 1342 | $user = $users[0]; |
||
| 1343 | } |
||
| 1344 | |||
| 1345 | /** |
||
| 1346 | * @depends testDaoGeneration |
||
| 1347 | */ |
||
| 1348 | public function testOrderByProtectedColumn() |
||
| 1349 | { |
||
| 1350 | $animalDao = new AnimalDao($this->tdbmService); |
||
| 1351 | $animals = $animalDao->findAll(); |
||
| 1352 | $animals = $animals->withOrder('`order` ASC'); |
||
| 1353 | |||
| 1354 | $this->assertInstanceOf(DogBean::class, $animals[0]); |
||
| 1355 | $this->assertInstanceOf(CatBean::class, $animals[1]); |
||
| 1356 | |||
| 1357 | $animals = $animals->withOrder('`order` DESC'); |
||
| 1358 | |||
| 1359 | $this->assertInstanceOf(CatBean::class, $animals[0]); |
||
| 1360 | $this->assertInstanceOf(DogBean::class, $animals[1]); |
||
| 1361 | } |
||
| 1362 | |||
| 1363 | /** |
||
| 1364 | * @depends testDaoGeneration |
||
| 1365 | */ |
||
| 1366 | public function testGetOnAllNullableValues() |
||
| 1367 | { |
||
| 1368 | // Tests that a get performed on a column that has only nullable fields succeeds. |
||
| 1369 | $allNullable = new AllNullableBean(); |
||
| 1370 | $this->assertNull($allNullable->getId()); |
||
| 1371 | $this->assertNull($allNullable->getLabel()); |
||
| 1372 | $this->assertNull($allNullable->getCountry()); |
||
| 1373 | } |
||
| 1374 | |||
| 1375 | /** |
||
| 1376 | * @depends testDaoGeneration |
||
| 1377 | */ |
||
| 1378 | public function testExceptionOnMultipleInheritance() |
||
| 1379 | { |
||
| 1380 | $connection = self::getConnection(); |
||
| 1381 | self::insert($connection, 'animal', [ |
||
| 1382 | 'id' => 99, 'name' => 'Snoofield', |
||
| 1383 | ]); |
||
| 1384 | self::insert($connection, 'dog', [ |
||
| 1385 | 'id' => 99, 'race' => 'dog', |
||
| 1386 | ]); |
||
| 1387 | self::insert($connection, 'cat', [ |
||
| 1388 | 'id' => 99, 'cuteness_level' => 0, |
||
| 1389 | ]); |
||
| 1390 | |||
| 1391 | $catched = false; |
||
| 1392 | try { |
||
| 1393 | $animalDao = new AnimalDao($this->tdbmService); |
||
| 1394 | $animalDao->getById(99); |
||
| 1395 | } catch (TDBMInheritanceException $e) { |
||
| 1396 | $catched = true; |
||
| 1397 | } |
||
| 1398 | $this->assertTrue($catched, 'Exception TDBMInheritanceException was not caught'); |
||
| 1399 | |||
| 1400 | self::delete($connection, 'cat', ['id' => 99]); |
||
| 1401 | self::delete($connection, 'dog', ['id' => 99]); |
||
| 1402 | self::delete($connection, 'animal', ['id' => 99]); |
||
| 1403 | } |
||
| 1404 | |||
| 1405 | /** |
||
| 1406 | * @depends testDaoGeneration |
||
| 1407 | */ |
||
| 1408 | public function testReferenceNotSaved() |
||
| 1409 | { |
||
| 1410 | $boatDao = new BoatDao($this->tdbmService); |
||
| 1411 | |||
| 1412 | $country = new CountryBean('Atlantis'); |
||
| 1413 | $boat = new BoatBean($country, 'Titanic'); |
||
| 1414 | |||
| 1415 | $boatDao->save($boat); |
||
| 1416 | $this->assertNotNull($country->getId()); |
||
| 1417 | } |
||
| 1418 | |||
| 1419 | /** |
||
| 1420 | * @depends testReferenceNotSaved |
||
| 1421 | */ |
||
| 1422 | public function testUniqueIndexOnForeignKeyThenScalar() |
||
| 1423 | { |
||
| 1424 | $boatDao = new BoatDao($this->tdbmService); |
||
| 1425 | $countryDao = new CountryDao($this->tdbmService); |
||
| 1426 | |||
| 1427 | $countryBean = $countryDao->findOneByLabel('Atlantis'); |
||
| 1428 | $boatBean = $boatDao->findOneByAnchorageCountryAndName($countryBean, 'Titanic'); |
||
| 1429 | |||
| 1430 | $this->assertNotNull($boatBean); |
||
| 1431 | } |
||
| 1432 | |||
| 1433 | /** |
||
| 1434 | * @depends testDaoGeneration |
||
| 1435 | */ |
||
| 1436 | public function testReferenceDeleted() |
||
| 1437 | { |
||
| 1438 | $countryDao = new CountryDao($this->tdbmService); |
||
| 1439 | $boatDao = new BoatDao($this->tdbmService); |
||
| 1440 | |||
| 1441 | $country = new CountryBean('Bikini Bottom'); |
||
| 1442 | $countryDao->save($country); |
||
| 1443 | |||
| 1444 | $boat = new BoatBean($country, 'Squirrel boat'); |
||
| 1445 | $countryDao->delete($country); |
||
| 1446 | |||
| 1447 | $this->expectException(TDBMMissingReferenceException::class); |
||
| 1448 | $boatDao->save($boat); |
||
| 1449 | } |
||
| 1450 | |||
| 1451 | /** |
||
| 1452 | * @depends testDaoGeneration |
||
| 1453 | */ |
||
| 1454 | public function testCyclicReferenceWithInheritance() |
||
| 1455 | { |
||
| 1456 | $userDao = new UserDao($this->tdbmService); |
||
| 1457 | |||
| 1458 | $country = new CountryBean('Norrisland'); |
||
| 1459 | $user = new UserBean('Chuck Norris', '[email protected]', $country, 'chuck.norris'); |
||
| 1460 | |||
| 1461 | $user->setManager($user); |
||
| 1462 | |||
| 1463 | $this->expectException(TDBMCyclicReferenceException::class); |
||
| 1464 | $userDao->save($user); |
||
| 1465 | } |
||
| 1466 | |||
| 1467 | /** |
||
| 1468 | * @depends testDaoGeneration |
||
| 1469 | */ |
||
| 1470 | public function testCyclicReference() |
||
| 1471 | { |
||
| 1472 | $categoryDao = new CategoryDao($this->tdbmService); |
||
| 1473 | |||
| 1474 | $category = new CategoryBean('Root'); |
||
| 1475 | |||
| 1476 | $category->setParent($category); |
||
| 1477 | |||
| 1478 | $this->expectException(TDBMCyclicReferenceException::class); |
||
| 1479 | $categoryDao->save($category); |
||
| 1480 | } |
||
| 1481 | |||
| 1482 | /** |
||
| 1483 | * @depends testDaoGeneration |
||
| 1484 | */ |
||
| 1485 | public function testCorrectTypeForPrimaryKeyAfterSave() |
||
| 1486 | { |
||
| 1487 | // PosqtgreSQL does not particularly like empty inserts (i.e.: "INSERT INTO all_nullable () VALUES ()" ) |
||
| 1488 | $this->onlyMySql(); |
||
| 1489 | |||
| 1490 | $allNullableDao = new AllNullableDao($this->tdbmService); |
||
| 1491 | $allNullable = new AllNullableBean(); |
||
| 1492 | $allNullableDao->save($allNullable); |
||
| 1493 | $id = $allNullable->getId(); |
||
| 1494 | |||
| 1495 | $this->assertTrue(is_int($id)); |
||
| 1496 | } |
||
| 1497 | |||
| 1498 | /** |
||
| 1499 | * @depends testDaoGeneration |
||
| 1500 | */ |
||
| 1501 | public function testPSR2Compliance() |
||
| 1502 | { |
||
| 1503 | $process = new Process('vendor/bin/php-cs-fixer fix src/Test/ --dry-run --diff --rules=@PSR2'); |
||
| 1504 | $process->run(); |
||
| 1505 | |||
| 1506 | // executes after the command finishes |
||
| 1507 | if (!$process->isSuccessful()) { |
||
| 1508 | echo $process->getOutput(); |
||
| 1509 | $this->fail('Generated code is not PSR-2 compliant'); |
||
| 1510 | } |
||
| 1511 | $this->assertTrue($process->isSuccessful()); |
||
| 1512 | } |
||
| 1513 | |||
| 1514 | /** |
||
| 1515 | * @depends testDaoGeneration |
||
| 1516 | */ |
||
| 1517 | public function testFindOneByGeneration() |
||
| 1518 | { |
||
| 1519 | $reflectionMethod = new \ReflectionMethod(UserBaseDao::class, 'findOneByLogin'); |
||
| 1520 | $parameters = $reflectionMethod->getParameters(); |
||
| 1521 | |||
| 1522 | $this->assertCount(2, $parameters); |
||
| 1523 | $this->assertSame('login', $parameters[0]->getName()); |
||
| 1524 | $this->assertSame('additionalTablesFetch', $parameters[1]->getName()); |
||
| 1525 | } |
||
| 1526 | |||
| 1527 | /** |
||
| 1528 | * @depends testDaoGeneration |
||
| 1529 | */ |
||
| 1530 | public function testUuid() |
||
| 1531 | { |
||
| 1532 | $article = new ArticleBean('content'); |
||
| 1533 | $this->assertSame('content', $article->getContent()); |
||
| 1534 | $this->assertNotEmpty($article->getId()); |
||
| 1535 | $uuid = Uuid::fromString($article->getId()); |
||
| 1536 | $this->assertSame(1, $uuid->getVersion()); |
||
| 1537 | } |
||
| 1538 | |||
| 1539 | /** |
||
| 1540 | * @depends testDaoGeneration |
||
| 1541 | */ |
||
| 1542 | public function testUuidv4() |
||
| 1543 | { |
||
| 1544 | $article = new Article2Bean('content'); |
||
| 1545 | $this->assertSame('content', $article->getContent()); |
||
| 1546 | $this->assertNotEmpty($article->getId()); |
||
| 1547 | $uuid = Uuid::fromString($article->getId()); |
||
| 1548 | $this->assertSame(4, $uuid->getVersion()); |
||
| 1549 | } |
||
| 1550 | |||
| 1551 | /** |
||
| 1552 | * @depends testDaoGeneration |
||
| 1553 | */ |
||
| 1554 | public function testTypeHintedConstructors() |
||
| 1555 | { |
||
| 1556 | $userBaseBeanReflectionConstructor = new \ReflectionMethod(UserBaseBean::class, '__construct'); |
||
| 1557 | $nameParam = $userBaseBeanReflectionConstructor->getParameters()[0]; |
||
| 1558 | |||
| 1559 | $this->assertSame('string', (string)$nameParam->getType()); |
||
| 1560 | } |
||
| 1561 | |||
| 1562 | /** |
||
| 1563 | * @depends testDaoGeneration |
||
| 1564 | */ |
||
| 1565 | public function testSaveTransaction() |
||
| 1566 | { |
||
| 1567 | $countryDao = new CountryDao($this->tdbmService); |
||
| 1568 | |||
| 1569 | $boatDao = new BoatDao($this->tdbmService); |
||
| 1570 | $boatBean = $boatDao->getById(1); |
||
| 1571 | $boatBean->setName('Bismark'); |
||
| 1572 | |||
| 1573 | $boatBean->getCountry(); |
||
| 1574 | |||
| 1575 | // Let's insert a row without telling TDBM to trigger an error! |
||
| 1576 | self::insert($this->getConnection(), 'sailed_countries', [ |
||
| 1577 | 'boat_id' => 1, |
||
| 1578 | 'country_id' => 2 |
||
| 1579 | ]); |
||
| 1580 | |||
| 1581 | $boatBean->addCountry($countryDao->getById(2)); |
||
| 1582 | |||
| 1583 | $this->expectException(UniqueConstraintViolationException::class); |
||
| 1584 | |||
| 1585 | $boatDao->save($boatBean); |
||
| 1586 | } |
||
| 1587 | |||
| 1588 | /** |
||
| 1589 | * @depends testSaveTransaction |
||
| 1590 | */ |
||
| 1591 | public function testSaveTransaction2() |
||
| 1592 | { |
||
| 1593 | $boatDao = new BoatDao($this->tdbmService); |
||
| 1594 | $boatBean = $boatDao->getById(1); |
||
| 1595 | |||
| 1596 | // The name should not have been saved because the transaction of the previous test should have rollbacked. |
||
| 1597 | $this->assertNotSame('Bismark', $boatBean->getName()); |
||
| 1598 | } |
||
| 1599 | |||
| 1600 | /** |
||
| 1601 | * @depends testDaoGeneration |
||
| 1602 | */ |
||
| 1603 | public function testForeignKeyPointingToNonPrimaryKey() |
||
| 1604 | { |
||
| 1605 | $dao = new RefNoPrimKeyDao($this->tdbmService); |
||
| 1606 | $bean = $dao->getById(1); |
||
| 1607 | |||
| 1608 | $this->assertSame('foo', $bean->getFrom()->getTo()); |
||
| 1609 | |||
| 1610 | $newBean = new RefNoPrimKeyBean($bean, 'baz'); |
||
| 1611 | $dao->save($newBean); |
||
| 1612 | $this->assertSame('foo', $newBean->getFrom()->getTo()); |
||
| 1613 | |||
| 1614 | $resultSet = $bean->getRefNoPrimKey(); |
||
| 1615 | $this->assertCount(2, $resultSet); |
||
| 1616 | } |
||
| 1617 | |||
| 1618 | /** |
||
| 1619 | * @depends testDaoGeneration |
||
| 1620 | */ |
||
| 1621 | public function testCloningUuidBean() |
||
| 1628 | } |
||
| 1629 | |||
| 1630 | /** |
||
| 1631 | * @depends testDaoGeneration |
||
| 1632 | */ |
||
| 1633 | public function testRecursiveSave() |
||
| 1634 | { |
||
| 1635 | $categoryDao = new CategoryDao($this->tdbmService); |
||
| 1636 | |||
| 1637 | $root1 = new CategoryBean('Root1'); |
||
| 1638 | $categoryDao->save($root1); |
||
| 1639 | // Root 2 is not saved yet. |
||
| 1640 | $root2 = new CategoryBean('Root2'); |
||
| 1641 | $intermediate = new CategoryBean('Intermediate'); |
||
| 1642 | $categoryDao->save($intermediate); |
||
| 1643 | |||
| 1644 | // Let's switch the parent to a bean in detached state. |
||
| 1645 | $intermediate->setParent($root2); |
||
| 1646 | |||
| 1647 | // Now, let's save a new category that references the leaf category. |
||
| 1648 | $leaf = new CategoryBean('Leaf'); |
||
| 1649 | $leaf->setParent($intermediate); |
||
| 1650 | $categoryDao->save($leaf); |
||
| 1651 | $this->assertNull($root2->getId()); |
||
| 1652 | } |
||
| 1653 | |||
| 1654 | /** |
||
| 1655 | * @depends testDaoGeneration |
||
| 1656 | */ |
||
| 1657 | public function testBlob() |
||
| 1658 | { |
||
| 1659 | $fp = fopen(__FILE__, 'r'); |
||
| 1660 | $file = new FileBean($fp); |
||
| 1661 | |||
| 1662 | $fileDao = new FileDao($this->tdbmService); |
||
| 1663 | |||
| 1664 | $fileDao->save($file); |
||
| 1665 | |||
| 1666 | $loadedFile = $fileDao->getById($file->getId()); |
||
| 1667 | |||
| 1668 | $resource = $loadedFile->getFile(); |
||
| 1669 | $result = fseek($resource, 0); |
||
| 1670 | $this->assertSame(0, $result); |
||
| 1671 | $this->assertInternalType('resource', $resource); |
||
| 1672 | $firstLine = fgets($resource); |
||
| 1673 | $this->assertSame("<?php\n", $firstLine); |
||
| 1674 | } |
||
| 1675 | |||
| 1676 | /** |
||
| 1677 | * @depends testBlob |
||
| 1678 | */ |
||
| 1679 | public function testReadBlob() |
||
| 1680 | { |
||
| 1681 | $fileDao = new FileDao($this->tdbmService); |
||
| 1682 | $loadedFile = $fileDao->getById(1); |
||
| 1683 | |||
| 1684 | $resource = $loadedFile->getFile(); |
||
| 1685 | $this->assertInternalType('resource', $resource); |
||
| 1686 | $firstLine = fgets($resource); |
||
| 1687 | $this->assertSame("<?php\n", $firstLine); |
||
| 1688 | |||
| 1689 | stream_get_contents($resource); |
||
| 1690 | |||
| 1691 | $loadedFile->setId($loadedFile->getId()); |
||
| 1692 | |||
| 1693 | $fileDao->save($loadedFile); |
||
| 1694 | } |
||
| 1695 | |||
| 1696 | /** |
||
| 1697 | * @depends testReadBlob |
||
| 1698 | */ |
||
| 1699 | public function testReadAndSaveBlob() |
||
| 1700 | { |
||
| 1701 | $fileDao = new FileDao($this->tdbmService); |
||
| 1702 | $loadedFile = $fileDao->getById(1); |
||
| 1703 | |||
| 1704 | $resource = $loadedFile->getFile(); |
||
| 1705 | |||
| 1706 | $firstLine = fgets($resource); |
||
| 1707 | $this->assertSame("<?php\n", $firstLine); |
||
| 1708 | } |
||
| 1709 | |||
| 1710 | /** |
||
| 1711 | * @depends testDaoGeneration |
||
| 1712 | */ |
||
| 1713 | public function testBlobResourceException() |
||
| 1714 | { |
||
| 1715 | $this->expectException(TDBMInvalidArgumentException::class); |
||
| 1716 | $this->expectExceptionMessage('Invalid argument passed to \'TheCodingMachine\\TDBM\\Test\\Dao\\Bean\\Generated\\FileBaseBean::setFile\'. Expecting a resource. Got a string.'); |
||
| 1717 | new FileBean('foobar'); |
||
| 1718 | } |
||
| 1719 | |||
| 1720 | /** |
||
| 1721 | * @depends testDaoGeneration |
||
| 1722 | */ |
||
| 1723 | public function testFilterBag() |
||
| 1724 | { |
||
| 1725 | $userDao = new TestUserDao($this->tdbmService); |
||
| 1726 | $countryDao = new CountryDao($this->tdbmService); |
||
| 1727 | |||
| 1728 | $country = $countryDao->getById(2); |
||
| 1729 | |||
| 1730 | // Let's test filter bags by bean and filter bag with many values. |
||
| 1731 | $users = $userDao->getUsersByComplexFilterBag($country, ['John Doe', 'Jane Doe']); |
||
| 1732 | |||
| 1733 | $this->assertCount(1, $users); |
||
| 1734 | $this->assertSame('John Doe', $users[0]->getName()); |
||
| 1735 | } |
||
| 1736 | |||
| 1737 | /** |
||
| 1738 | * @depends testDaoGeneration |
||
| 1739 | */ |
||
| 1740 | public function testDecimalIsMappedToString() |
||
| 1741 | { |
||
| 1742 | $reflectionClass = new \ReflectionClass(BoatBaseBean::class); |
||
| 1743 | $this->assertSame('string', (string) $reflectionClass->getMethod('getLength')->getReturnType()); |
||
| 1744 | } |
||
| 1745 | |||
| 1746 | /** |
||
| 1747 | * @depends testDaoGeneration |
||
| 1748 | */ |
||
| 1749 | public function testNoGetByIdOnMultiPrimaryKeys() |
||
| 1750 | { |
||
| 1751 | $reflectionClass = new \ReflectionClass(StateDao::class); |
||
| 1752 | $this->assertFalse($reflectionClass->hasMethod('getById')); |
||
| 1753 | } |
||
| 1754 | |||
| 1755 | /** |
||
| 1756 | * @depends testDaoGeneration |
||
| 1757 | */ |
||
| 1758 | public function testInsertMultiPrimaryKeysBean() |
||
| 1769 | } |
||
| 1770 | |||
| 1771 | /** |
||
| 1772 | * @depends testInsertMultiPrimaryKeysBean |
||
| 1773 | */ |
||
| 1774 | public function testDeleteMultiPrimaryKeysBean() |
||
| 1781 | |||
| 1782 | } |
||
| 1783 | |||
| 1784 | /** |
||
| 1785 | * @depends testDaoGeneration |
||
| 1786 | */ |
||
| 1787 | public function testSortOnInheritedTable() |
||
| 1788 | { |
||
| 1789 | $animalDao = new AnimalDao($this->tdbmService); |
||
| 1790 | |||
| 1791 | // Let's insert an animal that is nothing. |
||
| 1792 | $animal = new AnimalBean('Mickey'); |
||
| 1793 | $animalDao->save($animal); |
||
| 1794 | |||
| 1795 | $animals = $animalDao->findAll()->withOrder('dog.race ASC'); |
||
| 1796 | |||
| 1797 | $animalsArr = $animals->toArray(); |
||
| 1798 | $this->assertCount(3, $animalsArr); |
||
| 1799 | } |
||
| 1800 | } |
||
| 1801 |
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