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