From 50832fd3bc448bdd389e94eb0b151d0ec5eba820 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Gallego?= Date: Sun, 16 Feb 2014 18:40:43 +0100 Subject: [PATCH] Add tests for PersistentCollection --- lib/Doctrine/ORM/PersistentCollection.php | 2 +- .../PersistentCollectionCriteriaTest.php | 95 +++++++++++++++++++ 2 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 tests/Doctrine/Tests/ORM/Functional/PersistentCollectionCriteriaTest.php diff --git a/lib/Doctrine/ORM/PersistentCollection.php b/lib/Doctrine/ORM/PersistentCollection.php index 1142c4266..37a6c11be 100644 --- a/lib/Doctrine/ORM/PersistentCollection.php +++ b/lib/Doctrine/ORM/PersistentCollection.php @@ -883,7 +883,7 @@ final class PersistentCollection implements Collection, Selectable $persister = $this->em->getUnitOfWork()->getEntityPersister($this->association['targetEntity']); - if ($this->association['fetch'] === ClassMetadataInfo::FETCH_EXTRA_LAZY) { + if ($this->association['fetch'] !== ClassMetadataInfo::FETCH_EAGER) { return new LazyCriteriaCollection($persister, $criteria); } else { return new ArrayCollection($persister->loadCriteria($criteria)); diff --git a/tests/Doctrine/Tests/ORM/Functional/PersistentCollectionCriteriaTest.php b/tests/Doctrine/Tests/ORM/Functional/PersistentCollectionCriteriaTest.php new file mode 100644 index 000000000..e4c2082e4 --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Functional/PersistentCollectionCriteriaTest.php @@ -0,0 +1,95 @@ + + */ +class PersistentCollectionCriteriaTest extends \Doctrine\Tests\OrmFunctionalTestCase +{ + protected function setUp() + { + $this->useModelSet('cms'); + parent::setUp(); + } + + public function tearDown() + { + if ($this->_em) { + $this->_em->getConfiguration()->setEntityNamespaces(array()); + } + parent::tearDown(); + } + + public function loadFixture() + { + $article = new CmsArticle(); + $article->topic = 'Criteria is awesome'; + $article->text = 'foo'; + $this->_em->persist($article); + + $comment1 = new CmsComment(); + $comment1->topic = 'I agree'; + $comment1->text = 'bar'; + $this->_em->persist($comment1); + $article->addComment($comment1); + + $comment2 = new CmsComment(); + $comment2->topic = 'I disagree'; + $comment2->text = 'baz'; + $this->_em->persist($comment2); + $article->addComment($comment2); + + $this->_em->flush(); + + unset($article); + unset($comment1); + unset($comment2); + + $this->_em->clear(); + } + + public function testCanCountWithoutLoadingPersistentCollection() + { + $this->loadFixture(); + $repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsArticle'); + + $article = $repository->findOneBy(array('topic' => 'Criteria is awesome')); + $comments = $article->comments->matching(new Criteria()); + + $this->assertInstanceOf('Doctrine\ORM\LazyCriteriaCollection', $comments); + $this->assertFalse($comments->isInitialized()); + $this->assertCount(2, $comments); + $this->assertFalse($comments->isInitialized()); + + // Make sure it works with constraints + $comments = $article->comments->matching(new Criteria( + Criteria::expr()->eq('text', 'bar') + )); + + $this->assertInstanceOf('Doctrine\ORM\LazyCriteriaCollection', $comments); + $this->assertFalse($comments->isInitialized()); + $this->assertCount(1, $comments); + $this->assertFalse($comments->isInitialized()); + } +}