2009-07-01 09:18:08 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Doctrine\Tests\ORM\Functional;
|
|
|
|
|
|
|
|
use Doctrine\Tests\Models\ECommerce\ECommerceCart;
|
|
|
|
use Doctrine\Tests\Models\ECommerce\ECommerceCustomer;
|
2009-07-18 13:15:54 +00:00
|
|
|
use Doctrine\ORM\Mapping\AssociationMapping;
|
2010-08-09 13:13:21 +02:00
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata;
|
2016-05-11 02:41:26 +07:00
|
|
|
use Doctrine\Tests\OrmFunctionalTestCase;
|
2009-07-01 09:18:08 +00:00
|
|
|
|
|
|
|
/**
|
2009-07-01 10:04:22 +00:00
|
|
|
* Tests a bidirectional one-to-one association mapping (without inheritance).
|
2009-07-01 09:18:08 +00:00
|
|
|
*/
|
2016-05-11 02:41:26 +07:00
|
|
|
class OneToOneBidirectionalAssociationTest extends OrmFunctionalTestCase
|
2009-07-01 09:18:08 +00:00
|
|
|
{
|
2009-07-01 10:04:22 +00:00
|
|
|
private $customer;
|
|
|
|
private $cart;
|
2009-07-01 09:18:08 +00:00
|
|
|
|
|
|
|
protected function setUp()
|
|
|
|
{
|
|
|
|
$this->useModelSet('ecommerce');
|
|
|
|
parent::setUp();
|
|
|
|
$this->customer = new ECommerceCustomer();
|
2009-07-01 10:04:22 +00:00
|
|
|
$this->customer->setName('John Doe');
|
2009-07-01 09:18:08 +00:00
|
|
|
$this->cart = new ECommerceCart();
|
2009-07-01 10:04:22 +00:00
|
|
|
$this->cart->setPayment('Credit card');
|
2009-07-01 09:18:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testSavesAOneToOneAssociationWithCascadeSaveSet() {
|
|
|
|
$this->customer->setCart($this->cart);
|
2009-07-19 16:54:53 +00:00
|
|
|
$this->_em->persist($this->customer);
|
2009-07-18 11:41:37 +00:00
|
|
|
$this->_em->flush();
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2009-07-01 10:04:22 +00:00
|
|
|
$this->assertCartForeignKeyIs($this->customer->getId());
|
2009-07-01 09:18:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testDoesNotSaveAnInverseSideSet() {
|
2009-07-01 10:04:22 +00:00
|
|
|
$this->customer->brokenSetCart($this->cart);
|
2009-07-19 16:54:53 +00:00
|
|
|
$this->_em->persist($this->customer);
|
2009-07-18 11:41:37 +00:00
|
|
|
$this->_em->flush();
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2009-07-01 09:18:08 +00:00
|
|
|
$this->assertCartForeignKeyIs(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRemovesOneToOneAssociation()
|
|
|
|
{
|
|
|
|
$this->customer->setCart($this->cart);
|
2009-07-19 16:54:53 +00:00
|
|
|
$this->_em->persist($this->customer);
|
2009-07-01 09:18:08 +00:00
|
|
|
$this->customer->removeCart();
|
|
|
|
|
|
|
|
$this->_em->flush();
|
|
|
|
|
|
|
|
$this->assertCartForeignKeyIs(null);
|
|
|
|
}
|
|
|
|
|
2009-07-01 10:04:22 +00:00
|
|
|
public function testEagerLoad()
|
2009-07-01 09:18:08 +00:00
|
|
|
{
|
2009-07-18 13:15:54 +00:00
|
|
|
$this->_createFixture();
|
2009-07-01 10:04:22 +00:00
|
|
|
|
|
|
|
$query = $this->_em->createQuery('select c, ca from Doctrine\Tests\Models\ECommerce\ECommerceCustomer c join c.cart ca');
|
2009-08-03 17:18:37 +00:00
|
|
|
$result = $query->getResult();
|
2009-07-01 10:04:22 +00:00
|
|
|
$customer = $result[0];
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2011-07-26 15:22:57 +02:00
|
|
|
$this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceCart', $customer->getCart());
|
2009-07-01 10:04:22 +00:00
|
|
|
$this->assertEquals('paypal', $customer->getCart()->getPayment());
|
2009-07-01 09:18:08 +00:00
|
|
|
}
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2009-07-20 12:05:19 +00:00
|
|
|
public function testLazyLoadsObjectsOnTheOwningSide() {
|
|
|
|
$this->_createFixture();
|
|
|
|
$metadata = $this->_em->getClassMetadata('Doctrine\Tests\Models\ECommerce\ECommerceCart');
|
2010-08-09 13:13:21 +02:00
|
|
|
$metadata->associationMappings['customer']['fetchMode'] = ClassMetadata::FETCH_LAZY;
|
2009-07-20 12:05:19 +00:00
|
|
|
|
|
|
|
$query = $this->_em->createQuery('select c from Doctrine\Tests\Models\ECommerce\ECommerceCart c');
|
2009-08-03 17:18:37 +00:00
|
|
|
$result = $query->getResult();
|
2009-07-20 12:05:19 +00:00
|
|
|
$cart = $result[0];
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2011-07-26 15:22:57 +02:00
|
|
|
$this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceCustomer', $cart->getCustomer());
|
2009-07-20 12:05:19 +00:00
|
|
|
$this->assertEquals('Giorgio', $cart->getCustomer()->getName());
|
|
|
|
}
|
|
|
|
|
2009-10-28 10:31:47 +00:00
|
|
|
public function testInverseSideIsNeverLazy()
|
2009-10-06 10:04:32 +00:00
|
|
|
{
|
2009-07-18 13:15:54 +00:00
|
|
|
$this->_createFixture();
|
|
|
|
$metadata = $this->_em->getClassMetadata('Doctrine\Tests\Models\ECommerce\ECommerceCustomer');
|
2010-08-09 13:13:21 +02:00
|
|
|
$metadata->associationMappings['mentor']['fetch'] = ClassMetadata::FETCH_EAGER;
|
2009-07-18 13:15:54 +00:00
|
|
|
|
|
|
|
$query = $this->_em->createQuery('select c from Doctrine\Tests\Models\ECommerce\ECommerceCustomer c');
|
2009-08-03 17:18:37 +00:00
|
|
|
$result = $query->getResult();
|
2009-07-18 13:15:54 +00:00
|
|
|
$customer = $result[0];
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2009-07-21 09:25:14 +00:00
|
|
|
$this->assertNull($customer->getMentor());
|
2016-05-11 02:41:26 +07:00
|
|
|
$this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceCart', $customer->getCart());
|
2011-07-26 15:22:57 +02:00
|
|
|
$this->assertNotInstanceOf('Doctrine\ORM\Proxy\Proxy', $customer->getCart());
|
2009-07-18 13:15:54 +00:00
|
|
|
$this->assertEquals('paypal', $customer->getCart()->getPayment());
|
|
|
|
}
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2009-10-06 10:04:32 +00:00
|
|
|
public function testUpdateWithProxyObject()
|
2011-12-19 22:56:19 +01:00
|
|
|
{
|
2009-10-06 10:04:32 +00:00
|
|
|
$cust = new ECommerceCustomer;
|
|
|
|
$cust->setName('Roman');
|
|
|
|
$cart = new ECommerceCart;
|
|
|
|
$cart->setPayment('CARD');
|
|
|
|
$cust->setCart($cart);
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2009-10-06 10:04:32 +00:00
|
|
|
$this->_em->persist($cust);
|
|
|
|
$this->_em->flush();
|
|
|
|
$this->_em->clear();
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2011-07-26 15:22:57 +02:00
|
|
|
$this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceCart', $cust->getCart());
|
2009-10-06 10:04:32 +00:00
|
|
|
$this->assertEquals('Roman', $cust->getName());
|
|
|
|
$this->assertSame($cust, $cart->getCustomer());
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2009-10-06 10:04:32 +00:00
|
|
|
$query = $this->_em->createQuery('select ca from Doctrine\Tests\Models\ECommerce\ECommerceCart ca where ca.id =?1');
|
|
|
|
$query->setParameter(1, $cart->getId());
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2009-10-06 10:04:32 +00:00
|
|
|
$cart2 = $query->getSingleResult();
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2009-10-06 10:04:32 +00:00
|
|
|
$cart2->setPayment('CHEQUE');
|
|
|
|
|
|
|
|
$this->_em->flush();
|
|
|
|
$this->_em->clear();
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2009-10-06 10:04:32 +00:00
|
|
|
$query2 = $this->_em->createQuery('select ca, c from Doctrine\Tests\Models\ECommerce\ECommerceCart ca left join ca.customer c where ca.id =?1');
|
|
|
|
$query2->setParameter(1, $cart->getId());
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2009-10-06 10:04:32 +00:00
|
|
|
$cart3 = $query2->getSingleResult();
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2011-07-26 15:22:57 +02:00
|
|
|
$this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceCustomer', $cart3->getCustomer());
|
2009-10-15 19:03:27 +00:00
|
|
|
$this->assertEquals('Roman', $cart3->getCustomer()->getName());
|
2009-10-06 10:04:32 +00:00
|
|
|
}
|
2009-07-18 13:15:54 +00:00
|
|
|
|
|
|
|
protected function _createFixture()
|
|
|
|
{
|
|
|
|
$customer = new ECommerceCustomer;
|
|
|
|
$customer->setName('Giorgio');
|
|
|
|
$cart = new ECommerceCart;
|
|
|
|
$cart->setPayment('paypal');
|
|
|
|
$customer->setCart($cart);
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2009-07-19 16:54:53 +00:00
|
|
|
$this->_em->persist($customer);
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2009-07-18 13:15:54 +00:00
|
|
|
$this->_em->flush();
|
|
|
|
$this->_em->clear();
|
|
|
|
}
|
2009-07-01 09:18:08 +00:00
|
|
|
|
|
|
|
public function assertCartForeignKeyIs($value) {
|
2016-12-07 23:33:41 +01:00
|
|
|
$foreignKey = $this->_em->getConnection()->executeQuery('SELECT customer_id FROM ecommerce_carts WHERE id=?', [$this->cart->getId()]
|
|
|
|
)->fetchColumn();
|
2009-07-01 09:18:08 +00:00
|
|
|
$this->assertEquals($value, $foreignKey);
|
|
|
|
}
|
|
|
|
}
|