2009-05-21 12:53:40 +04:00
< ? php
namespace Doctrine\Tests\ORM\Functional ;
require_once __DIR__ . '/../../TestInit.php' ;
2009-09-15 16:24:38 +04:00
use Doctrine\Tests\Models\Company\CompanyPerson ,
Doctrine\Tests\Models\Company\CompanyEmployee ,
Doctrine\Tests\Models\Company\CompanyManager ,
Doctrine\Tests\Models\Company\CompanyOrganization ,
Doctrine\Tests\Models\Company\CompanyEvent ,
Doctrine\Tests\Models\Company\CompanyAuction ,
2009-10-22 23:12:00 +04:00
Doctrine\Tests\Models\Company\CompanyRaffle ,
Doctrine\Tests\Models\Company\CompanyCar ;
2009-05-21 12:53:40 +04:00
/**
2009-05-26 15:30:07 +04:00
* Functional tests for the Class Table Inheritance mapping strategy .
2009-05-21 12:53:40 +04:00
*
* @ author robo
*/
class ClassTableInheritanceTest extends \Doctrine\Tests\OrmFunctionalTestCase
{
protected function setUp () {
$this -> useModelSet ( 'company' );
parent :: setUp ();
2010-04-01 00:47:35 +04:00
//$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);
2009-05-21 12:53:40 +04:00
}
public function testCRUD ()
2010-03-29 17:20:41 +04:00
{
2009-05-21 12:53:40 +04:00
$person = new CompanyPerson ;
$person -> setName ( 'Roman S. Borschel' );
2009-07-19 20:54:53 +04:00
$this -> _em -> persist ( $person );
2009-05-21 12:53:40 +04:00
$employee = new CompanyEmployee ;
$employee -> setName ( 'Roman S. Borschel' );
$employee -> setSalary ( 100000 );
$employee -> setDepartment ( 'IT' );
2009-07-19 20:54:53 +04:00
$this -> _em -> persist ( $employee );
2009-05-21 12:53:40 +04:00
$employee -> setName ( 'Guilherme Blanco' );
$this -> _em -> flush ();
$this -> _em -> clear ();
2009-10-26 00:40:57 +03:00
$query = $this -> _em -> createQuery ( " select p from Doctrine \T ests \ Models \ Company \ CompanyPerson p order by p.name desc " );
2009-05-21 12:53:40 +04:00
2009-08-03 21:18:37 +04:00
$entities = $query -> getResult ();
2009-05-21 12:53:40 +04:00
$this -> assertEquals ( 2 , count ( $entities ));
$this -> assertTrue ( $entities [ 0 ] instanceof CompanyPerson );
$this -> assertTrue ( $entities [ 1 ] instanceof CompanyEmployee );
$this -> assertTrue ( is_numeric ( $entities [ 0 ] -> getId ()));
$this -> assertTrue ( is_numeric ( $entities [ 1 ] -> getId ()));
$this -> assertEquals ( 'Roman S. Borschel' , $entities [ 0 ] -> getName ());
$this -> assertEquals ( 'Guilherme Blanco' , $entities [ 1 ] -> getName ());
$this -> assertEquals ( 100000 , $entities [ 1 ] -> getSalary ());
$this -> _em -> clear ();
$query = $this -> _em -> createQuery ( " select p from Doctrine \T ests \ Models \ Company \ CompanyEmployee p " );
2009-08-03 21:18:37 +04:00
$entities = $query -> getResult ();
2009-05-21 12:53:40 +04:00
$this -> assertEquals ( 1 , count ( $entities ));
$this -> assertTrue ( $entities [ 0 ] instanceof CompanyEmployee );
$this -> assertTrue ( is_numeric ( $entities [ 0 ] -> getId ()));
$this -> assertEquals ( 'Guilherme Blanco' , $entities [ 0 ] -> getName ());
$this -> assertEquals ( 100000 , $entities [ 0 ] -> getSalary ());
$this -> _em -> clear ();
2009-06-22 22:48:42 +04:00
2010-03-18 14:40:43 +03:00
$guilherme = $this -> _em -> getRepository ( get_class ( $employee )) -> findOneBy ( array ( 'name' => 'Guilherme Blanco' ));
$this -> assertTrue ( $guilherme instanceof CompanyEmployee );
$this -> assertEquals ( 'Guilherme Blanco' , $guilherme -> getName ());
$this -> _em -> clear ();
2009-06-23 21:50:13 +04:00
$query = $this -> _em -> createQuery ( " update Doctrine \T ests \ Models \ Company \ CompanyEmployee p set p.name = ?1, p.department = ?2 where p.name='Guilherme Blanco' and p.salary = ?3 " );
2010-03-29 17:20:41 +04:00
$query -> setParameter ( 1 , 'NewName' , 'string' );
2009-06-23 21:50:13 +04:00
$query -> setParameter ( 2 , 'NewDepartment' );
$query -> setParameter ( 3 , 100000 );
$query -> getSql ();
$numUpdated = $query -> execute ();
$this -> assertEquals ( 1 , $numUpdated );
2009-06-22 22:48:42 +04:00
$query = $this -> _em -> createQuery ( " delete from Doctrine \T ests \ Models \ Company \ CompanyPerson p " );
$numDeleted = $query -> execute ();
$this -> assertEquals ( 2 , $numDeleted );
2009-05-21 12:53:40 +04:00
}
2009-05-26 15:30:07 +04:00
public function testMultiLevelUpdateAndFind () {
$manager = new CompanyManager ;
$manager -> setName ( 'Roman S. Borschel' );
$manager -> setSalary ( 100000 );
$manager -> setDepartment ( 'IT' );
$manager -> setTitle ( 'CTO' );
2009-07-19 20:54:53 +04:00
$this -> _em -> persist ( $manager );
2009-05-26 15:30:07 +04:00
$this -> _em -> flush ();
$manager -> setName ( 'Roman B.' );
$manager -> setSalary ( 119000 );
$manager -> setTitle ( 'CEO' );
2009-07-19 20:54:53 +04:00
$this -> _em -> persist ( $manager );
2009-05-26 15:30:07 +04:00
$this -> _em -> flush ();
$this -> _em -> clear ();
$manager = $this -> _em -> find ( 'Doctrine\Tests\Models\Company\CompanyManager' , $manager -> getId ());
2009-08-17 21:58:16 +04:00
$this -> assertTrue ( $manager instanceof CompanyManager );
2009-05-26 15:30:07 +04:00
$this -> assertEquals ( 'Roman B.' , $manager -> getName ());
$this -> assertEquals ( 119000 , $manager -> getSalary ());
$this -> assertEquals ( 'CEO' , $manager -> getTitle ());
$this -> assertTrue ( is_numeric ( $manager -> getId ()));
}
2009-10-22 23:12:00 +04:00
public function testFindOnBaseClass () {
$manager = new CompanyManager ;
$manager -> setName ( 'Roman S. Borschel' );
$manager -> setSalary ( 100000 );
$manager -> setDepartment ( 'IT' );
$manager -> setTitle ( 'CTO' );
$this -> _em -> persist ( $manager );
$this -> _em -> flush ();
$this -> _em -> clear ();
$person = $this -> _em -> find ( 'Doctrine\Tests\Models\Company\CompanyPerson' , $manager -> getId ());
$this -> assertTrue ( $person instanceof CompanyManager );
$this -> assertEquals ( 'Roman S. Borschel' , $person -> getName ());
$this -> assertEquals ( 100000 , $person -> getSalary ());
$this -> assertEquals ( 'CTO' , $person -> getTitle ());
$this -> assertTrue ( is_numeric ( $person -> getId ()));
//$this->assertTrue($person->getCar() instanceof CompanyCar);
}
2009-05-26 15:30:07 +04:00
public function testSelfReferencingOneToOne () {
$manager = new CompanyManager ;
$manager -> setName ( 'John Smith' );
$manager -> setSalary ( 100000 );
$manager -> setDepartment ( 'IT' );
$manager -> setTitle ( 'CTO' );
$wife = new CompanyPerson ;
$wife -> setName ( 'Mary Smith' );
$wife -> setSpouse ( $manager );
$this -> assertSame ( $manager , $wife -> getSpouse ());
$this -> assertSame ( $wife , $manager -> getSpouse ());
2009-07-19 20:54:53 +04:00
$this -> _em -> persist ( $manager );
$this -> _em -> persist ( $wife );
2009-05-26 15:30:07 +04:00
$this -> _em -> flush ();
//var_dump($this->_em->getConnection()->fetchAll('select * from company_persons'));
//var_dump($this->_em->getConnection()->fetchAll('select * from company_employees'));
//var_dump($this->_em->getConnection()->fetchAll('select * from company_managers'));
$this -> _em -> clear ();
$query = $this -> _em -> createQuery ( 'select p, s from Doctrine\Tests\Models\Company\CompanyPerson p join p.spouse s where p.name=\'Mary Smith\'' );
2009-08-03 21:18:37 +04:00
$result = $query -> getResult ();
2009-05-26 15:30:07 +04:00
$this -> assertEquals ( 1 , count ( $result ));
$this -> assertTrue ( $result [ 0 ] instanceof CompanyPerson );
$this -> assertEquals ( 'Mary Smith' , $result [ 0 ] -> getName ());
$this -> assertTrue ( $result [ 0 ] -> getSpouse () instanceof CompanyEmployee );
2009-05-26 19:42:54 +04:00
$this -> assertEquals ( 'John Smith' , $result [ 0 ] -> getSpouse () -> getName ());
$this -> assertSame ( $result [ 0 ], $result [ 0 ] -> getSpouse () -> getSpouse ());
2009-05-26 15:30:07 +04:00
}
2009-06-14 21:34:28 +04:00
public function testSelfReferencingManyToMany ()
{
$person1 = new CompanyPerson ;
$person1 -> setName ( 'Roman' );
$person2 = new CompanyPerson ;
$person2 -> setName ( 'Jonathan' );
$person1 -> addFriend ( $person2 );
$this -> assertEquals ( 1 , count ( $person1 -> getFriends ()));
$this -> assertEquals ( 1 , count ( $person2 -> getFriends ()));
2009-07-19 20:54:53 +04:00
$this -> _em -> persist ( $person1 );
$this -> _em -> persist ( $person2 );
2009-06-14 21:34:28 +04:00
$this -> _em -> flush ();
$this -> _em -> clear ();
$query = $this -> _em -> createQuery ( 'select p, f from Doctrine\Tests\Models\Company\CompanyPerson p join p.friends f where p.name=?1' );
$query -> setParameter ( 1 , 'Roman' );
2009-08-03 21:18:37 +04:00
$result = $query -> getResult ();
2009-06-14 21:34:28 +04:00
$this -> assertEquals ( 1 , count ( $result ));
$this -> assertEquals ( 1 , count ( $result [ 0 ] -> getFriends ()));
$this -> assertEquals ( 'Roman' , $result [ 0 ] -> getName ());
$friends = $result [ 0 ] -> getFriends ();
$this -> assertEquals ( 'Jonathan' , $friends [ 0 ] -> getName ());
}
2009-09-15 16:24:38 +04:00
public function testLazyLoading1 ()
{
$org = new CompanyOrganization ;
$event1 = new CompanyAuction ;
$event1 -> setData ( 'auction' );
$org -> addEvent ( $event1 );
$event2 = new CompanyRaffle ;
$event2 -> setData ( 'raffle' );
$org -> addEvent ( $event2 );
$this -> _em -> persist ( $org );
$this -> _em -> flush ();
$this -> _em -> clear ();
$orgId = $org -> getId ();
$q = $this -> _em -> createQuery ( 'select a from Doctrine\Tests\Models\Company\CompanyOrganization a where a.id = ?1' );
$q -> setParameter ( 1 , $orgId );
$result = $q -> getResult ();
$this -> assertEquals ( 1 , count ( $result ));
$this -> assertTrue ( $result [ 0 ] instanceof CompanyOrganization );
2009-11-03 21:30:21 +03:00
$this -> assertNull ( $result [ 0 ] -> getMainEvent ());
2009-09-15 16:24:38 +04:00
$events = $result [ 0 ] -> getEvents ();
$this -> assertTrue ( $events instanceof \Doctrine\ORM\PersistentCollection );
$this -> assertFalse ( $events -> isInitialized ());
$this -> assertEquals ( 2 , count ( $events ));
if ( $events [ 0 ] instanceof CompanyAuction ) {
$this -> assertTrue ( $events [ 1 ] instanceof CompanyRaffle );
} else {
$this -> assertTrue ( $events [ 0 ] instanceof CompanyRaffle );
$this -> assertTrue ( $events [ 1 ] instanceof CompanyAuction );
}
}
2009-11-03 21:30:21 +03:00
public function testLazyLoading2 ()
{
$org = new CompanyOrganization ;
$event1 = new CompanyAuction ;
$event1 -> setData ( 'auction' );
$org -> setMainEvent ( $event1 );
$this -> _em -> persist ( $org );
$this -> _em -> flush ();
$this -> _em -> clear ();
$q = $this -> _em -> createQuery ( 'select a from Doctrine\Tests\Models\Company\CompanyEvent a where a.id = ?1' );
$q -> setParameter ( 1 , $event1 -> getId ());
$result = $q -> getResult ();
$this -> assertEquals ( 1 , count ( $result ));
$this -> assertTrue ( $result [ 0 ] instanceof CompanyAuction , sprintf ( " Is of class %s " , get_class ( $result [ 0 ])));
$this -> _em -> clear ();
$q = $this -> _em -> createQuery ( 'select a from Doctrine\Tests\Models\Company\CompanyOrganization a where a.id = ?1' );
$q -> setParameter ( 1 , $org -> getId ());
$result = $q -> getResult ();
$this -> assertEquals ( 1 , count ( $result ));
$this -> assertTrue ( $result [ 0 ] instanceof CompanyOrganization );
$mainEvent = $result [ 0 ] -> getMainEvent ();
// mainEvent should have been loaded because it can't be lazy
$this -> assertTrue ( $mainEvent instanceof CompanyAuction );
$this -> assertFalse ( $mainEvent instanceof \Doctrine\ORM\Proxy\Proxy );
}
2010-02-25 01:05:40 +03:00
/**
* @ group DDC - 368
*/
public function testBulkUpdateIssueDDC368 ()
{
$dql = 'UPDATE Doctrine\Tests\Models\Company\CompanyEmployee AS p SET p.salary = 1' ;
$this -> _em -> createQuery ( $dql ) -> execute ();
$this -> assertTrue ( count ( $this -> _em -> createQuery (
'SELECT count(p.id) FROM Doctrine\Tests\Models\Company\CompanyEmployee p WHERE p.salary = 1' )
-> getResult ()) > 0 );
}
2010-07-10 00:55:30 +04:00
/**
* @ group DDC - 130
*/
public function testDeleteJoinTableRecords ()
{
2010-07-10 15:35:58 +04:00
#$this->markTestSkipped('Nightmare! friends adds both ID 6-7 and 7-6 into two rows of the join table. How to detect this?');
2010-07-10 00:55:30 +04:00
$employee1 = new CompanyEmployee ();
$employee1 -> setName ( 'gblanco' );
$employee1 -> setSalary ( 0 );
$employee1 -> setDepartment ( 'IT' );
$employee2 = new CompanyEmployee ();
$employee2 -> setName ( 'jwage' );
$employee2 -> setSalary ( 0 );
$employee2 -> setDepartment ( 'IT' );
$employee1 -> addFriend ( $employee2 );
$this -> _em -> persist ( $employee1 );
$this -> _em -> persist ( $employee2 );
$this -> _em -> flush ();
2010-07-10 16:04:32 +04:00
$employee1Id = $employee1 -> getId ();
2010-07-10 00:55:30 +04:00
$this -> _em -> remove ( $employee1 );
$this -> _em -> flush ();
2010-07-10 16:04:32 +04:00
$this -> assertNull ( $this -> _em -> find ( get_class ( $employee1 ), $employee1Id ));
2010-07-10 00:55:30 +04:00
}
2010-08-08 16:23:57 +04:00
/**
* @ group DDC - 728
*/
public function testQueryForInheritedSingleValuedAssociation ()
{
$manager = new CompanyManager ();
$manager -> setName ( 'gblanco' );
$manager -> setSalary ( 1234 );
$manager -> setTitle ( 'Awesome!' );
$manager -> setDepartment ( 'IT' );
$person = new CompanyPerson ();
$person -> setName ( 'spouse' );
$manager -> setSpouse ( $person );
$this -> _em -> persist ( $manager );
$this -> _em -> persist ( $person );
$this -> _em -> flush ();
$this -> _em -> clear ();
$dql = " SELECT m FROM Doctrine \T ests \ Models \ Company \ CompanyManager m WHERE m.spouse = ?1 " ;
$dqlManager = $this -> _em -> createQuery ( $dql ) -> setParameter ( 1 , $person -> getId ()) -> getSingleResult ();
$this -> assertEquals ( $manager -> getId (), $dqlManager -> getId ());
$this -> assertEquals ( $person -> getId (), $dqlManager -> getSpouse () -> getId ());
}
2010-09-28 00:13:14 +04:00
/**
* @ group DDC - 817
*/
public function testFindByAssociation ()
{
$manager = new CompanyManager ();
$manager -> setName ( 'gblanco' );
$manager -> setSalary ( 1234 );
$manager -> setTitle ( 'Awesome!' );
$manager -> setDepartment ( 'IT' );
$person = new CompanyPerson ();
$person -> setName ( 'spouse' );
$manager -> setSpouse ( $person );
$this -> _em -> persist ( $manager );
$this -> _em -> persist ( $person );
$this -> _em -> flush ();
$this -> _em -> clear ();
$repos = $this -> _em -> getRepository ( 'Doctrine\Tests\Models\Company\CompanyManager' );
$pmanager = $repos -> findOneBy ( array ( 'spouse' => $person -> getId ()));
$this -> assertEquals ( $manager -> getId (), $pmanager -> getId ());
$repos = $this -> _em -> getRepository ( 'Doctrine\Tests\Models\Company\CompanyPerson' );
$pmanager = $repos -> findOneBy ( array ( 'spouse' => $person -> getId ()));
$this -> assertEquals ( $manager -> getId (), $pmanager -> getId ());
}
2010-10-11 22:11:23 +04:00
/**
* @ group DDC - 834
*/
public function testGetReferenceEntityWithSubclasses ()
{
$manager = new CompanyManager ();
$manager -> setName ( 'gblanco' );
$manager -> setSalary ( 1234 );
$manager -> setTitle ( 'Awesome!' );
$manager -> setDepartment ( 'IT' );
$this -> _em -> persist ( $manager );
$this -> _em -> flush ();
$this -> _em -> clear ();
$ref = $this -> _em -> getReference ( 'Doctrine\Tests\Models\Company\CompanyPerson' , $manager -> getId ());
$this -> assertNotType ( 'Doctrine\ORM\Proxy\Proxy' , $ref , " Cannot Request a proxy from a class that has subclasses. " );
$this -> assertType ( 'Doctrine\Tests\Models\Company\CompanyPerson' , $ref );
$this -> assertType ( 'Doctrine\Tests\Models\Company\CompanyEmployee' , $ref , " Direct fetch of the reference has to load the child class Emplyoee directly. " );
$this -> _em -> clear ();
$ref = $this -> _em -> getReference ( 'Doctrine\Tests\Models\Company\CompanyManager' , $manager -> getId ());
$this -> assertType ( 'Doctrine\ORM\Proxy\Proxy' , $ref , " A proxy can be generated only if no subclasses exists for the requested reference. " );
}
2009-05-21 12:53:40 +04:00
}