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