3 namespace Doctrine\Tests\ORM\Mapping;
5 use Doctrine\ORM\Mapping\ClassMetadataFactory;
6 use Doctrine\ORM\Tools\SchemaTool;
8 require_once __DIR__ . '/../../TestInit.php';
10 class BasicInheritanceMappingTest extends \Doctrine\Tests\OrmTestCase
14 protected function setUp() {
15 $this->_factory = new ClassMetadataFactory();
16 $this->_factory->setEntityManager($this->_getTestEntityManager());
20 * @expectedException Doctrine\ORM\Mapping\MappingException
22 public function testGetMetadataForTransientClassThrowsException()
24 $this->_factory->getMetadataFor('Doctrine\Tests\ORM\Mapping\TransientBaseClass');
27 public function testGetMetadataForSubclassWithTransientBaseClass()
29 $class = $this->_factory->getMetadataFor('Doctrine\Tests\ORM\Mapping\EntitySubClass');
31 $this->assertTrue(empty($class->subClasses));
32 $this->assertTrue(empty($class->parentClasses));
33 $this->assertTrue(isset($class->fieldMappings['id']));
34 $this->assertTrue(isset($class->fieldMappings['name']));
37 public function testGetMetadataForSubclassWithMappedSuperclass()
39 $class = $this->_factory->getMetadataFor('Doctrine\Tests\ORM\Mapping\EntitySubClass2');
41 $this->assertTrue(empty($class->subClasses));
42 $this->assertTrue(empty($class->parentClasses));
44 $this->assertTrue(isset($class->fieldMappings['mapped1']));
45 $this->assertTrue(isset($class->fieldMappings['mapped2']));
46 $this->assertTrue(isset($class->fieldMappings['id']));
47 $this->assertTrue(isset($class->fieldMappings['name']));
49 $this->assertFalse(isset($class->fieldMappings['mapped1']['inherited']));
50 $this->assertFalse(isset($class->fieldMappings['mapped2']['inherited']));
51 $this->assertFalse(isset($class->fieldMappings['transient']));
53 $this->assertTrue(isset($class->associationMappings['mappedRelated1']));
59 public function testGetMetadataForSubclassWithMappedSuperclassWhithRepository()
61 $class = $this->_factory->getMetadataFor('Doctrine\Tests\Models\DDC869\DDC869CreditCardPayment');
63 $this->assertTrue(isset($class->fieldMappings['id']));
64 $this->assertTrue(isset($class->fieldMappings['value']));
65 $this->assertTrue(isset($class->fieldMappings['creditCardNumber']));
66 $this->assertEquals($class->customRepositoryClassName, "Doctrine\Tests\Models\DDC869\DDC869PaymentRepository");
69 $class = $this->_factory->getMetadataFor('Doctrine\Tests\Models\DDC869\DDC869ChequePayment');
71 $this->assertTrue(isset($class->fieldMappings['id']));
72 $this->assertTrue(isset($class->fieldMappings['value']));
73 $this->assertTrue(isset($class->fieldMappings['serialNumber']));
74 $this->assertEquals($class->customRepositoryClassName, "Doctrine\Tests\Models\DDC869\DDC869PaymentRepository");
77 // override repositoryClass
78 $class = $this->_factory->getMetadataFor('Doctrine\Tests\ORM\Mapping\SubclassWithRepository');
80 $this->assertTrue(isset($class->fieldMappings['id']));
81 $this->assertTrue(isset($class->fieldMappings['value']));
82 $this->assertEquals($class->customRepositoryClassName, "Doctrine\ORM\EntityRepository");
88 public function testSerializationWithPrivateFieldsFromMappedSuperclass()
91 $class = $this->_factory->getMetadataFor(__NAMESPACE__ . '\\EntitySubClass2');
93 $class2 = unserialize(serialize($class));
94 $class2->wakeupReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService);
96 $this->assertTrue(isset($class2->reflFields['mapped1']));
97 $this->assertTrue(isset($class2->reflFields['mapped2']));
98 $this->assertTrue(isset($class2->reflFields['mappedRelated1']));
104 public function testUnmappedSuperclassInHierachy()
106 $class = $this->_factory->getMetadataFor(__NAMESPACE__ . '\\HierachyD');
108 $this->assertTrue(isset($class->fieldMappings['id']));
109 $this->assertTrue(isset($class->fieldMappings['a']));
110 $this->assertTrue(isset($class->fieldMappings['d']));
116 public function testUnmappedEntityInHierachy()
118 $this->setExpectedException('Doctrine\ORM\Mapping\MappingException', "Entity 'Doctrine\Tests\ORM\Mapping\HierachyBEntity' has to be part of the discriminator map of 'Doctrine\Tests\ORM\Mapping\HierachyBase' to be properly mapped in the inheritance hierachy. Alternatively you can make 'Doctrine\Tests\ORM\Mapping\HierachyBEntity' an abstract class to avoid this exception from occuring.");
120 $class = $this->_factory->getMetadataFor(__NAMESPACE__ . '\\HierachyE');
127 public function testMappedSuperclassWithId()
129 $class = $this->_factory->getMetadataFor(__NAMESPACE__ . '\\SuperclassEntity');
131 $this->assertTrue(isset($class->fieldMappings['id']));
138 public function testGeneratedValueFromMappedSuperclass()
140 $class = $this->_factory->getMetadataFor(__NAMESPACE__ . '\\SuperclassEntity');
141 /* @var $class ClassMetadataInfo */
143 $this->assertInstanceOf('Doctrine\ORM\Id\SequenceGenerator', $class->idGenerator);
144 $this->assertEquals(array('allocationSize' => 1, 'initialValue' => 10, 'sequenceName' => 'foo'), $class->sequenceGeneratorDefinition);
151 public function testSequenceDefinitionInHierachyWithSandwichMappedSuperclass()
153 $class = $this->_factory->getMetadataFor(__NAMESPACE__ . '\\HierachyD');
154 /* @var $class ClassMetadataInfo */
156 $this->assertInstanceOf('Doctrine\ORM\Id\SequenceGenerator', $class->idGenerator);
157 $this->assertEquals(array('allocationSize' => 1, 'initialValue' => 10, 'sequenceName' => 'foo'), $class->sequenceGeneratorDefinition);
164 public function testMultipleMappedSuperclasses()
166 $class = $this->_factory->getMetadataFor(__NAMESPACE__ . '\\MediumSuperclassEntity');
167 /* @var $class ClassMetadataInfo */
169 $this->assertInstanceOf('Doctrine\ORM\Id\SequenceGenerator', $class->idGenerator);
170 $this->assertEquals(array('allocationSize' => 1, 'initialValue' => 10, 'sequenceName' => 'foo'), $class->sequenceGeneratorDefinition);
174 class TransientBaseClass {
180 class EntitySubClass extends TransientBaseClass
182 /** @Id @Column(type="integer") */
184 /** @Column(type="string") */
188 /** @MappedSuperclass */
189 class MappedSuperclassBase {
190 /** @Column(type="integer") */
192 /** @Column(type="string") */
195 * @OneToOne(targetEntity="MappedSuperclassRelated1")
196 * @JoinColumn(name="related1_id", referencedColumnName="id")
198 private $mappedRelated1;
201 class MappedSuperclassRelated1 {}
204 class EntitySubClass2 extends MappedSuperclassBase {
205 /** @Id @Column(type="integer") */
207 /** @Column(type="string") */
213 * @InheritanceType("SINGLE_TABLE")
214 * @DiscriminatorColumn(name="type", type="string", length=20)
215 * @DiscriminatorMap({
221 abstract class HierachyBase
224 * @Column(type="integer") @Id @GeneratedValue(strategy="SEQUENCE")
225 * @SequenceGenerator(sequenceName="foo", initialValue=10)
234 abstract class HierachyASuperclass extends HierachyBase
236 /** @Column(type="string") */
243 class HierachyBEntity extends HierachyBase
245 /** @Column(type="string") */
252 class HierachyC extends HierachyBase
254 /** @Column(type="string") */
261 class HierachyD extends HierachyASuperclass
263 /** @Column(type="string") */
270 class HierachyE extends HierachyBEntity
272 /** @Column(type="string") */
279 class SuperclassEntity extends SuperclassBase
287 abstract class SuperclassBase
290 * @Column(type="integer") @Id @GeneratedValue(strategy="SEQUENCE")
291 * @SequenceGenerator(sequenceName="foo", initialValue=10)
300 abstract class MediumSuperclassBase extends SuperclassBase
308 class MediumSuperclassEntity extends MediumSuperclassBase
314 * @Entity(repositoryClass = "Doctrine\ORM\EntityRepository")
316 class SubclassWithRepository extends \Doctrine\Tests\Models\DDC869\DDC869Payment