From 8ccb7df1bbf707b63bfed97e838b5588cadf8449 Mon Sep 17 00:00:00 2001 From: piccoloprincipe Date: Wed, 1 Jul 2009 12:03:41 +0000 Subject: [PATCH] [2.0] added new unidirectional one-one association test --- .../Tests/Models/ECommerce/ECommerceCart.php | 9 +++ .../Models/ECommerce/ECommerceCustomer.php | 5 ++ .../Models/ECommerce/ECommerceProduct.php | 80 +++++++++++++++++++ .../Models/ECommerce/ECommerceShipping.php | 41 ++++++++++ .../Tests/ORM/Functional/AllTests.php | 3 +- .../OneToOneUnidirectionalAssociationTest.php | 77 ++++++++++++++++++ .../Doctrine/Tests/OrmFunctionalTestCase.php | 6 +- 7 files changed, 219 insertions(+), 2 deletions(-) create mode 100644 tests/Doctrine/Tests/Models/ECommerce/ECommerceProduct.php create mode 100644 tests/Doctrine/Tests/Models/ECommerce/ECommerceShipping.php create mode 100644 tests/Doctrine/Tests/ORM/Functional/OneToOneUnidirectionalAssociationTest.php diff --git a/tests/Doctrine/Tests/Models/ECommerce/ECommerceCart.php b/tests/Doctrine/Tests/Models/ECommerce/ECommerceCart.php index 896d4079a..803f4c313 100644 --- a/tests/Doctrine/Tests/Models/ECommerce/ECommerceCart.php +++ b/tests/Doctrine/Tests/Models/ECommerce/ECommerceCart.php @@ -29,6 +29,14 @@ class ECommerceCart * @JoinColumn(name="customer_id", referencedColumnName="id") */ private $customer; + + /** + * @ManyToMany(targetEntity="ECommerceProduct", cascade={"save"}) + * @JoinTable(name="ecommerce_carts_products", + joinColumns={{"name"="cart_id", "referencedColumnName"="id"}}, + inverseJoinColumns={{"name"="product_id", "referencedColumnName"="id"}}) + */ + private $products; public function getId() { return $this->id; @@ -62,4 +70,5 @@ class ECommerceCart public function getCustomer() { return $this->customer; } + } diff --git a/tests/Doctrine/Tests/Models/ECommerce/ECommerceCustomer.php b/tests/Doctrine/Tests/Models/ECommerce/ECommerceCustomer.php index ba44ea5a4..d872ade99 100644 --- a/tests/Doctrine/Tests/Models/ECommerce/ECommerceCustomer.php +++ b/tests/Doctrine/Tests/Models/ECommerce/ECommerceCustomer.php @@ -41,6 +41,11 @@ class ECommerceCustomer $this->name = $name; } + /** + * @OneToMany(targetEntity="ECommerceProduct") + public $watched; + */ + public function setCart(ECommerceCart $cart) { if ($this->cart !== $cart) { diff --git a/tests/Doctrine/Tests/Models/ECommerce/ECommerceProduct.php b/tests/Doctrine/Tests/Models/ECommerce/ECommerceProduct.php new file mode 100644 index 000000000..a9e586c96 --- /dev/null +++ b/tests/Doctrine/Tests/Models/ECommerce/ECommerceProduct.php @@ -0,0 +1,80 @@ +id; + } + + public function getName() + { + return $this->name; + } + + public function setName($name) + { + $this->name = $name; + } + + public function getPrice() + { + return $this->price; + } + + public function setPrice($price) + { + $this->price = $price; + } + + public function getShipping() + { + return $this->shipping; + } + + public function setShipping(ECommerceShipping $shipping) + { + $this->shipping = $shipping; + } + + public function removeShipping() + { + $this->shipping = null; + } +} diff --git a/tests/Doctrine/Tests/Models/ECommerce/ECommerceShipping.php b/tests/Doctrine/Tests/Models/ECommerce/ECommerceShipping.php new file mode 100644 index 000000000..fb64ca638 --- /dev/null +++ b/tests/Doctrine/Tests/Models/ECommerce/ECommerceShipping.php @@ -0,0 +1,41 @@ +id; + } + + public function getDays() + { + return $this->days; + } + + public function setDays($days) + { + $this->days = $days; + } +} diff --git a/tests/Doctrine/Tests/ORM/Functional/AllTests.php b/tests/Doctrine/Tests/ORM/Functional/AllTests.php index ac00834e3..717450143 100644 --- a/tests/Doctrine/Tests/ORM/Functional/AllTests.php +++ b/tests/Doctrine/Tests/ORM/Functional/AllTests.php @@ -26,6 +26,7 @@ class AllTests $suite->addTestSuite('Doctrine\Tests\ORM\Functional\DetachedEntityTest'); $suite->addTestSuite('Doctrine\Tests\ORM\Functional\QueryCacheTest'); $suite->addTestSuite('Doctrine\Tests\ORM\Functional\QueryTest'); + $suite->addTestSuite('Doctrine\Tests\ORM\Functional\OneToOneUnidirectionalAssociationTest'); $suite->addTestSuite('Doctrine\Tests\ORM\Functional\OneToOneBidirectionalAssociationTest'); return $suite; @@ -34,4 +35,4 @@ class AllTests if (PHPUnit_MAIN_METHOD == 'Orm_Functional_AllTests::main') { AllTests::main(); -} \ No newline at end of file +} diff --git a/tests/Doctrine/Tests/ORM/Functional/OneToOneUnidirectionalAssociationTest.php b/tests/Doctrine/Tests/ORM/Functional/OneToOneUnidirectionalAssociationTest.php new file mode 100644 index 000000000..07c1914d8 --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Functional/OneToOneUnidirectionalAssociationTest.php @@ -0,0 +1,77 @@ +useModelSet('ecommerce'); + parent::setUp(); + $this->product = new ECommerceProduct(); + $this->product->setName('Doctrine 2 Manual'); + $this->shipping = new ECommerceShipping(); + $this->shipping->setDays('5'); + } + + public function testSavesAOneToOneAssociationWithCascadeSaveSet() { + $this->product->setShipping($this->shipping); + $this->_em->save($this->product); + + $this->assertForeignKeyIs($this->shipping->getId()); + } + + public function testRemovesOneToOneAssociation() + { + $this->product->setShipping($this->shipping); + $this->_em->save($this->product); + $this->product->removeShipping(); + + $this->_em->flush(); + + $this->assertForeignKeyIs(null); + } + + public function testEagerLoad() + { + $product = new ECommerceProduct; + $product->setName('Php manual'); + $shipping = new ECommerceShipping; + $shipping->setDays('1'); + $product->setShipping($shipping); + + $this->_em->save($product); + + $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(); + $this->assertEquals($value, $foreignKey); + } +} diff --git a/tests/Doctrine/Tests/OrmFunctionalTestCase.php b/tests/Doctrine/Tests/OrmFunctionalTestCase.php index f8e53232d..beb7d3fdb 100644 --- a/tests/Doctrine/Tests/OrmFunctionalTestCase.php +++ b/tests/Doctrine/Tests/OrmFunctionalTestCase.php @@ -46,7 +46,9 @@ class OrmFunctionalTestCase extends OrmTestCase ), 'ecommerce' => array( 'Doctrine\Tests\Models\ECommerce\ECommerceCart', - 'Doctrine\Tests\Models\ECommerce\ECommerceCustomer' + 'Doctrine\Tests\Models\ECommerce\ECommerceCustomer', + 'Doctrine\Tests\Models\ECommerce\ECommerceProduct', + 'Doctrine\Tests\Models\ECommerce\ECommerceShipping' ), 'generic' => array( 'Doctrine\Tests\Models\Generic\DateTimeModel' @@ -75,6 +77,8 @@ class OrmFunctionalTestCase extends OrmTestCase if (isset($this->_usedModelSets['ecommerce'])) { $conn->exec('DELETE FROM ecommerce_carts'); $conn->exec('DELETE FROM ecommerce_customers'); + $conn->exec('DELETE FROM ecommerce_products'); + $conn->exec('DELETE FROM ecommerce_shippings'); } if (isset($this->_usedModelSets['company'])) { $conn->exec('DELETE FROM company_persons_friends');