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