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