BasicInheritanceMappingTest.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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-869
  44. */
  45. public function testGetMetadataForSubclassWithMappedSuperclassWhithRepository()
  46. {
  47. $class = $this->_factory->getMetadataFor('Doctrine\Tests\Models\DDC869\DDC869CreditCardPayment');
  48. $this->assertTrue(isset($class->fieldMappings['id']));
  49. $this->assertTrue(isset($class->fieldMappings['value']));
  50. $this->assertTrue(isset($class->fieldMappings['creditCardNumber']));
  51. $this->assertEquals($class->customRepositoryClassName, "Doctrine\Tests\Models\DDC869\DDC869PaymentRepository");
  52. $class = $this->_factory->getMetadataFor('Doctrine\Tests\Models\DDC869\DDC869ChequePayment');
  53. $this->assertTrue(isset($class->fieldMappings['id']));
  54. $this->assertTrue(isset($class->fieldMappings['value']));
  55. $this->assertTrue(isset($class->fieldMappings['serialNumber']));
  56. $this->assertEquals($class->customRepositoryClassName, "Doctrine\Tests\Models\DDC869\DDC869PaymentRepository");
  57. // override repositoryClass
  58. $class = $this->_factory->getMetadataFor('Doctrine\Tests\ORM\Mapping\SubclassWithRepository');
  59. $this->assertTrue(isset($class->fieldMappings['id']));
  60. $this->assertTrue(isset($class->fieldMappings['value']));
  61. $this->assertEquals($class->customRepositoryClassName, "Doctrine\ORM\EntityRepository");
  62. }
  63. /**
  64. * @group DDC-388
  65. */
  66. public function testSerializationWithPrivateFieldsFromMappedSuperclass()
  67. {
  68. $class = $this->_factory->getMetadataFor(__NAMESPACE__ . '\\EntitySubClass2');
  69. $class2 = unserialize(serialize($class));
  70. $class2->wakeupReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService);
  71. $this->assertTrue(isset($class2->reflFields['mapped1']));
  72. $this->assertTrue(isset($class2->reflFields['mapped2']));
  73. $this->assertTrue(isset($class2->reflFields['mappedRelated1']));
  74. }
  75. /**
  76. * @group DDC-1203
  77. */
  78. public function testUnmappedSuperclassInHierachy()
  79. {
  80. $class = $this->_factory->getMetadataFor(__NAMESPACE__ . '\\HierachyD');
  81. $this->assertTrue(isset($class->fieldMappings['id']));
  82. $this->assertTrue(isset($class->fieldMappings['a']));
  83. $this->assertTrue(isset($class->fieldMappings['d']));
  84. }
  85. /**
  86. * @group DDC-1204
  87. */
  88. public function testUnmappedEntityInHierachy()
  89. {
  90. $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.");
  91. $class = $this->_factory->getMetadataFor(__NAMESPACE__ . '\\HierachyE');
  92. }
  93. /**
  94. * @group DDC-1204
  95. * @group DDC-1203
  96. */
  97. public function testMappedSuperclassWithId()
  98. {
  99. $class = $this->_factory->getMetadataFor(__NAMESPACE__ . '\\SuperclassEntity');
  100. $this->assertTrue(isset($class->fieldMappings['id']));
  101. }
  102. /**
  103. * @group DDC-1156
  104. * @group DDC-1218
  105. */
  106. public function testGeneratedValueFromMappedSuperclass()
  107. {
  108. $class = $this->_factory->getMetadataFor(__NAMESPACE__ . '\\SuperclassEntity');
  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. * @group DDC-1156
  115. * @group DDC-1218
  116. */
  117. public function testSequenceDefinitionInHierachyWithSandwichMappedSuperclass()
  118. {
  119. $class = $this->_factory->getMetadataFor(__NAMESPACE__ . '\\HierachyD');
  120. /* @var $class ClassMetadataInfo */
  121. $this->assertInstanceOf('Doctrine\ORM\Id\SequenceGenerator', $class->idGenerator);
  122. $this->assertEquals(array('allocationSize' => 1, 'initialValue' => 10, 'sequenceName' => 'foo'), $class->sequenceGeneratorDefinition);
  123. }
  124. /**
  125. * @group DDC-1156
  126. * @group DDC-1218
  127. */
  128. public function testMultipleMappedSuperclasses()
  129. {
  130. $class = $this->_factory->getMetadataFor(__NAMESPACE__ . '\\MediumSuperclassEntity');
  131. /* @var $class ClassMetadataInfo */
  132. $this->assertInstanceOf('Doctrine\ORM\Id\SequenceGenerator', $class->idGenerator);
  133. $this->assertEquals(array('allocationSize' => 1, 'initialValue' => 10, 'sequenceName' => 'foo'), $class->sequenceGeneratorDefinition);
  134. }
  135. }
  136. class TransientBaseClass {
  137. private $transient1;
  138. private $transient2;
  139. }
  140. /** @Entity */
  141. class EntitySubClass extends TransientBaseClass
  142. {
  143. /** @Id @Column(type="integer") */
  144. private $id;
  145. /** @Column(type="string") */
  146. private $name;
  147. }
  148. /** @MappedSuperclass */
  149. class MappedSuperclassBase {
  150. /** @Column(type="integer") */
  151. private $mapped1;
  152. /** @Column(type="string") */
  153. private $mapped2;
  154. /**
  155. * @OneToOne(targetEntity="MappedSuperclassRelated1")
  156. * @JoinColumn(name="related1_id", referencedColumnName="id")
  157. */
  158. private $mappedRelated1;
  159. private $transient;
  160. }
  161. class MappedSuperclassRelated1 {}
  162. /** @Entity */
  163. class EntitySubClass2 extends MappedSuperclassBase {
  164. /** @Id @Column(type="integer") */
  165. private $id;
  166. /** @Column(type="string") */
  167. private $name;
  168. }
  169. /**
  170. * @Entity
  171. * @InheritanceType("SINGLE_TABLE")
  172. * @DiscriminatorColumn(name="type", type="string", length=20)
  173. * @DiscriminatorMap({
  174. * "c" = "HierachyC",
  175. * "d" = "HierachyD",
  176. * "e" = "HierachyE"
  177. * })
  178. */
  179. abstract class HierachyBase
  180. {
  181. /**
  182. * @Column(type="integer") @Id @GeneratedValue(strategy="SEQUENCE")
  183. * @SequenceGenerator(sequenceName="foo", initialValue=10)
  184. * @var int
  185. */
  186. public $id;
  187. }
  188. /**
  189. * @MappedSuperclass
  190. */
  191. abstract class HierachyASuperclass extends HierachyBase
  192. {
  193. /** @Column(type="string") */
  194. public $a;
  195. }
  196. /**
  197. * @Entity
  198. */
  199. class HierachyBEntity extends HierachyBase
  200. {
  201. /** @Column(type="string") */
  202. public $b;
  203. }
  204. /**
  205. * @Entity
  206. */
  207. class HierachyC extends HierachyBase
  208. {
  209. /** @Column(type="string") */
  210. public $c;
  211. }
  212. /**
  213. * @Entity
  214. */
  215. class HierachyD extends HierachyASuperclass
  216. {
  217. /** @Column(type="string") */
  218. public $d;
  219. }
  220. /**
  221. * @Entity
  222. */
  223. class HierachyE extends HierachyBEntity
  224. {
  225. /** @Column(type="string") */
  226. public $e;
  227. }
  228. /**
  229. * @Entity
  230. */
  231. class SuperclassEntity extends SuperclassBase
  232. {
  233. }
  234. /**
  235. * @MappedSuperclass
  236. */
  237. abstract class SuperclassBase
  238. {
  239. /**
  240. * @Column(type="integer") @Id @GeneratedValue(strategy="SEQUENCE")
  241. * @SequenceGenerator(sequenceName="foo", initialValue=10)
  242. * @var int
  243. */
  244. public $id;
  245. }
  246. /**
  247. * @MappedSuperclass
  248. */
  249. abstract class MediumSuperclassBase extends SuperclassBase
  250. {
  251. }
  252. /**
  253. * @Entity
  254. */
  255. class MediumSuperclassEntity extends MediumSuperclassBase
  256. {
  257. }
  258. /**
  259. * @Entity(repositoryClass = "Doctrine\ORM\EntityRepository")
  260. */
  261. class SubclassWithRepository extends \Doctrine\Tests\Models\DDC869\DDC869Payment
  262. {
  263. }