2009-07-28 15:43:42 +04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Doctrine\Tests\ORM;
|
|
|
|
|
2009-08-03 17:25:56 +04:00
|
|
|
use Doctrine\Common\Collections\ArrayCollection;
|
2009-07-28 15:43:42 +04:00
|
|
|
use Doctrine\ORM\PersistentCollection;
|
|
|
|
use Doctrine\Tests\Mocks\ConnectionMock;
|
|
|
|
use Doctrine\Tests\Mocks\EntityManagerMock;
|
2013-07-23 17:40:46 +04:00
|
|
|
use Doctrine\Tests\Models\ECommerce\ECommerceCart;
|
|
|
|
use Doctrine\Tests\OrmTestCase;
|
2009-07-28 15:43:42 +04:00
|
|
|
|
|
|
|
/**
|
2013-07-23 17:40:46 +04:00
|
|
|
* Tests the lazy-loading capabilities of the PersistentCollection and the initialization of collections.
|
2009-07-28 15:43:42 +04:00
|
|
|
* @author Giorgio Sironi <piccoloprincipeazzurro@gmail.com>
|
2013-07-23 17:40:46 +04:00
|
|
|
* @author Austin Morris <austin.morris@gmail.com>
|
2009-07-28 15:43:42 +04:00
|
|
|
*/
|
2013-07-23 17:40:46 +04:00
|
|
|
class PersistentCollectionTest extends OrmTestCase
|
2009-07-28 15:43:42 +04:00
|
|
|
{
|
2013-07-23 17:40:46 +04:00
|
|
|
/**
|
|
|
|
* @var PersistentCollection
|
|
|
|
*/
|
|
|
|
protected $collection;
|
|
|
|
|
2009-07-28 15:43:42 +04:00
|
|
|
private $_connectionMock;
|
|
|
|
private $_emMock;
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2009-07-28 15:43:42 +04:00
|
|
|
protected function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
// SUT
|
|
|
|
$this->_connectionMock = new ConnectionMock(array(), new \Doctrine\Tests\Mocks\DriverMock());
|
|
|
|
$this->_emMock = EntityManagerMock::create($this->_connectionMock);
|
|
|
|
}
|
|
|
|
|
2013-07-23 17:40:46 +04:00
|
|
|
/**
|
|
|
|
* Set up the PersistentCollection used for collection initialization tests.
|
|
|
|
*/
|
|
|
|
public function setUpPersistentCollection()
|
|
|
|
{
|
|
|
|
$classMetaData = $this->_emMock->getClassMetadata('Doctrine\Tests\Models\ECommerce\ECommerceCart');
|
|
|
|
$this->collection = new PersistentCollection($this->_emMock, $classMetaData, new ArrayCollection);
|
|
|
|
$this->collection->setInitialized(false);
|
|
|
|
$this->collection->setOwner(new ECommerceCart(), $classMetaData->getAssociationMapping('products'));
|
|
|
|
}
|
|
|
|
|
2009-07-28 15:43:42 +04:00
|
|
|
public function testCanBePutInLazyLoadingMode()
|
|
|
|
{
|
|
|
|
$class = $this->_emMock->getClassMetadata('Doctrine\Tests\Models\ECommerce\ECommerceProduct');
|
2009-08-03 17:25:56 +04:00
|
|
|
$collection = new PersistentCollection($this->_emMock, $class, new ArrayCollection);
|
|
|
|
$collection->setInitialized(false);
|
2010-08-10 00:27:34 +04:00
|
|
|
$this->assertFalse($collection->isInitialized());
|
2009-07-28 15:43:42 +04:00
|
|
|
}
|
2013-07-23 17:40:46 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that PersistentCollection::current() initializes the collection.
|
|
|
|
*/
|
|
|
|
public function testCurrentInitializesCollection()
|
|
|
|
{
|
|
|
|
$this->setUpPersistentCollection();
|
|
|
|
$this->collection->current();
|
|
|
|
$this->assertTrue($this->collection->isInitialized());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that PersistentCollection::key() initializes the collection.
|
|
|
|
*/
|
|
|
|
public function testKeyInitializesCollection()
|
|
|
|
{
|
|
|
|
$this->setUpPersistentCollection();
|
|
|
|
$this->collection->key();
|
|
|
|
$this->assertTrue($this->collection->isInitialized());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that PersistentCollection::next() initializes the collection.
|
|
|
|
*/
|
|
|
|
public function testNextInitializesCollection()
|
|
|
|
{
|
|
|
|
$this->setUpPersistentCollection();
|
|
|
|
$this->collection->next();
|
|
|
|
$this->assertTrue($this->collection->isInitialized());
|
|
|
|
}
|
2009-07-28 15:43:42 +04:00
|
|
|
}
|