* @author Austin Morris */ class PersistentCollectionTest extends OrmTestCase { /** * @var PersistentCollection */ protected $collection; /** * @var \Doctrine\ORM\EntityManagerInterface */ private $_emMock; protected function setUp() { parent::setUp(); $this->_emMock = EntityManagerMock::create(new ConnectionMock([], new DriverMock())); } /** * 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')); } public function testCanBePutInLazyLoadingMode() { $class = $this->_emMock->getClassMetadata('Doctrine\Tests\Models\ECommerce\ECommerceProduct'); $collection = new PersistentCollection($this->_emMock, $class, new ArrayCollection); $collection->setInitialized(false); $this->assertFalse($collection->isInitialized()); } /** * 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()); } /** * @group DDC-3382 */ public function testNonObjects() { $this->setUpPersistentCollection(); $this->assertEmpty($this->collection); $this->collection->add("dummy"); $this->assertNotEmpty($this->collection); $product = new ECommerceProduct(); $this->collection->set(1, $product); $this->collection->set(2, "dummy"); $this->collection->set(3, null); $this->assertSame($product, $this->collection->get(1)); $this->assertSame("dummy", $this->collection->get(2)); $this->assertSame(null, $this->collection->get(3)); } }