Commit 66f377fb authored by piccoloprincipe's avatar piccoloprincipe

[2.0] extended tests for proxy class generation

parent 8fbee579
...@@ -58,24 +58,26 @@ class ProxyClassGenerator ...@@ -58,24 +58,26 @@ class ProxyClassGenerator
* Generates a reference proxy class. * Generates a reference proxy class.
* This is a proxy for an object which we have the id for retrieval. * This is a proxy for an object which we have the id for retrieval.
* *
* @param string $className * @param string $originalClassName
* @param string $proxyClassName * @return string name of the proxy class
* @param string $fileName
*/ */
public function generateReferenceProxyClass($className) public function generateReferenceProxyClass($originalClassName)
{ {
$class = $this->_em->getClassMetadata($className); $proxyClassName = str_replace('\\', '_', $originalClassName) . 'RProxy';
$proxyClassName = str_replace('\\', '_', $className) . 'RProxy'; //$proxyClassName = $originalClassName . 'RProxy';
$proxyFullyQualifiedClassName = self::$_ns . $proxyClassName;
if (class_exists($proxyFullyQualifiedClassName, false)) {
return $proxyFullyQualifiedClassName;
}
if ( ! class_exists($proxyClassName, false)) { $class = $this->_em->getClassMetadata($originalClassName);
$this->_em->getMetadataFactory()->setMetadataFor(self::$_ns . $proxyClassName, $class); $this->_em->getMetadataFactory()->setMetadataFor($proxyFullyQualifiedClassName, $class);
$fileName = $this->_cacheDir . $proxyClassName . '.g.php'; $fileName = $this->_cacheDir . $proxyClassName . '.g.php';
if (file_exists($fileName)) { if (file_exists($fileName)) {
require $fileName; require $fileName;
$proxyClassName = '\\' . self::$_ns . $proxyClassName; return $proxyFullyQualifiedClassName;
return $proxyClassName;
}
} }
$file = self::$_proxyClassTemplate; $file = self::$_proxyClassTemplate;
...@@ -87,7 +89,7 @@ class ProxyClassGenerator ...@@ -87,7 +89,7 @@ class ProxyClassGenerator
'<methods>', '<sleepImpl>' '<methods>', '<sleepImpl>'
); );
$replacements = array( $replacements = array(
$proxyClassName, $className, $methods, $sleepImpl $proxyClassName, $originalClassName, $methods, $sleepImpl
); );
$file = str_replace($placeholders, $replacements, $file); $file = str_replace($placeholders, $replacements, $file);
...@@ -95,9 +97,7 @@ class ProxyClassGenerator ...@@ -95,9 +97,7 @@ class ProxyClassGenerator
file_put_contents($fileName, $file); file_put_contents($fileName, $file);
require $fileName; require $fileName;
$proxyClassName = '\\' . self::$_ns . $proxyClassName; return $proxyFullyQualifiedClassName;
return $proxyClassName;
} }
protected function _generateMethods(ClassMetadata $class) protected function _generateMethods(ClassMetadata $class)
...@@ -172,12 +172,12 @@ class ProxyClassGenerator ...@@ -172,12 +172,12 @@ class ProxyClassGenerator
* This is a proxy class for an object which we have the association where * This is a proxy class for an object which we have the association where
* it is involved, but no primary key to retrieve it. * it is involved, but no primary key to retrieve it.
* *
* @param string $className * @param string $originalClassName
* @param string $proxyClassName * @param string $proxyClassName
*/ */
public function generateAssociationProxyClass($className, $proxyClassName) public function generateAssociationProxyClass($originalClassName, $proxyClassName)
{ {
$class = $this->_em->getClassMetadata($className); $class = $this->_em->getClassMetadata($originalClassName);
$file = self::$_assocProxyClassTemplate; $file = self::$_assocProxyClassTemplate;
$methods = ''; $methods = '';
...@@ -231,7 +231,7 @@ class ProxyClassGenerator ...@@ -231,7 +231,7 @@ class ProxyClassGenerator
'<methods>', '<sleepImpl>' '<methods>', '<sleepImpl>'
); );
$replacements = array( $replacements = array(
$proxyClassName, $className, $methods, $sleepImpl $proxyClassName, $originalClassName, $methods, $sleepImpl
); );
$file = str_replace($placeholders, $replacements, $file); $file = str_replace($placeholders, $replacements, $file);
......
...@@ -9,6 +9,7 @@ use Doctrine\Tests\Models\ECommerce\ECommerceCart; ...@@ -9,6 +9,7 @@ use Doctrine\Tests\Models\ECommerce\ECommerceCart;
use Doctrine\Tests\Models\ECommerce\ECommerceCustomer; use Doctrine\Tests\Models\ECommerce\ECommerceCustomer;
use Doctrine\Tests\Models\ECommerce\ECommerceFeature; use Doctrine\Tests\Models\ECommerce\ECommerceFeature;
use Doctrine\Tests\Models\ECommerce\ECommerceShipping; use Doctrine\Tests\Models\ECommerce\ECommerceShipping;
use Doctrine\ORM\Persisters\StandardEntityPersister;
require_once __DIR__ . '/../../TestInit.php'; require_once __DIR__ . '/../../TestInit.php';
...@@ -59,15 +60,57 @@ class ProxyClassGeneratorTest extends \Doctrine\Tests\OrmTestCase ...@@ -59,15 +60,57 @@ class ProxyClassGeneratorTest extends \Doctrine\Tests\OrmTestCase
$this->assertTrue(is_subclass_of($proxyClass, '\Doctrine\Tests\Models\ECommerce\ECommerceCart')); $this->assertTrue(is_subclass_of($proxyClass, '\Doctrine\Tests\Models\ECommerce\ECommerceCart'));
} }
public function _testGenerateProxiesWhichForwardsToTheModelWithTheGivenIdentifier() public function testAllowsIdempotentCreationOfProxyClass()
{ {
$feature = new ECommerceFeature; $proxyClass = $this->_generator->generateReferenceProxyClass('Doctrine\Tests\Models\ECommerce\ECommerceFeature');
$feature->setDescription('An interesting feature'); $theSameProxyClass = $this->_generator->generateReferenceProxyClass('Doctrine\Tests\Models\ECommerce\ECommerceFeature');
$this->_emMock->save($feature); $this->assertEquals($proxyClass, $theSameProxyClass);
$id = $feature->getId(); }
$this->_emMock->clear();
public function testCreatesClassesThatRequirePersisterInTheConstructor()
{
$proxyClass = $this->_generator->generateReferenceProxyClass('Doctrine\Tests\Models\ECommerce\ECommerceFeature');
$proxy = new $proxyClass($this->_getMockPersister(), null);
}
public function testCreatesClassesThatDelegateLoadingToThePersister()
{
$identifier = array('id' => 42);
$proxyClass = $this->_generator->generateReferenceProxyClass('Doctrine\Tests\Models\ECommerce\ECommerceFeature');
$persister = $this->_getMockPersister();
$proxy = new $proxyClass($persister, $identifier);
$persister->expects($this->any())
->method('load')
->with($this->equalTo($identifier), $this->isInstanceOf($proxyClass));
$proxy->getDescription();
}
$proxy = $this->_generator->generateReferenceProxyClass('Doctrine\Tests\Models\ECommerce\ECommerceFeature', 1); public function testCreatesClassesThatExecutesLoadingOnlyOnce()
$this->assertEquals('An interesting feature', $proxy->getDescription()); {
$identifier = array('id' => 42);
$proxyClass = $this->_generator->generateReferenceProxyClass('Doctrine\Tests\Models\ECommerce\ECommerceFeature');
$persister = $this->_getMockPersister();
$proxy = new $proxyClass($persister, $identifier);
$persister->expects($this->once())
->method('load')
->with($this->equalTo($identifier), $this->isInstanceOf($proxyClass));
$proxy->getId();
$proxy->getDescription();
}
/**
* @expectedException PHPUnit_Framework_Error
*/
public function testRespectsMethodsParametersTypeHinting()
{
$proxyClass = $this->_generator->generateReferenceProxyClass('Doctrine\Tests\Models\ECommerce\ECommerceFeature');
$proxy = new $proxyClass($this->_getMockPersister(), null);
$proxy->setProduct(array('invalid parameter'));
}
protected function _getMockPersister()
{
$persister = $this->getMock('Doctrine\ORM\Persisters\StandardEntityPersister', array('load'), array(), '', false);
return $persister;
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment