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