Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / common / tests / Doctrine / Tests / Common / Annotations / AbstractReaderTest.php
1 <?php
2
3 namespace Doctrine\Tests\Common\Annotations;
4
5 use Doctrine\Common\Annotations\DoctrineReader;
6 use Doctrine\Common\Reflection\StaticReflectionParser;
7 use Doctrine\Common\Reflection\Psr0FindFile;
8 use Doctrine\Common\Annotations\Annotation\IgnoreAnnotation;
9 use Doctrine\Common\Annotations\Annotation\IgnorePhpDoc;
10 use ReflectionClass, Doctrine\Common\Annotations\AnnotationReader;
11
12 use Doctrine\Tests\Common\Annotations\DummyAnnotation;
13 use Doctrine\Tests\Common\Annotations\Name;
14 use Doctrine\Tests\Common\Annotations\DummyId;
15 use Doctrine\Tests\Common\Annotations\DummyJoinTable;
16 use Doctrine\Tests\Common\Annotations\DummyJoinColumn;
17 use Doctrine\Tests\Common\Annotations\DummyColumn;
18 use Doctrine\Tests\Common\Annotations\DummyGeneratedValue;
19
20 require_once __DIR__ . '/TopLevelAnnotation.php';
21
22 abstract class AbstractReaderTest extends \PHPUnit_Framework_TestCase
23 {
24     public function getReflectionClass()
25     {
26         $className = 'Doctrine\Tests\Common\Annotations\DummyClass';
27         $testsRoot = substr(__DIR__, 0, -strlen(__NAMESPACE__) - 1);
28         $paths = array(
29             'Doctrine\\Tests' => array($testsRoot),
30         );
31         $staticReflectionParser = new StaticReflectionParser($className, new Psr0FindFile($paths));
32         return array(
33             'native' => array(new ReflectionClass($className)),
34             'static' => array($staticReflectionParser->getReflectionClass()),
35         );
36     }
37
38     /**
39      * @dataProvider getReflectionClass
40      */
41     public function testAnnotations($class)
42     {
43         $reader = $this->getReader();
44         $this->assertEquals(1, count($reader->getClassAnnotations($class)));
45         $this->assertInstanceOf($annotName = 'Doctrine\Tests\Common\Annotations\DummyAnnotation', $annot = $reader->getClassAnnotation($class, $annotName));
46         $this->assertEquals("hello", $annot->dummyValue);
47
48         $field1Prop = $class->getProperty('field1');
49         $propAnnots = $reader->getPropertyAnnotations($field1Prop);
50         $this->assertEquals(1, count($propAnnots));
51         $this->assertInstanceOf($annotName, $annot = $reader->getPropertyAnnotation($field1Prop, $annotName));
52         $this->assertEquals("fieldHello", $annot->dummyValue);
53
54         $getField1Method = $class->getMethod('getField1');
55         $methodAnnots = $reader->getMethodAnnotations($getField1Method);
56         $this->assertEquals(1, count($methodAnnots));
57         $this->assertInstanceOf($annotName, $annot = $reader->getMethodAnnotation($getField1Method, $annotName));
58         $this->assertEquals(array(1, 2, "three"), $annot->value);
59
60         $field2Prop = $class->getProperty('field2');
61         $propAnnots = $reader->getPropertyAnnotations($field2Prop);
62         $this->assertEquals(1, count($propAnnots));
63         $this->assertInstanceOf($annotName = 'Doctrine\Tests\Common\Annotations\DummyJoinTable', $joinTableAnnot = $reader->getPropertyAnnotation($field2Prop, $annotName));
64         $this->assertEquals(1, count($joinTableAnnot->joinColumns));
65         $this->assertEquals(1, count($joinTableAnnot->inverseJoinColumns));
66         $this->assertTrue($joinTableAnnot->joinColumns[0] instanceof DummyJoinColumn);
67         $this->assertTrue($joinTableAnnot->inverseJoinColumns[0] instanceof DummyJoinColumn);
68         $this->assertEquals('col1', $joinTableAnnot->joinColumns[0]->name);
69         $this->assertEquals('col2', $joinTableAnnot->joinColumns[0]->referencedColumnName);
70         $this->assertEquals('col3', $joinTableAnnot->inverseJoinColumns[0]->name);
71         $this->assertEquals('col4', $joinTableAnnot->inverseJoinColumns[0]->referencedColumnName);
72
73         $dummyAnnot = $reader->getMethodAnnotation($class->getMethod('getField1'), 'Doctrine\Tests\Common\Annotations\DummyAnnotation');
74         $this->assertEquals('', $dummyAnnot->dummyValue);
75         $this->assertEquals(array(1, 2, 'three'), $dummyAnnot->value);
76
77         $dummyAnnot = $reader->getPropertyAnnotation($class->getProperty('field1'), 'Doctrine\Tests\Common\Annotations\DummyAnnotation');
78         $this->assertEquals('fieldHello', $dummyAnnot->dummyValue);
79
80         $classAnnot = $reader->getClassAnnotation($class, 'Doctrine\Tests\Common\Annotations\DummyAnnotation');
81         $this->assertEquals('hello', $classAnnot->dummyValue);
82     }
83
84     public function testAnnotationsWithValidTargets()
85     {
86         $reader = $this->getReader();
87         $class  = new ReflectionClass('Doctrine\Tests\Common\Annotations\Fixtures\ClassWithValidAnnotationTarget');
88
89         $this->assertEquals(1,count($reader->getClassAnnotations($class)));
90         $this->assertEquals(1,count($reader->getPropertyAnnotations($class->getProperty('foo'))));
91         $this->assertEquals(1,count($reader->getMethodAnnotations($class->getMethod('someFunction'))));
92         $this->assertEquals(1,count($reader->getPropertyAnnotations($class->getProperty('nested'))));
93     }
94
95     public function testAnnotationsWithVarType()
96     {
97         $reader = $this->getReader();
98         $class  = new ReflectionClass('Doctrine\Tests\Common\Annotations\Fixtures\ClassWithAnnotationWithVarType');
99
100         $this->assertEquals(1,count($fooAnnot = $reader->getPropertyAnnotations($class->getProperty('foo'))));
101         $this->assertEquals(1,count($barAnnot = $reader->getMethodAnnotations($class->getMethod('bar'))));
102
103         $this->assertInternalType('string',  $fooAnnot[0]->string);
104         $this->assertInstanceOf('Doctrine\Tests\Common\Annotations\Fixtures\AnnotationTargetAll', $barAnnot[0]->annotation);
105     }
106
107      /**
108      * @expectedException Doctrine\Common\Annotations\AnnotationException
109      * @expectedExceptionMessage [Semantical Error] Annotation @AnnotationTargetPropertyMethod is not allowed to be declared on class Doctrine\Tests\Common\Annotations\Fixtures\ClassWithInvalidAnnotationTargetAtClass. You may only use this annotation on these code elements: METHOD, PROPERTY
110      */
111     public function testClassWithInvalidAnnotationTargetAtClassDocBlock()
112     {
113         $reader  = $this->getReader();
114         $reader->getClassAnnotations(new \ReflectionClass('Doctrine\Tests\Common\Annotations\Fixtures\ClassWithInvalidAnnotationTargetAtClass'));
115     }
116
117      /**
118      * @expectedException Doctrine\Common\Annotations\AnnotationException
119      * @expectedExceptionMessage [Semantical Error] Annotation @AnnotationTargetClass is not allowed to be declared on property Doctrine\Tests\Common\Annotations\Fixtures\ClassWithInvalidAnnotationTargetAtProperty::$foo. You may only use this annotation on these code elements: CLASS
120      */
121     public function testClassWithInvalidAnnotationTargetAtPropertyDocBlock()
122     {
123         $reader  = $this->getReader();
124         $reader->getPropertyAnnotations(new \ReflectionProperty('Doctrine\Tests\Common\Annotations\Fixtures\ClassWithInvalidAnnotationTargetAtProperty', 'foo'));
125     }
126
127      /**
128      * @expectedException Doctrine\Common\Annotations\AnnotationException
129      * @expectedExceptionMessage [Semantical Error] Annotation @AnnotationTargetAnnotation is not allowed to be declared on property Doctrine\Tests\Common\Annotations\Fixtures\ClassWithInvalidAnnotationTargetAtProperty::$bar. You may only use this annotation on these code elements: ANNOTATION
130      */
131     public function testClassWithInvalidNestedAnnotationTargetAtPropertyDocBlock()
132     {
133         $reader  = $this->getReader();
134         $reader->getPropertyAnnotations(new \ReflectionProperty('Doctrine\Tests\Common\Annotations\Fixtures\ClassWithInvalidAnnotationTargetAtProperty', 'bar'));
135     }
136
137      /**
138      * @expectedException Doctrine\Common\Annotations\AnnotationException
139      * @expectedExceptionMessage [Semantical Error] Annotation @AnnotationTargetClass is not allowed to be declared on method Doctrine\Tests\Common\Annotations\Fixtures\ClassWithInvalidAnnotationTargetAtMethod::functionName(). You may only use this annotation on these code elements: CLASS
140      */
141     public function testClassWithInvalidAnnotationTargetAtMethodDocBlock()
142     {
143         $reader  = $this->getReader();
144         $reader->getMethodAnnotations(new \ReflectionMethod('Doctrine\Tests\Common\Annotations\Fixtures\ClassWithInvalidAnnotationTargetAtMethod', 'functionName'));
145     }
146
147     /**
148      * @expectedException Doctrine\Common\Annotations\AnnotationException
149      * @expectedExceptionMessage Expected namespace separator or identifier, got ')' at position 24 in class @Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithTargetSyntaxError.
150      */
151     public function testClassWithAnnotationWithTargetSyntaxErrorAtClassDocBlock()
152     {
153         $reader  = $this->getReader();
154         $reader->getClassAnnotations(new \ReflectionClass('Doctrine\Tests\Common\Annotations\Fixtures\ClassWithAnnotationWithTargetSyntaxError'));
155     }
156
157     /**
158      * @expectedException Doctrine\Common\Annotations\AnnotationException
159      * @expectedExceptionMessage Expected namespace separator or identifier, got ')' at position 24 in class @Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithTargetSyntaxError.
160      */
161     public function testClassWithAnnotationWithTargetSyntaxErrorAtPropertyDocBlock()
162     {
163         $reader  = $this->getReader();
164         $reader->getPropertyAnnotations(new \ReflectionProperty('Doctrine\Tests\Common\Annotations\Fixtures\ClassWithAnnotationWithTargetSyntaxError','foo'));
165     }
166
167     /**
168      * @expectedException Doctrine\Common\Annotations\AnnotationException
169      * @expectedExceptionMessage Expected namespace separator or identifier, got ')' at position 24 in class @Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithTargetSyntaxError.
170      */
171     public function testClassWithAnnotationWithTargetSyntaxErrorAtMethodDocBlock()
172     {
173         $reader  = $this->getReader();
174         $reader->getMethodAnnotations(new \ReflectionMethod('Doctrine\Tests\Common\Annotations\Fixtures\ClassWithAnnotationWithTargetSyntaxError','bar'));
175     }
176
177     /**
178      * @expectedException Doctrine\Common\Annotations\AnnotationException
179      * @expectedExceptionMessage [Type Error] Attribute "string" of @AnnotationWithVarType declared on property Doctrine\Tests\Common\Annotations\Fixtures\ClassWithAnnotationWithVarType::$invalidProperty expects a(n) string, but got integer.
180      */
181     public function testClassWithPropertyInvalidVarTypeError()
182     {
183         $reader = $this->getReader();
184         $class  = new ReflectionClass('Doctrine\Tests\Common\Annotations\Fixtures\ClassWithAnnotationWithVarType');
185
186         $reader->getPropertyAnnotations($class->getProperty('invalidProperty'));
187     }
188
189     /**
190      * @expectedException Doctrine\Common\Annotations\AnnotationException
191      * @expectedExceptionMessage [Type Error] Attribute "annotation" of @AnnotationWithVarType declared on method Doctrine\Tests\Common\Annotations\Fixtures\ClassWithAnnotationWithVarType::invalidMethod() expects a(n) Doctrine\Tests\Common\Annotations\Fixtures\AnnotationTargetAll, but got an instance of Doctrine\Tests\Common\Annotations\Fixtures\AnnotationTargetAnnotation.
192      */
193     public function testClassWithMethodInvalidVarTypeError()
194     {
195         $reader = $this->getReader();
196         $class  = new ReflectionClass('Doctrine\Tests\Common\Annotations\Fixtures\ClassWithAnnotationWithVarType');
197
198         $reader->getMethodAnnotations($class->getMethod('invalidMethod'));
199     }
200
201     /**
202      * @expectedException Doctrine\Common\Annotations\AnnotationException
203      * @expectedExceptionMessage Expected namespace separator or identifier, got ')' at position 18 in class Doctrine\Tests\Common\Annotations\DummyClassSyntaxError.
204      */
205     public function testClassSyntaxErrorContext()
206     {
207         $reader = $this->getReader();
208         $reader->getClassAnnotations(new \ReflectionClass('Doctrine\Tests\Common\Annotations\DummyClassSyntaxError'));
209     }
210
211     /**
212      * @expectedException Doctrine\Common\Annotations\AnnotationException
213      * @expectedExceptionMessage Expected namespace separator or identifier, got ')' at position 18 in method Doctrine\Tests\Common\Annotations\DummyClassMethodSyntaxError::foo().
214      */
215     public function testMethodSyntaxErrorContext()
216     {
217         $reader = $this->getReader();
218         $reader->getMethodAnnotations(new \ReflectionMethod('Doctrine\Tests\Common\Annotations\DummyClassMethodSyntaxError', 'foo'));
219     }
220
221     /**
222      * @expectedException Doctrine\Common\Annotations\AnnotationException
223      * @expectedExceptionMessage Expected namespace separator or identifier, got ')' at position 18 in property Doctrine\Tests\Common\Annotations\DummyClassPropertySyntaxError::$foo.
224      */
225     public function testPropertySyntaxErrorContext()
226     {
227         $reader = $this->getReader();
228         $reader->getPropertyAnnotations(new \ReflectionProperty('Doctrine\Tests\Common\Annotations\DummyClassPropertySyntaxError', 'foo'));
229     }
230
231     /**
232      * @group regression
233      */
234     public function testMultipleAnnotationsOnSameLine()
235     {
236         $reader = $this->getReader();
237         $annots = $reader->getPropertyAnnotations(new \ReflectionProperty('Doctrine\Tests\Common\Annotations\DummyClass2', 'id'));
238         $this->assertEquals(3, count($annots));
239     }
240
241     public function testNonAnnotationProblem()
242     {
243         $reader = $this->getReader();
244
245         $this->assertNotNull($annot = $reader->getPropertyAnnotation(new \ReflectionProperty('Doctrine\Tests\Common\Annotations\DummyClassNonAnnotationProblem', 'foo'), $name = 'Doctrine\Tests\Common\Annotations\DummyAnnotation'));
246         $this->assertInstanceOf($name, $annot);
247     }
248
249     public function testImportWithConcreteAnnotation()
250     {
251         $reader = $this->getReader();
252         $property = new \ReflectionProperty('Doctrine\Tests\Common\Annotations\TestImportWithConcreteAnnotation', 'field');
253         $annotations = $reader->getPropertyAnnotations($property);
254         $this->assertEquals(1, count($annotations));
255         $this->assertNotNull($reader->getPropertyAnnotation($property, 'Doctrine\Tests\Common\Annotations\DummyAnnotation'));
256     }
257
258     public function testImportWithInheritance()
259     {
260         $reader = $this->getReader();
261
262         $class = new TestParentClass();
263         $ref = new \ReflectionClass($class);
264
265         $childAnnotations = $reader->getPropertyAnnotations($ref->getProperty('child'));
266         $this->assertEquals(1, count($childAnnotations));
267         $this->assertInstanceOf('Doctrine\Tests\Common\Annotations\Foo\Name', reset($childAnnotations));
268
269         $parentAnnotations = $reader->getPropertyAnnotations($ref->getProperty('parent'));
270         $this->assertEquals(1, count($parentAnnotations));
271         $this->assertInstanceOf('Doctrine\Tests\Common\Annotations\Bar\Name', reset($parentAnnotations));
272     }
273
274     /**
275      * @expectedException Doctrine\Common\Annotations\AnnotationException
276      * @expectedExceptionMessage The annotation "@NameFoo" in property Doctrine\Tests\Common\Annotations\TestAnnotationNotImportedClass::$field was never imported.
277      */
278     public function testImportDetectsNotImportedAnnotation()
279     {
280         $reader = $this->getReader();
281         $reader->getPropertyAnnotations(new \ReflectionProperty('Doctrine\Tests\Common\Annotations\TestAnnotationNotImportedClass', 'field'));
282     }
283
284     /**
285      * @expectedException Doctrine\Common\Annotations\AnnotationException
286      * @expectedExceptionMessage The annotation "@Foo\Bar\Name" in property Doctrine\Tests\Common\Annotations\TestNonExistentAnnotationClass::$field was never imported.
287      */
288     public function testImportDetectsNonExistentAnnotation()
289     {
290         $reader = $this->getReader();
291         $reader->getPropertyAnnotations(new \ReflectionProperty('Doctrine\Tests\Common\Annotations\TestNonExistentAnnotationClass', 'field'));
292     }
293
294     public function testTopLevelAnnotation()
295     {
296         $reader = $this->getReader();
297         $annotations = $reader->getPropertyAnnotations(new \ReflectionProperty('Doctrine\Tests\Common\Annotations\TestTopLevelAnnotationClass', 'field'));
298
299         $this->assertEquals(1, count($annotations));
300         $this->assertInstanceOf('\TopLevelAnnotation', reset($annotations));
301     }
302
303     public function testIgnoresAnnotationsNotPrefixedWithWhitespace()
304     {
305         $reader = $this->getReader();
306
307         $annotation = $reader->getClassAnnotation(new \ReflectionClass(new TestIgnoresNonAnnotationsClass()), 'Doctrine\Tests\Common\Annotations\Name');
308         $this->assertInstanceOf('Doctrine\Tests\Common\Annotations\Name', $annotation);
309     }
310
311     /**
312      * @expectedException Doctrine\Common\Annotations\AnnotationException
313      * @expectedExceptionMessage The class "Doctrine\Tests\Common\Annotations\Fixtures\NoAnnotation" is not annotated with @Annotation. Are you sure this class can be used as annotation? If so, then you need to add @Annotation to the _class_ doc comment of "Doctrine\Tests\Common\Annotations\Fixtures\NoAnnotation". If it is indeed no annotation, then you need to add @IgnoreAnnotation("NoAnnotation") to the _class_ doc comment of class Doctrine\Tests\Common\Annotations\Fixtures\InvalidAnnotationUsageClass.
314      */
315     public function testErrorWhenInvalidAnnotationIsUsed()
316     {
317         $reader = $this->getReader();
318         $ref = new \ReflectionClass('Doctrine\Tests\Common\Annotations\Fixtures\InvalidAnnotationUsageClass');
319         $reader->getClassAnnotations($ref);
320     }
321
322     public function testInvalidAnnotationUsageButIgnoredClass()
323     {
324         $reader = $this->getReader();
325         $ref = new \ReflectionClass('Doctrine\Tests\Common\Annotations\Fixtures\InvalidAnnotationUsageButIgnoredClass');
326         $annots = $reader->getClassAnnotations($ref);
327
328         $this->assertEquals(2, count($annots));
329     }
330
331     /**
332      * @group DDC-1660
333      * @group regression
334      */
335     public function testInvalidAnnotationButIgnored()
336     {
337         $reader = $this->getReader();
338         $class  = new \ReflectionClass('Doctrine\Tests\Common\Annotations\Fixtures\ClassDDC1660');
339
340         $this->assertTrue(class_exists('Doctrine\Tests\Common\Annotations\Fixtures\Annotation\Version'));
341         $this->assertCount(0, $reader->getClassAnnotations($class));
342         $this->assertCount(0, $reader->getMethodAnnotations($class->getMethod('bar')));
343         $this->assertCount(0, $reader->getPropertyAnnotations($class->getProperty('foo')));
344     }
345
346     abstract protected function getReader();
347 }
348
349 /**
350  * @parseAnnotation("var")
351  * @author Johannes M. Schmitt <schmittjoh@gmail.com>
352  *
353  */
354 class TestParseAnnotationClass
355 {
356     /**
357      * @var
358      */
359     private $field;
360 }
361
362 /**
363  * @Name
364  * @author Johannes M. Schmitt <schmittjoh@gmail.com>
365  */
366 class TestIgnoresNonAnnotationsClass
367 {
368 }
369
370 class TestTopLevelAnnotationClass
371 {
372     /**
373      * @\TopLevelAnnotation
374      */
375     private $field;
376 }
377
378 class TestNonExistentAnnotationClass
379 {
380     /**
381      * @Foo\Bar\Name
382      */
383     private $field;
384 }
385
386 class TestAnnotationNotImportedClass
387 {
388     /**
389      * @NameFoo
390      */
391     private $field;
392 }
393
394 class TestChildClass
395 {
396     /**
397      * @\Doctrine\Tests\Common\Annotations\Foo\Name(name = "foo")
398      */
399     protected $child;
400 }
401
402 class TestParentClass extends TestChildClass
403 {
404     /**
405      * @\Doctrine\Tests\Common\Annotations\Bar\Name(name = "bar")
406      */
407     private $parent;
408 }
409
410 class TestImportWithConcreteAnnotation
411 {
412     /**
413      * @DummyAnnotation(dummyValue = "bar")
414      */
415     private $field;
416 }
417
418 /**
419  * @ignoreAnnotation("var")
420  */
421 class DummyClass2 {
422     /**
423      * @DummyId @DummyColumn(type="integer") @DummyGeneratedValue
424      * @var integer
425      */
426     private $id;
427 }
428
429 /** @Annotation */
430 class DummyId extends \Doctrine\Common\Annotations\Annotation {}
431 /** @Annotation */
432 class DummyColumn extends \Doctrine\Common\Annotations\Annotation {
433     public $type;
434 }
435 /** @Annotation */
436 class DummyGeneratedValue extends \Doctrine\Common\Annotations\Annotation {}
437 /** @Annotation */
438 class DummyAnnotation extends \Doctrine\Common\Annotations\Annotation {
439     public $dummyValue;
440 }
441 /** @Annotation */
442 class DummyJoinColumn extends \Doctrine\Common\Annotations\Annotation {
443     public $name;
444     public $referencedColumnName;
445 }
446 /** @Annotation */
447 class DummyJoinTable extends \Doctrine\Common\Annotations\Annotation {
448     public $name;
449     public $joinColumns;
450     public $inverseJoinColumns;
451 }
452
453 /**
454  * @DummyAnnotation(@)
455  */
456 class DummyClassSyntaxError
457 {
458
459 }
460
461 class DummyClassMethodSyntaxError
462 {
463     /**
464      * @DummyAnnotation(@)
465      */
466     public function foo()
467     {
468
469     }
470 }
471
472 class DummyClassPropertySyntaxError
473 {
474     /**
475      * @DummyAnnotation(@)
476      */
477     public $foo;
478 }
479
480 /**
481  * @ignoreAnnotation({"since", "var"})
482  */
483 class DummyClassNonAnnotationProblem
484 {
485     /**
486      * @DummyAnnotation
487      *
488      * @var \Test
489      * @since 0.1
490      */
491     public $foo;
492 }
493
494
495 /**
496 * @DummyAnnotation Foo bar <foobar@1domain.com>
497 */
498 class DummyClassWithEmail
499 {
500
501 }
502
503 namespace Doctrine\Tests\Common\Annotations\Foo;
504
505 /** @Annotation */
506 class Name extends \Doctrine\Common\Annotations\Annotation
507 {
508     public $name;
509 }
510
511 namespace Doctrine\Tests\Common\Annotations\Bar;
512
513 /** @Annotation */
514 class Name extends \Doctrine\Common\Annotations\Annotation
515 {
516     public $name;
517 }