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