ReferenceProxyTest.php 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. require_once __DIR__ . '/../../TestInit.php';
  7. /**
  8. * Tests the generation of a proxy object for lazy loading.
  9. * @author Giorgio Sironi <piccoloprincipeazzurro@gmail.com>
  10. * @author Benjamin Eberlei <kontakt@beberlei.de>
  11. */
  12. class ReferenceProxyTest extends \Doctrine\Tests\OrmFunctionalTestCase
  13. {
  14. protected function setUp()
  15. {
  16. $this->useModelSet('ecommerce');
  17. parent::setUp();
  18. $this->_factory = new ProxyFactory(
  19. $this->_em,
  20. __DIR__ . '/../../Proxies',
  21. 'Doctrine\Tests\Proxies',
  22. true);
  23. }
  24. public function createProduct()
  25. {
  26. $product = new ECommerceProduct();
  27. $product->setName('Doctrine Cookbook');
  28. $this->_em->persist($product);
  29. $this->_em->flush();
  30. $this->_em->clear();
  31. return $product->getId();
  32. }
  33. public function testLazyLoadsFieldValuesFromDatabase()
  34. {
  35. $id = $this->createProduct();
  36. $productProxy = $this->_em->getReference('Doctrine\Tests\Models\ECommerce\ECommerceProduct', array('id' => $id));
  37. $this->assertEquals('Doctrine Cookbook', $productProxy->getName());
  38. }
  39. /**
  40. * @group DDC-727
  41. */
  42. public function testAccessMetatadaForProxy()
  43. {
  44. $id = $this->createProduct();
  45. $entity = $this->_em->getReference('Doctrine\Tests\Models\ECommerce\ECommerceProduct' , $id);
  46. $class = $this->_em->getClassMetadata(get_class($entity));
  47. $this->assertEquals('Doctrine\Tests\Models\ECommerce\ECommerceProduct', $class->name);
  48. }
  49. /**
  50. * @group DDC-1033
  51. */
  52. public function testReferenceFind()
  53. {
  54. $id = $this->createProduct();
  55. $entity = $this->_em->getReference('Doctrine\Tests\Models\ECommerce\ECommerceProduct' , $id);
  56. $entity2 = $this->_em->find('Doctrine\Tests\Models\ECommerce\ECommerceProduct' , $id);
  57. $this->assertSame($entity, $entity2);
  58. $this->assertEquals('Doctrine Cookbook', $entity2->getName());
  59. }
  60. /**
  61. * @group DDC-1033
  62. */
  63. public function testCloneProxy()
  64. {
  65. $id = $this->createProduct();
  66. /* @var $entity Doctrine\Tests\Models\ECommerce\ECommerceProduct */
  67. $entity = $this->_em->getReference('Doctrine\Tests\Models\ECommerce\ECommerceProduct' , $id);
  68. /* @var $clone Doctrine\Tests\Models\ECommerce\ECommerceProduct */
  69. $clone = clone $entity;
  70. $this->assertEquals($id, $entity->getId());
  71. $this->assertEquals('Doctrine Cookbook', $entity->getName());
  72. $this->assertFalse($this->_em->contains($clone), "Cloning a reference proxy should return an unmanaged/detached entity.");
  73. $this->assertEquals($id, $clone->getId(), "Cloning a reference proxy should return same id.");
  74. $this->assertEquals('Doctrine Cookbook', $clone->getName(), "Cloning a reference proxy should return same product name.");
  75. // domain logic, Product::__clone sets isCloned public property
  76. $this->assertTrue($clone->isCloned);
  77. $this->assertFalse($entity->isCloned);
  78. }
  79. /**
  80. * @group DDC-733
  81. */
  82. public function testInitializeProxy()
  83. {
  84. $id = $this->createProduct();
  85. /* @var $entity Doctrine\Tests\Models\ECommerce\ECommerceProduct */
  86. $entity = $this->_em->getReference('Doctrine\Tests\Models\ECommerce\ECommerceProduct' , $id);
  87. $this->assertFalse($entity->__isInitialized__, "Pre-Condition: Object is unitialized proxy.");
  88. $this->_em->getUnitOfWork()->initializeObject($entity);
  89. $this->assertTrue($entity->__isInitialized__, "Should be initialized after called UnitOfWork::initializeObject()");
  90. }
  91. /**
  92. * @group DDC-1163
  93. */
  94. public function testInitializeChangeAndFlushProxy()
  95. {
  96. $id = $this->createProduct();
  97. /* @var $entity Doctrine\Tests\Models\ECommerce\ECommerceProduct */
  98. $entity = $this->_em->getReference('Doctrine\Tests\Models\ECommerce\ECommerceProduct' , $id);
  99. $entity->setName('Doctrine 2 Cookbook');
  100. $this->_em->flush();
  101. $this->_em->clear();
  102. $entity = $this->_em->getReference('Doctrine\Tests\Models\ECommerce\ECommerceProduct' , $id);
  103. $this->assertEquals('Doctrine 2 Cookbook', $entity->getName());
  104. }
  105. /**
  106. * @group DDC-1022
  107. */
  108. public function testWakeupCalledOnProxy()
  109. {
  110. $id = $this->createProduct();
  111. /* @var $entity Doctrine\Tests\Models\ECommerce\ECommerceProduct */
  112. $entity = $this->_em->getReference('Doctrine\Tests\Models\ECommerce\ECommerceProduct' , $id);
  113. $this->assertFalse($entity->wakeUp);
  114. $entity->setName('Doctrine 2 Cookbook');
  115. $this->assertTrue($entity->wakeUp, "Loading the proxy should call __wakeup().");
  116. }
  117. }