2009-07-16 17:20:31 +04:00
|
|
|
<?php
|
|
|
|
|
2009-07-16 17:59:26 +04:00
|
|
|
namespace Doctrine\Tests\ORM\Proxy;
|
2009-07-16 17:20:31 +04:00
|
|
|
|
2009-10-15 00:18:36 +04:00
|
|
|
use Doctrine\ORM\Proxy\ProxyFactory;
|
2009-07-16 17:20:31 +04:00
|
|
|
use Doctrine\Tests\Mocks\ConnectionMock;
|
|
|
|
use Doctrine\Tests\Mocks\EntityManagerMock;
|
2009-10-15 00:18:36 +04:00
|
|
|
use Doctrine\Tests\Mocks\UnitOfWorkMock;
|
2009-07-16 17:20:31 +04:00
|
|
|
use Doctrine\Tests\Models\ECommerce\ECommerceCart;
|
|
|
|
use Doctrine\Tests\Models\ECommerce\ECommerceCustomer;
|
|
|
|
use Doctrine\Tests\Models\ECommerce\ECommerceFeature;
|
2009-07-17 17:35:44 +04:00
|
|
|
use Doctrine\Tests\Models\ECommerce\ECommerceProduct;
|
2009-07-16 17:20:31 +04:00
|
|
|
use Doctrine\Tests\Models\ECommerce\ECommerceShipping;
|
|
|
|
|
|
|
|
require_once __DIR__ . '/../../TestInit.php';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the proxy generator. Its work is generating on-the-fly subclasses of a given model, which implement the Proxy pattern.
|
2009-07-16 17:59:26 +04:00
|
|
|
* @author Giorgio Sironi <piccoloprincipeazzurro@gmail.com>
|
2009-07-16 17:20:31 +04:00
|
|
|
*/
|
2009-07-16 17:59:26 +04:00
|
|
|
class ProxyClassGeneratorTest extends \Doctrine\Tests\OrmTestCase
|
2009-07-16 17:20:31 +04:00
|
|
|
{
|
|
|
|
private $_connectionMock;
|
2009-10-15 00:18:36 +04:00
|
|
|
private $_uowMock;
|
2009-07-16 17:20:31 +04:00
|
|
|
private $_emMock;
|
2009-10-31 11:59:27 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Doctrine\ORM\Proxy\ProxyFactory
|
|
|
|
*/
|
2009-10-15 00:18:36 +04:00
|
|
|
private $_proxyFactory;
|
2010-05-10 18:17:17 +04:00
|
|
|
|
2009-07-16 17:20:31 +04:00
|
|
|
protected function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
$this->_connectionMock = new ConnectionMock(array(), new \Doctrine\Tests\Mocks\DriverMock());
|
|
|
|
$this->_emMock = EntityManagerMock::create($this->_connectionMock);
|
2009-10-15 00:18:36 +04:00
|
|
|
$this->_uowMock = new UnitOfWorkMock($this->_emMock);
|
|
|
|
$this->_emMock->setUnitOfWork($this->_uowMock);
|
|
|
|
// SUT
|
|
|
|
$this->_proxyFactory = new ProxyFactory($this->_emMock, __DIR__ . '/generated', 'Proxies', true);
|
2009-07-16 17:20:31 +04:00
|
|
|
}
|
2010-05-10 18:17:17 +04:00
|
|
|
|
2009-07-16 17:20:31 +04:00
|
|
|
protected function tearDown()
|
|
|
|
{
|
|
|
|
foreach (new \DirectoryIterator(__DIR__ . '/generated') as $file) {
|
|
|
|
if (strstr($file->getFilename(), '.php')) {
|
|
|
|
unlink($file->getPathname());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-17 17:35:44 +04:00
|
|
|
public function testReferenceProxyDelegatesLoadingToThePersister()
|
2009-07-17 16:43:37 +04:00
|
|
|
{
|
|
|
|
$identifier = array('id' => 42);
|
2009-10-28 18:32:55 +03:00
|
|
|
$proxyClass = 'Proxies\DoctrineTestsModelsECommerceECommerceFeatureProxy';
|
2009-07-17 16:43:37 +04:00
|
|
|
$persister = $this->_getMockPersister();
|
2009-10-15 00:18:36 +04:00
|
|
|
$this->_uowMock->setEntityPersister('Doctrine\Tests\Models\ECommerce\ECommerceFeature', $persister);
|
2010-05-10 18:17:17 +04:00
|
|
|
|
2009-10-28 18:32:55 +03:00
|
|
|
$proxy = $this->_proxyFactory->getProxy('Doctrine\Tests\Models\ECommerce\ECommerceFeature', $identifier);
|
2010-05-10 18:17:17 +04:00
|
|
|
|
2009-07-28 15:43:42 +04:00
|
|
|
$persister->expects($this->atLeastOnce())
|
2009-07-17 16:43:37 +04:00
|
|
|
->method('load')
|
2010-03-15 00:20:13 +03:00
|
|
|
->with($this->equalTo($identifier), $this->isInstanceOf($proxyClass))
|
|
|
|
->will($this->returnValue(new \stdClass())); // fake return of entity instance
|
2010-05-10 18:17:17 +04:00
|
|
|
|
2009-07-17 16:43:37 +04:00
|
|
|
$proxy->getDescription();
|
|
|
|
}
|
2009-07-16 17:20:31 +04:00
|
|
|
|
2009-07-17 17:35:44 +04:00
|
|
|
public function testReferenceProxyExecutesLoadingOnlyOnce()
|
2009-07-17 16:43:37 +04:00
|
|
|
{
|
|
|
|
$identifier = array('id' => 42);
|
2009-10-28 18:32:55 +03:00
|
|
|
$proxyClass = 'Proxies\DoctrineTestsModelsECommerceECommerceFeatureProxy';
|
2009-07-17 16:43:37 +04:00
|
|
|
$persister = $this->_getMockPersister();
|
2009-10-15 00:18:36 +04:00
|
|
|
$this->_uowMock->setEntityPersister('Doctrine\Tests\Models\ECommerce\ECommerceFeature', $persister);
|
2009-10-28 18:32:55 +03:00
|
|
|
$proxy = $this->_proxyFactory->getProxy('Doctrine\Tests\Models\ECommerce\ECommerceFeature', $identifier);
|
2011-10-15 19:58:00 +04:00
|
|
|
|
2009-07-28 15:43:42 +04:00
|
|
|
$persister->expects($this->atLeastOnce())
|
2009-07-17 16:43:37 +04:00
|
|
|
->method('load')
|
2010-03-15 00:20:13 +03:00
|
|
|
->with($this->equalTo($identifier), $this->isInstanceOf($proxyClass))
|
|
|
|
->will($this->returnValue(new \stdClass())); // fake return of entity instance
|
2009-07-17 16:43:37 +04:00
|
|
|
$proxy->getDescription();
|
2011-10-15 19:58:00 +04:00
|
|
|
$proxy->getProduct();
|
2009-07-17 16:43:37 +04:00
|
|
|
}
|
|
|
|
|
2009-07-17 17:35:44 +04:00
|
|
|
public function testReferenceProxyRespectsMethodsParametersTypeHinting()
|
2009-07-17 16:43:37 +04:00
|
|
|
{
|
2009-10-28 18:32:55 +03:00
|
|
|
$proxyClass = 'Proxies\DoctrineTestsModelsECommerceECommerceFeatureProxy';
|
2009-10-15 00:18:36 +04:00
|
|
|
$persister = $this->_getMockPersister();
|
|
|
|
$this->_uowMock->setEntityPersister('Doctrine\Tests\Models\ECommerce\ECommerceFeature', $persister);
|
2009-10-28 18:32:55 +03:00
|
|
|
$proxy = $this->_proxyFactory->getProxy('Doctrine\Tests\Models\ECommerce\ECommerceFeature', null);
|
2010-05-10 18:17:17 +04:00
|
|
|
|
2009-08-13 14:13:06 +04:00
|
|
|
$method = new \ReflectionMethod(get_class($proxy), 'setProduct');
|
|
|
|
$params = $method->getParameters();
|
2010-05-10 18:17:17 +04:00
|
|
|
|
2009-08-13 14:13:06 +04:00
|
|
|
$this->assertEquals(1, count($params));
|
|
|
|
$this->assertEquals('Doctrine\Tests\Models\ECommerce\ECommerceProduct', $params[0]->getClass()->getName());
|
2009-07-17 16:43:37 +04:00
|
|
|
}
|
|
|
|
|
2010-05-10 18:17:17 +04:00
|
|
|
/**
|
|
|
|
* Test that the proxy behaves in regard to methods like &foo() correctly
|
|
|
|
*/
|
|
|
|
public function testProxyRespectsMethodsWhichReturnValuesByReference() {
|
|
|
|
$proxy = $this->_proxyFactory->getProxy('Doctrine\Tests\Models\Forum\ForumEntry', null);
|
|
|
|
$method = new \ReflectionMethod(get_class($proxy), 'getTopicByReference');
|
|
|
|
|
|
|
|
$this->assertTrue($method->returnsReference());
|
|
|
|
}
|
|
|
|
|
2009-07-17 17:35:44 +04:00
|
|
|
public function testCreatesAssociationProxyAsSubclassOfTheOriginalOne()
|
|
|
|
{
|
2009-10-28 18:32:55 +03:00
|
|
|
$proxyClass = 'Proxies\DoctrineTestsModelsECommerceECommerceFeatureProxy';
|
2009-10-15 00:18:36 +04:00
|
|
|
$this->assertTrue(is_subclass_of($proxyClass, 'Doctrine\Tests\Models\ECommerce\ECommerceFeature'));
|
2009-07-17 17:35:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function testAllowsConcurrentCreationOfBothProxyTypes()
|
|
|
|
{
|
2009-10-28 18:32:55 +03:00
|
|
|
$referenceProxyClass = 'Proxies\DoctrineTestsModelsECommerceECommerceFeatureProxy';
|
2009-10-15 00:18:36 +04:00
|
|
|
$associationProxyClass = 'Proxies\DoctrineTestsModelsECommerceECommerceFeatureAProxy';
|
2009-07-17 17:35:44 +04:00
|
|
|
$this->assertNotEquals($referenceProxyClass, $associationProxyClass);
|
|
|
|
}
|
|
|
|
|
2009-10-31 11:59:27 +03:00
|
|
|
public function testNonNamespacedProxyGeneration()
|
2009-07-17 17:35:44 +04:00
|
|
|
{
|
2009-10-31 11:59:27 +03:00
|
|
|
require_once dirname(__FILE__)."/fixtures/NonNamespacedProxies.php";
|
|
|
|
|
|
|
|
$className = "\DoctrineOrmTestEntity";
|
|
|
|
$proxyName = "DoctrineOrmTestEntityProxy";
|
|
|
|
$classMetadata = new \Doctrine\ORM\Mapping\ClassMetadata($className);
|
|
|
|
$classMetadata->mapField(array('fieldName' => 'id', 'type' => 'integer'));
|
|
|
|
$classMetadata->setIdentifier(array('id'));
|
|
|
|
|
|
|
|
$this->_proxyFactory->generateProxyClasses(array($classMetadata));
|
|
|
|
|
|
|
|
$classCode = file_get_contents(dirname(__FILE__)."/generated/".$proxyName.".php");
|
2010-05-10 18:17:17 +04:00
|
|
|
|
2009-10-31 11:59:27 +03:00
|
|
|
$this->assertNotContains("class DoctrineOrmTestEntityProxy extends \\\\DoctrineOrmTestEntity", $classCode);
|
|
|
|
$this->assertContains("class DoctrineOrmTestEntityProxy extends \\DoctrineOrmTestEntity", $classCode);
|
2009-07-17 17:35:44 +04:00
|
|
|
}
|
2010-01-06 17:12:27 +03:00
|
|
|
|
|
|
|
public function testClassWithSleepProxyGeneration()
|
|
|
|
{
|
|
|
|
$className = "\Doctrine\Tests\ORM\Proxy\SleepClass";
|
|
|
|
$proxyName = "DoctrineTestsORMProxySleepClassProxy";
|
|
|
|
$classMetadata = new \Doctrine\ORM\Mapping\ClassMetadata($className);
|
|
|
|
$classMetadata->mapField(array('fieldName' => 'id', 'type' => 'integer'));
|
|
|
|
$classMetadata->setIdentifier(array('id'));
|
|
|
|
|
|
|
|
$this->_proxyFactory->generateProxyClasses(array($classMetadata));
|
|
|
|
|
|
|
|
$classCode = file_get_contents(dirname(__FILE__)."/generated/".$proxyName.".php");
|
|
|
|
|
|
|
|
$this->assertEquals(1, substr_count($classCode, 'function __sleep'));
|
|
|
|
}
|
2010-04-02 19:05:53 +04:00
|
|
|
|
|
|
|
public function testNoConfigDir_ThrowsException()
|
|
|
|
{
|
|
|
|
$this->setExpectedException('Doctrine\ORM\Proxy\ProxyException');
|
|
|
|
new ProxyFactory($this->_getTestEntityManager(), null, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testNoNamespace_ThrowsException()
|
|
|
|
{
|
|
|
|
$this->setExpectedException('Doctrine\ORM\Proxy\ProxyException');
|
|
|
|
new ProxyFactory($this->_getTestEntityManager(), __DIR__ . '/generated', null);
|
|
|
|
}
|
2010-05-10 18:17:17 +04:00
|
|
|
|
2009-10-15 00:18:36 +04:00
|
|
|
protected function _getMockPersister()
|
|
|
|
{
|
2010-04-26 15:12:46 +04:00
|
|
|
$persister = $this->getMock('Doctrine\ORM\Persisters\BasicEntityPersister', array('load'), array(), '', false);
|
2009-10-15 00:18:36 +04:00
|
|
|
return $persister;
|
|
|
|
}
|
2009-07-16 17:20:31 +04:00
|
|
|
}
|
2010-01-06 17:12:27 +03:00
|
|
|
|
|
|
|
class SleepClass
|
|
|
|
{
|
|
|
|
public $id;
|
|
|
|
|
|
|
|
public function __sleep()
|
|
|
|
{
|
|
|
|
return array('id');
|
|
|
|
}
|
2011-10-15 19:58:00 +04:00
|
|
|
}
|