diff --git a/tests/Doctrine/Tests/Models/ECommerce/ECommerceCart.php b/tests/Doctrine/Tests/Models/ECommerce/ECommerceCart.php index ff7d2a149..896d4079a 100644 --- a/tests/Doctrine/Tests/Models/ECommerce/ECommerceCart.php +++ b/tests/Doctrine/Tests/Models/ECommerce/ECommerceCart.php @@ -17,16 +17,49 @@ class ECommerceCart * @Id * @GeneratedValue(strategy="AUTO") */ - public $id; + private $id; /** * @Column(type="string", length=50) */ - public $payment; + private $payment; /** * @OneToOne(targetEntity="ECommerceCustomer") * @JoinColumn(name="customer_id", referencedColumnName="id") */ - public $customer; + private $customer; + + public function getId() { + return $this->id; + } + + public function getPayment() { + return $this->payment; + } + + public function setPayment($payment) { + $this->payment = $payment; + } + + public function setCustomer(ECommerceCustomer $customer) { + if ($this->customer !== $customer) { + $this->customer = $customer; + $customer->setCart($this); + } + } + + public function removeCustomer() { + if ($this->customer !== null) { + $customer = $this->customer; + $this->customer = null; + if ($customer->getCart() !== null) { + $customer->removeCart(); + } + } + } + + 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 f35a26e00..ba44ea5a4 100644 --- a/tests/Doctrine/Tests/Models/ECommerce/ECommerceCustomer.php +++ b/tests/Doctrine/Tests/Models/ECommerce/ECommerceCustomer.php @@ -17,27 +17,55 @@ class ECommerceCustomer * @Id * @GeneratedValue(strategy="AUTO") */ - public $id; + private $id; /** * @Column(type="string", length=50) */ - public $name; + private $name; /** * @OneToOne(targetEntity="ECommerceCart", mappedBy="customer", cascade={"save"}) */ - public $cart; + private $cart; + + public function getId() { + return $this->id; + } + + public function getName() { + return $this->name; + } + + public function setName($name) { + $this->name = $name; + } public function setCart(ECommerceCart $cart) { + if ($this->cart !== $cart) { + $this->cart = $cart; + $cart->setCustomer($this); + } + } + + /* Does not properly maintain the bidirectional association! */ + public function brokenSetCart(ECommerceCart $cart) { $this->cart = $cart; - $cart->customer = $this; + } + + public function getCart() { + return $this->cart; } public function removeCart() { - $this->cart->customer = null; - $this->cart = null; + if ($this->cart !== null) { + $cart = $this->cart; + $this->cart = null; + if ($cart->getCustomer() !== null) { + $cart->removeCustomer(); + } + } } } diff --git a/tests/Doctrine/Tests/ORM/Functional/AllTests.php b/tests/Doctrine/Tests/ORM/Functional/AllTests.php index e73cbf33d..ac00834e3 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\OneToOneBidirectionalAssociationTest'); return $suite; } diff --git a/tests/Doctrine/Tests/ORM/Functional/OneToOneAssociationTest.php b/tests/Doctrine/Tests/ORM/Functional/OneToOneBidirectionalAssociationTest.php similarity index 50% rename from tests/Doctrine/Tests/ORM/Functional/OneToOneAssociationTest.php rename to tests/Doctrine/Tests/ORM/Functional/OneToOneBidirectionalAssociationTest.php index d38edd3fb..dcd4785ea 100644 --- a/tests/Doctrine/Tests/ORM/Functional/OneToOneAssociationTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/OneToOneBidirectionalAssociationTest.php @@ -8,35 +8,34 @@ use Doctrine\Tests\Models\ECommerce\ECommerceCustomer; require_once __DIR__ . '/../../TestInit.php'; /** - * Tests association mapping for ECommerceCustomer and ECommerceCart. - * The latter is the owning side of the relation. + * Tests a bidirectional one-to-one association mapping (without inheritance). */ -class OneToOneAssociationTest extends \Doctrine\Tests\OrmFunctionalTestCase +class OneToOneBidirectionalAssociationTest extends \Doctrine\Tests\OrmFunctionalTestCase { - public $customer; - public $cart; + private $customer; + private $cart; protected function setUp() { $this->useModelSet('ecommerce'); parent::setUp(); $this->customer = new ECommerceCustomer(); - $this->customer->name = 'John Doe'; + $this->customer->setName('John Doe'); $this->cart = new ECommerceCart(); - $this->cart->payment = 'Credit card'; + $this->cart->setPayment('Credit card'); } public function testSavesAOneToOneAssociationWithCascadeSaveSet() { $this->customer->setCart($this->cart); $this->_em->save($this->customer); - - $this->assertCartForeignKeyIs($this->customer->id); + + $this->assertCartForeignKeyIs($this->customer->getId()); } public function testDoesNotSaveAnInverseSideSet() { - $this->customer->cart = $this->cart; + $this->customer->brokenSetCart($this->cart); $this->_em->save($this->customer); - + $this->assertCartForeignKeyIs(null); } @@ -51,20 +50,34 @@ class OneToOneAssociationTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->assertCartForeignKeyIs(null); } - public function testLoadsAnAssociation() + public function testEagerLoad() { - $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 = new ECommerceCustomer; + $customer->setName('Giorgio'); + $cart = new ECommerceCart; + $cart->setPayment('paypal'); + $customer->setCart($cart); + + $this->_em->save($customer); + + $this->_em->flush(); + $this->_em->clear(); - $customer = $this->_em->find("Doctrine\Tests\Models\ECommerce\ECommerceCustomer", $customerId); - - $this->assertEquals('paypal', $customer->cart->payment); + $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->id))->fetchColumn(); + $foreignKey = $this->_em->getConnection()->execute('SELECT customer_id FROM ecommerce_carts WHERE id=?', array($this->cart->getId()))->fetchColumn(); $this->assertEquals($value, $foreignKey); } }