2014-02-16 18:40:43 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Doctrine\Tests\ORM\Functional;
|
|
|
|
|
|
|
|
use Doctrine\Common\Collections\Criteria;
|
2015-01-15 03:14:48 +00:00
|
|
|
use Doctrine\Tests\Models\Quote\Group;
|
|
|
|
use Doctrine\Tests\Models\Quote\User as QuoteUser;
|
2014-02-21 10:30:41 +01:00
|
|
|
use Doctrine\Tests\Models\Tweet\Tweet;
|
2015-01-15 03:14:48 +00:00
|
|
|
use Doctrine\Tests\Models\Tweet\User as TweetUser;
|
2016-05-11 02:41:26 +07:00
|
|
|
use Doctrine\Tests\OrmFunctionalTestCase;
|
2014-02-16 18:40:43 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Michaël Gallego <mic.gallego@gmail.com>
|
|
|
|
*/
|
2016-05-11 02:41:26 +07:00
|
|
|
class PersistentCollectionCriteriaTest extends OrmFunctionalTestCase
|
2014-02-16 18:40:43 +01:00
|
|
|
{
|
|
|
|
protected function setUp()
|
|
|
|
{
|
2014-02-21 10:30:41 +01:00
|
|
|
$this->useModelSet('tweet');
|
2015-01-15 03:14:48 +00:00
|
|
|
$this->useModelSet('quote');
|
2014-02-16 18:40:43 +01:00
|
|
|
parent::setUp();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function tearDown()
|
|
|
|
{
|
|
|
|
if ($this->_em) {
|
2016-12-07 23:33:41 +01:00
|
|
|
$this->_em->getConfiguration()->setEntityNamespaces([]);
|
2014-02-16 18:40:43 +01:00
|
|
|
}
|
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
2015-01-15 03:14:48 +00:00
|
|
|
public function loadTweetFixture()
|
2014-02-16 18:40:43 +01:00
|
|
|
{
|
2015-01-15 03:14:48 +00:00
|
|
|
$author = new TweetUser();
|
2014-02-21 10:30:41 +01:00
|
|
|
$author->name = 'ngal';
|
|
|
|
$this->_em->persist($author);
|
2014-02-16 18:40:43 +01:00
|
|
|
|
2014-02-21 10:30:41 +01:00
|
|
|
$tweet1 = new Tweet();
|
|
|
|
$tweet1->content = 'Foo';
|
|
|
|
$author->addTweet($tweet1);
|
2014-02-16 18:40:43 +01:00
|
|
|
|
2014-02-21 10:30:41 +01:00
|
|
|
$tweet2 = new Tweet();
|
|
|
|
$tweet2->content = 'Bar';
|
|
|
|
$author->addTweet($tweet2);
|
2014-02-16 18:40:43 +01:00
|
|
|
|
|
|
|
$this->_em->flush();
|
|
|
|
|
2014-02-21 10:30:41 +01:00
|
|
|
unset($author);
|
|
|
|
unset($tweet1);
|
|
|
|
unset($tweet2);
|
2014-02-16 18:40:43 +01:00
|
|
|
|
|
|
|
$this->_em->clear();
|
|
|
|
}
|
|
|
|
|
2015-01-15 03:14:48 +00:00
|
|
|
public function loadQuoteFixture()
|
|
|
|
{
|
|
|
|
$user = new QuoteUser();
|
|
|
|
$user->name = 'mgal';
|
|
|
|
$this->_em->persist($user);
|
|
|
|
|
|
|
|
$quote1 = new Group('quote1');
|
|
|
|
$user->groups->add($quote1);
|
|
|
|
|
|
|
|
$quote2 = new Group('quote2');
|
|
|
|
$user->groups->add($quote2);
|
|
|
|
|
|
|
|
$this->_em->flush();
|
|
|
|
|
|
|
|
$this->_em->clear();
|
|
|
|
}
|
|
|
|
|
2014-02-16 18:40:43 +01:00
|
|
|
public function testCanCountWithoutLoadingPersistentCollection()
|
|
|
|
{
|
2015-01-15 03:14:48 +00:00
|
|
|
$this->loadTweetFixture();
|
|
|
|
|
2014-02-21 10:30:41 +01:00
|
|
|
$repository = $this->_em->getRepository('Doctrine\Tests\Models\Tweet\User');
|
2014-02-16 18:40:43 +01:00
|
|
|
|
2016-12-07 23:33:41 +01:00
|
|
|
$user = $repository->findOneBy(['name' => 'ngal']);
|
2014-02-21 10:30:41 +01:00
|
|
|
$tweets = $user->tweets->matching(new Criteria());
|
2014-02-16 18:40:43 +01:00
|
|
|
|
2014-02-21 10:30:41 +01:00
|
|
|
$this->assertInstanceOf('Doctrine\ORM\LazyCriteriaCollection', $tweets);
|
|
|
|
$this->assertFalse($tweets->isInitialized());
|
|
|
|
$this->assertCount(2, $tweets);
|
|
|
|
$this->assertFalse($tweets->isInitialized());
|
2014-02-16 18:40:43 +01:00
|
|
|
|
|
|
|
// Make sure it works with constraints
|
2014-02-21 10:30:41 +01:00
|
|
|
$tweets = $user->tweets->matching(new Criteria(
|
|
|
|
Criteria::expr()->eq('content', 'Foo')
|
2014-02-16 18:40:43 +01:00
|
|
|
));
|
|
|
|
|
2014-02-21 10:30:41 +01:00
|
|
|
$this->assertInstanceOf('Doctrine\ORM\LazyCriteriaCollection', $tweets);
|
|
|
|
$this->assertFalse($tweets->isInitialized());
|
|
|
|
$this->assertCount(1, $tweets);
|
|
|
|
$this->assertFalse($tweets->isInitialized());
|
2014-02-16 18:40:43 +01:00
|
|
|
}
|
2015-01-15 03:14:48 +00:00
|
|
|
|
|
|
|
/*public function testCanCountWithoutLoadingManyToManyPersistentCollection()
|
|
|
|
{
|
|
|
|
$this->loadQuoteFixture();
|
|
|
|
|
|
|
|
$repository = $this->_em->getRepository('Doctrine\Tests\Models\Quote\User');
|
|
|
|
|
|
|
|
$user = $repository->findOneBy(array('name' => 'mgal'));
|
|
|
|
$groups = $user->groups->matching(new Criteria());
|
|
|
|
|
|
|
|
$this->assertInstanceOf('Doctrine\ORM\LazyManyToManyCriteriaCollection', $groups);
|
|
|
|
$this->assertFalse($groups->isInitialized());
|
|
|
|
$this->assertCount(2, $groups);
|
|
|
|
$this->assertFalse($groups->isInitialized());
|
|
|
|
|
|
|
|
// Make sure it works with constraints
|
|
|
|
$criteria = new Criteria(Criteria::expr()->eq('name', 'quote1'));
|
|
|
|
$groups = $user->groups->matching($criteria);
|
|
|
|
|
|
|
|
$this->assertInstanceOf('Doctrine\ORM\LazyManyToManyCriteriaCollection', $groups);
|
|
|
|
$this->assertFalse($groups->isInitialized());
|
|
|
|
$this->assertCount(1, $groups);
|
|
|
|
$this->assertFalse($groups->isInitialized());
|
|
|
|
}*/
|
2014-02-16 18:40:43 +01:00
|
|
|
}
|