2009-07-06 16:42:14 +04:00
< ? php
namespace Doctrine\Tests\ORM\Functional ;
use Doctrine\Tests\Models\ECommerce\ECommerceCustomer ;
2009-07-28 15:43:42 +04:00
use Doctrine\ORM\Mapping\AssociationMapping ;
2009-07-06 16:42:14 +04:00
require_once __DIR__ . '/../../TestInit.php' ;
/**
* Tests a self referential one - to - one association mapping ( without inheritance ) .
* Relation is defined as the mentor that a customer choose . The mentor could
* help only one other customer , while a customer can choose only one mentor
* for receiving support .
* Inverse side is not present .
*/
class OneToOneSelfReferentialAssociationTest extends \Doctrine\Tests\OrmFunctionalTestCase
{
private $customer ;
private $mentor ;
protected function setUp ()
{
$this -> useModelSet ( 'ecommerce' );
parent :: setUp ();
$this -> customer = new ECommerceCustomer ();
$this -> customer -> setName ( 'Anakin Skywalker' );
$this -> mentor = new ECommerceCustomer ();
$this -> mentor -> setName ( 'Obi-wan Kenobi' );
}
public function testSavesAOneToOneAssociationWithCascadeSaveSet () {
$this -> customer -> setMentor ( $this -> mentor );
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-06 16:42:14 +04:00
$this -> assertForeignKeyIs ( $this -> mentor -> getId ());
}
public function testRemovesOneToOneAssociation ()
{
$this -> customer -> setMentor ( $this -> mentor );
2009-07-19 20:54:53 +04:00
$this -> _em -> persist ( $this -> customer );
2009-07-06 16:42:14 +04:00
$this -> customer -> removeMentor ();
$this -> _em -> flush ();
$this -> assertForeignKeyIs ( null );
}
2009-07-28 15:43:42 +04:00
public function testEagerLoadsAssociation ()
2009-07-06 16:42:14 +04:00
{
2009-07-28 15:43:42 +04:00
$this -> _createFixture ();
2009-07-06 16:42:14 +04:00
2009-07-18 15:41:37 +04:00
$query = $this -> _em -> createQuery ( 'select c, m from Doctrine\Tests\Models\ECommerce\ECommerceCustomer c left join c.mentor m order by c.id asc' );
2009-08-03 21:18:37 +04:00
$result = $query -> getResult ();
2009-07-06 16:42:14 +04:00
$customer = $result [ 0 ];
2009-07-28 15:43:42 +04:00
$this -> assertLoadingOfAssociation ( $customer );
}
2009-08-13 14:13:06 +04:00
/**
* @ group mine
* @ return unknown_type
*/
2009-07-28 15:43:42 +04:00
public function testLazyLoadsAssociation ()
{
$this -> _createFixture ();
$this -> _em -> getConfiguration () -> setAllowPartialObjects ( false );
$metadata = $this -> _em -> getClassMetadata ( 'Doctrine\Tests\Models\ECommerce\ECommerceCustomer' );
$metadata -> getAssociationMapping ( 'mentor' ) -> fetchMode = AssociationMapping :: FETCH_LAZY ;
2009-07-06 16:42:14 +04:00
2009-08-13 14:13:06 +04:00
$query = $this -> _em -> createQuery ( " select c from Doctrine \T ests \ Models \ ECommerce \ ECommerceCustomer c where c.name='Luke Skywalker' " );
2009-08-03 21:18:37 +04:00
$result = $query -> getResult ();
2009-07-28 15:43:42 +04:00
$customer = $result [ 0 ];
$this -> assertLoadingOfAssociation ( $customer );
}
public function assertLoadingOfAssociation ( $customer )
{
2009-07-06 16:42:14 +04:00
$this -> assertTrue ( $customer -> getMentor () instanceof ECommerceCustomer );
$this -> assertEquals ( 'Obi-wan Kenobi' , $customer -> getMentor () -> getName ());
}
public function assertForeignKeyIs ( $value ) {
$foreignKey = $this -> _em -> getConnection () -> execute ( 'SELECT mentor_id FROM ecommerce_customers WHERE id=?' , array ( $this -> customer -> getId ())) -> fetchColumn ();
$this -> assertEquals ( $value , $foreignKey );
}
2009-07-28 15:43:42 +04:00
private function _createFixture ()
{
$customer = new ECommerceCustomer ;
$customer -> setName ( 'Luke Skywalker' );
$mentor = new ECommerceCustomer ;
$mentor -> setName ( 'Obi-wan Kenobi' );
$customer -> setMentor ( $mentor );
$this -> _em -> persist ( $customer );
$this -> _em -> flush ();
$this -> _em -> clear ();
}
2009-07-06 16:42:14 +04:00
}