3 namespace Doctrine\Tests\ORM\Functional;
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;
11 require_once __DIR__ . '/../../TestInit.php';
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>
18 class ReferenceProxyTest extends \Doctrine\Tests\OrmFunctionalTestCase
20 protected function setUp()
22 $this->useModelSet('ecommerce');
24 $this->_factory = new ProxyFactory(
26 __DIR__ . '/../../Proxies',
27 'Doctrine\Tests\Proxies',
31 public function createProduct()
33 $product = new ECommerceProduct();
34 $product->setName('Doctrine Cookbook');
35 $this->_em->persist($product);
40 return $product->getId();
43 public function createAuction()
45 $event = new CompanyAuction();
46 $event->setData('Doctrine Cookbook');
47 $this->_em->persist($event);
52 return $event->getId();
55 public function testLazyLoadsFieldValuesFromDatabase()
57 $id = $this->createProduct();
59 $productProxy = $this->_em->getReference('Doctrine\Tests\Models\ECommerce\ECommerceProduct', array('id' => $id));
60 $this->assertEquals('Doctrine Cookbook', $productProxy->getName());
66 public function testAccessMetatadaForProxy()
68 $id = $this->createProduct();
70 $entity = $this->_em->getReference('Doctrine\Tests\Models\ECommerce\ECommerceProduct' , $id);
71 $class = $this->_em->getClassMetadata(get_class($entity));
73 $this->assertEquals('Doctrine\Tests\Models\ECommerce\ECommerceProduct', $class->name);
79 public function testReferenceFind()
81 $id = $this->createProduct();
83 $entity = $this->_em->getReference('Doctrine\Tests\Models\ECommerce\ECommerceProduct' , $id);
84 $entity2 = $this->_em->find('Doctrine\Tests\Models\ECommerce\ECommerceProduct' , $id);
86 $this->assertSame($entity, $entity2);
87 $this->assertEquals('Doctrine Cookbook', $entity2->getName());
93 public function testCloneProxy()
95 $id = $this->createProduct();
97 /* @var $entity Doctrine\Tests\Models\ECommerce\ECommerceProduct */
98 $entity = $this->_em->getReference('Doctrine\Tests\Models\ECommerce\ECommerceProduct' , $id);
100 /* @var $clone Doctrine\Tests\Models\ECommerce\ECommerceProduct */
101 $clone = clone $entity;
103 $this->assertEquals($id, $entity->getId());
104 $this->assertEquals('Doctrine Cookbook', $entity->getName());
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.");
110 // domain logic, Product::__clone sets isCloned public property
111 $this->assertTrue($clone->isCloned);
112 $this->assertFalse($entity->isCloned);
118 public function testInitializeProxy()
120 $id = $this->createProduct();
122 /* @var $entity Doctrine\Tests\Models\ECommerce\ECommerceProduct */
123 $entity = $this->_em->getReference('Doctrine\Tests\Models\ECommerce\ECommerceProduct' , $id);
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()");
133 public function testInitializeChangeAndFlushProxy()
135 $id = $this->createProduct();
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');
144 $entity = $this->_em->getReference('Doctrine\Tests\Models\ECommerce\ECommerceProduct' , $id);
145 $this->assertEquals('Doctrine 2 Cookbook', $entity->getName());
151 public function testWakeupCalledOnProxy()
153 $id = $this->createProduct();
155 /* @var $entity Doctrine\Tests\Models\ECommerce\ECommerceProduct */
156 $entity = $this->_em->getReference('Doctrine\Tests\Models\ECommerce\ECommerceProduct' , $id);
158 $this->assertFalse($entity->wakeUp);
160 $entity->setName('Doctrine 2 Cookbook');
162 $this->assertTrue($entity->wakeUp, "Loading the proxy should call __wakeup().");
165 public function testDoNotInitializeProxyOnGettingTheIdentifier()
167 $id = $this->createProduct();
169 /* @var $entity Doctrine\Tests\Models\ECommerce\ECommerceProduct */
170 $entity = $this->_em->getReference('Doctrine\Tests\Models\ECommerce\ECommerceProduct' , $id);
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.");
180 public function testDoNotInitializeProxyOnGettingTheIdentifier_DDC_1625()
182 $id = $this->createAuction();
184 /* @var $entity Doctrine\Tests\Models\Company\CompanyAuction */
185 $entity = $this->_em->getReference('Doctrine\Tests\Models\Company\CompanyAuction' , $id);
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.");
192 public function testDoNotInitializeProxyOnGettingTheIdentifierAndReturnTheRightType()
194 $product = new ECommerceProduct();
195 $product->setName('Doctrine Cookbook');
197 $shipping = new ECommerceShipping();
198 $shipping->setDays(1);
199 $product->setShipping($shipping);
200 $this->_em->persist($product);
204 $id = $shipping->getId();
206 $product = $this->_em->getRepository('Doctrine\Tests\Models\ECommerce\ECommerceProduct')->find($product->getId());
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.");
215 public function testInitializeProxyOnGettingSomethingOtherThanTheIdentifier()
217 $id = $this->createProduct();
219 /* @var $entity Doctrine\Tests\Models\ECommerce\ECommerceProduct */
220 $entity = $this->_em->getReference('Doctrine\Tests\Models\ECommerce\ECommerceProduct' , $id);
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.");
230 public function testCommonPersistenceProxy()
232 $id = $this->createProduct();
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);
238 $this->assertInstanceOf('Doctrine\Common\Persistence\Proxy', $entity);
239 $this->assertFalse($entity->__isInitialized());
240 $this->assertEquals('Doctrine\Tests\Models\ECommerce\ECommerceProduct', $className);
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.");
248 $this->assertTrue($entity->__isInitialized());