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