2009-07-30 19:16:02 +04:00
< ? php
namespace Doctrine\Tests\ORM\Mapping ;
use Doctrine\ORM\Mapping\ClassMetadataFactory ;
2011-06-16 00:27:24 +04:00
use Doctrine\ORM\Tools\SchemaTool ;
2009-07-30 19:16:02 +04:00
require_once __DIR__ . '/../../TestInit.php' ;
class BasicInheritanceMappingTest extends \Doctrine\Tests\OrmTestCase
{
private $_factory ;
2011-12-20 01:56:19 +04:00
2009-07-30 19:16:02 +04:00
protected function setUp () {
2010-11-27 22:53:26 +03:00
$this -> _factory = new ClassMetadataFactory ();
$this -> _factory -> setEntityManager ( $this -> _getTestEntityManager ());
2009-07-30 19:16:02 +04:00
}
2011-12-20 01:56:19 +04:00
2009-07-30 19:16:02 +04:00
/**
2010-02-03 00:17:00 +03:00
* @ expectedException Doctrine\ORM\Mapping\MappingException
2009-07-30 19:16:02 +04:00
*/
public function testGetMetadataForTransientClassThrowsException ()
{
$this -> _factory -> getMetadataFor ( 'Doctrine\Tests\ORM\Mapping\TransientBaseClass' );
}
2011-12-20 01:56:19 +04:00
2009-07-30 19:16:02 +04:00
public function testGetMetadataForSubclassWithTransientBaseClass ()
{
$class = $this -> _factory -> getMetadataFor ( 'Doctrine\Tests\ORM\Mapping\EntitySubClass' );
2011-12-20 01:56:19 +04:00
2009-07-30 19:16:02 +04:00
$this -> assertTrue ( empty ( $class -> subClasses ));
$this -> assertTrue ( empty ( $class -> parentClasses ));
$this -> assertTrue ( isset ( $class -> fieldMappings [ 'id' ]));
$this -> assertTrue ( isset ( $class -> fieldMappings [ 'name' ]));
}
2011-12-20 01:56:19 +04:00
2009-07-30 19:16:02 +04:00
public function testGetMetadataForSubclassWithMappedSuperclass ()
{
$class = $this -> _factory -> getMetadataFor ( 'Doctrine\Tests\ORM\Mapping\EntitySubClass2' );
2011-12-20 01:56:19 +04:00
2009-07-30 19:16:02 +04:00
$this -> assertTrue ( empty ( $class -> subClasses ));
$this -> assertTrue ( empty ( $class -> parentClasses ));
2011-12-20 01:56:19 +04:00
2009-07-30 19:16:02 +04:00
$this -> assertTrue ( isset ( $class -> fieldMappings [ 'mapped1' ]));
$this -> assertTrue ( isset ( $class -> fieldMappings [ 'mapped2' ]));
$this -> assertTrue ( isset ( $class -> fieldMappings [ 'id' ]));
$this -> assertTrue ( isset ( $class -> fieldMappings [ 'name' ]));
2011-12-20 01:56:19 +04:00
2009-07-30 19:16:02 +04:00
$this -> assertFalse ( isset ( $class -> fieldMappings [ 'mapped1' ][ 'inherited' ]));
$this -> assertFalse ( isset ( $class -> fieldMappings [ 'mapped2' ][ 'inherited' ]));
$this -> assertFalse ( isset ( $class -> fieldMappings [ 'transient' ]));
2011-12-20 01:56:19 +04:00
2009-07-30 19:16:02 +04:00
$this -> assertTrue ( isset ( $class -> associationMappings [ 'mappedRelated1' ]));
}
2011-12-20 01:56:19 +04:00
2011-09-08 19:55:55 +04:00
/**
* @ group DDC - 869
*/
2011-09-08 00:27:05 +04:00
public function testGetMetadataForSubclassWithMappedSuperclassWhithRepository ()
{
2011-09-08 01:29:43 +04:00
$class = $this -> _factory -> getMetadataFor ( 'Doctrine\Tests\Models\DDC869\DDC869CreditCardPayment' );
2011-12-20 01:56:19 +04:00
2011-09-08 00:27:05 +04:00
$this -> assertTrue ( isset ( $class -> fieldMappings [ 'id' ]));
2011-09-08 01:29:43 +04:00
$this -> assertTrue ( isset ( $class -> fieldMappings [ 'value' ]));
$this -> assertTrue ( isset ( $class -> fieldMappings [ 'creditCardNumber' ]));
$this -> assertEquals ( $class -> customRepositoryClassName , " Doctrine \T ests \ Models \ DDC869 \ DDC869PaymentRepository " );
2011-12-20 01:56:19 +04:00
2011-09-08 01:29:43 +04:00
$class = $this -> _factory -> getMetadataFor ( 'Doctrine\Tests\Models\DDC869\DDC869ChequePayment' );
2011-12-20 01:56:19 +04:00
2011-09-08 01:29:43 +04:00
$this -> assertTrue ( isset ( $class -> fieldMappings [ 'id' ]));
$this -> assertTrue ( isset ( $class -> fieldMappings [ 'value' ]));
$this -> assertTrue ( isset ( $class -> fieldMappings [ 'serialNumber' ]));
$this -> assertEquals ( $class -> customRepositoryClassName , " Doctrine \T ests \ Models \ DDC869 \ DDC869PaymentRepository " );
2011-12-20 01:56:19 +04:00
2011-09-08 19:55:55 +04:00
// override repositoryClass
$class = $this -> _factory -> getMetadataFor ( 'Doctrine\Tests\ORM\Mapping\SubclassWithRepository' );
2011-12-20 01:56:19 +04:00
2011-09-08 19:55:55 +04:00
$this -> assertTrue ( isset ( $class -> fieldMappings [ 'id' ]));
$this -> assertTrue ( isset ( $class -> fieldMappings [ 'value' ]));
$this -> assertEquals ( $class -> customRepositoryClassName , " Doctrine \ ORM \ EntityRepository " );
2011-09-08 00:27:05 +04:00
}
2010-04-13 02:49:19 +04:00
/**
* @ group DDC - 388
*/
public function testSerializationWithPrivateFieldsFromMappedSuperclass ()
{
$class = $this -> _factory -> getMetadataFor ( __NAMESPACE__ . '\\EntitySubClass2' );
$class2 = unserialize ( serialize ( $class ));
2012-01-02 18:57:32 +04:00
$class2 -> wakeupReflection ( new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService );
2010-04-13 02:49:19 +04:00
$this -> assertTrue ( isset ( $class2 -> reflFields [ 'mapped1' ]));
$this -> assertTrue ( isset ( $class2 -> reflFields [ 'mapped2' ]));
2010-04-14 19:07:08 +04:00
$this -> assertTrue ( isset ( $class2 -> reflFields [ 'mappedRelated1' ]));
2010-04-13 02:49:19 +04:00
}
2011-12-20 01:56:19 +04:00
2011-06-16 00:27:24 +04:00
/**
* @ group DDC - 1203
*/
public function testUnmappedSuperclassInHierachy ()
{
$class = $this -> _factory -> getMetadataFor ( __NAMESPACE__ . '\\HierachyD' );
2011-12-20 01:56:19 +04:00
2011-06-16 00:27:24 +04:00
$this -> assertTrue ( isset ( $class -> fieldMappings [ 'id' ]));
$this -> assertTrue ( isset ( $class -> fieldMappings [ 'a' ]));
$this -> assertTrue ( isset ( $class -> fieldMappings [ 'd' ]));
}
2011-12-20 01:56:19 +04:00
2011-06-16 00:27:24 +04:00
/**
* @ group DDC - 1204
*/
public function testUnmappedEntityInHierachy ()
{
2011-10-24 13:01:27 +04:00
$this -> setExpectedException ( 'Doctrine\ORM\Mapping\MappingException' , " Entity 'Doctrine \T ests \ ORM \ Mapping \ HierachyBEntity' has to be part of the discriminator map of 'Doctrine \T ests \ ORM \ Mapping \ HierachyBase' to be properly mapped in the inheritance hierachy. Alternatively you can make 'Doctrine \T ests \ ORM \ Mapping \ HierachyBEntity' an abstract class to avoid this exception from occuring. " );
2011-12-20 01:56:19 +04:00
2011-06-16 00:27:24 +04:00
$class = $this -> _factory -> getMetadataFor ( __NAMESPACE__ . '\\HierachyE' );
}
2011-12-20 01:56:19 +04:00
2011-06-17 01:00:59 +04:00
/**
* @ group DDC - 1204
* @ group DDC - 1203
*/
public function testMappedSuperclassWithId ()
{
$class = $this -> _factory -> getMetadataFor ( __NAMESPACE__ . '\\SuperclassEntity' );
2011-12-20 01:56:19 +04:00
2011-06-17 01:00:59 +04:00
$this -> assertTrue ( isset ( $class -> fieldMappings [ 'id' ]));
}
2011-12-20 01:56:19 +04:00
2011-06-25 11:57:15 +04:00
/**
* @ group DDC - 1156
* @ group DDC - 1218
*/
public function testGeneratedValueFromMappedSuperclass ()
{
$class = $this -> _factory -> getMetadataFor ( __NAMESPACE__ . '\\SuperclassEntity' );
/* @var $class ClassMetadataInfo */
2011-12-20 01:56:19 +04:00
2011-06-25 11:57:15 +04:00
$this -> assertInstanceOf ( 'Doctrine\ORM\Id\SequenceGenerator' , $class -> idGenerator );
$this -> assertEquals ( array ( 'allocationSize' => 1 , 'initialValue' => 10 , 'sequenceName' => 'foo' ), $class -> sequenceGeneratorDefinition );
}
2011-12-20 01:56:19 +04:00
2011-06-25 12:20:37 +04:00
/**
* @ group DDC - 1156
* @ group DDC - 1218
*/
public function testSequenceDefinitionInHierachyWithSandwichMappedSuperclass ()
{
$class = $this -> _factory -> getMetadataFor ( __NAMESPACE__ . '\\HierachyD' );
/* @var $class ClassMetadataInfo */
2011-12-20 01:56:19 +04:00
2011-06-25 12:20:37 +04:00
$this -> assertInstanceOf ( 'Doctrine\ORM\Id\SequenceGenerator' , $class -> idGenerator );
$this -> assertEquals ( array ( 'allocationSize' => 1 , 'initialValue' => 10 , 'sequenceName' => 'foo' ), $class -> sequenceGeneratorDefinition );
}
2011-12-20 01:56:19 +04:00
2011-06-25 12:20:37 +04:00
/**
* @ group DDC - 1156
* @ group DDC - 1218
*/
public function testMultipleMappedSuperclasses ()
{
$class = $this -> _factory -> getMetadataFor ( __NAMESPACE__ . '\\MediumSuperclassEntity' );
/* @var $class ClassMetadataInfo */
2011-12-20 01:56:19 +04:00
2011-06-25 12:20:37 +04:00
$this -> assertInstanceOf ( 'Doctrine\ORM\Id\SequenceGenerator' , $class -> idGenerator );
$this -> assertEquals ( array ( 'allocationSize' => 1 , 'initialValue' => 10 , 'sequenceName' => 'foo' ), $class -> sequenceGeneratorDefinition );
}
2009-07-30 19:16:02 +04:00
}
class TransientBaseClass {
private $transient1 ;
private $transient2 ;
}
/** @Entity */
class EntitySubClass extends TransientBaseClass
{
/** @Id @Column(type="integer") */
private $id ;
/** @Column(type="string") */
private $name ;
}
/** @MappedSuperclass */
class MappedSuperclassBase {
/** @Column(type="integer") */
private $mapped1 ;
/** @Column(type="string") */
private $mapped2 ;
/**
* @ OneToOne ( targetEntity = " MappedSuperclassRelated1 " )
* @ JoinColumn ( name = " related1_id " , referencedColumnName = " id " )
*/
private $mappedRelated1 ;
private $transient ;
}
2011-10-15 19:38:55 +04:00
class MappedSuperclassRelated1 {}
2009-07-30 19:16:02 +04:00
/** @Entity */
class EntitySubClass2 extends MappedSuperclassBase {
/** @Id @Column(type="integer") */
private $id ;
/** @Column(type="string") */
private $name ;
}
2011-06-16 00:27:24 +04:00
/**
* @ Entity
* @ InheritanceType ( " SINGLE_TABLE " )
* @ DiscriminatorColumn ( name = " type " , type = " string " , length = 20 )
* @ DiscriminatorMap ({
* " c " = " HierachyC " ,
* " d " = " HierachyD " ,
* " e " = " HierachyE "
* })
*/
abstract class HierachyBase
{
/**
2011-06-25 12:20:37 +04:00
* @ Column ( type = " integer " ) @ Id @ GeneratedValue ( strategy = " SEQUENCE " )
2011-09-13 07:59:24 +04:00
* @ SequenceGenerator ( sequenceName = " foo " , initialValue = 10 )
2011-06-16 00:27:24 +04:00
* @ var int
*/
public $id ;
}
/**
* @ MappedSuperclass
*/
abstract class HierachyASuperclass extends HierachyBase
{
/** @Column(type="string") */
public $a ;
}
/**
* @ Entity
*/
2011-06-30 22:57:29 +04:00
class HierachyBEntity extends HierachyBase
2011-06-16 00:27:24 +04:00
{
/** @Column(type="string") */
public $b ;
}
/**
* @ Entity
*/
class HierachyC extends HierachyBase
{
/** @Column(type="string") */
public $c ;
}
/**
* @ Entity
*/
class HierachyD extends HierachyASuperclass
{
/** @Column(type="string") */
public $d ;
}
/**
* @ Entity
*/
class HierachyE extends HierachyBEntity
{
/** @Column(type="string") */
public $e ;
2011-06-17 01:00:59 +04:00
}
/**
* @ Entity
*/
class SuperclassEntity extends SuperclassBase
{
}
/**
* @ MappedSuperclass
*/
abstract class SuperclassBase
{
/**
2011-12-20 01:56:19 +04:00
* @ Column ( type = " integer " ) @ Id @ GeneratedValue ( strategy = " SEQUENCE " )
2011-09-13 07:59:24 +04:00
* @ SequenceGenerator ( sequenceName = " foo " , initialValue = 10 )
2011-06-17 01:00:59 +04:00
* @ var int
*/
public $id ;
2011-06-25 12:20:37 +04:00
}
/**
* @ MappedSuperclass
*/
abstract class MediumSuperclassBase extends SuperclassBase
{
2011-12-20 01:56:19 +04:00
2011-06-25 12:20:37 +04:00
}
/**
* @ Entity
*/
class MediumSuperclassEntity extends MediumSuperclassBase
{
2011-12-20 01:56:19 +04:00
2011-09-08 00:27:05 +04:00
}
/**
2011-09-08 19:55:55 +04:00
* @ Entity ( repositoryClass = " Doctrine \ ORM \ EntityRepository " )
2011-09-08 00:27:05 +04:00
*/
2011-09-08 19:55:55 +04:00
class SubclassWithRepository extends \Doctrine\Tests\Models\DDC869\DDC869Payment
2011-09-08 00:27:05 +04:00
{
2011-12-20 01:56:19 +04:00
2012-01-02 18:57:32 +04:00
}