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