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
|
<?php
declare(strict_types=1);
namespace Doctrine\Tests\ORM\Functional;
use Doctrine\Common\Proxy\Proxy as CommonProxy;
use Doctrine\ORM\Proxy\DefaultProxyClassNameResolver;
use Doctrine\Tests\Models\Company\CompanyAuction;
use Doctrine\Tests\Models\ECommerce\ECommerceProduct;
use Doctrine\Tests\Models\ECommerce\ECommerceProduct2;
use Doctrine\Tests\Models\ECommerce\ECommerceShipping;
use Doctrine\Tests\OrmFunctionalTestCase;
use PHPUnit\Framework\Attributes\Group;
use function assert;
use function file_exists;
use function str_replace;
use function strlen;
use function substr;
use const DIRECTORY_SEPARATOR;
/**
* Tests the generation of a proxy object for lazy loading.
*/
class ReferenceProxyTest extends OrmFunctionalTestCase
{
protected function setUp(): void
{
$this->useModelSet('ecommerce');
$this->useModelSet('company');
parent::setUp();
}
public function createProduct(): int
{
$product = new ECommerceProduct();
$product->setName('Doctrine Cookbook');
$this->_em->persist($product);
$this->_em->flush();
$this->_em->clear();
return $product->getId();
}
public function createAuction(): int
{
$event = new CompanyAuction();
$event->setData('Doctrine Cookbook');
$this->_em->persist($event);
$this->_em->flush();
$this->_em->clear();
return $event->getId();
}
public function testLazyLoadsFieldValuesFromDatabase(): void
{
$id = $this->createProduct();
$productProxy = $this->_em->getReference(ECommerceProduct::class, ['id' => $id]);
self::assertEquals('Doctrine Cookbook', $productProxy->getName());
}
#[Group('DDC-727')]
public function testAccessMetatadaForProxy(): void
{
$id = $this->createProduct();
$entity = $this->_em->getReference(ECommerceProduct::class, $id);
$class = $this->_em->getClassMetadata($entity::class);
self::assertEquals(ECommerceProduct::class, $class->name);
}
#[Group('DDC-1033')]
public function testReferenceFind(): void
{
$id = $this->createProduct();
$entity = $this->_em->getReference(ECommerceProduct::class, $id);
$entity2 = $this->_em->find(ECommerceProduct::class, $id);
self::assertSame($entity, $entity2);
self::assertEquals('Doctrine Cookbook', $entity2->getName());
}
#[Group('DDC-1033')]
public function testCloneProxy(): void
{
$id = $this->createProduct();
$entity = $this->_em->getReference(ECommerceProduct::class, $id);
assert($entity instanceof ECommerceProduct);
$clone = clone $entity;
assert($clone instanceof ECommerceProduct);
self::assertEquals($id, $entity->getId());
self::assertEquals('Doctrine Cookbook', $entity->getName());
self::assertFalse($this->_em->contains($clone), 'Cloning a reference proxy should return an unmanaged/detached entity.');
self::assertEquals($id, $clone->getId(), 'Cloning a reference proxy should return same id.');
self::assertEquals('Doctrine Cookbook', $clone->getName(), 'Cloning a reference proxy should return same product name.');
// domain logic, Product::__clone sets isCloned public property
self::assertTrue($clone->isCloned);
self::assertFalse($entity->isCloned);
}
public function testCloneProxyWithResetId(): void
{
$id = $this->createProduct();
$entity = $this->_em->getReference(ECommerceProduct2::class, $id);
assert($entity instanceof ECommerceProduct2);
$clone = clone $entity;
assert($clone instanceof ECommerceProduct2);
self::assertEquals($id, $entity->getId());
self::assertEquals('Doctrine Cookbook', $entity->getName());
self::assertFalse($this->_em->contains($clone));
self::assertNull($clone->getId());
self::assertEquals('Clone of Doctrine Cookbook', $clone->getName());
}
#[Group('DDC-733')]
public function testInitializeProxy(): void
{
$id = $this->createProduct();
$entity = $this->_em->getReference(ECommerceProduct::class, $id);
assert($entity instanceof ECommerceProduct);
self::assertTrue($this->isUninitializedObject($entity), 'Pre-Condition: Object is unitialized proxy.');
$this->_em->getUnitOfWork()->initializeObject($entity);
self::assertFalse($this->isUninitializedObject($entity), 'Should be initialized after called UnitOfWork::initializeObject()');
}
#[Group('DDC-1163')]
public function testInitializeChangeAndFlushProxy(): void
{
$id = $this->createProduct();
$entity = $this->_em->getReference(ECommerceProduct::class, $id);
assert($entity instanceof ECommerceProduct);
$entity->setName('Doctrine 2 Cookbook');
$this->_em->flush();
$this->_em->clear();
$entity = $this->_em->getReference(ECommerceProduct::class, $id);
self::assertEquals('Doctrine 2 Cookbook', $entity->getName());
}
#[Group('DDC-1022')]
public function testWakeupOnProxy(): void
{
$id = $this->createProduct();
$entity = $this->_em->getReference(ECommerceProduct::class, $id);
assert($entity instanceof ECommerceProduct);
self::assertFalse($entity->wakeUp);
$entity->setName('Doctrine 2 Cookbook');
if ($entity instanceof CommonProxy) {
self::assertTrue($entity->wakeUp, 'Loading the proxy should call __wakeup().');
} else {
self::assertFalse($entity->wakeUp, 'Loading the proxy should call __wakeup().');
}
}
public function testDoNotInitializeProxyOnGettingTheIdentifier(): void
{
$id = $this->createProduct();
$entity = $this->_em->getReference(ECommerceProduct::class, $id);
assert($entity instanceof ECommerceProduct);
self::assertTrue($this->isUninitializedObject($entity), 'Pre-Condition: Object is unitialized proxy.');
self::assertEquals($id, $entity->getId());
self::assertTrue($this->isUninitializedObject($entity), "Getting the identifier doesn't initialize the proxy.");
}
#[Group('DDC-1625')]
public function testDoNotInitializeProxyOnGettingTheIdentifierDDC1625(): void
{
$id = $this->createAuction();
$entity = $this->_em->getReference(CompanyAuction::class, $id);
assert($entity instanceof CompanyAuction);
self::assertTrue($this->isUninitializedObject($entity), 'Pre-Condition: Object is unitialized proxy.');
self::assertEquals($id, $entity->getId());
self::assertTrue($this->isUninitializedObject($entity), "Getting the identifier doesn't initialize the proxy when extending.");
}
public function testDoNotInitializeProxyOnGettingTheIdentifierAndReturnTheRightType(): void
{
$product = new ECommerceProduct();
$product->setName('Doctrine Cookbook');
$shipping = new ECommerceShipping();
$shipping->setDays(1);
$product->setShipping($shipping);
$this->_em->persist($product);
$this->_em->flush();
$this->_em->clear();
$id = $shipping->getId();
$product = $this->_em->getRepository(ECommerceProduct::class)->find($product->getId());
$entity = $product->getShipping();
self::assertTrue($this->isUninitializedObject($entity), 'Pre-Condition: Object is unitialized proxy.');
self::assertEquals($id, $entity->getId());
self::assertSame($id, $entity->getId(), "Check that the id's are the same value, and type.");
self::assertTrue($this->isUninitializedObject($entity), "Getting the identifier doesn't initialize the proxy.");
}
public function testInitializeProxyOnGettingSomethingOtherThanTheIdentifier(): void
{
$id = $this->createProduct();
$entity = $this->_em->getReference(ECommerceProduct::class, $id);
assert($entity instanceof ECommerceProduct);
self::assertTrue($this->isUninitializedObject($entity), 'Pre-Condition: Object is unitialized proxy.');
self::assertEquals('Doctrine Cookbook', $entity->getName());
self::assertFalse($this->isUninitializedObject($entity), 'Getting something other than the identifier initializes the proxy.');
}
#[Group('DDC-1604')]
public function testCommonPersistenceProxy(): void
{
if ($this->_em->getConfiguration()->isNativeLazyObjectsEnabled()) {
self::markTestSkipped('Test only works with proxy generation disabled.');
}
$id = $this->createProduct();
$entity = $this->_em->getReference(ECommerceProduct::class, $id);
assert($entity instanceof ECommerceProduct);
$className = DefaultProxyClassNameResolver::getClass($entity);
self::assertTrue($this->isUninitializedObject($entity));
self::assertEquals(ECommerceProduct::class, $className);
$restName = str_replace($this->_em->getConfiguration()->getProxyNamespace(), '', $entity::class);
$restName = substr($entity::class, strlen($this->_em->getConfiguration()->getProxyNamespace()) + 1);
$proxyFileName = $this->_em->getConfiguration()->getProxyDir() . DIRECTORY_SEPARATOR . str_replace('\\', '', $restName) . '.php';
self::assertTrue(file_exists($proxyFileName), 'Proxy file name cannot be found generically.');
$this->initializeObject($entity);
self::assertFalse($this->isUninitializedObject($entity));
}
}
|