Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / tests / Doctrine / Tests / ORM / Mapping / AnnotationDriverTest.php
1 <?php
2
3 namespace Doctrine\Tests\ORM\Mapping;
4
5 use Doctrine\ORM\Mapping\ClassMetadata;
6 use Doctrine\ORM\Events;
7
8 require_once __DIR__ . '/../../TestInit.php';
9
10 class AnnotationDriverTest extends AbstractMappingDriverTest
11 {
12     /**
13      * @group DDC-268
14      */
15     public function testLoadMetadataForNonEntityThrowsException()
16     {
17         $cm = new ClassMetadata('stdClass');
18         $cm->initializeReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService);
19         $reader = new \Doctrine\Common\Annotations\AnnotationReader(new \Doctrine\Common\Cache\ArrayCache());
20         $annotationDriver = new \Doctrine\ORM\Mapping\Driver\AnnotationDriver($reader);
21
22         $this->setExpectedException('Doctrine\ORM\Mapping\MappingException');
23         $annotationDriver->loadMetadataForClass('stdClass', $cm);
24     }
25
26     /**
27      * @group DDC-268
28      */
29     public function testColumnWithMissingTypeDefaultsToString()
30     {
31         $cm = new ClassMetadata('Doctrine\Tests\ORM\Mapping\ColumnWithoutType');
32         $cm->initializeReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService);
33         $annotationDriver = $this->_loadDriver();
34
35         $annotationDriver->loadMetadataForClass('Doctrine\Tests\ORM\Mapping\InvalidColumn', $cm);
36         $this->assertEquals('string', $cm->fieldMappings['id']['type']);
37     }
38
39     /**
40      * @group DDC-318
41      */
42     public function testGetAllClassNamesIsIdempotent()
43     {
44         $annotationDriver = $this->_loadDriverForCMSModels();
45         $original = $annotationDriver->getAllClassNames();
46
47         $annotationDriver = $this->_loadDriverForCMSModels();
48         $afterTestReset = $annotationDriver->getAllClassNames();
49
50         $this->assertEquals($original, $afterTestReset);
51     }
52
53     /**
54      * @group DDC-318
55      */
56     public function testGetAllClassNamesIsIdempotentEvenWithDifferentDriverInstances()
57     {
58         $annotationDriver = $this->_loadDriverForCMSModels();
59         $original = $annotationDriver->getAllClassNames();
60
61         $annotationDriver = $this->_loadDriverForCMSModels();
62         $afterTestReset = $annotationDriver->getAllClassNames();
63
64         $this->assertEquals($original, $afterTestReset);
65     }
66
67     /**
68      * @group DDC-318
69      */
70     public function testGetAllClassNamesReturnsAlreadyLoadedClassesIfAppropriate()
71     {
72         $rightClassName = 'Doctrine\Tests\Models\CMS\CmsUser';
73         $this->_ensureIsLoaded($rightClassName);
74
75         $annotationDriver = $this->_loadDriverForCMSModels();
76         $classes = $annotationDriver->getAllClassNames();
77
78         $this->assertContains($rightClassName, $classes);
79     }
80
81     /**
82      * @group DDC-318
83      */
84     public function testGetClassNamesReturnsOnlyTheAppropriateClasses()
85     {
86         $extraneousClassName = 'Doctrine\Tests\Models\ECommerce\ECommerceCart';
87         $this->_ensureIsLoaded($extraneousClassName);
88
89         $annotationDriver = $this->_loadDriverForCMSModels();
90         $classes = $annotationDriver->getAllClassNames();
91
92         $this->assertNotContains($extraneousClassName, $classes);
93     }
94
95     protected function _loadDriverForCMSModels()
96     {
97         $annotationDriver = $this->_loadDriver();
98         $annotationDriver->addPaths(array(__DIR__ . '/../../Models/CMS/'));
99         return $annotationDriver;
100     }
101
102     protected function _loadDriver()
103     {
104         return $this->createAnnotationDriver();
105     }
106
107     protected function _ensureIsLoaded($entityClassName)
108     {
109         new $entityClassName;
110     }
111
112     /**
113      * @group DDC-671
114      *
115      * Entities for this test are in AbstractMappingDriverTest
116      */
117     public function testJoinTablesWithMappedSuperclassForAnnotationDriver()
118     {
119         $annotationDriver = $this->_loadDriver();
120         $annotationDriver->addPaths(array(__DIR__ . '/../../Models/DirectoryTree/'));
121
122         $em = $this->_getTestEntityManager();
123         $em->getConfiguration()->setMetadataDriverImpl($annotationDriver);
124         $factory = new \Doctrine\ORM\Mapping\ClassMetadataFactory();
125         $factory->setEntityManager($em);
126
127         $classPage = $factory->getMetadataFor('Doctrine\Tests\Models\DirectoryTree\File');
128         $this->assertEquals('Doctrine\Tests\Models\DirectoryTree\File', $classPage->associationMappings['parentDirectory']['sourceEntity']);
129
130         $classDirectory = $factory->getMetadataFor('Doctrine\Tests\Models\DirectoryTree\Directory');
131         $this->assertEquals('Doctrine\Tests\Models\DirectoryTree\Directory', $classDirectory->associationMappings['parentDirectory']['sourceEntity']);
132     }
133
134     /**
135      * @group DDC-945
136      */
137     public function testInvalidMappedSuperClassWithManyToManyAssociation()
138     {
139         $annotationDriver = $this->_loadDriver();
140
141         $em = $this->_getTestEntityManager();
142         $em->getConfiguration()->setMetadataDriverImpl($annotationDriver);
143         $factory = new \Doctrine\ORM\Mapping\ClassMetadataFactory();
144         $factory->setEntityManager($em);
145
146         $this->setExpectedException('Doctrine\ORM\Mapping\MappingException',
147             "It is illegal to put an inverse side one-to-many or many-to-many association on ".
148             "mapped superclass 'Doctrine\Tests\ORM\Mapping\InvalidMappedSuperClass#users'");
149         $usingInvalidMsc = $factory->getMetadataFor('Doctrine\Tests\ORM\Mapping\UsingInvalidMappedSuperClass');
150     }
151
152     /**
153      * @group DDC-1050
154      */
155     public function testInvalidMappedSuperClassWithInheritanceInformation()
156     {
157         $annotationDriver = $this->_loadDriver();
158
159         $em = $this->_getTestEntityManager();
160         $em->getConfiguration()->setMetadataDriverImpl($annotationDriver);
161         $factory = new \Doctrine\ORM\Mapping\ClassMetadataFactory();
162         $factory->setEntityManager($em);
163
164         $this->setExpectedException('Doctrine\ORM\Mapping\MappingException',
165             "Its not supported to define inheritance information on a mapped ".
166             "superclass 'Doctrine\Tests\ORM\Mapping\MappedSuperClassInheritence'.");
167         $usingInvalidMsc = $factory->getMetadataFor('Doctrine\Tests\ORM\Mapping\MappedSuperClassInheritence');
168     }
169
170     /**
171      * @group DDC-1034
172      */
173     public function testInheritanceSkipsParentLifecycleCallbacks()
174     {
175         $annotationDriver = $this->_loadDriver();
176
177         $cm = new ClassMetadata('Doctrine\Tests\ORM\Mapping\AnnotationChild');
178         $em = $this->_getTestEntityManager();
179         $em->getConfiguration()->setMetadataDriverImpl($annotationDriver);
180         $factory = new \Doctrine\ORM\Mapping\ClassMetadataFactory();
181         $factory->setEntityManager($em);
182
183         $cm = $factory->getMetadataFor('Doctrine\Tests\ORM\Mapping\AnnotationChild');
184         $this->assertEquals(array("postLoad" => array("postLoad"), "preUpdate" => array("preUpdate")), $cm->lifecycleCallbacks);
185
186         $cm = $factory->getMetadataFor('Doctrine\Tests\ORM\Mapping\AnnotationParent');
187         $this->assertEquals(array("postLoad" => array("postLoad"), "preUpdate" => array("preUpdate")), $cm->lifecycleCallbacks);
188     }
189
190     /**
191      * @group DDC-1156
192      */
193     public function testMappedSuperclassInMiddleOfInheritanceHierachy()
194     {
195         $annotationDriver = $this->_loadDriver();
196
197         $em = $this->_getTestEntityManager();
198         $em->getConfiguration()->setMetadataDriverImpl($annotationDriver);
199         $factory = new \Doctrine\ORM\Mapping\ClassMetadataFactory();
200         $factory->setEntityManager($em);
201
202         $cm = $factory->getMetadataFor('Doctrine\Tests\ORM\Mapping\ChildEntity');
203     }
204
205     public function testInvalidFetchOptionThrowsException()
206     {
207         $annotationDriver = $this->_loadDriver();
208
209         $em = $this->_getTestEntityManager();
210         $em->getConfiguration()->setMetadataDriverImpl($annotationDriver);
211         $factory = new \Doctrine\ORM\Mapping\ClassMetadataFactory();
212         $factory->setEntityManager($em);
213
214         $this->setExpectedException('Doctrine\ORM\Mapping\MappingException',
215             "Entity 'Doctrine\Tests\ORM\Mapping\InvalidFetchOption' has a mapping with invalid fetch mode 'eager");
216         $cm = $factory->getMetadataFor('Doctrine\Tests\ORM\Mapping\InvalidFetchOption');
217     }
218 }
219
220 /**
221  * @Entity
222  */
223 class ColumnWithoutType
224 {
225     /** @Id @Column */
226     public $id;
227 }
228
229 /**
230  * @MappedSuperclass
231  */
232 class InvalidMappedSuperClass
233 {
234     /**
235      * @ManyToMany(targetEntity="Doctrine\Tests\Models\CMS\CmsUser", mappedBy="invalid")
236      */
237     private $users;
238 }
239
240 /**
241  * @Entity
242  */
243 class UsingInvalidMappedSuperClass extends InvalidMappedSuperClass
244 {
245     /**
246      * @Id @Column(type="integer") @GeneratedValue
247      */
248     private $id;
249 }
250
251 /**
252  * @MappedSuperclass
253  * @InheritanceType("JOINED")
254  * @DiscriminatorMap({"test" = "ColumnWithoutType"})
255  */
256 class MappedSuperClassInheritence
257 {
258
259 }
260
261 /**
262  * @Entity
263  * @InheritanceType("JOINED")
264  * @DiscriminatorMap({"parent" = "AnnotationParent", "child" = "AnnotationChild"})
265  * @HasLifecycleCallbacks
266  */
267 class AnnotationParent
268 {
269     /**
270      * @Id @Column(type="integer") @GeneratedValue
271      */
272     private $id;
273
274     /**
275      * @PostLoad
276      */
277     public function postLoad()
278     {
279
280     }
281
282     /**
283      * @PreUpdate
284      */
285     public function preUpdate()
286     {
287
288     }
289 }
290
291 /**
292  * @Entity
293  * @HasLifecycleCallbacks
294  */
295 class AnnotationChild extends AnnotationParent
296 {
297
298 }
299
300 /**
301  * @Entity
302  * @InheritanceType("SINGLE_TABLE")
303  * @DiscriminatorMap({"s"="SuperEntity", "c"="ChildEntity"})
304  */
305 class SuperEntity
306 {
307     /** @Id @Column(type="string") */
308     private $id;
309 }
310
311 /**
312  * @MappedSuperclass
313  */
314 class MiddleMappedSuperclass extends SuperEntity
315 {
316     /** @Column(type="string") */
317     private $name;
318 }
319
320 /**
321  * @Entity
322  */
323 class ChildEntity extends MiddleMappedSuperclass
324 {
325     /**
326      * @Column(type="string")
327      */
328     private $text;
329 }
330
331 /**
332  * @Entity
333  */
334 class InvalidFetchOption
335 {
336     /**
337      * @OneToMany(targetEntity="Doctrine\Tests\Models\CMS\CmsUser", fetch="eager")
338      */
339     private $collection;
340 }