1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
|
<?php
declare(strict_types=1);
namespace Doctrine\Tests\ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\EventManager;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Result;
use Doctrine\ORM\PersistentCollection;
use Doctrine\ORM\UnitOfWork;
use Doctrine\Tests\Mocks\EntityManagerMock;
use Doctrine\Tests\Models\ECommerce\ECommerceCart;
use Doctrine\Tests\Models\ECommerce\ECommerceProduct;
use Doctrine\Tests\OrmTestCase;
use PHPUnit\Framework\MockObject\MockObject;
use stdClass;
use function array_keys;
use function assert;
use function method_exists;
use function serialize;
use function unserialize;
/**
* Tests the lazy-loading capabilities of the PersistentCollection and the initialization of collections.
*/
class PersistentCollectionTest extends OrmTestCase
{
/** @var PersistentCollection */
protected $collection;
/** @var EntityManagerMock */
private $_emMock;
protected function setUp(): void
{
parent::setUp();
$platform = $this->createMock(AbstractPlatform::class);
$platform->method('supportsIdentityColumns')
->willReturn(true);
if (method_exists($platform, 'getSQLResultCasing')) {
$platform->method('getSQLResultCasing')
->willReturnArgument(0);
}
$connection = $this->createMock(Connection::class);
$connection->method('getDatabasePlatform')
->willReturn($platform);
$connection->method('getEventManager')
->willReturn(new EventManager());
$connection->method('executeQuery')
->willReturn($this->createMock(Result::class));
$this->_emMock = new EntityManagerMock($connection);
$this->setUpPersistentCollection();
}
/**
* Set up the PersistentCollection used for collection initialization tests.
*/
public function setUpPersistentCollection(): void
{
$classMetaData = $this->_emMock->getClassMetadata(ECommerceCart::class);
$this->collection = new PersistentCollection($this->_emMock, $classMetaData, new ArrayCollection());
$this->collection->setInitialized(false);
$this->collection->setOwner(new ECommerceCart(), $classMetaData->getAssociationMapping('products'));
}
public function testCanBePutInLazyLoadingMode(): void
{
$class = $this->_emMock->getClassMetadata(ECommerceProduct::class);
$collection = new PersistentCollection($this->_emMock, $class, new ArrayCollection());
$collection->setInitialized(false);
self::assertFalse($collection->isInitialized());
}
/**
* Test that PersistentCollection::current() initializes the collection.
*/
public function testCurrentInitializesCollection(): void
{
$this->collection->current();
self::assertTrue($this->collection->isInitialized());
}
/**
* Test that PersistentCollection::key() initializes the collection.
*/
public function testKeyInitializesCollection(): void
{
$this->collection->key();
self::assertTrue($this->collection->isInitialized());
}
/**
* Test that PersistentCollection::next() initializes the collection.
*/
public function testNextInitializesCollection(): void
{
$this->collection->next();
self::assertTrue($this->collection->isInitialized());
}
/** @group DDC-3382 */
public function testNonObjects(): void
{
self::assertEmpty($this->collection);
$this->collection->add('dummy');
self::assertNotEmpty($this->collection);
$product = new ECommerceProduct();
$this->collection->set(1, $product);
$this->collection->set(2, 'dummy');
$this->collection->set(3, null);
self::assertSame($product, $this->collection->get(1));
self::assertSame('dummy', $this->collection->get(2));
self::assertNull($this->collection->get(3));
}
/** @group 6110 */
public function testRemovingElementsAlsoRemovesKeys(): void
{
$dummy = new stdClass();
$this->collection->add($dummy);
self::assertEquals([0], array_keys($this->collection->toArray()));
$this->collection->removeElement($dummy);
self::assertEquals([], array_keys($this->collection->toArray()));
}
/** @group 6110 */
public function testClearWillAlsoClearKeys(): void
{
$this->collection->add(new stdClass());
$this->collection->clear();
self::assertEquals([], array_keys($this->collection->toArray()));
}
/** @group 6110 */
public function testClearWillAlsoResetKeyPositions(): void
{
$dummy = new stdClass();
$this->collection->add($dummy);
$this->collection->removeElement($dummy);
$this->collection->clear();
$this->collection->add($dummy);
self::assertEquals([0], array_keys($this->collection->toArray()));
}
/**
* @group 6613
* @group 6614
* @group 6616
*/
public function testWillKeepNewItemsInDirtyCollectionAfterInitialization(): void
{
$unitOfWork = $this->createMock(UnitOfWork::class);
assert($unitOfWork instanceof UnitOfWork || $unitOfWork instanceof MockObject);
$this->_emMock->setUnitOfWork($unitOfWork);
$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(static function (PersistentCollection $persistentCollection) use ($persistedElement): void {
$persistentCollection->unwrap()->add($persistedElement);
});
$this->collection->initialize();
self::assertSame([$persistedElement, $newElement], $this->collection->toArray());
self::assertTrue($this->collection->isInitialized());
self::assertTrue($this->collection->isDirty());
}
/**
* @group 6613
* @group 6614
* @group 6616
*/
public function testWillDeDuplicateNewItemsThatWerePreviouslyPersistedInDirtyCollectionAfterInitialization(): void
{
$unitOfWork = $this->createMock(UnitOfWork::class);
assert($unitOfWork instanceof UnitOfWork || $unitOfWork instanceof MockObject);
$this->_emMock->setUnitOfWork($unitOfWork);
$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(static function (PersistentCollection $persistentCollection) use (
$persistedElement,
$newElementThatIsAlsoPersisted
): void {
$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());
}
/**
* @group 6613
* @group 6614
* @group 6616
*/
public function testWillNotMarkCollectionAsDirtyAfterInitializationIfNoElementsWereAdded(): void
{
$unitOfWork = $this->createMock(UnitOfWork::class);
assert($unitOfWork instanceof UnitOfWork || $unitOfWork instanceof MockObject);
$this->_emMock->setUnitOfWork($unitOfWork);
$newElementThatIsAlsoPersisted = new stdClass();
$persistedElement = new stdClass();
$this->collection->add($newElementThatIsAlsoPersisted);
self::assertFalse($this->collection->isInitialized());
self::assertTrue($this->collection->isDirty());
$unitOfWork
->expects(self::once())
->method('loadCollection')
->with($this->collection)
->willReturnCallback(static function (PersistentCollection $persistentCollection) use (
$persistedElement,
$newElementThatIsAlsoPersisted
): void {
$persistentCollection->unwrap()->add($newElementThatIsAlsoPersisted);
$persistentCollection->unwrap()->add($persistedElement);
});
$this->collection->initialize();
self::assertSame(
[$newElementThatIsAlsoPersisted, $persistedElement],
$this->collection->toArray()
);
self::assertTrue($this->collection->isInitialized());
self::assertFalse($this->collection->isDirty());
}
public function testModifyUOWForDeferredImplicitOwnerOnClear(): void
{
$unitOfWork = $this->createMock(UnitOfWork::class);
$unitOfWork->expects(self::once())->method('scheduleCollectionDeletion');
$this->_emMock->setUnitOfWork($unitOfWork);
$this->collection->clear();
}
public function testItCanBeSerializedAndUnserializedBack(): void
{
$this->collection->add(new stdClass());
$collection = unserialize(serialize($this->collection));
$collection->add(new stdClass());
$collection[3] = new stdClass();
self::assertCount(3, $collection);
}
}
|