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 ;
2009-07-18 17:15:54 +04:00
use Doctrine\ORM\Mapping\AssociationMapping ;
2009-07-01 13:18:08 +04:00
require_once __DIR__ . '/../../TestInit.php' ;
/**
2009-07-01 14:04:22 +04:00
* Tests a bidirectional one - to - one association mapping ( without inheritance ) .
2009-07-01 13:18:08 +04:00
*/
2009-07-01 14:04:22 +04:00
class OneToOneBidirectionalAssociationTest extends \Doctrine\Tests\OrmFunctionalTestCase
2009-07-01 13:18:08 +04:00
{
2009-07-01 14:04:22 +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 ();
2009-07-01 14:04:22 +04:00
$this -> customer -> setName ( 'John Doe' );
2009-07-01 13:18:08 +04:00
$this -> cart = new ECommerceCart ();
2009-07-01 14:04:22 +04:00
$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 );
2009-07-18 15:41:37 +04:00
$this -> _em -> flush ();
2009-07-01 14:04:22 +04:00
$this -> assertCartForeignKeyIs ( $this -> customer -> getId ());
2009-07-01 13:18:08 +04:00
}
public function testDoesNotSaveAnInverseSideSet () {
2009-07-01 14:04:22 +04:00
$this -> customer -> brokenSetCart ( $this -> cart );
2009-07-19 20:54:53 +04:00
$this -> _em -> persist ( $this -> customer );
2009-07-18 15:41:37 +04:00
$this -> _em -> flush ();
2009-07-01 14:04:22 +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 );
}
2009-07-01 14:04:22 +04:00
public function testEagerLoad ()
2009-07-01 13:18:08 +04:00
{
2009-07-18 17:15:54 +04:00
$this -> _createFixture ();
2009-07-01 14:04:22 +04:00
$query = $this -> _em -> createQuery ( 'select c, ca from Doctrine\Tests\Models\ECommerce\ECommerceCustomer c join c.cart ca' );
2009-08-03 21:18:37 +04:00
$result = $query -> getResult ();
2009-07-01 14:04:22 +04:00
$customer = $result [ 0 ];
$this -> assertTrue ( $customer -> getCart () instanceof ECommerceCart );
$this -> assertEquals ( 'paypal' , $customer -> getCart () -> getPayment ());
2009-07-01 13:18:08 +04:00
}
2009-07-01 14:04:22 +04:00
2009-07-20 16:05:19 +04:00
public function testLazyLoadsObjectsOnTheOwningSide () {
$this -> _createFixture ();
$metadata = $this -> _em -> getClassMetadata ( 'Doctrine\Tests\Models\ECommerce\ECommerceCart' );
$metadata -> getAssociationMapping ( 'customer' ) -> fetchMode = AssociationMapping :: FETCH_LAZY ;
$query = $this -> _em -> createQuery ( 'select c from Doctrine\Tests\Models\ECommerce\ECommerceCart c' );
2009-08-03 21:18:37 +04:00
$result = $query -> getResult ();
2009-07-20 16:05:19 +04:00
$cart = $result [ 0 ];
$this -> assertTrue ( $cart -> getCustomer () instanceof ECommerceCustomer );
$this -> assertEquals ( 'Giorgio' , $cart -> getCustomer () -> getName ());
}
2009-10-28 13:31:47 +03:00
public function testInverseSideIsNeverLazy ()
2009-10-06 14:04:32 +04:00
{
2009-07-18 17:15:54 +04:00
$this -> _createFixture ();
$metadata = $this -> _em -> getClassMetadata ( 'Doctrine\Tests\Models\ECommerce\ECommerceCustomer' );
2009-07-21 19:53:58 +04:00
$metadata -> getAssociationMapping ( 'mentor' ) -> fetchMode = AssociationMapping :: FETCH_EAGER ;
2009-07-18 17:15:54 +04:00
$query = $this -> _em -> createQuery ( 'select c from Doctrine\Tests\Models\ECommerce\ECommerceCustomer c' );
2009-08-03 21:18:37 +04:00
$result = $query -> getResult ();
2009-07-18 17:15:54 +04:00
$customer = $result [ 0 ];
2009-07-01 14:04:22 +04:00
2009-07-21 13:25:14 +04:00
$this -> assertNull ( $customer -> getMentor ());
2009-07-18 17:15:54 +04:00
$this -> assertTrue ( $customer -> getCart () instanceof ECommerceCart );
2009-10-28 13:31:47 +03:00
$this -> assertFalse ( $customer -> getCart () instanceof \Doctrine\ORM\Proxy\Proxy );
2009-07-18 17:15:54 +04:00
$this -> assertEquals ( 'paypal' , $customer -> getCart () -> getPayment ());
}
2009-10-06 14:04:32 +04:00
public function testUpdateWithProxyObject ()
2009-10-15 23:03:27 +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 );
$this -> _em -> persist ( $cust );
$this -> _em -> flush ();
$this -> _em -> clear ();
$this -> assertTrue ( $cust -> getCart () instanceof ECommerceCart );
$this -> assertEquals ( 'Roman' , $cust -> getName ());
$this -> assertSame ( $cust , $cart -> getCustomer ());
$query = $this -> _em -> createQuery ( 'select ca from Doctrine\Tests\Models\ECommerce\ECommerceCart ca where ca.id =?1' );
$query -> setParameter ( 1 , $cart -> getId ());
$cart2 = $query -> getSingleResult ();
$cart2 -> setPayment ( 'CHEQUE' );
$this -> _em -> flush ();
$this -> _em -> clear ();
$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 ());
$cart3 = $query2 -> getSingleResult ();
$this -> assertTrue ( $cart3 -> getCustomer () instanceof ECommerceCustomer );
2009-10-15 23:03:27 +04:00
$this -> assertEquals ( 'Roman' , $cart3 -> getCustomer () -> getName ());
2009-10-06 14:04:32 +04:00
}
2009-07-18 17:15:54 +04:00
protected function _createFixture ()
{
$customer = new ECommerceCustomer ;
$customer -> setName ( 'Giorgio' );
$cart = new ECommerceCart ;
$cart -> setPayment ( 'paypal' );
$customer -> setCart ( $cart );
2009-07-19 20:54:53 +04:00
$this -> _em -> persist ( $customer );
2009-07-18 17:15:54 +04:00
$this -> _em -> flush ();
$this -> _em -> clear ();
}
2009-07-01 13:18:08 +04:00
public function assertCartForeignKeyIs ( $value ) {
2010-04-01 01:13:34 +04:00
$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 );
}
}