| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace Doctrine\Tests\ORM\Functional; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Doctrine\Common\Collections\Criteria; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Doctrine\ORM\LazyCriteriaCollection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Doctrine\Tests\Models\Quote\Group; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Doctrine\Tests\Models\Quote\User as QuoteUser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Doctrine\Tests\Models\Tweet\Tweet; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Doctrine\Tests\Models\Tweet\User; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Doctrine\Tests\Models\Tweet\User as TweetUser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Doctrine\Tests\OrmFunctionalTestCase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @author Michaël Gallego <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | class PersistentCollectionCriteriaTest extends OrmFunctionalTestCase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     protected function setUp() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         $this->useModelSet('tweet'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         $this->useModelSet('quote'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         parent::setUp(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     public function loadTweetFixture() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         $author = new TweetUser(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         $author->name = 'ngal'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         $this->em->persist($author); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $tweet1 = new Tweet(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $tweet1->content = 'Foo'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $author->addTweet($tweet1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $tweet2 = new Tweet(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $tweet2->content = 'Bar'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $author->addTweet($tweet2); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $this->em->flush(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         unset($author, $tweet1, $tweet2); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $this->em->clear(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     public function loadQuoteFixture() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $user = new QuoteUser(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $user->name = 'mgal'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         $this->em->persist($user); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $quote1 = new Group('quote1'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $user->groups->add($quote1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $quote2 = new Group('quote2'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         $user->groups->add($quote2); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         $this->em->flush(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         $this->em->clear(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |     public function testCanCountWithoutLoadingPersistentCollection() | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |         $this->loadTweetFixture(); | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |         $repository = $this->em->getRepository(User::class); | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |         $user   = $repository->findOneBy(['name' => 'ngal']); | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |         $tweets = $user->tweets->matching(new Criteria()); | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |         self::assertInstanceOf(LazyCriteriaCollection::class, $tweets); | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |         self::assertFalse($tweets->isInitialized()); | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |         self::assertCount(2, $tweets); | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |         self::assertFalse($tweets->isInitialized()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |         // Make sure it works with constraints | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |         $tweets = $user->tweets->matching(new Criteria( | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |             Criteria::expr()->eq('content', 'Foo') | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |         )); | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |         self::assertInstanceOf(LazyCriteriaCollection::class, $tweets); | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |         self::assertFalse($tweets->isInitialized()); | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |         self::assertCount(1, $tweets); | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |         self::assertFalse($tweets->isInitialized()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 91 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 92 |  |  |  |