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