2009-07-01 16:03:41 +04:00
< ? php
namespace Doctrine\Tests\ORM\Functional ;
use Doctrine\Tests\Models\ECommerce\ECommerceProduct ;
use Doctrine\Tests\Models\ECommerce\ECommerceShipping ;
2009-07-18 17:15:54 +04:00
use Doctrine\ORM\Mapping\AssociationMapping ;
2009-07-01 16:03:41 +04:00
require_once __DIR__ . '/../../TestInit.php' ;
/**
* Tests a unidirectional one - to - one association mapping ( without inheritance ) .
* Inverse side is not present .
*/
class OneToOneUnidirectionalAssociationTest extends \Doctrine\Tests\OrmFunctionalTestCase
{
private $product ;
private $shipping ;
protected function setUp ()
{
$this -> useModelSet ( 'ecommerce' );
parent :: setUp ();
$this -> product = new ECommerceProduct ();
$this -> product -> setName ( 'Doctrine 2 Manual' );
$this -> shipping = new ECommerceShipping ();
$this -> shipping -> setDays ( '5' );
}
public function testSavesAOneToOneAssociationWithCascadeSaveSet () {
$this -> product -> setShipping ( $this -> shipping );
2009-07-19 20:54:53 +04:00
$this -> _em -> persist ( $this -> product );
2009-07-18 15:41:37 +04:00
$this -> _em -> flush ();
2009-07-01 16:03:41 +04:00
$this -> assertForeignKeyIs ( $this -> shipping -> getId ());
}
public function testRemovesOneToOneAssociation ()
{
$this -> product -> setShipping ( $this -> shipping );
2009-07-19 20:54:53 +04:00
$this -> _em -> persist ( $this -> product );
2009-07-01 16:03:41 +04:00
$this -> product -> removeShipping ();
$this -> _em -> flush ();
$this -> assertForeignKeyIs ( null );
}
2009-07-18 17:15:54 +04:00
public function _testEagerLoad ()
2009-07-01 16:03:41 +04:00
{
2009-07-18 17:15:54 +04:00
$this -> _createFixture ();
2009-07-01 16:03:41 +04:00
$query = $this -> _em -> createQuery ( 'select p, s from Doctrine\Tests\Models\ECommerce\ECommerceProduct p left join p.shipping s' );
$result = $query -> getResultList ();
$product = $result [ 0 ];
$this -> assertTrue ( $product -> getShipping () instanceof ECommerceShipping );
$this -> assertEquals ( 1 , $product -> getShipping () -> getDays ());
}
public function testLazyLoad () {
2009-07-18 17:15:54 +04:00
$this -> markTestSkipped ();
$this -> _createFixture ();
$this -> _em -> getConfiguration () -> setAllowPartialObjects ( false );
$metadata = $this -> _em -> getClassMetadata ( 'Doctrine\Tests\Models\ECommerce\ECommerceProduct' );
$metadata -> getAssociationMapping ( 'shipping' ) -> fetchMode = AssociationMapping :: FETCH_LAZY ;
$query = $this -> _em -> createQuery ( 'select p from Doctrine\Tests\Models\ECommerce\ECommerceProduct p' );
$result = $query -> getResultList ();
$product = $result [ 0 ];
2009-07-01 16:03:41 +04:00
2009-07-18 17:15:54 +04:00
$this -> assertTrue ( $product -> getShipping () instanceof ECommerceShipping );
$this -> assertEquals ( 1 , $product -> getShipping () -> getDays ());
}
protected function _createFixture ()
{
$product = new ECommerceProduct ;
$product -> setName ( 'Php manual' );
$shipping = new ECommerceShipping ;
$shipping -> setDays ( '1' );
$product -> setShipping ( $shipping );
2009-07-19 20:54:53 +04:00
$this -> _em -> persist ( $product );
2009-07-18 17:15:54 +04:00
$this -> _em -> flush ();
$this -> _em -> clear ();
}
2009-07-01 16:03:41 +04:00
public function assertForeignKeyIs ( $value ) {
$foreignKey = $this -> _em -> getConnection () -> execute ( 'SELECT shipping_id FROM ecommerce_products WHERE id=?' , array ( $this -> product -> getId ())) -> fetchColumn ();
$this -> assertEquals ( $value , $foreignKey );
}
}