From 3747365b1c85d7a0e42c94e91b969dceae1f38ce Mon Sep 17 00:00:00 2001 From: piccoloprincipe Date: Sat, 18 Jul 2009 13:15:54 +0000 Subject: [PATCH] [2.0] added tests for lazy loading; added error_reporting level; wired association proxy factory --- lib/Doctrine/ORM/Proxy/ProxyFactory.php | 13 +----- .../OneToOneBidirectionalAssociationTest.php | 40 ++++++++++++------ .../OneToOneUnidirectionalAssociationTest.php | 42 +++++++++++++------ tests/Doctrine/Tests/TestInit.php | 4 +- 4 files changed, 61 insertions(+), 38 deletions(-) diff --git a/lib/Doctrine/ORM/Proxy/ProxyFactory.php b/lib/Doctrine/ORM/Proxy/ProxyFactory.php index aa5446923..eee4e4fe4 100644 --- a/lib/Doctrine/ORM/Proxy/ProxyFactory.php +++ b/lib/Doctrine/ORM/Proxy/ProxyFactory.php @@ -68,18 +68,7 @@ class ProxyFactory */ public function getAssociationProxy($owner, \Doctrine\ORM\Mapping\AssociationMapping $assoc) { - throw new Exception("Not yet implemented."); - $proxyClassName = str_replace('\\', '_', $assoc->getTargetEntityName()) . 'AProxy'; - if ( ! class_exists($proxyClassName, false)) { - $this->_em->getMetadataFactory()->setMetadataFor(self::$_ns . $proxyClassName, $this->_em->getClassMetadata($assoc->getTargetEntityName())); - $fileName = $this->_cacheDir . $proxyClassName . '.g.php'; - if ( ! file_exists($fileName)) { - $this->_generateAssociationProxyClass($assoc->getTargetEntityName(), $proxyClassName, $fileName); - } - require $fileName; - } - $proxyClassName = '\\' . self::$_ns . $proxyClassName; - + $proxyClassName = $this->_generator->generateAssociationProxyClass($assoc->getTargetEntityName()); return new $proxyClassName($this->_em, $assoc, $owner); } } diff --git a/tests/Doctrine/Tests/ORM/Functional/OneToOneBidirectionalAssociationTest.php b/tests/Doctrine/Tests/ORM/Functional/OneToOneBidirectionalAssociationTest.php index 30f9589fe..606585e40 100644 --- a/tests/Doctrine/Tests/ORM/Functional/OneToOneBidirectionalAssociationTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/OneToOneBidirectionalAssociationTest.php @@ -4,6 +4,7 @@ namespace Doctrine\Tests\ORM\Functional; use Doctrine\Tests\Models\ECommerce\ECommerceCart; use Doctrine\Tests\Models\ECommerce\ECommerceCustomer; +use Doctrine\ORM\Mapping\AssociationMapping; require_once __DIR__ . '/../../TestInit.php'; @@ -53,6 +54,33 @@ class OneToOneBidirectionalAssociationTest extends \Doctrine\Tests\OrmFunctional } public function testEagerLoad() + { + $this->_createFixture(); + + $query = $this->_em->createQuery('select c, ca from Doctrine\Tests\Models\ECommerce\ECommerceCustomer c join c.cart ca'); + $result = $query->getResultList(); + $customer = $result[0]; + + $this->assertTrue($customer->getCart() instanceof ECommerceCart); + $this->assertEquals('paypal', $customer->getCart()->getPayment()); + } + + public function testLazyLoad() { + $this->markTestSkipped(); + $this->_createFixture(); + $this->_em->getConfiguration()->setAllowPartialObjects(false); + $metadata = $this->_em->getClassMetadata('Doctrine\Tests\Models\ECommerce\ECommerceCustomer'); + $metadata->getAssociationMapping('cart')->fetchMode = AssociationMapping::FETCH_LAZY; + + $query = $this->_em->createQuery('select c from Doctrine\Tests\Models\ECommerce\ECommerceCustomer c'); + $result = $query->getResultList(); + $customer = $result[0]; + + $this->assertTrue($customer->getCart() instanceof ECommerceCart); + $this->assertEquals('paypal', $customer->getCart()->getPayment()); + } + + protected function _createFixture() { $customer = new ECommerceCustomer; $customer->setName('Giorgio'); @@ -64,19 +92,7 @@ class OneToOneBidirectionalAssociationTest extends \Doctrine\Tests\OrmFunctional $this->_em->flush(); $this->_em->clear(); - - $query = $this->_em->createQuery('select c, ca from Doctrine\Tests\Models\ECommerce\ECommerceCustomer c join c.cart ca'); - $result = $query->getResultList(); - $customer = $result[0]; - - $this->assertTrue($customer->getCart() instanceof ECommerceCart); - $this->assertEquals('paypal', $customer->getCart()->getPayment()); } - - /* TODO: not yet implemented - public function testLazyLoad() { - - }*/ public function assertCartForeignKeyIs($value) { $foreignKey = $this->_em->getConnection()->execute('SELECT customer_id FROM ecommerce_carts WHERE id=?', array($this->cart->getId()))->fetchColumn(); diff --git a/tests/Doctrine/Tests/ORM/Functional/OneToOneUnidirectionalAssociationTest.php b/tests/Doctrine/Tests/ORM/Functional/OneToOneUnidirectionalAssociationTest.php index eb32819f7..a8af8d5c1 100644 --- a/tests/Doctrine/Tests/ORM/Functional/OneToOneUnidirectionalAssociationTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/OneToOneUnidirectionalAssociationTest.php @@ -4,6 +4,7 @@ namespace Doctrine\Tests\ORM\Functional; use Doctrine\Tests\Models\ECommerce\ECommerceProduct; use Doctrine\Tests\Models\ECommerce\ECommerceShipping; +use Doctrine\ORM\Mapping\AssociationMapping; require_once __DIR__ . '/../../TestInit.php'; @@ -45,7 +46,34 @@ class OneToOneUnidirectionalAssociationTest extends \Doctrine\Tests\OrmFunctiona $this->assertForeignKeyIs(null); } - public function testEagerLoad() + public function _testEagerLoad() + { + $this->_createFixture(); + + $query = $this->_em->createQuery('select p, s from Doctrine\Tests\Models\ECommerce\ECommerceProduct p left join p.shipping s'); + $result = $query->getResultList(); + $product = $result[0]; + + $this->assertTrue($product->getShipping() instanceof ECommerceShipping); + $this->assertEquals(1, $product->getShipping()->getDays()); + } + + public function testLazyLoad() { + $this->markTestSkipped(); + $this->_createFixture(); + $this->_em->getConfiguration()->setAllowPartialObjects(false); + $metadata = $this->_em->getClassMetadata('Doctrine\Tests\Models\ECommerce\ECommerceProduct'); + $metadata->getAssociationMapping('shipping')->fetchMode = AssociationMapping::FETCH_LAZY; + + $query = $this->_em->createQuery('select p from Doctrine\Tests\Models\ECommerce\ECommerceProduct p'); + $result = $query->getResultList(); + $product = $result[0]; + + $this->assertTrue($product->getShipping() instanceof ECommerceShipping); + $this->assertEquals(1, $product->getShipping()->getDays()); + } + + protected function _createFixture() { $product = new ECommerceProduct; $product->setName('Php manual'); @@ -57,19 +85,7 @@ class OneToOneUnidirectionalAssociationTest extends \Doctrine\Tests\OrmFunctiona $this->_em->flush(); $this->_em->clear(); - - $query = $this->_em->createQuery('select p, s from Doctrine\Tests\Models\ECommerce\ECommerceProduct p left join p.shipping s'); - $result = $query->getResultList(); - $product = $result[0]; - - $this->assertTrue($product->getShipping() instanceof ECommerceShipping); - $this->assertEquals(1, $product->getShipping()->getDays()); } - - /* TODO: not yet implemented - public function testLazyLoad() { - - }*/ public function assertForeignKeyIs($value) { $foreignKey = $this->_em->getConnection()->execute('SELECT shipping_id FROM ecommerce_products WHERE id=?', array($this->product->getId()))->fetchColumn(); diff --git a/tests/Doctrine/Tests/TestInit.php b/tests/Doctrine/Tests/TestInit.php index ce85b4ec2..3de030c43 100644 --- a/tests/Doctrine/Tests/TestInit.php +++ b/tests/Doctrine/Tests/TestInit.php @@ -4,6 +4,8 @@ */ namespace Doctrine\Tests; +error_reporting(E_ALL | E_STRICT); + require_once 'PHPUnit/Framework.php'; require_once 'PHPUnit/TextUI/TestRunner.php'; require_once __DIR__ . '/../../../lib/Doctrine/Common/ClassLoader.php'; @@ -13,4 +15,4 @@ $classLoader = new \Doctrine\Common\ClassLoader(); set_include_path( get_include_path() . PATH_SEPARATOR . __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'lib' -); \ No newline at end of file +);