1
0
mirror of synced 2025-02-03 13:59:27 +03:00
doctrine2/tests/Doctrine/Tests/ORM/Functional/PersistentCollectionCriteriaTest.php

121 lines
3.5 KiB
PHP
Raw Normal View History

2014-02-16 18:40:43 +01:00
<?php
namespace Doctrine\Tests\ORM\Functional;
use Doctrine\Common\Collections\Criteria;
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;
use Doctrine\Tests\Models\Tweet\User as TweetUser;
use Doctrine\Tests\OrmFunctionalTestCase;
2014-02-16 18:40:43 +01:00
/**
* @author Michaël Gallego <mic.gallego@gmail.com>
*/
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');
$this->useModelSet('quote');
2014-02-16 18:40:43 +01:00
parent::setUp();
}
public function tearDown()
{
if ($this->_em) {
$this->_em->getConfiguration()->setEntityNamespaces(array());
}
parent::tearDown();
}
public function loadTweetFixture()
2014-02-16 18:40:43 +01: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();
}
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()
{
$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
2014-02-21 10:30:41 +01:00
$user = $repository->findOneBy(array('name' => 'ngal'));
$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
}
/*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
}