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