Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / tests / Doctrine / Tests / ORM / Mapping / BasicInheritanceMappingTest.php
1 <?php
2
3 namespace Doctrine\Tests\ORM\Mapping;
4
5 use Doctrine\ORM\Mapping\ClassMetadataFactory;
6 use Doctrine\ORM\Tools\SchemaTool;
7
8 require_once __DIR__ . '/../../TestInit.php';
9
10 class BasicInheritanceMappingTest extends \Doctrine\Tests\OrmTestCase
11 {
12     private $_factory;
13
14     protected function setUp() {
15         $this->_factory = new ClassMetadataFactory();
16         $this->_factory->setEntityManager($this->_getTestEntityManager());
17     }
18
19     /**
20      * @expectedException Doctrine\ORM\Mapping\MappingException
21      */
22     public function testGetMetadataForTransientClassThrowsException()
23     {
24         $this->_factory->getMetadataFor('Doctrine\Tests\ORM\Mapping\TransientBaseClass');
25     }
26
27     public function testGetMetadataForSubclassWithTransientBaseClass()
28     {
29         $class = $this->_factory->getMetadataFor('Doctrine\Tests\ORM\Mapping\EntitySubClass');
30
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']));
35     }
36
37     public function testGetMetadataForSubclassWithMappedSuperclass()
38     {
39         $class = $this->_factory->getMetadataFor('Doctrine\Tests\ORM\Mapping\EntitySubClass2');
40
41         $this->assertTrue(empty($class->subClasses));
42         $this->assertTrue(empty($class->parentClasses));
43
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']));
48
49         $this->assertFalse(isset($class->fieldMappings['mapped1']['inherited']));
50         $this->assertFalse(isset($class->fieldMappings['mapped2']['inherited']));
51         $this->assertFalse(isset($class->fieldMappings['transient']));
52
53         $this->assertTrue(isset($class->associationMappings['mappedRelated1']));
54     }
55
56     /**
57      * @group DDC-869
58      */
59     public function testGetMetadataForSubclassWithMappedSuperclassWhithRepository()
60     {
61         $class = $this->_factory->getMetadataFor('Doctrine\Tests\Models\DDC869\DDC869CreditCardPayment');
62
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");
67
68
69         $class = $this->_factory->getMetadataFor('Doctrine\Tests\Models\DDC869\DDC869ChequePayment');
70
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");
75
76
77         // override repositoryClass
78         $class = $this->_factory->getMetadataFor('Doctrine\Tests\ORM\Mapping\SubclassWithRepository');
79
80         $this->assertTrue(isset($class->fieldMappings['id']));
81         $this->assertTrue(isset($class->fieldMappings['value']));
82         $this->assertEquals($class->customRepositoryClassName, "Doctrine\ORM\EntityRepository");
83     }
84
85     /**
86      * @group DDC-388
87      */
88     public function testSerializationWithPrivateFieldsFromMappedSuperclass()
89     {
90
91         $class = $this->_factory->getMetadataFor(__NAMESPACE__ . '\\EntitySubClass2');
92
93         $class2 = unserialize(serialize($class));
94         $class2->wakeupReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService);
95
96         $this->assertTrue(isset($class2->reflFields['mapped1']));
97         $this->assertTrue(isset($class2->reflFields['mapped2']));
98         $this->assertTrue(isset($class2->reflFields['mappedRelated1']));
99     }
100
101     /**
102      * @group DDC-1203
103      */
104     public function testUnmappedSuperclassInHierachy()
105     {
106         $class = $this->_factory->getMetadataFor(__NAMESPACE__ . '\\HierachyD');
107
108         $this->assertTrue(isset($class->fieldMappings['id']));
109         $this->assertTrue(isset($class->fieldMappings['a']));
110         $this->assertTrue(isset($class->fieldMappings['d']));
111     }
112
113     /**
114      * @group DDC-1204
115      */
116     public function testUnmappedEntityInHierachy()
117     {
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.");
119
120         $class = $this->_factory->getMetadataFor(__NAMESPACE__ . '\\HierachyE');
121     }
122
123     /**
124      * @group DDC-1204
125      * @group DDC-1203
126      */
127     public function testMappedSuperclassWithId()
128     {
129         $class = $this->_factory->getMetadataFor(__NAMESPACE__ . '\\SuperclassEntity');
130
131         $this->assertTrue(isset($class->fieldMappings['id']));
132     }
133
134     /**
135      * @group DDC-1156
136      * @group DDC-1218
137      */
138     public function testGeneratedValueFromMappedSuperclass()
139     {
140         $class = $this->_factory->getMetadataFor(__NAMESPACE__ . '\\SuperclassEntity');
141         /* @var $class ClassMetadataInfo */
142
143         $this->assertInstanceOf('Doctrine\ORM\Id\SequenceGenerator', $class->idGenerator);
144         $this->assertEquals(array('allocationSize' => 1, 'initialValue' => 10, 'sequenceName' => 'foo'), $class->sequenceGeneratorDefinition);
145     }
146
147     /**
148      * @group DDC-1156
149      * @group DDC-1218
150      */
151     public function testSequenceDefinitionInHierachyWithSandwichMappedSuperclass()
152     {
153         $class = $this->_factory->getMetadataFor(__NAMESPACE__ . '\\HierachyD');
154         /* @var $class ClassMetadataInfo */
155
156         $this->assertInstanceOf('Doctrine\ORM\Id\SequenceGenerator', $class->idGenerator);
157         $this->assertEquals(array('allocationSize' => 1, 'initialValue' => 10, 'sequenceName' => 'foo'), $class->sequenceGeneratorDefinition);
158     }
159
160     /**
161      * @group DDC-1156
162      * @group DDC-1218
163      */
164     public function testMultipleMappedSuperclasses()
165     {
166         $class = $this->_factory->getMetadataFor(__NAMESPACE__ . '\\MediumSuperclassEntity');
167         /* @var $class ClassMetadataInfo */
168
169         $this->assertInstanceOf('Doctrine\ORM\Id\SequenceGenerator', $class->idGenerator);
170         $this->assertEquals(array('allocationSize' => 1, 'initialValue' => 10, 'sequenceName' => 'foo'), $class->sequenceGeneratorDefinition);
171     }
172 }
173
174 class TransientBaseClass {
175     private $transient1;
176     private $transient2;
177 }
178
179 /** @Entity */
180 class EntitySubClass extends TransientBaseClass
181 {
182     /** @Id @Column(type="integer") */
183     private $id;
184     /** @Column(type="string") */
185     private $name;
186 }
187
188 /** @MappedSuperclass */
189 class MappedSuperclassBase {
190     /** @Column(type="integer") */
191     private $mapped1;
192     /** @Column(type="string") */
193     private $mapped2;
194     /**
195      * @OneToOne(targetEntity="MappedSuperclassRelated1")
196      * @JoinColumn(name="related1_id", referencedColumnName="id")
197      */
198     private $mappedRelated1;
199     private $transient;
200 }
201 class MappedSuperclassRelated1 {}
202
203 /** @Entity */
204 class EntitySubClass2 extends MappedSuperclassBase {
205     /** @Id @Column(type="integer") */
206     private $id;
207     /** @Column(type="string") */
208     private $name;
209 }
210
211 /**
212  * @Entity
213  * @InheritanceType("SINGLE_TABLE")
214  * @DiscriminatorColumn(name="type", type="string", length=20)
215  * @DiscriminatorMap({
216  *     "c"   = "HierachyC",
217  *     "d"   = "HierachyD",
218  *     "e"   = "HierachyE"
219  * })
220  */
221 abstract class HierachyBase
222 {
223     /**
224      * @Column(type="integer") @Id @GeneratedValue(strategy="SEQUENCE")
225      * @SequenceGenerator(sequenceName="foo", initialValue=10)
226      * @var int
227      */
228     public $id;
229 }
230
231 /**
232  * @MappedSuperclass
233  */
234 abstract class HierachyASuperclass extends HierachyBase
235 {
236     /** @Column(type="string") */
237     public $a;
238 }
239
240 /**
241  * @Entity
242  */
243 class HierachyBEntity extends HierachyBase
244 {
245     /** @Column(type="string") */
246     public $b;
247 }
248
249 /**
250  * @Entity
251  */
252 class HierachyC extends HierachyBase
253 {
254     /** @Column(type="string") */
255     public $c;
256 }
257
258 /**
259  * @Entity
260  */
261 class HierachyD extends HierachyASuperclass
262 {
263     /** @Column(type="string") */
264     public $d;
265 }
266
267 /**
268  * @Entity
269  */
270 class HierachyE extends HierachyBEntity
271 {
272     /** @Column(type="string") */
273     public $e;
274 }
275
276 /**
277  * @Entity
278  */
279 class SuperclassEntity extends SuperclassBase
280 {
281
282 }
283
284 /**
285  * @MappedSuperclass
286  */
287 abstract class SuperclassBase
288 {
289     /**
290      * @Column(type="integer") @Id @GeneratedValue(strategy="SEQUENCE")
291      * @SequenceGenerator(sequenceName="foo", initialValue=10)
292      * @var int
293      */
294     public $id;
295 }
296
297 /**
298  * @MappedSuperclass
299  */
300 abstract class MediumSuperclassBase extends SuperclassBase
301 {
302
303 }
304
305 /**
306  * @Entity
307  */
308 class MediumSuperclassEntity extends MediumSuperclassBase
309 {
310
311 }
312
313 /**
314  * @Entity(repositoryClass = "Doctrine\ORM\EntityRepository")
315  */
316 class SubclassWithRepository extends \Doctrine\Tests\Models\DDC869\DDC869Payment
317 {
318
319 }