@@ 270-313 (lines=44) @@ | ||
267 | /** |
|
268 | * @group DDC-546 |
|
269 | */ |
|
270 | public function testContainsOneToMany() |
|
271 | { |
|
272 | $user = $this->_em->find(CmsUser::class, $this->userId); |
|
273 | $this->assertFalse($user->articles->isInitialized(), "Pre-Condition: Collection is not initialized."); |
|
274 | ||
275 | // Test One to Many existence retrieved from DB |
|
276 | $article = $this->_em->find(CmsArticle::class, $this->articleId); |
|
277 | $queryCount = $this->getCurrentQueryCount(); |
|
278 | ||
279 | $this->assertTrue($user->articles->contains($article)); |
|
280 | $this->assertFalse($user->articles->isInitialized(), "Post-Condition: Collection is not initialized."); |
|
281 | $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount()); |
|
282 | ||
283 | // Test One to Many existence with state new |
|
284 | $article = new CmsArticle(); |
|
285 | $article->topic = "Testnew"; |
|
286 | $article->text = "blub"; |
|
287 | ||
288 | $queryCount = $this->getCurrentQueryCount(); |
|
289 | $this->assertFalse($user->articles->contains($article)); |
|
290 | $this->assertEquals($queryCount, $this->getCurrentQueryCount(), "Checking for contains of new entity should cause no query to be executed."); |
|
291 | ||
292 | // Test One to Many existence with state clear |
|
293 | $this->_em->persist($article); |
|
294 | $this->_em->flush(); |
|
295 | ||
296 | $queryCount = $this->getCurrentQueryCount(); |
|
297 | $this->assertFalse($user->articles->contains($article)); |
|
298 | $this->assertEquals($queryCount+1, $this->getCurrentQueryCount(), "Checking for contains of persisted entity should cause one query to be executed."); |
|
299 | $this->assertFalse($user->articles->isInitialized(), "Post-Condition: Collection is not initialized."); |
|
300 | ||
301 | // Test One to Many existence with state managed |
|
302 | $article = new CmsArticle(); |
|
303 | $article->topic = "How to not fail anymore on tests"; |
|
304 | $article->text = "That is simple! Just write more tests!"; |
|
305 | ||
306 | $this->_em->persist($article); |
|
307 | ||
308 | $queryCount = $this->getCurrentQueryCount(); |
|
309 | ||
310 | $this->assertFalse($user->articles->contains($article)); |
|
311 | $this->assertEquals($queryCount, $this->getCurrentQueryCount(), "Checking for contains of managed entity (but not persisted) should cause no query to be executed."); |
|
312 | $this->assertFalse($user->articles->isInitialized(), "Post-Condition: Collection is not initialized."); |
|
313 | } |
|
314 | ||
315 | /** |
|
316 | * @group DDC-2504 |
|
@@ 634-684 (lines=51) @@ | ||
631 | /** |
|
632 | * |
|
633 | */ |
|
634 | public function testRemoveElementManyToMany() |
|
635 | { |
|
636 | $user = $this->_em->find(CmsUser::class, $this->userId); |
|
637 | $this->assertFalse($user->groups->isInitialized(), "Pre-Condition: Collection is not initialized."); |
|
638 | ||
639 | // Test Many to Many removal with Entity retrieved from DB |
|
640 | $group = $this->_em->find(CmsGroup::class, $this->groupId); |
|
641 | $queryCount = $this->getCurrentQueryCount(); |
|
642 | ||
643 | $this->assertTrue($user->groups->removeElement($group)); |
|
644 | ||
645 | $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount(), "Removing a persisted entity should cause one query to be executed."); |
|
646 | $this->assertFalse($user->groups->isInitialized(), "Post-Condition: Collection is not initialized."); |
|
647 | ||
648 | $this->assertFalse($user->groups->removeElement($group), "Removing an already removed element returns false"); |
|
649 | ||
650 | // Test Many to Many removal with Entity state as new |
|
651 | $group = new CmsGroup(); |
|
652 | $group->name = "A New group!"; |
|
653 | ||
654 | $queryCount = $this->getCurrentQueryCount(); |
|
655 | ||
656 | $user->groups->removeElement($group); |
|
657 | ||
658 | $this->assertEquals($queryCount, $this->getCurrentQueryCount(), "Removing new entity should cause no query to be executed."); |
|
659 | $this->assertFalse($user->groups->isInitialized(), "Post-Condition: Collection is not initialized."); |
|
660 | ||
661 | // Test Many to Many removal with Entity state as clean |
|
662 | $this->_em->persist($group); |
|
663 | $this->_em->flush(); |
|
664 | ||
665 | $queryCount = $this->getCurrentQueryCount(); |
|
666 | ||
667 | $user->groups->removeElement($group); |
|
668 | ||
669 | $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount(), "Removing a persisted entity should cause one query to be executed."); |
|
670 | $this->assertFalse($user->groups->isInitialized(), "Post-Condition: Collection is not initialized."); |
|
671 | ||
672 | // Test Many to Many removal with Entity state as managed |
|
673 | $group = new CmsGroup(); |
|
674 | $group->name = "A New group!"; |
|
675 | ||
676 | $this->_em->persist($group); |
|
677 | ||
678 | $queryCount = $this->getCurrentQueryCount(); |
|
679 | ||
680 | $user->groups->removeElement($group); |
|
681 | ||
682 | $this->assertEquals($queryCount, $this->getCurrentQueryCount(), "Removing a managed entity should cause no query to be executed."); |
|
683 | $this->assertFalse($user->groups->isInitialized(), "Post-Condition: Collection is not initialized."); |
|
684 | } |
|
685 | ||
686 | /** |
|
687 | * |