1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\Tests\ORM\Functional; |
6
|
|
|
|
7
|
|
|
use Doctrine\ORM\Proxy\Factory\ProxyFactory; |
8
|
|
|
use Doctrine\ORM\Proxy\Factory\StaticProxyFactory; |
9
|
|
|
use Doctrine\ORM\Utility\StaticClassNameConverter; |
10
|
|
|
use Doctrine\Tests\Models\Company\CompanyAuction; |
11
|
|
|
use Doctrine\Tests\Models\ECommerce\ECommerceProduct; |
12
|
|
|
use Doctrine\Tests\Models\ECommerce\ECommerceShipping; |
13
|
|
|
use Doctrine\Tests\OrmFunctionalTestCase; |
14
|
|
|
use ProxyManager\GeneratorStrategy\FileWriterGeneratorStrategy; |
15
|
|
|
use ProxyManager\Proxy\GhostObjectInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Tests the generation of a proxy object for lazy loading. |
19
|
|
|
* |
20
|
|
|
* @author Giorgio Sironi <[email protected]> |
21
|
|
|
* @author Benjamin Eberlei <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class ReferenceProxyTest extends OrmFunctionalTestCase |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var ProxyFactory |
27
|
|
|
*/ |
28
|
|
|
private $factory; |
29
|
|
|
|
30
|
|
|
protected function setUp() |
31
|
|
|
{ |
32
|
|
|
$this->useModelSet('ecommerce'); |
33
|
|
|
$this->useModelSet('company'); |
34
|
|
|
|
35
|
|
|
parent::setUp(); |
36
|
|
|
|
37
|
|
|
$configuration = $this->em->getConfiguration(); |
38
|
|
|
|
39
|
|
|
$configuration->setProxyNamespace(__NAMESPACE__ . '\\ProxyTest'); |
40
|
|
|
$configuration->setProxyDir(__DIR__ . '/../../Proxies'); |
41
|
|
|
$configuration->setAutoGenerateProxyClasses(StaticProxyFactory::AUTOGENERATE_ALWAYS); |
42
|
|
|
|
43
|
|
|
$this->factory = new StaticProxyFactory($this->em, $configuration->buildGhostObjectFactory()); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function createProduct() |
47
|
|
|
{ |
48
|
|
|
$product = new ECommerceProduct(); |
49
|
|
|
$product->setName('Doctrine Cookbook'); |
50
|
|
|
$this->em->persist($product); |
51
|
|
|
|
52
|
|
|
$this->em->flush(); |
53
|
|
|
$this->em->clear(); |
54
|
|
|
|
55
|
|
|
return $product->getId(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function createAuction() |
59
|
|
|
{ |
60
|
|
|
$event = new CompanyAuction(); |
61
|
|
|
$event->setData('Doctrine Cookbook'); |
62
|
|
|
$this->em->persist($event); |
63
|
|
|
|
64
|
|
|
$this->em->flush(); |
65
|
|
|
$this->em->clear(); |
66
|
|
|
|
67
|
|
|
return $event->getId(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testLazyLoadsFieldValuesFromDatabase() |
71
|
|
|
{ |
72
|
|
|
$id = $this->createProduct(); |
73
|
|
|
|
74
|
|
|
$productProxy = $this->em->getReference(ECommerceProduct::class, ['id' => $id]); |
75
|
|
|
|
76
|
|
|
self::assertEquals('Doctrine Cookbook', $productProxy->getName()); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @group DDC-727 |
81
|
|
|
*/ |
82
|
|
View Code Duplication |
public function testAccessMetatadaForProxy() |
83
|
|
|
{ |
84
|
|
|
$id = $this->createProduct(); |
85
|
|
|
|
86
|
|
|
$entity = $this->em->getReference(ECommerceProduct::class , $id); |
87
|
|
|
$class = $this->em->getClassMetadata(get_class($entity)); |
88
|
|
|
|
89
|
|
|
self::assertEquals(ECommerceProduct::class, $class->getClassName()); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @group DDC-1033 |
94
|
|
|
*/ |
95
|
|
View Code Duplication |
public function testReferenceFind() |
96
|
|
|
{ |
97
|
|
|
$id = $this->createProduct(); |
98
|
|
|
|
99
|
|
|
$entity = $this->em->getReference(ECommerceProduct::class , $id); |
100
|
|
|
$entity2 = $this->em->find(ECommerceProduct::class , $id); |
101
|
|
|
|
102
|
|
|
self::assertSame($entity, $entity2); |
103
|
|
|
self::assertEquals('Doctrine Cookbook', $entity2->getName()); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @group DDC-1033 |
108
|
|
|
*/ |
109
|
|
|
public function testCloneProxy() |
110
|
|
|
{ |
111
|
|
|
$id = $this->createProduct(); |
112
|
|
|
|
113
|
|
|
/* @var $entity ECommerceProduct */ |
114
|
|
|
$entity = $this->em->getReference(ECommerceProduct::class , $id); |
115
|
|
|
|
116
|
|
|
/* @var $clone ECommerceProduct */ |
117
|
|
|
$clone = clone $entity; |
118
|
|
|
|
119
|
|
|
self::assertEquals($id, $entity->getId()); |
120
|
|
|
self::assertEquals('Doctrine Cookbook', $entity->getName()); |
121
|
|
|
|
122
|
|
|
self::assertFalse($this->em->contains($clone), "Cloning a reference proxy should return an unmanaged/detached entity."); |
123
|
|
|
self::assertTrue($this->em->contains($entity), "Real instance should be managed"); |
124
|
|
|
self::assertEquals($id, $clone->getId(), "Cloning a reference proxy should return same id."); |
125
|
|
|
self::assertEquals('Doctrine Cookbook', $clone->getName(), "Cloning a reference proxy should return same product name."); |
126
|
|
|
self::assertEquals('Doctrine Cookbook', $entity->getName(), "Real instance should contain the real data too"); |
127
|
|
|
|
128
|
|
|
// domain logic, Product::__clone sets isCloned public property |
129
|
|
|
self::assertTrue($clone->isCloned); |
130
|
|
|
self::assertFalse($entity->isCloned); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @group DDC-733 |
135
|
|
|
*/ |
136
|
|
|
public function testInitializeProxy() |
137
|
|
|
{ |
138
|
|
|
$id = $this->createProduct(); |
139
|
|
|
|
140
|
|
|
/* @var $entity ECommerceProduct|GhostObjectInterface */ |
141
|
|
|
$entity = $this->em->getReference(ECommerceProduct::class , $id); |
142
|
|
|
|
143
|
|
|
self::assertFalse($entity->isProxyInitialized(), "Pre-Condition: Object is unitialized proxy."); |
|
|
|
|
144
|
|
|
|
145
|
|
|
$this->em->getUnitOfWork()->initializeObject($entity); |
146
|
|
|
|
147
|
|
|
self::assertTrue($entity->isProxyInitialized(), "Should be initialized after called UnitOfWork::initializeObject()"); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @group DDC-1163 |
152
|
|
|
*/ |
153
|
|
View Code Duplication |
public function testInitializeChangeAndFlushProxy() |
154
|
|
|
{ |
155
|
|
|
$id = $this->createProduct(); |
156
|
|
|
|
157
|
|
|
/* @var $entity ECommerceProduct|GhostObjectInterface */ |
158
|
|
|
$entity = $this->em->getReference(ECommerceProduct::class , $id); |
159
|
|
|
|
160
|
|
|
$entity->setName('Doctrine 2 Cookbook'); |
|
|
|
|
161
|
|
|
|
162
|
|
|
$this->em->flush(); |
163
|
|
|
$this->em->clear(); |
164
|
|
|
|
165
|
|
|
/* @var $entity ECommerceProduct|GhostObjectInterface */ |
166
|
|
|
$entity = $this->em->getReference(ECommerceProduct::class , $id); |
167
|
|
|
|
168
|
|
|
self::assertEquals('Doctrine 2 Cookbook', $entity->getName()); |
|
|
|
|
169
|
|
|
} |
170
|
|
|
|
171
|
|
View Code Duplication |
public function testDoNotInitializeProxyOnGettingTheIdentifier() |
172
|
|
|
{ |
173
|
|
|
$id = $this->createProduct(); |
174
|
|
|
|
175
|
|
|
/* @var $entity ECommerceProduct|GhostObjectInterface */ |
176
|
|
|
$entity = $this->em->getReference(ECommerceProduct::class , $id); |
177
|
|
|
|
178
|
|
|
self::assertFalse($entity->isProxyInitialized(), "Pre-Condition: Object is unitialized proxy."); |
|
|
|
|
179
|
|
|
self::assertEquals($id, $entity->getId()); |
|
|
|
|
180
|
|
|
self::assertFalse($entity->isProxyInitialized(), "Getting the identifier doesn't initialize the proxy."); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @group DDC-1625 |
185
|
|
|
*/ |
186
|
|
View Code Duplication |
public function testDoNotInitializeProxyOnGettingTheIdentifier_DDC_1625() |
187
|
|
|
{ |
188
|
|
|
$id = $this->createAuction(); |
189
|
|
|
|
190
|
|
|
/* @var $entity CompanyAuction|GhostObjectInterface */ |
191
|
|
|
$entity = $this->em->getReference(CompanyAuction::class , $id); |
192
|
|
|
|
193
|
|
|
self::assertFalse($entity->isProxyInitialized(), "Pre-Condition: Object is unitialized proxy."); |
|
|
|
|
194
|
|
|
self::assertEquals($id, $entity->getId()); |
|
|
|
|
195
|
|
|
self::assertFalse($entity->isProxyInitialized(), "Getting the identifier doesn't initialize the proxy when extending."); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
public function testDoNotInitializeProxyOnGettingTheIdentifierAndReturnTheRightType() |
199
|
|
|
{ |
200
|
|
|
$product = new ECommerceProduct(); |
201
|
|
|
$product->setName('Doctrine Cookbook'); |
202
|
|
|
|
203
|
|
|
$shipping = new ECommerceShipping(); |
204
|
|
|
$shipping->setDays(1); |
205
|
|
|
$product->setShipping($shipping); |
206
|
|
|
|
207
|
|
|
$this->em->persist($product); |
208
|
|
|
$this->em->flush(); |
209
|
|
|
$this->em->clear(); |
210
|
|
|
|
211
|
|
|
$id = $shipping->getId(); |
212
|
|
|
|
213
|
|
|
/* @var $entity ECommerceProduct|GhostObjectInterface */ |
214
|
|
|
$product = $this->em->getRepository(ECommerceProduct::class)->find($product->getId()); |
215
|
|
|
|
216
|
|
|
$entity = $product->getShipping(); |
217
|
|
|
|
218
|
|
|
self::assertFalse($entity->isProxyInitialized(), "Pre-Condition: Object is unitialized proxy."); |
219
|
|
|
self::assertEquals($id, $entity->getId()); |
220
|
|
|
self::assertSame($id, $entity->getId(), "Check that the id's are the same value, and type."); |
221
|
|
|
self::assertFalse($entity->isProxyInitialized(), "Getting the identifier doesn't initialize the proxy."); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
View Code Duplication |
public function testInitializeProxyOnGettingSomethingOtherThanTheIdentifier() |
225
|
|
|
{ |
226
|
|
|
$id = $this->createProduct(); |
227
|
|
|
|
228
|
|
|
/* @var $entity ECommerceProduct|GhostObjectInterface */ |
229
|
|
|
$entity = $this->em->getReference(ECommerceProduct::class , $id); |
230
|
|
|
|
231
|
|
|
self::assertFalse($entity->isProxyInitialized(), "Pre-Condition: Object is unitialized proxy."); |
|
|
|
|
232
|
|
|
self::assertEquals('Doctrine Cookbook', $entity->getName()); |
|
|
|
|
233
|
|
|
self::assertTrue($entity->isProxyInitialized(), "Getting something other than the identifier initializes the proxy."); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* @group DDC-1604 |
238
|
|
|
*/ |
239
|
|
|
public function testCommonPersistenceProxy() |
240
|
|
|
{ |
241
|
|
|
$id = $this->createProduct(); |
242
|
|
|
|
243
|
|
|
/* @var $entity ECommerceProduct|GhostObjectInterface */ |
244
|
|
|
$entity = $this->em->getReference(ECommerceProduct::class , $id); |
245
|
|
|
|
246
|
|
|
$className = StaticClassNameConverter::getClass($entity); |
247
|
|
|
|
248
|
|
|
self::assertInstanceOf(GhostObjectInterface::class, $entity); |
249
|
|
|
self::assertFalse($entity->isProxyInitialized()); |
|
|
|
|
250
|
|
|
self::assertEquals(ECommerceProduct::class, $className); |
251
|
|
|
|
252
|
|
|
$proxyManagerConfiguration = $this->em->getConfiguration()->getProxyManagerConfiguration(); |
253
|
|
|
|
254
|
|
|
self::assertInstanceOf( |
255
|
|
|
FileWriterGeneratorStrategy::class, |
256
|
|
|
$proxyManagerConfiguration->getGeneratorStrategy(), |
257
|
|
|
'Proxies are being written to disk in this test' |
258
|
|
|
); |
259
|
|
|
|
260
|
|
|
$proxy = $this->factory->getProxy(ECommerceProduct::class, ['id' => 123]); |
261
|
|
|
|
262
|
|
|
$proxyClass = new \ReflectionClass($proxy); |
263
|
|
|
|
264
|
|
|
self::assertFileExists($proxyClass->getFileName()); |
265
|
|
|
|
266
|
|
|
$entity->initializeProxy(); |
|
|
|
|
267
|
|
|
|
268
|
|
|
self::assertTrue($entity->isProxyInitialized()); |
269
|
|
|
} |
270
|
|
|
} |
271
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.