Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / tests / Doctrine / Tests / ORM / Functional / ReferenceProxyTest.php
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
11 require_once __DIR__ . '/../../TestInit.php';
12
13 /**
14  * Tests the generation of a proxy object for lazy loading.
15  * @author Giorgio Sironi <piccoloprincipeazzurro@gmail.com>
16  * @author Benjamin Eberlei <kontakt@beberlei.de>
17  */
18 class ReferenceProxyTest extends \Doctrine\Tests\OrmFunctionalTestCase
19 {
20     protected function setUp()
21     {
22         $this->useModelSet('ecommerce');
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 Doctrine\Tests\Models\ECommerce\ECommerceProduct */
98         $entity = $this->_em->getReference('Doctrine\Tests\Models\ECommerce\ECommerceProduct' , $id);
99
100         /* @var $clone Doctrine\Tests\Models\ECommerce\ECommerceProduct */
101         $clone = clone $entity;
102
103         $this->assertEquals($id, $entity->getId());
104         $this->assertEquals('Doctrine Cookbook', $entity->getName());
105
106         $this->assertFalse($this->_em->contains($clone), "Cloning a reference proxy should return an unmanaged/detached entity.");
107         $this->assertEquals($id, $clone->getId(), "Cloning a reference proxy should return same id.");
108         $this->assertEquals('Doctrine Cookbook', $clone->getName(), "Cloning a reference proxy should return same product name.");
109
110         // domain logic, Product::__clone sets isCloned public property
111         $this->assertTrue($clone->isCloned);
112         $this->assertFalse($entity->isCloned);
113     }
114
115     /**
116      * @group DDC-733
117      */
118     public function testInitializeProxy()
119     {
120         $id = $this->createProduct();
121
122         /* @var $entity Doctrine\Tests\Models\ECommerce\ECommerceProduct */
123         $entity = $this->_em->getReference('Doctrine\Tests\Models\ECommerce\ECommerceProduct' , $id);
124
125         $this->assertFalse($entity->__isInitialized__, "Pre-Condition: Object is unitialized proxy.");
126         $this->_em->getUnitOfWork()->initializeObject($entity);
127         $this->assertTrue($entity->__isInitialized__, "Should be initialized after called UnitOfWork::initializeObject()");
128     }
129
130     /**
131      * @group DDC-1163
132      */
133     public function testInitializeChangeAndFlushProxy()
134     {
135         $id = $this->createProduct();
136
137         /* @var $entity Doctrine\Tests\Models\ECommerce\ECommerceProduct */
138         $entity = $this->_em->getReference('Doctrine\Tests\Models\ECommerce\ECommerceProduct' , $id);
139         $entity->setName('Doctrine 2 Cookbook');
140
141         $this->_em->flush();
142         $this->_em->clear();
143
144         $entity = $this->_em->getReference('Doctrine\Tests\Models\ECommerce\ECommerceProduct' , $id);
145         $this->assertEquals('Doctrine 2 Cookbook', $entity->getName());
146     }
147
148     /**
149      * @group DDC-1022
150      */
151     public function testWakeupCalledOnProxy()
152     {
153         $id = $this->createProduct();
154
155         /* @var $entity Doctrine\Tests\Models\ECommerce\ECommerceProduct */
156         $entity = $this->_em->getReference('Doctrine\Tests\Models\ECommerce\ECommerceProduct' , $id);
157
158         $this->assertFalse($entity->wakeUp);
159
160         $entity->setName('Doctrine 2 Cookbook');
161
162         $this->assertTrue($entity->wakeUp, "Loading the proxy should call __wakeup().");
163     }
164
165     public function testDoNotInitializeProxyOnGettingTheIdentifier()
166     {
167         $id = $this->createProduct();
168
169         /* @var $entity Doctrine\Tests\Models\ECommerce\ECommerceProduct */
170         $entity = $this->_em->getReference('Doctrine\Tests\Models\ECommerce\ECommerceProduct' , $id);
171
172         $this->assertFalse($entity->__isInitialized__, "Pre-Condition: Object is unitialized proxy.");
173         $this->assertEquals($id, $entity->getId());
174         $this->assertFalse($entity->__isInitialized__, "Getting the identifier doesn't initialize the proxy.");
175     }
176
177     /**
178      * @group DDC-1625
179      */
180     public function testDoNotInitializeProxyOnGettingTheIdentifier_DDC_1625()
181     {
182         $id = $this->createAuction();
183
184         /* @var $entity Doctrine\Tests\Models\Company\CompanyAuction */
185         $entity = $this->_em->getReference('Doctrine\Tests\Models\Company\CompanyAuction' , $id);
186
187         $this->assertFalse($entity->__isInitialized__, "Pre-Condition: Object is unitialized proxy.");
188         $this->assertEquals($id, $entity->getId());
189         $this->assertFalse($entity->__isInitialized__, "Getting the identifier doesn't initialize the proxy when extending.");
190     }
191     
192     public function testDoNotInitializeProxyOnGettingTheIdentifierAndReturnTheRightType()
193     {
194         $product = new ECommerceProduct();
195         $product->setName('Doctrine Cookbook');
196
197         $shipping = new ECommerceShipping();
198         $shipping->setDays(1);
199         $product->setShipping($shipping);
200         $this->_em->persist($product);
201         $this->_em->flush();
202         $this->_em->clear();
203
204         $id = $shipping->getId();
205
206         $product = $this->_em->getRepository('Doctrine\Tests\Models\ECommerce\ECommerceProduct')->find($product->getId());
207
208         $entity = $product->getShipping();
209         $this->assertFalse($entity->__isInitialized__, "Pre-Condition: Object is unitialized proxy.");
210         $this->assertEquals($id, $entity->getId());
211         $this->assertSame($id, $entity->getId(), "Check that the id's are the same value, and type.");
212         $this->assertFalse($entity->__isInitialized__, "Getting the identifier doesn't initialize the proxy.");
213     }
214
215     public function testInitializeProxyOnGettingSomethingOtherThanTheIdentifier()
216     {
217         $id = $this->createProduct();
218
219         /* @var $entity Doctrine\Tests\Models\ECommerce\ECommerceProduct */
220         $entity = $this->_em->getReference('Doctrine\Tests\Models\ECommerce\ECommerceProduct' , $id);
221
222         $this->assertFalse($entity->__isInitialized__, "Pre-Condition: Object is unitialized proxy.");
223         $this->assertEquals('Doctrine Cookbook', $entity->getName());
224         $this->assertTrue($entity->__isInitialized__, "Getting something other than the identifier initializes the proxy.");
225     }
226
227     /**
228      * @group DDC-1604
229      */
230     public function testCommonPersistenceProxy()
231     {
232         $id = $this->createProduct();
233
234         /* @var $entity Doctrine\Tests\Models\ECommerce\ECommerceProduct */
235         $entity = $this->_em->getReference('Doctrine\Tests\Models\ECommerce\ECommerceProduct' , $id);
236         $className = \Doctrine\Common\Util\ClassUtils::getClass($entity);
237
238         $this->assertInstanceOf('Doctrine\Common\Persistence\Proxy', $entity);
239         $this->assertFalse($entity->__isInitialized());
240         $this->assertEquals('Doctrine\Tests\Models\ECommerce\ECommerceProduct', $className);
241
242         $restName = str_replace($this->_em->getConfiguration()->getProxyNamespace(), "", get_class($entity));
243         $restName = substr(get_class($entity), strlen($this->_em->getConfiguration()->getProxyNamespace()) +1);
244         $proxyFileName = $this->_em->getConfiguration()->getProxyDir() . DIRECTORY_SEPARATOR . str_replace("\\", "", $restName) . ".php";
245         $this->assertTrue(file_exists($proxyFileName), "Proxy file name cannot be found generically.");
246
247         $entity->__load();
248         $this->assertTrue($entity->__isInitialized());
249     }
250 }