From 0b9a2e0c0b36b094a76145ee7a4148d421bff158 Mon Sep 17 00:00:00 2001 From: piccoloprincipe Date: Thu, 16 Jul 2009 13:20:31 +0000 Subject: [PATCH] [2.0] working implementation of reference proxies --- tests/Doctrine/Tests/ORM/AllTests.php | 3 +- .../Tests/ORM/DynamicProxy/GeneratorTest.php | 72 +++++++++++++++++++ .../Tests/ORM/Functional/AllTests.php | 1 + .../Tests/ORM/Functional/DynamicProxyTest.php | 38 ++++++++++ 4 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 tests/Doctrine/Tests/ORM/DynamicProxy/GeneratorTest.php create mode 100644 tests/Doctrine/Tests/ORM/Functional/DynamicProxyTest.php diff --git a/tests/Doctrine/Tests/ORM/AllTests.php b/tests/Doctrine/Tests/ORM/AllTests.php index 239a512ba..c454fba93 100644 --- a/tests/Doctrine/Tests/ORM/AllTests.php +++ b/tests/Doctrine/Tests/ORM/AllTests.php @@ -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(); -} \ No newline at end of file +} diff --git a/tests/Doctrine/Tests/ORM/DynamicProxy/GeneratorTest.php b/tests/Doctrine/Tests/ORM/DynamicProxy/GeneratorTest.php new file mode 100644 index 000000000..9c386e6eb --- /dev/null +++ b/tests/Doctrine/Tests/ORM/DynamicProxy/GeneratorTest.php @@ -0,0 +1,72 @@ +_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()); + } +} diff --git a/tests/Doctrine/Tests/ORM/Functional/AllTests.php b/tests/Doctrine/Tests/ORM/Functional/AllTests.php index 43fe7034f..795a1ef30 100644 --- a/tests/Doctrine/Tests/ORM/Functional/AllTests.php +++ b/tests/Doctrine/Tests/ORM/Functional/AllTests.php @@ -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; } diff --git a/tests/Doctrine/Tests/ORM/Functional/DynamicProxyTest.php b/tests/Doctrine/Tests/ORM/Functional/DynamicProxyTest.php new file mode 100644 index 000000000..34607ef07 --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Functional/DynamicProxyTest.php @@ -0,0 +1,38 @@ +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()); + } +}