BasicInheritanceMappingTest.php 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <?php
  2. namespace Doctrine\Tests\ORM\Mapping;
  3. use Doctrine\ORM\Mapping\ClassMetadataFactory;
  4. use Doctrine\ORM\Tools\SchemaTool;
  5. require_once __DIR__ . '/../../TestInit.php';
  6. class BasicInheritanceMappingTest extends \Doctrine\Tests\OrmTestCase
  7. {
  8. private $_factory;
  9. protected function setUp() {
  10. $this->_factory = new ClassMetadataFactory();
  11. $this->_factory->setEntityManager($this->_getTestEntityManager());
  12. }
  13. /**
  14. * @expectedException Doctrine\ORM\Mapping\MappingException
  15. */
  16. public function testGetMetadataForTransientClassThrowsException()
  17. {
  18. $this->_factory->getMetadataFor('Doctrine\Tests\ORM\Mapping\TransientBaseClass');
  19. }
  20. public function testGetMetadataForSubclassWithTransientBaseClass()
  21. {
  22. $class = $this->_factory->getMetadataFor('Doctrine\Tests\ORM\Mapping\EntitySubClass');
  23. $this->assertTrue(empty($class->subClasses));
  24. $this->assertTrue(empty($class->parentClasses));
  25. $this->assertTrue(isset($class->fieldMappings['id']));
  26. $this->assertTrue(isset($class->fieldMappings['name']));
  27. }
  28. public function testGetMetadataForSubclassWithMappedSuperclass()
  29. {
  30. $class = $this->_factory->getMetadataFor('Doctrine\Tests\ORM\Mapping\EntitySubClass2');
  31. $this->assertTrue(empty($class->subClasses));
  32. $this->assertTrue(empty($class->parentClasses));
  33. $this->assertTrue(isset($class->fieldMappings['mapped1']));
  34. $this->assertTrue(isset($class->fieldMappings['mapped2']));
  35. $this->assertTrue(isset($class->fieldMappings['id']));
  36. $this->assertTrue(isset($class->fieldMappings['name']));
  37. $this->assertFalse(isset($class->fieldMappings['mapped1']['inherited']));
  38. $this->assertFalse(isset($class->fieldMappings['mapped2']['inherited']));
  39. $this->assertFalse(isset($class->fieldMappings['transient']));
  40. $this->assertTrue(isset($class->associationMappings['mappedRelated1']));
  41. }
  42. /**
  43. * @group DDC-388
  44. */
  45. public function testSerializationWithPrivateFieldsFromMappedSuperclass()
  46. {
  47. $class = $this->_factory->getMetadataFor(__NAMESPACE__ . '\\EntitySubClass2');
  48. $class2 = unserialize(serialize($class));
  49. $this->assertTrue(isset($class2->reflFields['mapped1']));
  50. $this->assertTrue(isset($class2->reflFields['mapped2']));
  51. $this->assertTrue(isset($class2->reflFields['mappedRelated1']));
  52. }
  53. /**
  54. * @group DDC-1203
  55. */
  56. public function testUnmappedSuperclassInHierachy()
  57. {
  58. $class = $this->_factory->getMetadataFor(__NAMESPACE__ . '\\HierachyD');
  59. $this->assertTrue(isset($class->fieldMappings['id']));
  60. $this->assertTrue(isset($class->fieldMappings['a']));
  61. $this->assertTrue(isset($class->fieldMappings['d']));
  62. }
  63. /**
  64. * @group DDC-1204
  65. */
  66. public function testUnmappedEntityInHierachy()
  67. {
  68. $this->setExpectedException('Doctrine\ORM\Mapping\MappingException', "Entity 'Doctrine\Tests\ORM\Mapping\HierachyBEntity' has to be part of the descriminator 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.");
  69. $class = $this->_factory->getMetadataFor(__NAMESPACE__ . '\\HierachyE');
  70. }
  71. /**
  72. * @group DDC-1204
  73. * @group DDC-1203
  74. */
  75. public function testMappedSuperclassWithId()
  76. {
  77. $class = $this->_factory->getMetadataFor(__NAMESPACE__ . '\\SuperclassEntity');
  78. $this->assertTrue(isset($class->fieldMappings['id']));
  79. }
  80. /**
  81. * @group DDC-1156
  82. * @group DDC-1218
  83. */
  84. public function testGeneratedValueFromMappedSuperclass()
  85. {
  86. $class = $this->_factory->getMetadataFor(__NAMESPACE__ . '\\SuperclassEntity');
  87. /* @var $class ClassMetadataInfo */
  88. $this->assertInstanceOf('Doctrine\ORM\Id\SequenceGenerator', $class->idGenerator);
  89. $this->assertEquals(array('allocationSize' => 1, 'initialValue' => 10, 'sequenceName' => 'foo'), $class->sequenceGeneratorDefinition);
  90. }
  91. /**
  92. * @group DDC-1156
  93. * @group DDC-1218
  94. */
  95. public function testSequenceDefinitionInHierachyWithSandwichMappedSuperclass()
  96. {
  97. $class = $this->_factory->getMetadataFor(__NAMESPACE__ . '\\HierachyD');
  98. /* @var $class ClassMetadataInfo */
  99. $this->assertInstanceOf('Doctrine\ORM\Id\SequenceGenerator', $class->idGenerator);
  100. $this->assertEquals(array('allocationSize' => 1, 'initialValue' => 10, 'sequenceName' => 'foo'), $class->sequenceGeneratorDefinition);
  101. }
  102. /**
  103. * @group DDC-1156
  104. * @group DDC-1218
  105. */
  106. public function testMultipleMappedSuperclasses()
  107. {
  108. $class = $this->_factory->getMetadataFor(__NAMESPACE__ . '\\MediumSuperclassEntity');
  109. /* @var $class ClassMetadataInfo */
  110. $this->assertInstanceOf('Doctrine\ORM\Id\SequenceGenerator', $class->idGenerator);
  111. $this->assertEquals(array('allocationSize' => 1, 'initialValue' => 10, 'sequenceName' => 'foo'), $class->sequenceGeneratorDefinition);
  112. }
  113. }
  114. class TransientBaseClass {
  115. private $transient1;
  116. private $transient2;
  117. }
  118. /** @Entity */
  119. class EntitySubClass extends TransientBaseClass
  120. {
  121. /** @Id @Column(type="integer") */
  122. private $id;
  123. /** @Column(type="string") */
  124. private $name;
  125. }
  126. /** @MappedSuperclass */
  127. class MappedSuperclassBase {
  128. /** @Column(type="integer") */
  129. private $mapped1;
  130. /** @Column(type="string") */
  131. private $mapped2;
  132. /**
  133. * @OneToOne(targetEntity="MappedSuperclassRelated1")
  134. * @JoinColumn(name="related1_id", referencedColumnName="id")
  135. */
  136. private $mappedRelated1;
  137. private $transient;
  138. }
  139. /** @Entity */
  140. class EntitySubClass2 extends MappedSuperclassBase {
  141. /** @Id @Column(type="integer") */
  142. private $id;
  143. /** @Column(type="string") */
  144. private $name;
  145. }
  146. /**
  147. * @Entity
  148. * @InheritanceType("SINGLE_TABLE")
  149. * @DiscriminatorColumn(name="type", type="string", length=20)
  150. * @DiscriminatorMap({
  151. * "c" = "HierachyC",
  152. * "d" = "HierachyD",
  153. * "e" = "HierachyE"
  154. * })
  155. */
  156. abstract class HierachyBase
  157. {
  158. /**
  159. * @Column(type="integer") @Id @GeneratedValue(strategy="SEQUENCE")
  160. * @SequenceGenerator(sequenceName="foo", initialValue="10")
  161. * @var int
  162. */
  163. public $id;
  164. }
  165. /**
  166. * @MappedSuperclass
  167. */
  168. abstract class HierachyASuperclass extends HierachyBase
  169. {
  170. /** @Column(type="string") */
  171. public $a;
  172. }
  173. /**
  174. * @Entity
  175. */
  176. class HierachyBEntity extends HierachyBase
  177. {
  178. /** @Column(type="string") */
  179. public $b;
  180. }
  181. /**
  182. * @Entity
  183. */
  184. class HierachyC extends HierachyBase
  185. {
  186. /** @Column(type="string") */
  187. public $c;
  188. }
  189. /**
  190. * @Entity
  191. */
  192. class HierachyD extends HierachyASuperclass
  193. {
  194. /** @Column(type="string") */
  195. public $d;
  196. }
  197. /**
  198. * @Entity
  199. */
  200. class HierachyE extends HierachyBEntity
  201. {
  202. /** @Column(type="string") */
  203. public $e;
  204. }
  205. /**
  206. * @Entity
  207. */
  208. class SuperclassEntity extends SuperclassBase
  209. {
  210. }
  211. /**
  212. * @MappedSuperclass
  213. */
  214. abstract class SuperclassBase
  215. {
  216. /**
  217. * @Column(type="integer") @Id @GeneratedValue(strategy="SEQUENCE")
  218. * @SequenceGenerator(sequenceName="foo", initialValue="10")
  219. * @var int
  220. */
  221. public $id;
  222. }
  223. /**
  224. * @MappedSuperclass
  225. */
  226. abstract class MediumSuperclassBase extends SuperclassBase
  227. {
  228. }
  229. /**
  230. * @Entity
  231. */
  232. class MediumSuperclassEntity extends MediumSuperclassBase
  233. {
  234. }