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