1
0
mirror of synced 2025-01-31 04:21:44 +03:00

[2.0] working implementation of reference proxies

This commit is contained in:
piccoloprincipe 2009-07-16 13:20:31 +00:00
parent 9dba60a5fb
commit 0b9a2e0c0b
4 changed files with 113 additions and 1 deletions

View File

@ -32,6 +32,7 @@ class AllTests
$suite->addTestSuite('Doctrine\Tests\ORM\EntityManagerTest');
$suite->addTestSuite('Doctrine\Tests\ORM\CommitOrderCalculatorTest');
$suite->addTestSuite('Doctrine\Tests\ORM\QueryBuilderTest');
$suite->addTestSuite('Doctrine\Tests\ORM\DynamicProxy\GeneratorTest');
$suite->addTest(Query\AllTests::suite());
$suite->addTest(Hydration\AllTests::suite());
@ -48,4 +49,4 @@ class AllTests
if (PHPUnit_MAIN_METHOD == 'Orm_AllTests::main') {
AllTests::main();
}
}

View File

@ -0,0 +1,72 @@
<?php
namespace Doctrine\Tests\ORM\DynamicProxy;
use Doctrine\ORM\DynamicProxy\Generator;
use Doctrine\Tests\Mocks\ConnectionMock;
use Doctrine\Tests\Mocks\EntityManagerMock;
use Doctrine\Tests\Models\ECommerce\ECommerceCart;
use Doctrine\Tests\Models\ECommerce\ECommerceCustomer;
use Doctrine\Tests\Models\ECommerce\ECommerceFeature;
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.
*/
class GeneratorTest extends \Doctrine\Tests\OrmTestCase
{
private $_connectionMock;
private $_emMock;
private $_generator;
protected function setUp()
{
parent::setUp();
// SUT
$this->_connectionMock = new ConnectionMock(array(), new \Doctrine\Tests\Mocks\DriverMock());
$this->_emMock = EntityManagerMock::create($this->_connectionMock);
$this->_generator = new Generator($this->_emMock, __DIR__ . '/generated');
}
protected function tearDown()
{
foreach (new \DirectoryIterator(__DIR__ . '/generated') as $file) {
if (strstr($file->getFilename(), '.php')) {
unlink($file->getPathname());
}
}
}
public function testCreatesASubclassOfTheOriginalOne()
{
$proxyClass = $this->_generator->generateReferenceProxyClass('Doctrine\Tests\Models\ECommerce\ECommerceFeature');
$this->assertTrue(is_subclass_of($proxyClass, '\Doctrine\Tests\Models\ECommerce\ECommerceFeature'));
}
public function testCanGuessADefaultTempFolder()
{
$generator = new Generator($this->_emMock);
$proxyClass = $generator->generateReferenceProxyClass('Doctrine\Tests\Models\ECommerce\ECommerceShipping');
$this->assertTrue(is_subclass_of($proxyClass, '\Doctrine\Tests\Models\ECommerce\ECommerceShipping'));
}
public function testAllowsClassesWithAConstructor()
{
$proxyClass = $this->_generator->generateReferenceProxyClass('Doctrine\Tests\Models\ECommerce\ECommerceCart');
$this->assertTrue(is_subclass_of($proxyClass, '\Doctrine\Tests\Models\ECommerce\ECommerceCart'));
}
public function _testGenerateProxiesWhichForwardsToTheModelWithTheGivenIdentifier()
{
$feature = new ECommerceFeature;
$feature->setDescription('An interesting feature');
$this->_emMock->save($feature);
$id = $feature->getId();
$this->_emMock->clear();
$proxy = $this->_generator->generateReferenceProxyClass('Doctrine\Tests\Models\ECommerce\ECommerceFeature', 1);
$this->assertEquals('An interesting feature', $proxy->getDescription());
}
}

View File

@ -34,6 +34,7 @@ class AllTests
$suite->addTestSuite('Doctrine\Tests\ORM\Functional\OneToOneSelfReferentialAssociationTest');
$suite->addTestSuite('Doctrine\Tests\ORM\Functional\OneToManySelfReferentialAssociationTest');
$suite->addTestSuite('Doctrine\Tests\ORM\Functional\ManyToManySelfReferentialAssociationTest');
$suite->addTestSuite('Doctrine\Tests\ORM\Functional\DynamicProxyTest');
return $suite;
}

View File

@ -0,0 +1,38 @@
<?php
namespace Doctrine\Tests\ORM\Functional;
use Doctrine\ORM\DynamicProxy\Factory;
use Doctrine\ORM\DynamicProxy\Generator;
use Doctrine\Tests\Models\ECommerce\ECommerceProduct;
require_once __DIR__ . '/../../TestInit.php';
/**
* Tests the generation of a proxy object for lazy loading.
*/
class DynamicProxyTest extends \Doctrine\Tests\OrmFunctionalTestCase
{
private $product;
protected function setUp()
{
$this->useModelSet('ecommerce');
parent::setUp();
$this->_factory = new Factory($this->_em, new Generator($this->_em));
}
public function testLazyLoadsFieldValuesFromDatabase()
{
$product = new ECommerceProduct();
$product->setName('Doctrine Cookbook');
$this->_em->save($product);
$id = $product->getId();
$this->_em->flush();
$this->_em->clear();
$productProxy = $this->_factory->getReferenceProxy('Doctrine\Tests\Models\ECommerce\ECommerceProduct', array('id' => $id));
$this->assertEquals('Doctrine Cookbook', $productProxy->getName());
}
}