From 4e50792a8d068fe23f7bdac013476fef96d01821 Mon Sep 17 00:00:00 2001 From: piccoloprincipe Date: Wed, 1 Jul 2009 09:18:08 +0000 Subject: [PATCH] [2.0] references #2276 --- .../Tests/Models/ECommerce/ECommerceCart.php | 32 +++++++++ .../Models/ECommerce/ECommerceCustomer.php | 43 ++++++++++++ .../Functional/OneToOneAssociationTest.php | 70 +++++++++++++++++++ .../Doctrine/Tests/OrmFunctionalTestCase.php | 11 ++- 4 files changed, 154 insertions(+), 2 deletions(-) create mode 100644 tests/Doctrine/Tests/Models/ECommerce/ECommerceCart.php create mode 100644 tests/Doctrine/Tests/Models/ECommerce/ECommerceCustomer.php create mode 100644 tests/Doctrine/Tests/ORM/Functional/OneToOneAssociationTest.php diff --git a/tests/Doctrine/Tests/Models/ECommerce/ECommerceCart.php b/tests/Doctrine/Tests/Models/ECommerce/ECommerceCart.php new file mode 100644 index 000000000..ff7d2a149 --- /dev/null +++ b/tests/Doctrine/Tests/Models/ECommerce/ECommerceCart.php @@ -0,0 +1,32 @@ +cart = $cart; + $cart->customer = $this; + } + + public function removeCart() + { + $this->cart->customer = null; + $this->cart = null; + } +} diff --git a/tests/Doctrine/Tests/ORM/Functional/OneToOneAssociationTest.php b/tests/Doctrine/Tests/ORM/Functional/OneToOneAssociationTest.php new file mode 100644 index 000000000..d38edd3fb --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Functional/OneToOneAssociationTest.php @@ -0,0 +1,70 @@ +useModelSet('ecommerce'); + parent::setUp(); + $this->customer = new ECommerceCustomer(); + $this->customer->name = 'John Doe'; + $this->cart = new ECommerceCart(); + $this->cart->payment = 'Credit card'; + } + + public function testSavesAOneToOneAssociationWithCascadeSaveSet() { + $this->customer->setCart($this->cart); + $this->_em->save($this->customer); + + $this->assertCartForeignKeyIs($this->customer->id); + } + + public function testDoesNotSaveAnInverseSideSet() { + $this->customer->cart = $this->cart; + $this->_em->save($this->customer); + + $this->assertCartForeignKeyIs(null); + } + + public function testRemovesOneToOneAssociation() + { + $this->customer->setCart($this->cart); + $this->_em->save($this->customer); + $this->customer->removeCart(); + + $this->_em->flush(); + + $this->assertCartForeignKeyIs(null); + } + + public function testLoadsAnAssociation() + { + $conn = $this->_em->getConnection(); + $conn->execute('INSERT INTO ecommerce_customers (name) VALUES ("Giorgio")'); + $customerId = $conn->lastInsertId(); + $conn->execute("INSERT INTO ecommerce_carts (customer_id, payment) VALUES ('$customerId', 'paypal')"); + + $customer = $this->_em->find("Doctrine\Tests\Models\ECommerce\ECommerceCustomer", $customerId); + + $this->assertEquals('paypal', $customer->cart->payment); + } + + public function assertCartForeignKeyIs($value) { + $foreignKey = $this->_em->getConnection()->execute('SELECT customer_id FROM ecommerce_carts WHERE id=?', array($this->cart->id))->fetchColumn(); + $this->assertEquals($value, $foreignKey); + } +} diff --git a/tests/Doctrine/Tests/OrmFunctionalTestCase.php b/tests/Doctrine/Tests/OrmFunctionalTestCase.php index f20718ecf..f8e53232d 100644 --- a/tests/Doctrine/Tests/OrmFunctionalTestCase.php +++ b/tests/Doctrine/Tests/OrmFunctionalTestCase.php @@ -44,7 +44,10 @@ class OrmFunctionalTestCase extends OrmTestCase 'Doctrine\Tests\Models\Company\CompanyEmployee', 'Doctrine\Tests\Models\Company\CompanyManager' ), - 'ecommerce' => array(), + 'ecommerce' => array( + 'Doctrine\Tests\Models\ECommerce\ECommerceCart', + 'Doctrine\Tests\Models\ECommerce\ECommerceCustomer' + ), 'generic' => array( 'Doctrine\Tests\Models\Generic\DateTimeModel' ) @@ -69,6 +72,10 @@ class OrmFunctionalTestCase extends OrmTestCase $conn->exec('DELETE FROM cms_articles'); $conn->exec('DELETE FROM cms_users'); } + if (isset($this->_usedModelSets['ecommerce'])) { + $conn->exec('DELETE FROM ecommerce_carts'); + $conn->exec('DELETE FROM ecommerce_customers'); + } if (isset($this->_usedModelSets['company'])) { $conn->exec('DELETE FROM company_persons_friends'); $conn->exec('DELETE FROM company_managers'); @@ -142,4 +149,4 @@ class OrmFunctionalTestCase extends OrmTestCase return \Doctrine\ORM\EntityManager::create($conn, $config, $eventManager); } -} \ No newline at end of file +}