1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\Tests\ORM\Proxy; |
6
|
|
|
|
7
|
|
|
use Doctrine\ORM\EntityNotFoundException; |
8
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata; |
9
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataBuildingContext; |
10
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataFactory; |
11
|
|
|
use Doctrine\ORM\Persisters\Entity\BasicEntityPersister; |
12
|
|
|
use Doctrine\ORM\Proxy\Factory\StaticProxyFactory; |
13
|
|
|
use Doctrine\ORM\Reflection\RuntimeReflectionService; |
14
|
|
|
use Doctrine\Tests\Mocks\ConnectionMock; |
15
|
|
|
use Doctrine\Tests\Mocks\DriverMock; |
16
|
|
|
use Doctrine\Tests\Mocks\EntityManagerMock; |
17
|
|
|
use Doctrine\Tests\Mocks\UnitOfWorkMock; |
18
|
|
|
use Doctrine\Tests\Models\Company\CompanyEmployee; |
19
|
|
|
use Doctrine\Tests\Models\ECommerce\ECommerceFeature; |
20
|
|
|
use Doctrine\Tests\Models\FriendObject\ComparableObject; |
21
|
|
|
use Doctrine\Tests\Models\ProxySpecifics\FuncGetArgs; |
22
|
|
|
use Doctrine\Tests\OrmTestCase; |
23
|
|
|
use ProxyManager\Proxy\GhostObjectInterface; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Test the proxy generator. Its work is generating on-the-fly subclasses of a given model, which implement the Proxy pattern. |
27
|
|
|
* @author Giorgio Sironi <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
class ProxyFactoryTest extends OrmTestCase |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var ConnectionMock |
33
|
|
|
*/ |
34
|
|
|
private $connectionMock; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var UnitOfWorkMock |
38
|
|
|
*/ |
39
|
|
|
private $uowMock; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var EntityManagerMock |
43
|
|
|
*/ |
44
|
|
|
private $emMock; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var StaticProxyFactory |
48
|
|
|
*/ |
49
|
|
|
private $proxyFactory; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var ClassMetadataBuildingContext |
53
|
|
|
*/ |
54
|
|
|
private $metadataBuildingContext; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritDoc} |
58
|
|
|
*/ |
59
|
|
|
protected function setUp() |
60
|
|
|
{ |
61
|
|
|
parent::setUp(); |
62
|
|
|
|
63
|
|
|
$this->metadataBuildingContext = new ClassMetadataBuildingContext( |
64
|
|
|
$this->createMock(ClassMetadataFactory::class), |
65
|
|
|
new RuntimeReflectionService() |
66
|
|
|
); |
67
|
|
|
$this->connectionMock = new ConnectionMock([], new DriverMock()); |
68
|
|
|
$this->emMock = EntityManagerMock::create($this->connectionMock); |
69
|
|
|
$this->uowMock = new UnitOfWorkMock($this->emMock); |
70
|
|
|
|
71
|
|
|
$this->emMock->setUnitOfWork($this->uowMock); |
72
|
|
|
|
73
|
|
|
$this->proxyFactory = new StaticProxyFactory( |
74
|
|
|
$this->emMock, |
75
|
|
|
$this->emMock->getConfiguration()->buildGhostObjectFactory() |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function testReferenceProxyDelegatesLoadingToThePersister() |
80
|
|
|
{ |
81
|
|
|
$identifier = ['id' => 42]; |
82
|
|
|
$classMetaData = $this->emMock->getClassMetadata(ECommerceFeature::class); |
83
|
|
|
|
84
|
|
|
$persister = $this |
85
|
|
|
->getMockBuilder(BasicEntityPersister::class) |
86
|
|
|
->setConstructorArgs([$this->emMock, $classMetaData]) |
87
|
|
|
->setMethods(['loadById']) |
88
|
|
|
->getMock(); |
89
|
|
|
|
90
|
|
|
$persister |
91
|
|
|
->expects($this->atLeastOnce()) |
92
|
|
|
->method('loadById') |
93
|
|
|
->with( |
94
|
|
|
$identifier, |
95
|
|
|
self::logicalAnd( |
96
|
|
|
self::isInstanceOf(GhostObjectInterface::class), |
97
|
|
|
self::isInstanceOf(ECommerceFeature::class) |
98
|
|
|
) |
99
|
|
|
) |
100
|
|
|
->will($this->returnValue(new \stdClass())); |
101
|
|
|
|
102
|
|
|
$this->uowMock->setEntityPersister(ECommerceFeature::class, $persister); |
103
|
|
|
|
104
|
|
|
/* @var $proxy GhostObjectInterface|ECommerceFeature */ |
105
|
|
|
$proxy = $this->proxyFactory->getProxy(ECommerceFeature::class, $identifier); |
106
|
|
|
|
107
|
|
|
$proxy->getDescription(); |
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @group DDC-1771 |
112
|
|
|
*/ |
113
|
|
|
public function testSkipAbstractClassesOnGeneration() |
114
|
|
|
{ |
115
|
|
|
$cm = new ClassMetadata(AbstractClass::class, $this->metadataBuildingContext); |
116
|
|
|
|
117
|
|
|
self::assertNotNull($cm->getReflectionClass()); |
118
|
|
|
|
119
|
|
|
$num = $this->proxyFactory->generateProxyClasses([$cm]); |
120
|
|
|
|
121
|
|
|
self::assertEquals(0, $num, "No proxies generated."); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @group DDC-2432 |
126
|
|
|
*/ |
127
|
|
View Code Duplication |
public function testFailedProxyLoadingDoesNotMarkTheProxyAsInitialized() |
128
|
|
|
{ |
129
|
|
|
$classMetaData = $this->emMock->getClassMetadata(ECommerceFeature::class); |
130
|
|
|
|
131
|
|
|
$persister = $this |
132
|
|
|
->getMockBuilder(BasicEntityPersister::class) |
133
|
|
|
->setConstructorArgs([$this->emMock, $classMetaData]) |
134
|
|
|
->setMethods(['load']) |
135
|
|
|
->getMock(); |
136
|
|
|
|
137
|
|
|
$persister |
138
|
|
|
->expects($this->atLeastOnce()) |
139
|
|
|
->method('load') |
140
|
|
|
->will($this->returnValue(null)); |
141
|
|
|
|
142
|
|
|
$this->uowMock->setEntityPersister(ECommerceFeature::class, $persister); |
143
|
|
|
|
144
|
|
|
/* @var $proxy GhostObjectInterface|ECommerceFeature */ |
145
|
|
|
$proxy = $this->proxyFactory->getProxy(ECommerceFeature::class, ['id' => 42]); |
146
|
|
|
|
147
|
|
|
try { |
148
|
|
|
$proxy->getDescription(); |
|
|
|
|
149
|
|
|
$this->fail('An exception was expected to be raised'); |
150
|
|
|
} catch (EntityNotFoundException $exception) { |
|
|
|
|
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
self::assertFalse($proxy->isProxyInitialized()); |
|
|
|
|
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @group DDC-2432 |
158
|
|
|
*/ |
159
|
|
View Code Duplication |
public function testFailedProxyCloningDoesNotMarkTheProxyAsInitialized() |
160
|
|
|
{ |
161
|
|
|
$classMetaData = $this->emMock->getClassMetadata(ECommerceFeature::class); |
162
|
|
|
|
163
|
|
|
$persister = $this |
164
|
|
|
->getMockBuilder(BasicEntityPersister::class) |
165
|
|
|
->setConstructorArgs([$this->emMock, $classMetaData]) |
166
|
|
|
->setMethods(['load']) |
167
|
|
|
->getMock(); |
168
|
|
|
|
169
|
|
|
$persister |
170
|
|
|
->expects($this->atLeastOnce()) |
171
|
|
|
->method('load') |
172
|
|
|
->will($this->returnValue(null)); |
173
|
|
|
|
174
|
|
|
$this->uowMock->setEntityPersister(ECommerceFeature::class, $persister); |
175
|
|
|
|
176
|
|
|
/* @var $proxy GhostObjectInterface|ECommerceFeature */ |
177
|
|
|
$proxy = $this->proxyFactory->getProxy(ECommerceFeature::class, ['id' => 42]); |
178
|
|
|
|
179
|
|
|
try { |
180
|
|
|
$cloned = clone $proxy; |
|
|
|
|
181
|
|
|
$this->fail('An exception was expected to be raised'); |
182
|
|
|
} catch (EntityNotFoundException $exception) { |
|
|
|
|
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
self::assertFalse($proxy->isProxyInitialized()); |
|
|
|
|
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
public function testProxyClonesParentFields() |
189
|
|
|
{ |
190
|
|
|
$identifier = ['id' => 42]; |
191
|
|
|
$classMetaData = $this->emMock->getClassMetadata(CompanyEmployee::class); |
192
|
|
|
|
193
|
|
|
$persister = $this |
194
|
|
|
->getMockBuilder(BasicEntityPersister::class) |
195
|
|
|
->setConstructorArgs([$this->emMock, $classMetaData]) |
196
|
|
|
->setMethods(['loadById']) |
197
|
|
|
->getMock(); |
198
|
|
|
|
199
|
|
|
$persister |
200
|
|
|
->expects(self::atLeastOnce()) |
201
|
|
|
->method('loadById') |
202
|
|
|
->with( |
203
|
|
|
$identifier, |
204
|
|
|
self::logicalAnd( |
205
|
|
|
self::isInstanceOf(GhostObjectInterface::class), |
206
|
|
|
self::isInstanceOf(CompanyEmployee::class) |
207
|
|
|
) |
208
|
|
|
) |
209
|
|
|
->willReturnCallback(function (array $id, CompanyEmployee $companyEmployee) { |
210
|
|
|
$companyEmployee->setSalary(1000); // A property on the CompanyEmployee |
211
|
|
|
$companyEmployee->setName('Bob'); // A property on the parent class, CompanyPerson |
212
|
|
|
|
213
|
|
|
return $companyEmployee; |
214
|
|
|
}); |
215
|
|
|
|
216
|
|
|
$this->uowMock->setEntityPersister(CompanyEmployee::class, $persister); |
217
|
|
|
|
218
|
|
|
/* @var $proxy GhostObjectInterface|CompanyEmployee */ |
219
|
|
|
$proxy = $this->proxyFactory->getProxy(CompanyEmployee::class, $identifier); |
220
|
|
|
|
221
|
|
|
$cloned = clone $proxy; |
222
|
|
|
|
223
|
|
|
self::assertSame(42, $cloned->getId(), 'Expected the Id to be cloned'); |
|
|
|
|
224
|
|
|
self::assertSame(1000, $cloned->getSalary(), 'Expect properties on the CompanyEmployee class to be cloned'); |
|
|
|
|
225
|
|
|
self::assertSame('Bob', $cloned->getName(), 'Expect properties on the CompanyPerson class to be cloned'); |
|
|
|
|
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
public function testFriendObjectsDoNotLazyLoadIfNotAccessingLazyState() |
229
|
|
|
{ |
230
|
|
|
/* @var $persister BasicEntityPersister|\PHPUnit_Framework_MockObject_MockObject */ |
231
|
|
|
$persister = $this->createMock(BasicEntityPersister::class); |
232
|
|
|
$persister->expects(self::never())->method('loadById'); |
|
|
|
|
233
|
|
|
|
234
|
|
|
$this->uowMock->setEntityPersister(ComparableObject::class, $persister); |
|
|
|
|
235
|
|
|
|
236
|
|
|
/* @var $comparable ComparableObject|GhostObjectInterface */ |
237
|
|
|
$comparable = $this->proxyFactory->getProxy(ComparableObject::class, ['id' => 123]); |
238
|
|
|
|
239
|
|
|
self::assertInstanceOf(ComparableObject::class, $comparable); |
240
|
|
|
self::assertInstanceOf(GhostObjectInterface::class, $comparable); |
241
|
|
|
self::assertFalse($comparable->isProxyInitialized()); |
|
|
|
|
242
|
|
|
|
243
|
|
|
// due to implementation details, identity check is not reading lazy state: |
244
|
|
|
self::assertTrue($comparable->equalTo($comparable)); |
|
|
|
|
245
|
|
|
|
246
|
|
|
self::assertFalse($comparable->isProxyInitialized()); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
public function testFriendObjectsLazyLoadWhenAccessingLazyState() |
250
|
|
|
{ |
251
|
|
|
/* @var $persister BasicEntityPersister|\PHPUnit_Framework_MockObject_MockObject */ |
252
|
|
|
$persister = $this |
253
|
|
|
->getMockBuilder(BasicEntityPersister::class) |
254
|
|
|
->setConstructorArgs([$this->emMock, $this->emMock->getClassMetadata(ComparableObject::class)]) |
255
|
|
|
->setMethods(['loadById']) |
256
|
|
|
->getMock(); |
257
|
|
|
|
258
|
|
|
$persister |
|
|
|
|
259
|
|
|
->expects(self::exactly(2)) |
260
|
|
|
->method('loadById') |
261
|
|
|
->with( |
262
|
|
|
self::logicalOr(['id' => 123], ['id' => 456]), |
263
|
|
|
self::logicalAnd( |
264
|
|
|
self::isInstanceOf(GhostObjectInterface::class), |
265
|
|
|
self::isInstanceOf(ComparableObject::class) |
266
|
|
|
) |
267
|
|
|
) |
268
|
|
|
->willReturnCallback(function (array $id, ComparableObject $comparableObject) { |
269
|
|
|
$comparableObject->setComparedFieldValue(\json_encode($id)); |
270
|
|
|
|
271
|
|
|
return $comparableObject; |
272
|
|
|
}); |
273
|
|
|
|
274
|
|
|
$this->uowMock->setEntityPersister(ComparableObject::class, $persister); |
275
|
|
|
|
276
|
|
|
/* @var $comparable1 ComparableObject|GhostObjectInterface */ |
277
|
|
|
$comparable1 = $this->proxyFactory->getProxy(ComparableObject::class, ['id' => 123]); |
278
|
|
|
/* @var $comparable2 ComparableObject|GhostObjectInterface */ |
279
|
|
|
$comparable2 = $this->proxyFactory->getProxy(ComparableObject::class, ['id' => 456]); |
280
|
|
|
|
281
|
|
|
self::assertInstanceOf(ComparableObject::class, $comparable1); |
282
|
|
|
self::assertInstanceOf(ComparableObject::class, $comparable2); |
283
|
|
|
self::assertInstanceOf(GhostObjectInterface::class, $comparable1); |
284
|
|
|
self::assertInstanceOf(GhostObjectInterface::class, $comparable2); |
285
|
|
|
self::assertNotSame($comparable1, $comparable2); |
286
|
|
|
self::assertFalse($comparable1->isProxyInitialized()); |
|
|
|
|
287
|
|
|
self::assertFalse($comparable2->isProxyInitialized()); |
288
|
|
|
|
289
|
|
|
self::assertFalse( |
290
|
|
|
$comparable1->equalTo($comparable2), |
|
|
|
|
291
|
|
|
'Due to implementation details, identity check is not reading lazy state' |
292
|
|
|
); |
293
|
|
|
|
294
|
|
|
self::assertTrue($comparable1->isProxyInitialized()); |
295
|
|
|
self::assertTrue($comparable2->isProxyInitialized()); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
public function testProxyMethodsSupportFuncGetArgsLogic() |
299
|
|
|
{ |
300
|
|
|
/* @var $persister BasicEntityPersister|\PHPUnit_Framework_MockObject_MockObject */ |
301
|
|
|
$persister = $this->createMock(BasicEntityPersister::class); |
302
|
|
|
$persister->expects(self::never())->method('loadById'); |
|
|
|
|
303
|
|
|
|
304
|
|
|
$this->uowMock->setEntityPersister(FuncGetArgs::class, $persister); |
|
|
|
|
305
|
|
|
|
306
|
|
|
/* @var $funcGetArgs FuncGetArgs|GhostObjectInterface */ |
307
|
|
|
$funcGetArgs = $this->proxyFactory->getProxy(FuncGetArgs::class, ['id' => 123]); |
308
|
|
|
|
309
|
|
|
self::assertInstanceOf(GhostObjectInterface::class, $funcGetArgs); |
310
|
|
|
self::assertFalse($funcGetArgs->isProxyInitialized()); |
|
|
|
|
311
|
|
|
|
312
|
|
|
self::assertSame( |
313
|
|
|
[1, 2, 3, 4], |
314
|
|
|
$funcGetArgs->funcGetArgsCallingMethod(1, 2, 3, 4), |
|
|
|
|
315
|
|
|
'`func_get_args()` calls are now supported in proxy implementations' |
316
|
|
|
); |
317
|
|
|
|
318
|
|
|
self::assertFalse($funcGetArgs->isProxyInitialized(), 'No state was accessed anyway'); |
319
|
|
|
} |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
abstract class AbstractClass |
323
|
|
|
{ |
324
|
|
|
|
325
|
|
|
} |
326
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: