1
0
mirror of synced 2024-12-13 06:46:03 +03:00

[2.0] adjustments to new bidirectional one-one association test from Giorgio Sironi.

This commit is contained in:
romanb 2009-07-01 10:04:22 +00:00
parent 4e50792a8d
commit f1885cb76b
4 changed files with 104 additions and 29 deletions

View File

@ -17,16 +17,49 @@ class ECommerceCart
* @Id * @Id
* @GeneratedValue(strategy="AUTO") * @GeneratedValue(strategy="AUTO")
*/ */
public $id; private $id;
/** /**
* @Column(type="string", length=50) * @Column(type="string", length=50)
*/ */
public $payment; private $payment;
/** /**
* @OneToOne(targetEntity="ECommerceCustomer") * @OneToOne(targetEntity="ECommerceCustomer")
* @JoinColumn(name="customer_id", referencedColumnName="id") * @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;
}
} }

View File

@ -17,27 +17,55 @@ class ECommerceCustomer
* @Id * @Id
* @GeneratedValue(strategy="AUTO") * @GeneratedValue(strategy="AUTO")
*/ */
public $id; private $id;
/** /**
* @Column(type="string", length=50) * @Column(type="string", length=50)
*/ */
public $name; private $name;
/** /**
* @OneToOne(targetEntity="ECommerceCart", mappedBy="customer", cascade={"save"}) * @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) 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; $this->cart = $cart;
$cart->customer = $this; }
public function getCart() {
return $this->cart;
} }
public function removeCart() public function removeCart()
{ {
$this->cart->customer = null; if ($this->cart !== null) {
$this->cart = null; $cart = $this->cart;
$this->cart = null;
if ($cart->getCustomer() !== null) {
$cart->removeCustomer();
}
}
} }
} }

View File

@ -26,6 +26,7 @@ class AllTests
$suite->addTestSuite('Doctrine\Tests\ORM\Functional\DetachedEntityTest'); $suite->addTestSuite('Doctrine\Tests\ORM\Functional\DetachedEntityTest');
$suite->addTestSuite('Doctrine\Tests\ORM\Functional\QueryCacheTest'); $suite->addTestSuite('Doctrine\Tests\ORM\Functional\QueryCacheTest');
$suite->addTestSuite('Doctrine\Tests\ORM\Functional\QueryTest'); $suite->addTestSuite('Doctrine\Tests\ORM\Functional\QueryTest');
$suite->addTestSuite('Doctrine\Tests\ORM\Functional\OneToOneBidirectionalAssociationTest');
return $suite; return $suite;
} }

View File

@ -8,35 +8,34 @@ use Doctrine\Tests\Models\ECommerce\ECommerceCustomer;
require_once __DIR__ . '/../../TestInit.php'; require_once __DIR__ . '/../../TestInit.php';
/** /**
* Tests association mapping for ECommerceCustomer and ECommerceCart. * Tests a bidirectional one-to-one association mapping (without inheritance).
* The latter is the owning side of the relation.
*/ */
class OneToOneAssociationTest extends \Doctrine\Tests\OrmFunctionalTestCase class OneToOneBidirectionalAssociationTest extends \Doctrine\Tests\OrmFunctionalTestCase
{ {
public $customer; private $customer;
public $cart; private $cart;
protected function setUp() protected function setUp()
{ {
$this->useModelSet('ecommerce'); $this->useModelSet('ecommerce');
parent::setUp(); parent::setUp();
$this->customer = new ECommerceCustomer(); $this->customer = new ECommerceCustomer();
$this->customer->name = 'John Doe'; $this->customer->setName('John Doe');
$this->cart = new ECommerceCart(); $this->cart = new ECommerceCart();
$this->cart->payment = 'Credit card'; $this->cart->setPayment('Credit card');
} }
public function testSavesAOneToOneAssociationWithCascadeSaveSet() { public function testSavesAOneToOneAssociationWithCascadeSaveSet() {
$this->customer->setCart($this->cart); $this->customer->setCart($this->cart);
$this->_em->save($this->customer); $this->_em->save($this->customer);
$this->assertCartForeignKeyIs($this->customer->id); $this->assertCartForeignKeyIs($this->customer->getId());
} }
public function testDoesNotSaveAnInverseSideSet() { public function testDoesNotSaveAnInverseSideSet() {
$this->customer->cart = $this->cart; $this->customer->brokenSetCart($this->cart);
$this->_em->save($this->customer); $this->_em->save($this->customer);
$this->assertCartForeignKeyIs(null); $this->assertCartForeignKeyIs(null);
} }
@ -51,20 +50,34 @@ class OneToOneAssociationTest extends \Doctrine\Tests\OrmFunctionalTestCase
$this->assertCartForeignKeyIs(null); $this->assertCartForeignKeyIs(null);
} }
public function testLoadsAnAssociation() public function testEagerLoad()
{ {
$conn = $this->_em->getConnection(); $customer = new ECommerceCustomer;
$conn->execute('INSERT INTO ecommerce_customers (name) VALUES ("Giorgio")'); $customer->setName('Giorgio');
$customerId = $conn->lastInsertId(); $cart = new ECommerceCart;
$conn->execute("INSERT INTO ecommerce_carts (customer_id, payment) VALUES ('$customerId', 'paypal')"); $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); $query = $this->_em->createQuery('select c, ca from Doctrine\Tests\Models\ECommerce\ECommerceCustomer c join c.cart ca');
$result = $query->getResultList();
$this->assertEquals('paypal', $customer->cart->payment); $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) { 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); $this->assertEquals($value, $foreignKey);
} }
} }