1
0
mirror of synced 2024-12-14 15:16:04 +03:00
doctrine2/tests/Doctrine/Tests/ORM/Functional/OneToOneBidirectionalAssociationTest.php

152 lines
5.3 KiB
PHP
Raw Normal View History

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