2009-07-28 11:43:42 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Doctrine\Tests\ORM;
|
|
|
|
|
2009-08-03 13:25:56 +00:00
|
|
|
use Doctrine\Common\Collections\ArrayCollection;
|
2017-08-11 18:58:05 +02:00
|
|
|
use Doctrine\Common\Collections\Collection;
|
2009-07-28 11:43:42 +00:00
|
|
|
use Doctrine\ORM\PersistentCollection;
|
2017-08-11 18:58:05 +02:00
|
|
|
use Doctrine\ORM\UnitOfWork;
|
2009-07-28 11:43:42 +00:00
|
|
|
use Doctrine\Tests\Mocks\ConnectionMock;
|
2015-04-02 23:44:12 +01:00
|
|
|
use Doctrine\Tests\Mocks\DriverMock;
|
2009-07-28 11:43:42 +00:00
|
|
|
use Doctrine\Tests\Mocks\EntityManagerMock;
|
2013-07-23 09:40:46 -04:00
|
|
|
use Doctrine\Tests\Models\ECommerce\ECommerceCart;
|
2015-11-06 15:23:16 +00:00
|
|
|
use Doctrine\Tests\Models\ECommerce\ECommerceProduct;
|
2013-07-23 09:40:46 -04:00
|
|
|
use Doctrine\Tests\OrmTestCase;
|
2009-07-28 11:43:42 +00:00
|
|
|
|
|
|
|
/**
|
2013-07-23 09:40:46 -04:00
|
|
|
* Tests the lazy-loading capabilities of the PersistentCollection and the initialization of collections.
|
2009-07-28 11:43:42 +00:00
|
|
|
* @author Giorgio Sironi <piccoloprincipeazzurro@gmail.com>
|
2013-07-23 09:40:46 -04:00
|
|
|
* @author Austin Morris <austin.morris@gmail.com>
|
2009-07-28 11:43:42 +00:00
|
|
|
*/
|
2013-07-23 09:40:46 -04:00
|
|
|
class PersistentCollectionTest extends OrmTestCase
|
2009-07-28 11:43:42 +00:00
|
|
|
{
|
2013-07-23 09:40:46 -04:00
|
|
|
/**
|
|
|
|
* @var PersistentCollection
|
|
|
|
*/
|
|
|
|
protected $collection;
|
|
|
|
|
2015-04-02 23:44:12 +01:00
|
|
|
/**
|
2017-08-11 18:58:05 +02:00
|
|
|
* @var EntityManagerMock
|
2015-04-02 23:44:12 +01:00
|
|
|
*/
|
2009-07-28 11:43:42 +00:00
|
|
|
private $_emMock;
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2009-07-28 11:43:42 +00:00
|
|
|
protected function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
2015-04-02 23:44:12 +01:00
|
|
|
|
|
|
|
$this->_emMock = EntityManagerMock::create(new ConnectionMock([], new DriverMock()));
|
2009-07-28 11:43:42 +00:00
|
|
|
}
|
|
|
|
|
2013-07-23 09:40:46 -04:00
|
|
|
/**
|
|
|
|
* Set up the PersistentCollection used for collection initialization tests.
|
|
|
|
*/
|
|
|
|
public function setUpPersistentCollection()
|
|
|
|
{
|
2016-12-08 18:01:04 +01:00
|
|
|
$classMetaData = $this->_emMock->getClassMetadata(ECommerceCart::class);
|
2013-07-23 09:40:46 -04:00
|
|
|
$this->collection = new PersistentCollection($this->_emMock, $classMetaData, new ArrayCollection);
|
|
|
|
$this->collection->setInitialized(false);
|
|
|
|
$this->collection->setOwner(new ECommerceCart(), $classMetaData->getAssociationMapping('products'));
|
|
|
|
}
|
|
|
|
|
2009-07-28 11:43:42 +00:00
|
|
|
public function testCanBePutInLazyLoadingMode()
|
|
|
|
{
|
2016-12-08 18:01:04 +01:00
|
|
|
$class = $this->_emMock->getClassMetadata(ECommerceProduct::class);
|
2009-08-03 13:25:56 +00:00
|
|
|
$collection = new PersistentCollection($this->_emMock, $class, new ArrayCollection);
|
|
|
|
$collection->setInitialized(false);
|
2010-08-09 22:27:34 +02:00
|
|
|
$this->assertFalse($collection->isInitialized());
|
2009-07-28 11:43:42 +00:00
|
|
|
}
|
2013-07-23 09: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());
|
|
|
|
}
|
2015-11-06 15:23:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @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));
|
|
|
|
}
|
2016-10-31 12:03:19 +01:00
|
|
|
|
|
|
|
/**
|
2016-11-26 06:02:16 +01:00
|
|
|
* @group 6110
|
2016-10-31 12:03:19 +01:00
|
|
|
*/
|
2016-11-26 06:02:16 +01:00
|
|
|
public function testRemovingElementsAlsoRemovesKeys()
|
2016-10-31 12:03:19 +01:00
|
|
|
{
|
|
|
|
$this->setUpPersistentCollection();
|
|
|
|
|
2017-08-11 15:14:42 +02:00
|
|
|
$dummy = new \stdClass();
|
|
|
|
|
|
|
|
$this->collection->add($dummy);
|
2016-10-31 12:03:19 +01:00
|
|
|
$this->assertEquals([0], array_keys($this->collection->toArray()));
|
|
|
|
|
2017-08-11 15:14:42 +02:00
|
|
|
$this->collection->removeElement($dummy);
|
2016-10-31 12:03:19 +01:00
|
|
|
$this->assertEquals([], array_keys($this->collection->toArray()));
|
2016-11-26 06:02:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group 6110
|
|
|
|
*/
|
|
|
|
public function testClearWillAlsoClearKeys()
|
|
|
|
{
|
|
|
|
$this->setUpPersistentCollection();
|
2016-10-31 12:03:19 +01:00
|
|
|
|
2017-08-11 15:14:42 +02:00
|
|
|
$this->collection->add(new \stdClass());
|
2016-10-31 12:03:19 +01:00
|
|
|
$this->collection->clear();
|
|
|
|
$this->assertEquals([], array_keys($this->collection->toArray()));
|
2016-11-26 06:02:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group 6110
|
|
|
|
*/
|
|
|
|
public function testClearWillAlsoResetKeyPositions()
|
|
|
|
{
|
|
|
|
$this->setUpPersistentCollection();
|
2016-10-31 12:03:19 +01:00
|
|
|
|
2017-08-11 15:15:57 +02:00
|
|
|
$dummy = new \stdClass();
|
|
|
|
|
|
|
|
$this->collection->add($dummy);
|
|
|
|
$this->collection->removeElement($dummy);
|
2016-10-31 12:03:19 +01:00
|
|
|
$this->collection->clear();
|
2017-08-11 15:15:57 +02:00
|
|
|
$this->collection->add($dummy);
|
2016-10-31 12:03:19 +01:00
|
|
|
$this->assertEquals([0], array_keys($this->collection->toArray()));
|
|
|
|
}
|
2017-08-11 18:58:05 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @group 6613
|
|
|
|
* @group 6614
|
|
|
|
* @group 6616
|
|
|
|
*/
|
2017-08-11 19:02:39 +02:00
|
|
|
public function testWillKeepNewItemsInDirtyCollectionAfterInitialization()
|
2017-08-11 18:58:05 +02:00
|
|
|
{
|
|
|
|
/* @var $unitOfWork UnitOfWork|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
$unitOfWork = $this->createMock(UnitOfWork::class);
|
|
|
|
|
|
|
|
$this->_emMock->setUnitOfWork($unitOfWork);
|
|
|
|
|
|
|
|
$this->setUpPersistentCollection();
|
|
|
|
|
|
|
|
$newElement = new \stdClass();
|
|
|
|
$persistedElement = new \stdClass();
|
|
|
|
|
|
|
|
$this->collection->add($newElement);
|
|
|
|
|
|
|
|
self::assertFalse($this->collection->isInitialized());
|
|
|
|
self::assertTrue($this->collection->isDirty());
|
|
|
|
|
|
|
|
$unitOfWork
|
|
|
|
->expects(self::once())
|
|
|
|
->method('loadCollection')
|
|
|
|
->with($this->collection)
|
|
|
|
->willReturnCallback(function (PersistentCollection $persistentCollection) use ($persistedElement) {
|
|
|
|
$persistentCollection->unwrap()->add($persistedElement);
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->collection->initialize();
|
|
|
|
|
|
|
|
self::assertSame([$persistedElement, $newElement], $this->collection->toArray());
|
|
|
|
self::assertTrue($this->collection->isInitialized());
|
|
|
|
self::assertTrue($this->collection->isDirty());
|
|
|
|
}
|
2017-08-11 19:02:39 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @group 6613
|
|
|
|
* @group 6614
|
|
|
|
* @group 6616
|
|
|
|
*/
|
|
|
|
public function testWillDeDuplicateNewItemsThatWerePreviouslyPersistedInDirtyCollectionAfterInitialization()
|
|
|
|
{
|
|
|
|
/* @var $unitOfWork UnitOfWork|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
$unitOfWork = $this->createMock(UnitOfWork::class);
|
|
|
|
|
|
|
|
$this->_emMock->setUnitOfWork($unitOfWork);
|
|
|
|
|
|
|
|
$this->setUpPersistentCollection();
|
|
|
|
|
|
|
|
$newElement = new \stdClass();
|
|
|
|
$newElementThatIsAlsoPersisted = new \stdClass();
|
|
|
|
$persistedElement = new \stdClass();
|
|
|
|
|
|
|
|
$this->collection->add($newElementThatIsAlsoPersisted);
|
|
|
|
$this->collection->add($newElement);
|
|
|
|
|
|
|
|
self::assertFalse($this->collection->isInitialized());
|
|
|
|
self::assertTrue($this->collection->isDirty());
|
|
|
|
|
|
|
|
$unitOfWork
|
|
|
|
->expects(self::once())
|
|
|
|
->method('loadCollection')
|
|
|
|
->with($this->collection)
|
|
|
|
->willReturnCallback(function (PersistentCollection $persistentCollection) use (
|
|
|
|
$persistedElement,
|
|
|
|
$newElementThatIsAlsoPersisted
|
|
|
|
) {
|
|
|
|
$persistentCollection->unwrap()->add($newElementThatIsAlsoPersisted);
|
|
|
|
$persistentCollection->unwrap()->add($persistedElement);
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->collection->initialize();
|
|
|
|
|
|
|
|
self::assertSame(
|
|
|
|
[$newElementThatIsAlsoPersisted, $persistedElement, $newElement],
|
|
|
|
$this->collection->toArray()
|
|
|
|
);
|
|
|
|
self::assertTrue($this->collection->isInitialized());
|
|
|
|
self::assertTrue($this->collection->isDirty());
|
|
|
|
}
|
2009-07-28 11:43:42 +00:00
|
|
|
}
|