1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\Tests\ORM\Proxy; |
6
|
|
|
|
7
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataBuildingContext; |
8
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataFactory; |
9
|
|
|
use Doctrine\ORM\Proxy\Factory\DefaultProxyResolver; |
10
|
|
|
use Doctrine\ORM\Proxy\Factory\ProxyFactory; |
11
|
|
|
use Doctrine\ORM\Configuration\ProxyConfiguration; |
12
|
|
|
use Doctrine\ORM\EntityNotFoundException; |
13
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata; |
14
|
|
|
use Doctrine\ORM\Persisters\Entity\BasicEntityPersister; |
15
|
|
|
use Doctrine\ORM\Proxy\Factory\StaticProxyFactory; |
16
|
|
|
use Doctrine\ORM\Reflection\RuntimeReflectionService; |
17
|
|
|
use Doctrine\Tests\Mocks\ConnectionMock; |
18
|
|
|
use Doctrine\Tests\Mocks\DriverMock; |
19
|
|
|
use Doctrine\Tests\Mocks\EntityManagerMock; |
20
|
|
|
use Doctrine\Tests\Mocks\UnitOfWorkMock; |
21
|
|
|
use Doctrine\Tests\Models\Company\CompanyEmployee; |
22
|
|
|
use Doctrine\Tests\Models\Company\CompanyPerson; |
23
|
|
|
use Doctrine\Tests\Models\ECommerce\ECommerceFeature; |
24
|
|
|
use Doctrine\Tests\OrmTestCase; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Test the proxy generator. Its work is generating on-the-fly subclasses of a given model, which implement the Proxy pattern. |
28
|
|
|
* @author Giorgio Sironi <[email protected]> |
29
|
|
|
*/ |
30
|
|
|
class ProxyFactoryTest extends OrmTestCase |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @var ConnectionMock |
34
|
|
|
*/ |
35
|
|
|
private $connectionMock; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var UnitOfWorkMock |
39
|
|
|
*/ |
40
|
|
|
private $uowMock; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var EntityManagerMock |
44
|
|
|
*/ |
45
|
|
|
private $emMock; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var \Doctrine\ORM\Proxy\ProxyFactory |
49
|
|
|
*/ |
50
|
|
|
private $proxyFactory; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var ClassMetadataBuildingContext |
54
|
|
|
*/ |
55
|
|
|
private $metadataBuildingContext; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritDoc} |
59
|
|
|
*/ |
60
|
|
|
protected function setUp() |
61
|
|
|
{ |
62
|
|
|
parent::setUp(); |
63
|
|
|
|
64
|
|
|
$this->metadataBuildingContext = new ClassMetadataBuildingContext( |
65
|
|
|
$this->createMock(ClassMetadataFactory::class) |
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
|
|
|
$proxyConfiguration = new ProxyConfiguration(); |
74
|
|
|
|
75
|
|
|
$proxyConfiguration->setDirectory(sys_get_temp_dir()); |
76
|
|
|
$proxyConfiguration->setNamespace('Proxies'); |
77
|
|
|
$proxyConfiguration->setAutoGenerate(ProxyFactory::AUTOGENERATE_ALWAYS); |
78
|
|
|
$proxyConfiguration->setResolver(new DefaultProxyResolver( |
79
|
|
|
$proxyConfiguration->getNamespace(), |
80
|
|
|
$proxyConfiguration->getDirectory() |
81
|
|
|
)); |
82
|
|
|
|
83
|
|
|
$this->proxyFactory = new StaticProxyFactory($this->emMock, $proxyConfiguration); |
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function testReferenceProxyDelegatesLoadingToThePersister() |
87
|
|
|
{ |
88
|
|
|
$identifier = ['id' => 42]; |
89
|
|
|
$proxyClass = 'Proxies\__CG__\Doctrine\Tests\Models\ECommerce\ECommerceFeature'; |
90
|
|
|
|
91
|
|
|
$classMetaData = $this->emMock->getClassMetadata(ECommerceFeature::class); |
92
|
|
|
|
93
|
|
|
$persister = $this |
94
|
|
|
->getMockBuilder(BasicEntityPersister::class) |
95
|
|
|
->setConstructorArgs([$this->emMock, $classMetaData]) |
96
|
|
|
->setMethods(['load']) |
97
|
|
|
->getMock(); |
98
|
|
|
|
99
|
|
|
$persister |
100
|
|
|
->expects($this->atLeastOnce()) |
101
|
|
|
->method('load') |
102
|
|
|
->with($this->equalTo($identifier), $this->isInstanceOf($proxyClass)) |
103
|
|
|
->will($this->returnValue(new \stdClass())); |
104
|
|
|
|
105
|
|
|
$this->uowMock->setEntityPersister(ECommerceFeature::class, $persister); |
106
|
|
|
|
107
|
|
|
$proxy = $this->proxyFactory->getProxy(ECommerceFeature::class, $identifier); |
108
|
|
|
|
109
|
|
|
$proxy->getDescription(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @group DDC-1771 |
114
|
|
|
*/ |
115
|
|
|
public function testSkipAbstractClassesOnGeneration() |
116
|
|
|
{ |
117
|
|
|
$cm = new ClassMetadata(AbstractClass::class, $this->metadataBuildingContext); |
118
|
|
|
$cm->initializeReflection(new RuntimeReflectionService()); |
119
|
|
|
|
120
|
|
|
self::assertNotNull($cm->getReflectionClass()); |
121
|
|
|
|
122
|
|
|
$num = $this->proxyFactory->generateProxyClasses([$cm]); |
123
|
|
|
|
124
|
|
|
self::assertEquals(0, $num, "No proxies generated."); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @group DDC-2432 |
129
|
|
|
*/ |
130
|
|
View Code Duplication |
public function testFailedProxyLoadingDoesNotMarkTheProxyAsInitialized() |
131
|
|
|
{ |
132
|
|
|
$classMetaData = $this->emMock->getClassMetadata(ECommerceFeature::class); |
133
|
|
|
|
134
|
|
|
$persister = $this |
135
|
|
|
->getMockBuilder(BasicEntityPersister::class) |
136
|
|
|
->setConstructorArgs([$this->emMock, $classMetaData]) |
137
|
|
|
->setMethods(['load']) |
138
|
|
|
->getMock(); |
139
|
|
|
|
140
|
|
|
$persister |
141
|
|
|
->expects($this->atLeastOnce()) |
142
|
|
|
->method('load') |
143
|
|
|
->will($this->returnValue(null)); |
144
|
|
|
|
145
|
|
|
$this->uowMock->setEntityPersister(ECommerceFeature::class, $persister); |
146
|
|
|
|
147
|
|
|
/* @var $proxy \Doctrine\Common\Proxy\Proxy */ |
148
|
|
|
$proxy = $this->proxyFactory->getProxy(ECommerceFeature::class, ['id' => 42]); |
149
|
|
|
|
150
|
|
|
try { |
151
|
|
|
$proxy->getDescription(); |
|
|
|
|
152
|
|
|
$this->fail('An exception was expected to be raised'); |
153
|
|
|
} catch (EntityNotFoundException $exception) { |
|
|
|
|
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
self::assertFalse($proxy->__isInitialized()); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @group DDC-2432 |
161
|
|
|
*/ |
162
|
|
View Code Duplication |
public function testFailedProxyCloningDoesNotMarkTheProxyAsInitialized() |
163
|
|
|
{ |
164
|
|
|
$classMetaData = $this->emMock->getClassMetadata(ECommerceFeature::class); |
165
|
|
|
|
166
|
|
|
$persister = $this |
167
|
|
|
->getMockBuilder(BasicEntityPersister::class) |
168
|
|
|
->setConstructorArgs([$this->emMock, $classMetaData]) |
169
|
|
|
->setMethods(['load']) |
170
|
|
|
->getMock(); |
171
|
|
|
|
172
|
|
|
$persister |
173
|
|
|
->expects($this->atLeastOnce()) |
174
|
|
|
->method('load') |
175
|
|
|
->will($this->returnValue(null)); |
176
|
|
|
|
177
|
|
|
$this->uowMock->setEntityPersister(ECommerceFeature::class, $persister); |
178
|
|
|
|
179
|
|
|
/* @var $proxy \Doctrine\Common\Proxy\Proxy */ |
180
|
|
|
$proxy = $this->proxyFactory->getProxy(ECommerceFeature::class, ['id' => 42]); |
181
|
|
|
|
182
|
|
|
try { |
183
|
|
|
$cloned = clone $proxy; |
|
|
|
|
184
|
|
|
$this->fail('An exception was expected to be raised'); |
185
|
|
|
} catch (EntityNotFoundException $exception) { |
|
|
|
|
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
self::assertFalse($proxy->__isInitialized()); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function testProxyClonesParentFields() |
192
|
|
|
{ |
193
|
|
|
$companyEmployee = new CompanyEmployee(); |
194
|
|
|
$companyEmployee->setSalary(1000); // A property on the CompanyEmployee |
195
|
|
|
$companyEmployee->setName('Bob'); // A property on the parent class, CompanyPerson |
196
|
|
|
|
197
|
|
|
// Set the id of the CompanyEmployee (which is in the parent CompanyPerson) |
198
|
|
|
$property = new \ReflectionProperty(CompanyPerson::class, 'id'); |
199
|
|
|
|
200
|
|
|
$property->setAccessible(true); |
201
|
|
|
$property->setValue($companyEmployee, 42); |
202
|
|
|
|
203
|
|
|
$classMetaData = $this->emMock->getClassMetadata(CompanyEmployee::class); |
204
|
|
|
|
205
|
|
|
$persister = $this |
206
|
|
|
->getMockBuilder(BasicEntityPersister::class) |
207
|
|
|
->setConstructorArgs([$this->emMock, $classMetaData]) |
208
|
|
|
->setMethods(['load']) |
209
|
|
|
->getMock(); |
210
|
|
|
|
211
|
|
|
$persister |
212
|
|
|
->expects(self::atLeastOnce()) |
213
|
|
|
->method('load') |
214
|
|
|
->willReturn($companyEmployee); |
215
|
|
|
|
216
|
|
|
$this->uowMock->setEntityPersister(CompanyEmployee::class, $persister); |
217
|
|
|
|
218
|
|
|
/* @var $proxy \Doctrine\Common\Proxy\Proxy */ |
219
|
|
|
$proxy = $this->proxyFactory->getProxy(CompanyEmployee::class, ['id' => 42]); |
220
|
|
|
|
221
|
|
|
/* @var $cloned CompanyEmployee */ |
222
|
|
|
$cloned = clone $proxy; |
223
|
|
|
|
224
|
|
|
self::assertSame(42, $cloned->getId(), 'Expected the Id to be cloned'); |
225
|
|
|
self::assertSame(1000, $cloned->getSalary(), 'Expect properties on the CompanyEmployee class to be cloned'); |
226
|
|
|
self::assertSame('Bob', $cloned->getName(), 'Expect properties on the CompanyPerson class to be cloned'); |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
abstract class AbstractClass |
231
|
|
|
{ |
232
|
|
|
|
233
|
|
|
} |
234
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..