ReferenceProxyTest.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. namespace Doctrine\Tests\ORM\Functional;
  3. use Doctrine\ORM\Proxy\ProxyFactory;
  4. use Doctrine\ORM\Proxy\ProxyClassGenerator;
  5. use Doctrine\Tests\Models\ECommerce\ECommerceProduct;
  6. use Doctrine\Tests\Models\ECommerce\ECommerceShipping;
  7. use Doctrine\Tests\Models\Company\CompanyAuction;
  8. require_once __DIR__ . '/../../TestInit.php';
  9. /**
  10. * Tests the generation of a proxy object for lazy loading.
  11. * @author Giorgio Sironi <piccoloprincipeazzurro@gmail.com>
  12. * @author Benjamin Eberlei <kontakt@beberlei.de>
  13. */
  14. class ReferenceProxyTest extends \Doctrine\Tests\OrmFunctionalTestCase
  15. {
  16. protected function setUp()
  17. {
  18. $this->useModelSet('ecommerce');
  19. parent::setUp();
  20. $this->_factory = new ProxyFactory(
  21. $this->_em,
  22. __DIR__ . '/../../Proxies',
  23. 'Doctrine\Tests\Proxies',
  24. true);
  25. }
  26. public function createProduct()
  27. {
  28. $product = new ECommerceProduct();
  29. $product->setName('Doctrine Cookbook');
  30. $this->_em->persist($product);
  31. $this->_em->flush();
  32. $this->_em->clear();
  33. return $product->getId();
  34. }
  35. public function createAuction()
  36. {
  37. $event = new CompanyAuction();
  38. $event->setData('Doctrine Cookbook');
  39. $this->_em->persist($event);
  40. $this->_em->flush();
  41. $this->_em->clear();
  42. return $event->getId();
  43. }
  44. public function testLazyLoadsFieldValuesFromDatabase()
  45. {
  46. $id = $this->createProduct();
  47. $productProxy = $this->_em->getReference('Doctrine\Tests\Models\ECommerce\ECommerceProduct', array('id' => $id));
  48. $this->assertEquals('Doctrine Cookbook', $productProxy->getName());
  49. }
  50. /**
  51. * @group DDC-727
  52. */
  53. public function testAccessMetatadaForProxy()
  54. {
  55. $id = $this->createProduct();
  56. $entity = $this->_em->getReference('Doctrine\Tests\Models\ECommerce\ECommerceProduct' , $id);
  57. $class = $this->_em->getClassMetadata(get_class($entity));
  58. $this->assertEquals('Doctrine\Tests\Models\ECommerce\ECommerceProduct', $class->name);
  59. }
  60. /**
  61. * @group DDC-1033
  62. */
  63. public function testReferenceFind()
  64. {
  65. $id = $this->createProduct();
  66. $entity = $this->_em->getReference('Doctrine\Tests\Models\ECommerce\ECommerceProduct' , $id);
  67. $entity2 = $this->_em->find('Doctrine\Tests\Models\ECommerce\ECommerceProduct' , $id);
  68. $this->assertSame($entity, $entity2);
  69. $this->assertEquals('Doctrine Cookbook', $entity2->getName());
  70. }
  71. /**
  72. * @group DDC-1033
  73. */
  74. public function testCloneProxy()
  75. {
  76. $id = $this->createProduct();
  77. /* @var $entity Doctrine\Tests\Models\ECommerce\ECommerceProduct */
  78. $entity = $this->_em->getReference('Doctrine\Tests\Models\ECommerce\ECommerceProduct' , $id);
  79. /* @var $clone Doctrine\Tests\Models\ECommerce\ECommerceProduct */
  80. $clone = clone $entity;
  81. $this->assertEquals($id, $entity->getId());
  82. $this->assertEquals('Doctrine Cookbook', $entity->getName());
  83. $this->assertFalse($this->_em->contains($clone), "Cloning a reference proxy should return an unmanaged/detached entity.");
  84. $this->assertEquals($id, $clone->getId(), "Cloning a reference proxy should return same id.");
  85. $this->assertEquals('Doctrine Cookbook', $clone->getName(), "Cloning a reference proxy should return same product name.");
  86. // domain logic, Product::__clone sets isCloned public property
  87. $this->assertTrue($clone->isCloned);
  88. $this->assertFalse($entity->isCloned);
  89. }
  90. /**
  91. * @group DDC-733
  92. */
  93. public function testInitializeProxy()
  94. {
  95. $id = $this->createProduct();
  96. /* @var $entity Doctrine\Tests\Models\ECommerce\ECommerceProduct */
  97. $entity = $this->_em->getReference('Doctrine\Tests\Models\ECommerce\ECommerceProduct' , $id);
  98. $this->assertFalse($entity->__isInitialized__, "Pre-Condition: Object is unitialized proxy.");
  99. $this->_em->getUnitOfWork()->initializeObject($entity);
  100. $this->assertTrue($entity->__isInitialized__, "Should be initialized after called UnitOfWork::initializeObject()");
  101. }
  102. /**
  103. * @group DDC-1163
  104. */
  105. public function testInitializeChangeAndFlushProxy()
  106. {
  107. $id = $this->createProduct();
  108. /* @var $entity Doctrine\Tests\Models\ECommerce\ECommerceProduct */
  109. $entity = $this->_em->getReference('Doctrine\Tests\Models\ECommerce\ECommerceProduct' , $id);
  110. $entity->setName('Doctrine 2 Cookbook');
  111. $this->_em->flush();
  112. $this->_em->clear();
  113. $entity = $this->_em->getReference('Doctrine\Tests\Models\ECommerce\ECommerceProduct' , $id);
  114. $this->assertEquals('Doctrine 2 Cookbook', $entity->getName());
  115. }
  116. /**
  117. * @group DDC-1022
  118. */
  119. public function testWakeupCalledOnProxy()
  120. {
  121. $id = $this->createProduct();
  122. /* @var $entity Doctrine\Tests\Models\ECommerce\ECommerceProduct */
  123. $entity = $this->_em->getReference('Doctrine\Tests\Models\ECommerce\ECommerceProduct' , $id);
  124. $this->assertFalse($entity->wakeUp);
  125. $entity->setName('Doctrine 2 Cookbook');
  126. $this->assertTrue($entity->wakeUp, "Loading the proxy should call __wakeup().");
  127. }
  128. public function testDoNotInitializeProxyOnGettingTheIdentifier()
  129. {
  130. $id = $this->createProduct();
  131. /* @var $entity Doctrine\Tests\Models\ECommerce\ECommerceProduct */
  132. $entity = $this->_em->getReference('Doctrine\Tests\Models\ECommerce\ECommerceProduct' , $id);
  133. $this->assertFalse($entity->__isInitialized__, "Pre-Condition: Object is unitialized proxy.");
  134. $this->assertEquals($id, $entity->getId());
  135. $this->assertFalse($entity->__isInitialized__, "Getting the identifier doesn't initialize the proxy.");
  136. }
  137. /**
  138. * @group DDC-1625
  139. */
  140. public function testDoNotInitializeProxyOnGettingTheIdentifier_DDC_1625()
  141. {
  142. $id = $this->createAuction();
  143. /* @var $entity Doctrine\Tests\Models\Company\CompanyAuction */
  144. $entity = $this->_em->getReference('Doctrine\Tests\Models\Company\CompanyAuction' , $id);
  145. $this->assertFalse($entity->__isInitialized__, "Pre-Condition: Object is unitialized proxy.");
  146. $this->assertEquals($id, $entity->getId());
  147. $this->assertFalse($entity->__isInitialized__, "Getting the identifier doesn't initialize the proxy when extending.");
  148. }
  149. public function testDoNotInitializeProxyOnGettingTheIdentifierAndReturnTheRightType()
  150. {
  151. $product = new ECommerceProduct();
  152. $product->setName('Doctrine Cookbook');
  153. $shipping = new ECommerceShipping();
  154. $shipping->setDays(1);
  155. $product->setShipping($shipping);
  156. $this->_em->persist($product);
  157. $this->_em->flush();
  158. $this->_em->clear();
  159. $id = $shipping->getId();
  160. $product = $this->_em->getRepository('Doctrine\Tests\Models\ECommerce\ECommerceProduct')->find($product->getId());
  161. $entity = $product->getShipping();
  162. $this->assertFalse($entity->__isInitialized__, "Pre-Condition: Object is unitialized proxy.");
  163. $this->assertEquals($id, $entity->getId());
  164. $this->assertSame($id, $entity->getId(), "Check that the id's are the same value, and type.");
  165. $this->assertFalse($entity->__isInitialized__, "Getting the identifier doesn't initialize the proxy.");
  166. }
  167. public function testInitializeProxyOnGettingSomethingOtherThanTheIdentifier()
  168. {
  169. $id = $this->createProduct();
  170. /* @var $entity Doctrine\Tests\Models\ECommerce\ECommerceProduct */
  171. $entity = $this->_em->getReference('Doctrine\Tests\Models\ECommerce\ECommerceProduct' , $id);
  172. $this->assertFalse($entity->__isInitialized__, "Pre-Condition: Object is unitialized proxy.");
  173. $this->assertEquals('Doctrine Cookbook', $entity->getName());
  174. $this->assertTrue($entity->__isInitialized__, "Getting something other than the identifier initializes the proxy.");
  175. }
  176. /**
  177. * @group DDC-1604
  178. */
  179. public function testCommonPersistenceProxy()
  180. {
  181. $id = $this->createProduct();
  182. /* @var $entity Doctrine\Tests\Models\ECommerce\ECommerceProduct */
  183. $entity = $this->_em->getReference('Doctrine\Tests\Models\ECommerce\ECommerceProduct' , $id);
  184. $className = \Doctrine\Common\Util\ClassUtils::getClass($entity);
  185. $this->assertInstanceOf('Doctrine\Common\Persistence\Proxy', $entity);
  186. $this->assertFalse($entity->__isInitialized());
  187. $this->assertEquals('Doctrine\Tests\Models\ECommerce\ECommerceProduct', $className);
  188. $restName = str_replace($this->_em->getConfiguration()->getProxyNamespace(), "", get_class($entity));
  189. $restName = substr(get_class($entity), strlen($this->_em->getConfiguration()->getProxyNamespace()) +1);
  190. $proxyFileName = $this->_em->getConfiguration()->getProxyDir() . DIRECTORY_SEPARATOR . str_replace("\\", "", $restName) . ".php";
  191. $this->assertTrue(file_exists($proxyFileName), "Proxy file name cannot be found generically.");
  192. $entity->__load();
  193. $this->assertTrue($entity->__isInitialized());
  194. }
  195. }