|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Doctrine\Tests\ORM\Functional; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\Proxy\ProxyFactory; |
|
6
|
|
|
use Doctrine\ORM\Proxy\ProxyClassGenerator; |
|
7
|
|
|
use Doctrine\Tests\Models\ECommerce\ECommerceProduct; |
|
8
|
|
|
use Doctrine\Tests\Models\ECommerce\ECommerceShipping; |
|
9
|
|
|
use Doctrine\Tests\Models\Company\CompanyAuction; |
|
10
|
|
|
use ProxyManager\Proxy\GhostObjectInterface; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Tests the generation of a proxy object for lazy loading. |
|
14
|
|
|
* @author Giorgio Sironi <[email protected]> |
|
15
|
|
|
* @author Benjamin Eberlei <[email protected]> |
|
16
|
|
|
*/ |
|
17
|
|
|
class ReferenceProxyTest extends \Doctrine\Tests\OrmFunctionalTestCase |
|
18
|
|
|
{ |
|
19
|
|
|
protected function setUp() |
|
20
|
|
|
{ |
|
21
|
|
|
$this->useModelSet('ecommerce'); |
|
22
|
|
|
$this->useModelSet('company'); |
|
23
|
|
|
parent::setUp(); |
|
24
|
|
|
$this->_factory = new ProxyFactory( |
|
|
|
|
|
|
25
|
|
|
$this->_em, |
|
26
|
|
|
__DIR__ . '/../../Proxies', |
|
27
|
|
|
'Doctrine\Tests\Proxies', |
|
28
|
|
|
true); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function createProduct() |
|
32
|
|
|
{ |
|
33
|
|
|
$product = new ECommerceProduct(); |
|
34
|
|
|
$product->setName('Doctrine Cookbook'); |
|
35
|
|
|
$this->_em->persist($product); |
|
36
|
|
|
|
|
37
|
|
|
$this->_em->flush(); |
|
38
|
|
|
$this->_em->clear(); |
|
39
|
|
|
|
|
40
|
|
|
return $product->getId(); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function createAuction() |
|
44
|
|
|
{ |
|
45
|
|
|
$event = new CompanyAuction(); |
|
46
|
|
|
$event->setData('Doctrine Cookbook'); |
|
47
|
|
|
$this->_em->persist($event); |
|
48
|
|
|
|
|
49
|
|
|
$this->_em->flush(); |
|
50
|
|
|
$this->_em->clear(); |
|
51
|
|
|
|
|
52
|
|
|
return $event->getId(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function testLazyLoadsFieldValuesFromDatabase() |
|
56
|
|
|
{ |
|
57
|
|
|
$id = $this->createProduct(); |
|
58
|
|
|
|
|
59
|
|
|
$productProxy = $this->_em->getReference('Doctrine\Tests\Models\ECommerce\ECommerceProduct', array('id' => $id)); |
|
60
|
|
|
$this->assertEquals('Doctrine Cookbook', $productProxy->getName()); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @group DDC-727 |
|
65
|
|
|
*/ |
|
66
|
|
|
public function testAccessMetatadaForProxy() |
|
67
|
|
|
{ |
|
68
|
|
|
$id = $this->createProduct(); |
|
69
|
|
|
|
|
70
|
|
|
$entity = $this->_em->getReference('Doctrine\Tests\Models\ECommerce\ECommerceProduct' , $id); |
|
71
|
|
|
$class = $this->_em->getClassMetadata(get_class($entity)); |
|
72
|
|
|
|
|
73
|
|
|
$this->assertEquals('Doctrine\Tests\Models\ECommerce\ECommerceProduct', $class->name); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @group DDC-1033 |
|
78
|
|
|
*/ |
|
79
|
|
|
public function testReferenceFind() |
|
80
|
|
|
{ |
|
81
|
|
|
$id = $this->createProduct(); |
|
82
|
|
|
|
|
83
|
|
|
$entity = $this->_em->getReference('Doctrine\Tests\Models\ECommerce\ECommerceProduct' , $id); |
|
84
|
|
|
$entity2 = $this->_em->find('Doctrine\Tests\Models\ECommerce\ECommerceProduct' , $id); |
|
85
|
|
|
|
|
86
|
|
|
$this->assertSame($entity, $entity2); |
|
87
|
|
|
$this->assertEquals('Doctrine Cookbook', $entity2->getName()); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @group DDC-1033 |
|
92
|
|
|
*/ |
|
93
|
|
|
public function testCloneProxy() |
|
94
|
|
|
{ |
|
95
|
|
|
$id = $this->createProduct(); |
|
96
|
|
|
|
|
97
|
|
|
/* @var $entity ECommerceProduct */ |
|
98
|
|
|
$entity = $this->_em->getReference(ECommerceProduct::class , $id); |
|
99
|
|
|
|
|
100
|
|
|
$clone = clone $entity; |
|
101
|
|
|
|
|
102
|
|
|
$this->assertEquals($id, $entity->getId()); |
|
103
|
|
|
$this->assertEquals('Doctrine Cookbook', $entity->getName()); |
|
104
|
|
|
|
|
105
|
|
|
$this->markTestIncomplete('To be carefully verified'); |
|
106
|
|
|
|
|
107
|
|
|
$this->assertFalse($this->_em->contains($clone), "Cloning a reference proxy should return an unmanaged/detached entity."); |
|
108
|
|
|
$this->assertEquals($id, $clone->getId(), "Cloning a reference proxy should return same id."); |
|
109
|
|
|
$this->assertEquals('Doctrine Cookbook', $clone->getName(), "Cloning a reference proxy should return same product name."); |
|
110
|
|
|
|
|
111
|
|
|
// domain logic, Product::__clone sets isCloned public property |
|
112
|
|
|
$this->assertTrue($clone->isCloned); |
|
113
|
|
|
$this->assertFalse($entity->isCloned); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @group DDC-733 |
|
118
|
|
|
*/ |
|
119
|
|
|
public function testInitializeProxy() |
|
120
|
|
|
{ |
|
121
|
|
|
$id = $this->createProduct(); |
|
122
|
|
|
|
|
123
|
|
|
/* @var $entity \ProxyManager\Proxy\GhostObjectInterface|\Doctrine\Tests\Models\ECommerce\ECommerceProduct */ |
|
124
|
|
|
$entity = $this->_em->getReference('Doctrine\Tests\Models\ECommerce\ECommerceProduct' , $id); |
|
125
|
|
|
|
|
126
|
|
|
$this->assertFalse($entity->isProxyInitialized(), "Pre-Condition: Object is unitialized proxy."); |
|
|
|
|
|
|
127
|
|
|
$this->_em->getUnitOfWork()->initializeObject($entity); |
|
128
|
|
|
$this->assertTrue($entity->isProxyInitialized(), "Should be initialized after called UnitOfWork::initializeObject()"); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @group DDC-1163 |
|
133
|
|
|
*/ |
|
134
|
|
|
public function testInitializeChangeAndFlushProxy() |
|
135
|
|
|
{ |
|
136
|
|
|
$id = $this->createProduct(); |
|
137
|
|
|
|
|
138
|
|
|
/* @var $entity Doctrine\Tests\Models\ECommerce\ECommerceProduct */ |
|
139
|
|
|
$entity = $this->_em->getReference('Doctrine\Tests\Models\ECommerce\ECommerceProduct' , $id); |
|
140
|
|
|
$entity->setName('Doctrine 2 Cookbook'); |
|
141
|
|
|
|
|
142
|
|
|
$this->_em->flush(); |
|
143
|
|
|
$this->_em->clear(); |
|
144
|
|
|
|
|
145
|
|
|
$entity = $this->_em->getReference('Doctrine\Tests\Models\ECommerce\ECommerceProduct' , $id); |
|
146
|
|
|
$this->assertEquals('Doctrine 2 Cookbook', $entity->getName()); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
public function testDoNotInitializeProxyOnGettingTheIdentifier() |
|
150
|
|
|
{ |
|
151
|
|
|
$id = $this->createProduct(); |
|
152
|
|
|
|
|
153
|
|
|
/* @var $entity \ProxyManager\Proxy\GhostObjectInterface|\Doctrine\Tests\Models\ECommerce\ECommerceProduct */ |
|
154
|
|
|
$entity = $this->_em->getReference('Doctrine\Tests\Models\ECommerce\ECommerceProduct' , $id); |
|
155
|
|
|
|
|
156
|
|
|
$this->assertFalse($entity->isProxyInitialized(), "Pre-Condition: Object is unitialized proxy."); |
|
|
|
|
|
|
157
|
|
|
$this->assertEquals($id, $entity->getId()); |
|
|
|
|
|
|
158
|
|
|
$this->assertFalse($entity->isProxyInitialized(), "Getting the identifier doesn't initialize the proxy."); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* @group DDC-1625 |
|
163
|
|
|
*/ |
|
164
|
|
|
public function testDoNotInitializeProxyOnGettingTheIdentifier_DDC_1625() |
|
165
|
|
|
{ |
|
166
|
|
|
$id = $this->createAuction(); |
|
167
|
|
|
|
|
168
|
|
|
/* @var $entity \ProxyManager\Proxy\GhostObjectInterface|\Doctrine\Tests\Models\Company\CompanyAuction */ |
|
169
|
|
|
$entity = $this->_em->getReference('Doctrine\Tests\Models\Company\CompanyAuction' , $id); |
|
170
|
|
|
|
|
171
|
|
|
$this->assertFalse($entity->isProxyInitialized(), "Pre-Condition: Object is unitialized proxy."); |
|
|
|
|
|
|
172
|
|
|
$this->assertEquals($id, $entity->getId()); |
|
|
|
|
|
|
173
|
|
|
$this->assertFalse($entity->isProxyInitialized(), "Getting the identifier doesn't initialize the proxy when extending."); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
public function testDoNotInitializeProxyOnGettingTheIdentifierAndReturnTheRightType() |
|
177
|
|
|
{ |
|
178
|
|
|
$product = new ECommerceProduct(); |
|
179
|
|
|
$product->setName('Doctrine Cookbook'); |
|
180
|
|
|
|
|
181
|
|
|
$shipping = new ECommerceShipping(); |
|
182
|
|
|
$shipping->setDays(1); |
|
183
|
|
|
$product->setShipping($shipping); |
|
184
|
|
|
$this->_em->persist($product); |
|
185
|
|
|
$this->_em->flush(); |
|
186
|
|
|
$this->_em->clear(); |
|
187
|
|
|
|
|
188
|
|
|
$id = $shipping->getId(); |
|
189
|
|
|
/* @var $entity \ProxyManager\Proxy\GhostObjectInterface|ECommerceProduct */ |
|
190
|
|
|
$product = $this->_em->getRepository('Doctrine\Tests\Models\ECommerce\ECommerceProduct')->find($product->getId()); |
|
191
|
|
|
|
|
192
|
|
|
$entity = $product->getShipping(); |
|
193
|
|
|
$this->assertFalse($entity->isProxyInitialized(), "Pre-Condition: Object is unitialized proxy."); |
|
194
|
|
|
$this->assertEquals($id, $entity->getId()); |
|
195
|
|
|
$this->assertSame($id, $entity->getId(), "Check that the id's are the same value, and type."); |
|
196
|
|
|
$this->assertFalse($entity->isProxyInitialized(), "Getting the identifier doesn't initialize the proxy."); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
public function testInitializeProxyOnGettingSomethingOtherThanTheIdentifier() |
|
200
|
|
|
{ |
|
201
|
|
|
$id = $this->createProduct(); |
|
202
|
|
|
|
|
203
|
|
|
/* @var $entity \ProxyManager\Proxy\GhostObjectInterface|ECommerceProduct */ |
|
204
|
|
|
$entity = $this->_em->getReference('Doctrine\Tests\Models\ECommerce\ECommerceProduct' , $id); |
|
205
|
|
|
|
|
206
|
|
|
$this->assertFalse($entity->isProxyInitialized(), "Pre-Condition: Object is unitialized proxy."); |
|
|
|
|
|
|
207
|
|
|
$this->assertEquals('Doctrine Cookbook', $entity->getName()); |
|
|
|
|
|
|
208
|
|
|
$this->assertTrue($entity->isProxyInitialized(), "Getting something other than the identifier initializes the proxy."); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* @group DDC-1604 |
|
213
|
|
|
*/ |
|
214
|
|
|
public function testCommonPersistenceProxy() |
|
215
|
|
|
{ |
|
216
|
|
|
$id = $this->createProduct(); |
|
217
|
|
|
|
|
218
|
|
|
/* @var $entity \ProxyManager\Proxy\GhostObjectInterface|\Doctrine\Tests\Models\ECommerce\ECommerceProduct */ |
|
219
|
|
|
$entity = $this->_em->getReference('Doctrine\Tests\Models\ECommerce\ECommerceProduct' , $id); |
|
220
|
|
|
$className = \Doctrine\Common\Util\ClassUtils::getClass($entity); |
|
221
|
|
|
|
|
222
|
|
|
$this->assertInstanceOf(GhostObjectInterface::class, $entity); |
|
223
|
|
|
$this->assertFalse($entity->isProxyInitialized()); |
|
|
|
|
|
|
224
|
|
|
$this->assertEquals('Doctrine\Tests\Models\ECommerce\ECommerceProduct', $className); |
|
225
|
|
|
|
|
226
|
|
|
$restName = substr(get_class($entity), strlen($this->_em->getConfiguration()->getProxyNamespace()) +1); |
|
227
|
|
|
$proxyFileName = $this->_em->getConfiguration()->getProxyDir() . DIRECTORY_SEPARATOR . str_replace("\\", "", $restName) . ".php"; |
|
228
|
|
|
$this->assertTrue(file_exists($proxyFileName), "Proxy file name cannot be found generically."); |
|
229
|
|
|
|
|
230
|
|
|
$entity->initializeProxy(); |
|
|
|
|
|
|
231
|
|
|
$this->assertTrue($entity->isProxyInitialized()); |
|
232
|
|
|
} |
|
233
|
|
|
} |
|
234
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: