3
3
namespace Doctrine \Tests \ORM \Functional ;
4
4
5
5
use Doctrine \ORM \Mapping \ClassMetadataInfo ;
6
+ use Doctrine \Tests \Models \Tweet \Tweet ;
7
+ use Doctrine \Tests \Models \Tweet \User ;
6
8
7
9
require_once __DIR__ . '/../../TestInit.php ' ;
8
10
@@ -22,6 +24,7 @@ class ExtraLazyCollectionTest extends \Doctrine\Tests\OrmFunctionalTestCase
22
24
23
25
public function setUp ()
24
26
{
27
+ $ this ->useModelSet ('tweet ' );
25
28
$ this ->useModelSet ('cms ' );
26
29
parent ::setUp ();
27
30
@@ -363,7 +366,8 @@ public function testRemoveElementOneToMany()
363
366
$ user ->articles ->removeElement ($ article );
364
367
365
368
$ this ->assertFalse ($ user ->articles ->isInitialized (), "Post-Condition: Collection is not initialized. " );
366
- $ this ->assertEquals ($ queryCount + 1 , $ this ->getCurrentQueryCount ());
369
+ // NOTE: +2 queries because CmsArticle is a versioned entity, and that needs to be handled accordingly
370
+ $ this ->assertEquals ($ queryCount + 2 , $ this ->getCurrentQueryCount ());
367
371
368
372
// Test One to Many removal with Entity state as new
369
373
$ article = new \Doctrine \Tests \Models \CMS \CmsArticle ();
@@ -384,7 +388,7 @@ public function testRemoveElementOneToMany()
384
388
385
389
$ user ->articles ->removeElement ($ article );
386
390
387
- $ this ->assertEquals ($ queryCount + 1 , $ this ->getCurrentQueryCount (), "Removing a persisted entity should cause one query to be executed . " );
391
+ $ this ->assertEquals ($ queryCount , $ this ->getCurrentQueryCount (), "Removing a persisted entity will not cause queries when the owning side doesn't actually change . " );
388
392
$ this ->assertFalse ($ user ->articles ->isInitialized (), "Post-Condition: Collection is not initialized. " );
389
393
390
394
// Test One to Many removal with Entity state as managed
@@ -650,4 +654,101 @@ private function loadFixture()
650
654
$ this ->topic = $ article1 ->topic ;
651
655
$ this ->phonenumber = $ phonenumber1 ->phonenumber ;
652
656
}
657
+
658
+ /**
659
+ * @group DDC-3343
660
+ */
661
+ public function testRemovesManagedElementFromOneToManyExtraLazyCollection ()
662
+ {
663
+ list ($ userId , $ tweetId ) = $ this ->loadTweetFixture ();
664
+
665
+ /* @var $user User */
666
+ $ user = $ this ->_em ->find (User::CLASSNAME , $ userId );
667
+
668
+ $ user ->tweets ->removeElement ($ this ->_em ->find (Tweet::CLASSNAME , $ tweetId ));
669
+
670
+ $ this ->_em ->clear ();
671
+
672
+ /* @var $user User */
673
+ $ user = $ this ->_em ->find (User::CLASSNAME , $ userId );
674
+
675
+ $ this ->assertCount (0 , $ user ->tweets );
676
+ }
677
+
678
+ /**
679
+ * @group DDC-3343
680
+ */
681
+ public function testRemovesManagedElementFromOneToManyExtraLazyCollectionWithoutDeletingTheTargetEntityEntry ()
682
+ {
683
+ list ($ userId , $ tweetId ) = $ this ->loadTweetFixture ();
684
+
685
+ /* @var $user User */
686
+ $ user = $ this ->_em ->find (User::CLASSNAME , $ userId );
687
+
688
+ $ user ->tweets ->removeElement ($ this ->_em ->find (Tweet::CLASSNAME , $ tweetId ));
689
+
690
+ $ this ->_em ->clear ();
691
+
692 + /* @var $tweet Tweet */
693
+ $ tweet = $ this ->_em ->find (Tweet::CLASSNAME , $ tweetId );
694
+ $ this ->assertInstanceOf (
695
+ Tweet::CLASSNAME ,
696
+ $ tweet ,
697
+ 'Even though the collection is extra lazy, the tweet should not have been deleted '
698
+ );
699
+
700
+ $ this ->assertNull ($ tweet ->author , 'Tweet author link has been removed ' );
701
+ }
702
+
703
+ /**
704
+ * @group DDC-3343
705
+ */
706
+ public function testRemovingManagedLazyProxyFromExtraLazyOneToManyDoesRemoveTheAssociationButNotTheEntity ()
707
+ {
708
+ list ($ userId , $ tweetId ) = $ this ->loadTweetFixture ();
709
+
710
+ /* @var $user User */
711
+ $ user = $ this ->_em ->find (User::CLASSNAME , $ userId );
712
+ $ tweet = $ this ->_em ->getReference (Tweet::CLASSNAME , $ tweetId );
713
+
714
+ $ user ->tweets ->removeElement ($ this ->_em ->getReference (Tweet::CLASSNAME , $ tweetId ));
715
+
716
+ $ this ->_em ->clear ();
717
+
718
+ /* @var $tweet Tweet */
719
+ $ tweet = $ this ->_em ->find (Tweet::CLASSNAME , $ tweet ->id );
720
+ $ this ->assertInstanceOf (
721
+ Tweet::CLASSNAME ,
722
+ $ tweet ,
723
+ 'Even though the collection is extra lazy, the tweet should not have been deleted '
724
+ );
725
+
726
+ $ this ->assertNull ($ tweet ->author );
727
+
728
+ /* @var $user User */
729
+ $ user = $ this ->_em ->find (User::CLASSNAME , $ userId );
730
+
731
+ $ this ->assertCount (0 , $ user ->tweets );
732
+ }
733
+
734
+ /**
735
+ * @return int[] ordered tuple: user id and tweet id
736
+ */
737
+ private function loadTweetFixture ()
738
+ {
739
+ $ user = new User ();
740
+ $ tweet = new Tweet ();
741
+
742
+ $ user ->name = 'ocramius ' ;
743
+ $ tweet ->content = 'The cat is on the table ' ;
744
+
745
+ $ user ->addTweet ($ tweet );
746
+
747
+ $ this ->_em ->persist ($ user );
748
+ $ this ->_em ->persist ($ tweet );
749
+ $ this ->_em ->flush ();
750
+ $ this ->_em ->clear ();
751
+
752
+ return array ($ user ->id , $ tweet ->id );
753
+ }
653
754
}
0 commit comments