3 namespace Doctrine\Tests\Common\Annotations;
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;
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;
20 require_once __DIR__ . '/TopLevelAnnotation.php';
22 abstract class AbstractReaderTest extends \PHPUnit_Framework_TestCase
24 public function getReflectionClass()
26 $className = 'Doctrine\Tests\Common\Annotations\DummyClass';
27 $testsRoot = substr(__DIR__, 0, -strlen(__NAMESPACE__) - 1);
29 'Doctrine\\Tests' => array($testsRoot),
31 $staticReflectionParser = new StaticReflectionParser($className, new Psr0FindFile($paths));
33 'native' => array(new ReflectionClass($className)),
34 'static' => array($staticReflectionParser->getReflectionClass()),
39 * @dataProvider getReflectionClass
41 public function testAnnotations($class)
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);
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);
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);
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);
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);
77 $dummyAnnot = $reader->getPropertyAnnotation($class->getProperty('field1'), 'Doctrine\Tests\Common\Annotations\DummyAnnotation');
78 $this->assertEquals('fieldHello', $dummyAnnot->dummyValue);
80 $classAnnot = $reader->getClassAnnotation($class, 'Doctrine\Tests\Common\Annotations\DummyAnnotation');
81 $this->assertEquals('hello', $classAnnot->dummyValue);
84 public function testAnnotationsWithValidTargets()
86 $reader = $this->getReader();
87 $class = new ReflectionClass('Doctrine\Tests\Common\Annotations\Fixtures\ClassWithValidAnnotationTarget');
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'))));
95 public function testAnnotationsWithVarType()
97 $reader = $this->getReader();
98 $class = new ReflectionClass('Doctrine\Tests\Common\Annotations\Fixtures\ClassWithAnnotationWithVarType');
100 $this->assertEquals(1,count($fooAnnot = $reader->getPropertyAnnotations($class->getProperty('foo'))));
101 $this->assertEquals(1,count($barAnnot = $reader->getMethodAnnotations($class->getMethod('bar'))));
103 $this->assertInternalType('string', $fooAnnot[0]->string);
104 $this->assertInstanceOf('Doctrine\Tests\Common\Annotations\Fixtures\AnnotationTargetAll', $barAnnot[0]->annotation);
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
111 public function testClassWithInvalidAnnotationTargetAtClassDocBlock()
113 $reader = $this->getReader();
114 $reader->getClassAnnotations(new \ReflectionClass('Doctrine\Tests\Common\Annotations\Fixtures\ClassWithInvalidAnnotationTargetAtClass'));
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
121 public function testClassWithInvalidAnnotationTargetAtPropertyDocBlock()
123 $reader = $this->getReader();
124 $reader->getPropertyAnnotations(new \ReflectionProperty('Doctrine\Tests\Common\Annotations\Fixtures\ClassWithInvalidAnnotationTargetAtProperty', 'foo'));
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
131 public function testClassWithInvalidNestedAnnotationTargetAtPropertyDocBlock()
133 $reader = $this->getReader();
134 $reader->getPropertyAnnotations(new \ReflectionProperty('Doctrine\Tests\Common\Annotations\Fixtures\ClassWithInvalidAnnotationTargetAtProperty', 'bar'));
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
141 public function testClassWithInvalidAnnotationTargetAtMethodDocBlock()
143 $reader = $this->getReader();
144 $reader->getMethodAnnotations(new \ReflectionMethod('Doctrine\Tests\Common\Annotations\Fixtures\ClassWithInvalidAnnotationTargetAtMethod', 'functionName'));
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.
151 public function testClassWithAnnotationWithTargetSyntaxErrorAtClassDocBlock()
153 $reader = $this->getReader();
154 $reader->getClassAnnotations(new \ReflectionClass('Doctrine\Tests\Common\Annotations\Fixtures\ClassWithAnnotationWithTargetSyntaxError'));
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.
161 public function testClassWithAnnotationWithTargetSyntaxErrorAtPropertyDocBlock()
163 $reader = $this->getReader();
164 $reader->getPropertyAnnotations(new \ReflectionProperty('Doctrine\Tests\Common\Annotations\Fixtures\ClassWithAnnotationWithTargetSyntaxError','foo'));
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.
171 public function testClassWithAnnotationWithTargetSyntaxErrorAtMethodDocBlock()
173 $reader = $this->getReader();
174 $reader->getMethodAnnotations(new \ReflectionMethod('Doctrine\Tests\Common\Annotations\Fixtures\ClassWithAnnotationWithTargetSyntaxError','bar'));
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.
181 public function testClassWithPropertyInvalidVarTypeError()
183 $reader = $this->getReader();
184 $class = new ReflectionClass('Doctrine\Tests\Common\Annotations\Fixtures\ClassWithAnnotationWithVarType');
186 $reader->getPropertyAnnotations($class->getProperty('invalidProperty'));
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.
193 public function testClassWithMethodInvalidVarTypeError()
195 $reader = $this->getReader();
196 $class = new ReflectionClass('Doctrine\Tests\Common\Annotations\Fixtures\ClassWithAnnotationWithVarType');
198 $reader->getMethodAnnotations($class->getMethod('invalidMethod'));
202 * @expectedException Doctrine\Common\Annotations\AnnotationException
203 * @expectedExceptionMessage Expected namespace separator or identifier, got ')' at position 18 in class Doctrine\Tests\Common\Annotations\DummyClassSyntaxError.
205 public function testClassSyntaxErrorContext()
207 $reader = $this->getReader();
208 $reader->getClassAnnotations(new \ReflectionClass('Doctrine\Tests\Common\Annotations\DummyClassSyntaxError'));
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().
215 public function testMethodSyntaxErrorContext()
217 $reader = $this->getReader();
218 $reader->getMethodAnnotations(new \ReflectionMethod('Doctrine\Tests\Common\Annotations\DummyClassMethodSyntaxError', 'foo'));
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.
225 public function testPropertySyntaxErrorContext()
227 $reader = $this->getReader();
228 $reader->getPropertyAnnotations(new \ReflectionProperty('Doctrine\Tests\Common\Annotations\DummyClassPropertySyntaxError', 'foo'));
234 public function testMultipleAnnotationsOnSameLine()
236 $reader = $this->getReader();
237 $annots = $reader->getPropertyAnnotations(new \ReflectionProperty('Doctrine\Tests\Common\Annotations\DummyClass2', 'id'));
238 $this->assertEquals(3, count($annots));
241 public function testNonAnnotationProblem()
243 $reader = $this->getReader();
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);
249 public function testImportWithConcreteAnnotation()
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'));
258 public function testImportWithInheritance()
260 $reader = $this->getReader();
262 $class = new TestParentClass();
263 $ref = new \ReflectionClass($class);
265 $childAnnotations = $reader->getPropertyAnnotations($ref->getProperty('child'));
266 $this->assertEquals(1, count($childAnnotations));
267 $this->assertInstanceOf('Doctrine\Tests\Common\Annotations\Foo\Name', reset($childAnnotations));
269 $parentAnnotations = $reader->getPropertyAnnotations($ref->getProperty('parent'));
270 $this->assertEquals(1, count($parentAnnotations));
271 $this->assertInstanceOf('Doctrine\Tests\Common\Annotations\Bar\Name', reset($parentAnnotations));
275 * @expectedException Doctrine\Common\Annotations\AnnotationException
276 * @expectedExceptionMessage The annotation "@NameFoo" in property Doctrine\Tests\Common\Annotations\TestAnnotationNotImportedClass::$field was never imported.
278 public function testImportDetectsNotImportedAnnotation()
280 $reader = $this->getReader();
281 $reader->getPropertyAnnotations(new \ReflectionProperty('Doctrine\Tests\Common\Annotations\TestAnnotationNotImportedClass', 'field'));
285 * @expectedException Doctrine\Common\Annotations\AnnotationException
286 * @expectedExceptionMessage The annotation "@Foo\Bar\Name" in property Doctrine\Tests\Common\Annotations\TestNonExistentAnnotationClass::$field was never imported.
288 public function testImportDetectsNonExistentAnnotation()
290 $reader = $this->getReader();
291 $reader->getPropertyAnnotations(new \ReflectionProperty('Doctrine\Tests\Common\Annotations\TestNonExistentAnnotationClass', 'field'));
294 public function testTopLevelAnnotation()
296 $reader = $this->getReader();
297 $annotations = $reader->getPropertyAnnotations(new \ReflectionProperty('Doctrine\Tests\Common\Annotations\TestTopLevelAnnotationClass', 'field'));
299 $this->assertEquals(1, count($annotations));
300 $this->assertInstanceOf('\TopLevelAnnotation', reset($annotations));
303 public function testIgnoresAnnotationsNotPrefixedWithWhitespace()
305 $reader = $this->getReader();
307 $annotation = $reader->getClassAnnotation(new \ReflectionClass(new TestIgnoresNonAnnotationsClass()), 'Doctrine\Tests\Common\Annotations\Name');
308 $this->assertInstanceOf('Doctrine\Tests\Common\Annotations\Name', $annotation);
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.
315 public function testErrorWhenInvalidAnnotationIsUsed()
317 $reader = $this->getReader();
318 $ref = new \ReflectionClass('Doctrine\Tests\Common\Annotations\Fixtures\InvalidAnnotationUsageClass');
319 $reader->getClassAnnotations($ref);
322 public function testInvalidAnnotationUsageButIgnoredClass()
324 $reader = $this->getReader();
325 $ref = new \ReflectionClass('Doctrine\Tests\Common\Annotations\Fixtures\InvalidAnnotationUsageButIgnoredClass');
326 $annots = $reader->getClassAnnotations($ref);
328 $this->assertEquals(2, count($annots));
335 public function testInvalidAnnotationButIgnored()
337 $reader = $this->getReader();
338 $class = new \ReflectionClass('Doctrine\Tests\Common\Annotations\Fixtures\ClassDDC1660');
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')));
346 abstract protected function getReader();
350 * @parseAnnotation("var")
351 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
354 class TestParseAnnotationClass
364 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
366 class TestIgnoresNonAnnotationsClass
370 class TestTopLevelAnnotationClass
373 * @\TopLevelAnnotation
378 class TestNonExistentAnnotationClass
386 class TestAnnotationNotImportedClass
397 * @\Doctrine\Tests\Common\Annotations\Foo\Name(name = "foo")
402 class TestParentClass extends TestChildClass
405 * @\Doctrine\Tests\Common\Annotations\Bar\Name(name = "bar")
410 class TestImportWithConcreteAnnotation
413 * @DummyAnnotation(dummyValue = "bar")
419 * @ignoreAnnotation("var")
423 * @DummyId @DummyColumn(type="integer") @DummyGeneratedValue
430 class DummyId extends \Doctrine\Common\Annotations\Annotation {}
432 class DummyColumn extends \Doctrine\Common\Annotations\Annotation {
436 class DummyGeneratedValue extends \Doctrine\Common\Annotations\Annotation {}
438 class DummyAnnotation extends \Doctrine\Common\Annotations\Annotation {
442 class DummyJoinColumn extends \Doctrine\Common\Annotations\Annotation {
444 public $referencedColumnName;
447 class DummyJoinTable extends \Doctrine\Common\Annotations\Annotation {
450 public $inverseJoinColumns;
454 * @DummyAnnotation(@)
456 class DummyClassSyntaxError
461 class DummyClassMethodSyntaxError
464 * @DummyAnnotation(@)
466 public function foo()
472 class DummyClassPropertySyntaxError
475 * @DummyAnnotation(@)
481 * @ignoreAnnotation({"since", "var"})
483 class DummyClassNonAnnotationProblem
496 * @DummyAnnotation Foo bar <foobar@1domain.com>
498 class DummyClassWithEmail
503 namespace Doctrine\Tests\Common\Annotations\Foo;
506 class Name extends \Doctrine\Common\Annotations\Annotation
511 namespace Doctrine\Tests\Common\Annotations\Bar;
514 class Name extends \Doctrine\Common\Annotations\Annotation