2009-07-16 17:20:31 +04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Doctrine\Tests\ORM\Functional;
|
|
|
|
|
2009-07-16 17:59:26 +04:00
|
|
|
use Doctrine\ORM\Proxy\ProxyFactory;
|
|
|
|
use Doctrine\ORM\Proxy\ProxyClassGenerator;
|
2009-07-16 17:20:31 +04:00
|
|
|
use Doctrine\Tests\Models\ECommerce\ECommerceProduct;
|
2011-11-08 12:43:06 +04:00
|
|
|
use Doctrine\Tests\Models\ECommerce\ECommerceShipping;
|
2009-07-16 17:20:31 +04:00
|
|
|
|
|
|
|
require_once __DIR__ . '/../../TestInit.php';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests the generation of a proxy object for lazy loading.
|
2009-07-16 17:59:26 +04:00
|
|
|
* @author Giorgio Sironi <piccoloprincipeazzurro@gmail.com>
|
2009-12-08 01:10:40 +03:00
|
|
|
* @author Benjamin Eberlei <kontakt@beberlei.de>
|
2009-07-16 17:20:31 +04:00
|
|
|
*/
|
2009-07-16 17:59:26 +04:00
|
|
|
class ReferenceProxyTest extends \Doctrine\Tests\OrmFunctionalTestCase
|
2009-07-16 17:20:31 +04:00
|
|
|
{
|
|
|
|
protected function setUp()
|
|
|
|
{
|
|
|
|
$this->useModelSet('ecommerce');
|
|
|
|
parent::setUp();
|
2009-10-15 00:18:36 +04:00
|
|
|
$this->_factory = new ProxyFactory(
|
|
|
|
$this->_em,
|
|
|
|
__DIR__ . '/../../Proxies',
|
|
|
|
'Doctrine\Tests\Proxies',
|
|
|
|
true);
|
2009-07-16 17:20:31 +04:00
|
|
|
}
|
|
|
|
|
2011-02-26 14:47:59 +03:00
|
|
|
public function createProduct()
|
2009-07-16 17:20:31 +04:00
|
|
|
{
|
|
|
|
$product = new ECommerceProduct();
|
|
|
|
$product->setName('Doctrine Cookbook');
|
2009-07-19 20:54:53 +04:00
|
|
|
$this->_em->persist($product);
|
2009-07-16 17:20:31 +04:00
|
|
|
|
|
|
|
$this->_em->flush();
|
|
|
|
$this->_em->clear();
|
2011-02-26 14:47:59 +03:00
|
|
|
|
|
|
|
return $product->getId();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testLazyLoadsFieldValuesFromDatabase()
|
|
|
|
{
|
|
|
|
$id = $this->createProduct();
|
2009-07-16 17:20:31 +04:00
|
|
|
|
2011-03-06 23:26:54 +03:00
|
|
|
$productProxy = $this->_em->getReference('Doctrine\Tests\Models\ECommerce\ECommerceProduct', array('id' => $id));
|
2009-07-16 17:20:31 +04:00
|
|
|
$this->assertEquals('Doctrine Cookbook', $productProxy->getName());
|
|
|
|
}
|
2010-09-16 00:24:17 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @group DDC-727
|
|
|
|
*/
|
|
|
|
public function testAccessMetatadaForProxy()
|
|
|
|
{
|
2011-02-26 14:47:59 +03:00
|
|
|
$id = $this->createProduct();
|
|
|
|
|
|
|
|
$entity = $this->_em->getReference('Doctrine\Tests\Models\ECommerce\ECommerceProduct' , $id);
|
2010-09-16 00:24:17 +04:00
|
|
|
$class = $this->_em->getClassMetadata(get_class($entity));
|
|
|
|
|
|
|
|
$this->assertEquals('Doctrine\Tests\Models\ECommerce\ECommerceProduct', $class->name);
|
|
|
|
}
|
2011-02-26 14:47:59 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @group DDC-1033
|
|
|
|
*/
|
|
|
|
public function testReferenceFind()
|
|
|
|
{
|
|
|
|
$id = $this->createProduct();
|
|
|
|
|
|
|
|
$entity = $this->_em->getReference('Doctrine\Tests\Models\ECommerce\ECommerceProduct' , $id);
|
|
|
|
$entity2 = $this->_em->find('Doctrine\Tests\Models\ECommerce\ECommerceProduct' , $id);
|
|
|
|
|
|
|
|
$this->assertSame($entity, $entity2);
|
|
|
|
$this->assertEquals('Doctrine Cookbook', $entity2->getName());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group DDC-1033
|
|
|
|
*/
|
|
|
|
public function testCloneProxy()
|
|
|
|
{
|
|
|
|
$id = $this->createProduct();
|
|
|
|
|
|
|
|
/* @var $entity Doctrine\Tests\Models\ECommerce\ECommerceProduct */
|
|
|
|
$entity = $this->_em->getReference('Doctrine\Tests\Models\ECommerce\ECommerceProduct' , $id);
|
|
|
|
|
|
|
|
/* @var $clone Doctrine\Tests\Models\ECommerce\ECommerceProduct */
|
|
|
|
$clone = clone $entity;
|
|
|
|
|
|
|
|
$this->assertEquals($id, $entity->getId());
|
|
|
|
$this->assertEquals('Doctrine Cookbook', $entity->getName());
|
|
|
|
|
|
|
|
$this->assertFalse($this->_em->contains($clone), "Cloning a reference proxy should return an unmanaged/detached entity.");
|
|
|
|
$this->assertEquals($id, $clone->getId(), "Cloning a reference proxy should return same id.");
|
|
|
|
$this->assertEquals('Doctrine Cookbook', $clone->getName(), "Cloning a reference proxy should return same product name.");
|
|
|
|
|
|
|
|
// domain logic, Product::__clone sets isCloned public property
|
|
|
|
$this->assertTrue($clone->isCloned);
|
|
|
|
$this->assertFalse($entity->isCloned);
|
|
|
|
}
|
2011-11-13 01:16:39 +04:00
|
|
|
|
2011-06-05 10:44:38 +04:00
|
|
|
/**
|
|
|
|
* @group DDC-733
|
|
|
|
*/
|
|
|
|
public function testInitializeProxy()
|
|
|
|
{
|
|
|
|
$id = $this->createProduct();
|
|
|
|
|
|
|
|
/* @var $entity Doctrine\Tests\Models\ECommerce\ECommerceProduct */
|
|
|
|
$entity = $this->_em->getReference('Doctrine\Tests\Models\ECommerce\ECommerceProduct' , $id);
|
2011-11-13 01:16:39 +04:00
|
|
|
|
2011-06-05 10:44:38 +04:00
|
|
|
$this->assertFalse($entity->__isInitialized__, "Pre-Condition: Object is unitialized proxy.");
|
|
|
|
$this->_em->getUnitOfWork()->initializeObject($entity);
|
|
|
|
$this->assertTrue($entity->__isInitialized__, "Should be initialized after called UnitOfWork::initializeObject()");
|
|
|
|
}
|
2011-11-13 01:16:39 +04:00
|
|
|
|
2011-06-05 18:20:41 +04:00
|
|
|
/**
|
|
|
|
* @group DDC-1163
|
|
|
|
*/
|
|
|
|
public function testInitializeChangeAndFlushProxy()
|
|
|
|
{
|
|
|
|
$id = $this->createProduct();
|
|
|
|
|
|
|
|
/* @var $entity Doctrine\Tests\Models\ECommerce\ECommerceProduct */
|
|
|
|
$entity = $this->_em->getReference('Doctrine\Tests\Models\ECommerce\ECommerceProduct' , $id);
|
|
|
|
$entity->setName('Doctrine 2 Cookbook');
|
2011-11-13 01:16:39 +04:00
|
|
|
|
2011-06-05 18:20:41 +04:00
|
|
|
$this->_em->flush();
|
|
|
|
$this->_em->clear();
|
2011-11-13 01:16:39 +04:00
|
|
|
|
2011-06-05 18:20:41 +04:00
|
|
|
$entity = $this->_em->getReference('Doctrine\Tests\Models\ECommerce\ECommerceProduct' , $id);
|
|
|
|
$this->assertEquals('Doctrine 2 Cookbook', $entity->getName());
|
|
|
|
}
|
2011-07-09 14:12:44 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @group DDC-1022
|
|
|
|
*/
|
|
|
|
public function testWakeupCalledOnProxy()
|
|
|
|
{
|
|
|
|
$id = $this->createProduct();
|
|
|
|
|
|
|
|
/* @var $entity Doctrine\Tests\Models\ECommerce\ECommerceProduct */
|
|
|
|
$entity = $this->_em->getReference('Doctrine\Tests\Models\ECommerce\ECommerceProduct' , $id);
|
|
|
|
|
|
|
|
$this->assertFalse($entity->wakeUp);
|
|
|
|
|
|
|
|
$entity->setName('Doctrine 2 Cookbook');
|
|
|
|
|
|
|
|
$this->assertTrue($entity->wakeUp, "Loading the proxy should call __wakeup().");
|
|
|
|
}
|
2011-10-15 19:58:00 +04:00
|
|
|
|
|
|
|
public function testDoNotInitializeProxyOnGettingTheIdentifier()
|
|
|
|
{
|
|
|
|
$id = $this->createProduct();
|
|
|
|
|
|
|
|
/* @var $entity Doctrine\Tests\Models\ECommerce\ECommerceProduct */
|
|
|
|
$entity = $this->_em->getReference('Doctrine\Tests\Models\ECommerce\ECommerceProduct' , $id);
|
|
|
|
|
|
|
|
$this->assertFalse($entity->__isInitialized__, "Pre-Condition: Object is unitialized proxy.");
|
|
|
|
$this->assertEquals($id, $entity->getId());
|
|
|
|
$this->assertFalse($entity->__isInitialized__, "Getting the identifier doesn't initialize the proxy.");
|
|
|
|
}
|
|
|
|
|
2011-11-08 12:43:06 +04:00
|
|
|
public function testDoNotInitializeProxyOnGettingTheIdentifierAndReturnTheRightType()
|
|
|
|
{
|
|
|
|
$product = new ECommerceProduct();
|
|
|
|
$product->setName('Doctrine Cookbook');
|
|
|
|
|
|
|
|
$shipping = new ECommerceShipping();
|
|
|
|
$shipping->setDays(1);
|
|
|
|
$product->setShipping($shipping);
|
|
|
|
$this->_em->persist($product);
|
|
|
|
$this->_em->flush();
|
|
|
|
$this->_em->clear();
|
|
|
|
|
|
|
|
$id = $shipping->getId();
|
|
|
|
|
|
|
|
$product = $this->_em->getRepository('Doctrine\Tests\Models\ECommerce\ECommerceProduct')->find($product->getId());
|
|
|
|
|
|
|
|
$entity = $product->getShipping();
|
|
|
|
$this->assertFalse($entity->__isInitialized__, "Pre-Condition: Object is unitialized proxy.");
|
|
|
|
$this->assertEquals($id, $entity->getId());
|
2011-11-13 01:16:39 +04:00
|
|
|
$this->assertSame($id, $entity->getId(), "Check that the id's are the same value, and type.");
|
2011-11-08 12:43:06 +04:00
|
|
|
$this->assertFalse($entity->__isInitialized__, "Getting the identifier doesn't initialize the proxy.");
|
|
|
|
}
|
|
|
|
|
2011-10-15 19:58:00 +04:00
|
|
|
public function testInitializeProxyOnGettingSomethingOtherThanTheIdentifier()
|
|
|
|
{
|
|
|
|
$id = $this->createProduct();
|
|
|
|
|
|
|
|
/* @var $entity Doctrine\Tests\Models\ECommerce\ECommerceProduct */
|
|
|
|
$entity = $this->_em->getReference('Doctrine\Tests\Models\ECommerce\ECommerceProduct' , $id);
|
|
|
|
|
|
|
|
$this->assertFalse($entity->__isInitialized__, "Pre-Condition: Object is unitialized proxy.");
|
|
|
|
$this->assertEquals('Doctrine Cookbook', $entity->getName());
|
|
|
|
$this->assertTrue($entity->__isInitialized__, "Getting something other than the identifier initializes the proxy.");
|
|
|
|
}
|
2012-01-16 13:57:29 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @group DDC-1604
|
|
|
|
*/
|
|
|
|
public function testCommonPersistenceProxy()
|
|
|
|
{
|
|
|
|
$id = $this->createProduct();
|
|
|
|
|
|
|
|
/* @var $entity Doctrine\Tests\Models\ECommerce\ECommerceProduct */
|
|
|
|
$entity = $this->_em->getReference('Doctrine\Tests\Models\ECommerce\ECommerceProduct' , $id);
|
|
|
|
$className = \Doctrine\Common\Util\ClassUtils::getClass($entity);
|
|
|
|
|
|
|
|
$this->assertInstanceOf('Doctrine\Common\Persistence\Proxy', $entity);
|
|
|
|
$this->assertFalse($entity->__isInitialized());
|
|
|
|
$this->assertEquals('Doctrine\Tests\Models\ECommerce\ECommerceProduct', $className);
|
|
|
|
|
|
|
|
$restName = str_replace($this->_em->getConfiguration()->getProxyNamespace(), "", get_class($entity));
|
|
|
|
$restName = substr(get_class($entity), strlen($this->_em->getConfiguration()->getProxyNamespace()) +1);
|
|
|
|
$proxyFileName = $this->_em->getConfiguration()->getProxyDir() . DIRECTORY_SEPARATOR . str_replace("\\", "", $restName) . ".php";
|
|
|
|
$this->assertTrue(file_exists($proxyFileName), "Proxy file name cannot be found generically.");
|
|
|
|
|
|
|
|
$entity->__load();
|
|
|
|
$this->assertTrue($entity->__isInitialized());
|
|
|
|
}
|
2009-07-16 17:20:31 +04:00
|
|
|
}
|