1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Tests\Annotations; |
4
|
|
|
|
5
|
|
|
use Doctrine\Annotations\AnnotationException; |
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
use ReflectionClass, Doctrine\Annotations\AnnotationReader; |
8
|
|
|
|
9
|
|
|
require_once __DIR__ . '/TopLevelAnnotation.php'; |
10
|
|
|
|
11
|
|
|
abstract class AbstractReaderTest extends TestCase |
12
|
|
|
{ |
13
|
|
|
public function getReflectionClass() |
14
|
|
|
{ |
15
|
|
|
return new ReflectionClass(DummyClass::class); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
public function testAnnotations() |
19
|
|
|
{ |
20
|
|
|
$class = $this->getReflectionClass(); |
21
|
|
|
$reader = $this->getReader(); |
22
|
|
|
|
23
|
|
|
self::assertCount(1, $reader->getClassAnnotations($class)); |
24
|
|
|
self::assertInstanceOf($annotName = DummyAnnotation::class, $annot = $reader->getClassAnnotation($class, $annotName)); |
25
|
|
|
self::assertEquals('hello', $annot->dummyValue); |
26
|
|
|
|
27
|
|
|
$field1Prop = $class->getProperty('field1'); |
28
|
|
|
$propAnnots = $reader->getPropertyAnnotations($field1Prop); |
29
|
|
|
self::assertCount(1, $propAnnots); |
30
|
|
|
self::assertInstanceOf($annotName, $annot = $reader->getPropertyAnnotation($field1Prop, $annotName)); |
31
|
|
|
self::assertEquals('fieldHello', $annot->dummyValue); |
32
|
|
|
|
33
|
|
|
$getField1Method = $class->getMethod('getField1'); |
34
|
|
|
$methodAnnots = $reader->getMethodAnnotations($getField1Method); |
35
|
|
|
self::assertCount(1, $methodAnnots); |
36
|
|
|
self::assertInstanceOf($annotName, $annot = $reader->getMethodAnnotation($getField1Method, $annotName)); |
37
|
|
|
self::assertEquals([1, 2, 'three'], $annot->value); |
38
|
|
|
|
39
|
|
|
$field2Prop = $class->getProperty('field2'); |
40
|
|
|
$propAnnots = $reader->getPropertyAnnotations($field2Prop); |
41
|
|
|
self::assertCount(1, $propAnnots); |
42
|
|
|
self::assertInstanceOf($annotName = DummyJoinTable::class, $joinTableAnnot = $reader->getPropertyAnnotation($field2Prop, $annotName)); |
43
|
|
|
self::assertCount(1, $joinTableAnnot->joinColumns); |
44
|
|
|
self::assertCount(1, $joinTableAnnot->inverseJoinColumns); |
45
|
|
|
self::assertInstanceOf(DummyJoinColumn::class, $joinTableAnnot->joinColumns[0]); |
46
|
|
|
self::assertInstanceOf(DummyJoinColumn::class, $joinTableAnnot->inverseJoinColumns[0]); |
47
|
|
|
self::assertEquals('col1', $joinTableAnnot->joinColumns[0]->name); |
48
|
|
|
self::assertEquals('col2', $joinTableAnnot->joinColumns[0]->referencedColumnName); |
49
|
|
|
self::assertEquals('col3', $joinTableAnnot->inverseJoinColumns[0]->name); |
50
|
|
|
self::assertEquals('col4', $joinTableAnnot->inverseJoinColumns[0]->referencedColumnName); |
51
|
|
|
|
52
|
|
|
$dummyAnnot = $reader->getMethodAnnotation($class->getMethod('getField1'), DummyAnnotation::class); |
53
|
|
|
self::assertEquals('', $dummyAnnot->dummyValue); |
54
|
|
|
self::assertEquals([1, 2, 'three'], $dummyAnnot->value); |
55
|
|
|
|
56
|
|
|
$dummyAnnot = $reader->getMethodAnnotation($class->getMethod('getField3'), DummyAnnotation::class); |
57
|
|
|
self::assertEquals('\d{4}-[01]\d-[0-3]\d [0-2]\d:[0-5]\d:[0-5]\d', $dummyAnnot->value); |
58
|
|
|
|
59
|
|
|
$dummyAnnot = $reader->getPropertyAnnotation($class->getProperty('field1'), DummyAnnotation::class); |
60
|
|
|
self::assertEquals('fieldHello', $dummyAnnot->dummyValue); |
61
|
|
|
|
62
|
|
|
$classAnnot = $reader->getClassAnnotation($class, DummyAnnotation::class); |
63
|
|
|
self::assertEquals('hello', $classAnnot->dummyValue); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function testAnnotationsWithValidTargets() |
67
|
|
|
{ |
68
|
|
|
$reader = $this->getReader(); |
69
|
|
|
$class = new ReflectionClass(Fixtures\ClassWithValidAnnotationTarget::class); |
70
|
|
|
|
71
|
|
|
self::assertCount(1, $reader->getClassAnnotations($class)); |
72
|
|
|
self::assertCount(1, $reader->getPropertyAnnotations($class->getProperty('foo'))); |
73
|
|
|
self::assertCount(1, $reader->getMethodAnnotations($class->getMethod('someFunction'))); |
74
|
|
|
self::assertCount(1, $reader->getPropertyAnnotations($class->getProperty('nested'))); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function testAnnotationsWithVarType() |
78
|
|
|
{ |
79
|
|
|
$reader = $this->getReader(); |
80
|
|
|
$class = new ReflectionClass(Fixtures\ClassWithAnnotationWithVarType::class); |
81
|
|
|
|
82
|
|
|
self::assertCount(1, $fooAnnot = $reader->getPropertyAnnotations($class->getProperty('foo'))); |
83
|
|
|
self::assertCount(1, $barAnnot = $reader->getMethodAnnotations($class->getMethod('bar'))); |
84
|
|
|
|
85
|
|
|
self::assertInternalType('string', $fooAnnot[0]->string); |
86
|
|
|
self::assertInstanceOf(Fixtures\AnnotationTargetAll::class, $barAnnot[0]->annotation); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function testAtInDescription() |
90
|
|
|
{ |
91
|
|
|
$reader = $this->getReader(); |
92
|
|
|
$class = new ReflectionClass(Fixtures\ClassWithAtInDescriptionAndAnnotation::class); |
93
|
|
|
|
94
|
|
|
self::assertCount(1, $fooAnnot = $reader->getPropertyAnnotations($class->getProperty('foo'))); |
95
|
|
|
self::assertCount(1, $barAnnot = $reader->getPropertyAnnotations($class->getProperty('bar'))); |
96
|
|
|
|
97
|
|
|
self::assertInstanceOf(Fixtures\AnnotationTargetPropertyMethod::class, $fooAnnot[0]); |
98
|
|
|
self::assertInstanceOf(Fixtures\AnnotationTargetPropertyMethod::class, $barAnnot[0]); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function testClassWithWithDanglingComma() |
102
|
|
|
{ |
103
|
|
|
$reader = $this->getReader(); |
104
|
|
|
$annots = $reader->getClassAnnotations(new \ReflectionClass(DummyClassWithDanglingComma::class)); |
105
|
|
|
|
106
|
|
|
self::assertCount(1, $annots); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @expectedException \Doctrine\Annotations\AnnotationException |
111
|
|
|
* @expectedExceptionMessage [Semantical Error] Annotation @AnnotationTargetPropertyMethod is not allowed to be declared on class Doctrine\Tests\Annotations\Fixtures\ClassWithInvalidAnnotationTargetAtClass. You may only use this annotation on these code elements: METHOD, PROPERTY |
112
|
|
|
*/ |
113
|
|
|
public function testClassWithInvalidAnnotationTargetAtClassDocBlock() |
114
|
|
|
{ |
115
|
|
|
$reader = $this->getReader(); |
116
|
|
|
$reader->getClassAnnotations(new \ReflectionClass(Fixtures\ClassWithInvalidAnnotationTargetAtClass::class)); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function testClassWithWithInclude() |
120
|
|
|
{ |
121
|
|
|
$reader = $this->getReader(); |
122
|
|
|
$annots = $reader->getClassAnnotations(new \ReflectionClass(Fixtures\ClassWithRequire::class)); |
123
|
|
|
self::assertCount(1, $annots); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @expectedException \Doctrine\Annotations\AnnotationException |
128
|
|
|
* @expectedExceptionMessage [Semantical Error] Annotation @AnnotationTargetClass is not allowed to be declared on property Doctrine\Tests\Annotations\Fixtures\ClassWithInvalidAnnotationTargetAtProperty::$foo. You may only use this annotation on these code elements: CLASS |
129
|
|
|
*/ |
130
|
|
|
public function testClassWithInvalidAnnotationTargetAtPropertyDocBlock() |
131
|
|
|
{ |
132
|
|
|
$reader = $this->getReader(); |
133
|
|
|
$reader->getPropertyAnnotations(new \ReflectionProperty(Fixtures\ClassWithInvalidAnnotationTargetAtProperty::class, 'foo')); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @expectedException \Doctrine\Annotations\AnnotationException |
138
|
|
|
* @expectedExceptionMessage [Semantical Error] Annotation @AnnotationTargetAnnotation is not allowed to be declared on property Doctrine\Tests\Annotations\Fixtures\ClassWithInvalidAnnotationTargetAtProperty::$bar. You may only use this annotation on these code elements: ANNOTATION |
139
|
|
|
*/ |
140
|
|
|
public function testClassWithInvalidNestedAnnotationTargetAtPropertyDocBlock() |
141
|
|
|
{ |
142
|
|
|
$reader = $this->getReader(); |
143
|
|
|
$reader->getPropertyAnnotations(new \ReflectionProperty(Fixtures\ClassWithInvalidAnnotationTargetAtProperty::class, 'bar')); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @expectedException \Doctrine\Annotations\AnnotationException |
148
|
|
|
* @expectedExceptionMessage [Semantical Error] Annotation @AnnotationTargetClass is not allowed to be declared on method Doctrine\Tests\Annotations\Fixtures\ClassWithInvalidAnnotationTargetAtMethod::functionName(). You may only use this annotation on these code elements: CLASS |
149
|
|
|
*/ |
150
|
|
|
public function testClassWithInvalidAnnotationTargetAtMethodDocBlock() |
151
|
|
|
{ |
152
|
|
|
$reader = $this->getReader(); |
153
|
|
|
$reader->getMethodAnnotations(new \ReflectionMethod(Fixtures\ClassWithInvalidAnnotationTargetAtMethod::class, 'functionName')); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @expectedException \Doctrine\Annotations\AnnotationException |
158
|
|
|
* @expectedExceptionMessage Expected namespace separator or identifier, got ')' at position 24 in class @Doctrine\Tests\Annotations\Fixtures\AnnotationWithTargetSyntaxError. |
159
|
|
|
*/ |
160
|
|
|
public function testClassWithAnnotationWithTargetSyntaxErrorAtClassDocBlock() |
161
|
|
|
{ |
162
|
|
|
$reader = $this->getReader(); |
163
|
|
|
$reader->getClassAnnotations(new \ReflectionClass(Fixtures\ClassWithAnnotationWithTargetSyntaxError::class)); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @expectedException \Doctrine\Annotations\AnnotationException |
168
|
|
|
* @expectedExceptionMessage Expected namespace separator or identifier, got ')' at position 24 in class @Doctrine\Tests\Annotations\Fixtures\AnnotationWithTargetSyntaxError. |
169
|
|
|
*/ |
170
|
|
|
public function testClassWithAnnotationWithTargetSyntaxErrorAtPropertyDocBlock() |
171
|
|
|
{ |
172
|
|
|
$reader = $this->getReader(); |
173
|
|
|
$reader->getPropertyAnnotations(new \ReflectionProperty(Fixtures\ClassWithAnnotationWithTargetSyntaxError::class,'foo')); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @expectedException \Doctrine\Annotations\AnnotationException |
178
|
|
|
* @expectedExceptionMessage Expected namespace separator or identifier, got ')' at position 24 in class @Doctrine\Tests\Annotations\Fixtures\AnnotationWithTargetSyntaxError. |
179
|
|
|
*/ |
180
|
|
|
public function testClassWithAnnotationWithTargetSyntaxErrorAtMethodDocBlock() |
181
|
|
|
{ |
182
|
|
|
$reader = $this->getReader(); |
183
|
|
|
$reader->getMethodAnnotations(new \ReflectionMethod(Fixtures\ClassWithAnnotationWithTargetSyntaxError::class,'bar')); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @expectedException \Doctrine\Annotations\AnnotationException |
188
|
|
|
* @expectedExceptionMessage [Type Error] Attribute "string" of @AnnotationWithVarType declared on property Doctrine\Tests\Annotations\Fixtures\ClassWithAnnotationWithVarType::$invalidProperty expects a(n) string, but got integer. |
189
|
|
|
*/ |
190
|
|
|
public function testClassWithPropertyInvalidVarTypeError() |
191
|
|
|
{ |
192
|
|
|
$reader = $this->getReader(); |
193
|
|
|
$class = new ReflectionClass(Fixtures\ClassWithAnnotationWithVarType::class); |
194
|
|
|
|
195
|
|
|
$reader->getPropertyAnnotations($class->getProperty('invalidProperty')); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @expectedException \Doctrine\Annotations\AnnotationException |
200
|
|
|
* @expectedExceptionMessage [Type Error] Attribute "annotation" of @AnnotationWithVarType declared on method Doctrine\Tests\Annotations\Fixtures\ClassWithAnnotationWithVarType::invalidMethod() expects a(n) \Doctrine\Tests\Annotations\Fixtures\AnnotationTargetAll, but got an instance of Doctrine\Tests\Annotations\Fixtures\AnnotationTargetAnnotation. |
201
|
|
|
*/ |
202
|
|
|
public function testClassWithMethodInvalidVarTypeError() |
203
|
|
|
{ |
204
|
|
|
$reader = $this->getReader(); |
205
|
|
|
$class = new ReflectionClass(Fixtures\ClassWithAnnotationWithVarType::class); |
206
|
|
|
|
207
|
|
|
$reader->getMethodAnnotations($class->getMethod('invalidMethod')); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @expectedException \Doctrine\Annotations\AnnotationException |
212
|
|
|
* @expectedExceptionMessage Expected namespace separator or identifier, got ')' at position 18 in class Doctrine\Tests\Annotations\DummyClassSyntaxError. |
213
|
|
|
*/ |
214
|
|
|
public function testClassSyntaxErrorContext() |
215
|
|
|
{ |
216
|
|
|
$reader = $this->getReader(); |
217
|
|
|
$reader->getClassAnnotations(new \ReflectionClass(DummyClassSyntaxError::class)); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @expectedException \Doctrine\Annotations\AnnotationException |
222
|
|
|
* @expectedExceptionMessage Expected namespace separator or identifier, got ')' at position 18 in method Doctrine\Tests\Annotations\DummyClassMethodSyntaxError::foo(). |
223
|
|
|
*/ |
224
|
|
|
public function testMethodSyntaxErrorContext() |
225
|
|
|
{ |
226
|
|
|
$reader = $this->getReader(); |
227
|
|
|
$reader->getMethodAnnotations(new \ReflectionMethod(DummyClassMethodSyntaxError::class, 'foo')); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @expectedException \Doctrine\Annotations\AnnotationException |
232
|
|
|
* @expectedExceptionMessage Expected namespace separator or identifier, got ')' at position 18 in property Doctrine\Tests\Annotations\DummyClassPropertySyntaxError::$foo. |
233
|
|
|
*/ |
234
|
|
|
public function testPropertySyntaxErrorContext() |
235
|
|
|
{ |
236
|
|
|
$reader = $this->getReader(); |
237
|
|
|
$reader->getPropertyAnnotations(new \ReflectionProperty(DummyClassPropertySyntaxError::class, 'foo')); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @group regression |
242
|
|
|
*/ |
243
|
|
|
public function testMultipleAnnotationsOnSameLine() |
244
|
|
|
{ |
245
|
|
|
$reader = $this->getReader(); |
246
|
|
|
$annots = $reader->getPropertyAnnotations(new \ReflectionProperty(DummyClass2::class, 'id')); |
247
|
|
|
self::assertCount(3, $annots); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
public function testNonAnnotationProblem() |
251
|
|
|
{ |
252
|
|
|
$reader = $this->getReader(); |
253
|
|
|
|
254
|
|
|
self::assertNotNull($annot = $reader->getPropertyAnnotation(new \ReflectionProperty(DummyClassNonAnnotationProblem::class, 'foo'), $name = DummyAnnotation::class)); |
255
|
|
|
self::assertInstanceOf($name, $annot); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
public function testIncludeIgnoreAnnotation() |
259
|
|
|
{ |
260
|
|
|
$reader = $this->getReader(); |
261
|
|
|
|
262
|
|
|
$reader->getPropertyAnnotations(new \ReflectionProperty(Fixtures\ClassWithIgnoreAnnotation::class, 'foo')); |
263
|
|
|
self::assertFalse(class_exists(Fixtures\IgnoreAnnotationClass::class, false)); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
public function testImportWithConcreteAnnotation() |
267
|
|
|
{ |
268
|
|
|
$reader = $this->getReader(); |
269
|
|
|
$property = new \ReflectionProperty(TestImportWithConcreteAnnotation::class, 'field'); |
270
|
|
|
$annotations = $reader->getPropertyAnnotations($property); |
271
|
|
|
self::assertCount(1, $annotations); |
272
|
|
|
self::assertNotNull($reader->getPropertyAnnotation($property, DummyAnnotation::class)); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
public function testImportWithInheritance() |
276
|
|
|
{ |
277
|
|
|
$reader = $this->getReader(); |
278
|
|
|
|
279
|
|
|
$class = new TestParentClass(); |
280
|
|
|
$ref = new \ReflectionClass($class); |
281
|
|
|
|
282
|
|
|
$childAnnotations = $reader->getPropertyAnnotations($ref->getProperty('child')); |
283
|
|
|
self::assertCount(1, $childAnnotations); |
284
|
|
|
self::assertInstanceOf(Foo\Name::class, reset($childAnnotations)); |
285
|
|
|
|
286
|
|
|
$parentAnnotations = $reader->getPropertyAnnotations($ref->getProperty('parent')); |
287
|
|
|
self::assertCount(1, $parentAnnotations); |
288
|
|
|
self::assertInstanceOf(Bar\Name::class, reset($parentAnnotations)); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* @expectedException \Doctrine\Annotations\AnnotationException |
293
|
|
|
* @expectedExceptionMessage The annotation "@NameFoo" in property Doctrine\Tests\Annotations\TestAnnotationNotImportedClass::$field was never imported. |
294
|
|
|
*/ |
295
|
|
|
public function testImportDetectsNotImportedAnnotation() |
296
|
|
|
{ |
297
|
|
|
$reader = $this->getReader(); |
298
|
|
|
$reader->getPropertyAnnotations(new \ReflectionProperty(TestAnnotationNotImportedClass::class, 'field')); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* @expectedException \Doctrine\Annotations\AnnotationException |
303
|
|
|
* @expectedExceptionMessage The annotation "@Foo\Bar\Name" in property Doctrine\Tests\Annotations\TestNonExistentAnnotationClass::$field was never imported. |
304
|
|
|
*/ |
305
|
|
|
public function testImportDetectsNonExistentAnnotation() |
306
|
|
|
{ |
307
|
|
|
$reader = $this->getReader(); |
308
|
|
|
$reader->getPropertyAnnotations(new \ReflectionProperty(TestNonExistentAnnotationClass::class, 'field')); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
public function testTopLevelAnnotation() |
312
|
|
|
{ |
313
|
|
|
$reader = $this->getReader(); |
314
|
|
|
$annotations = $reader->getPropertyAnnotations(new \ReflectionProperty(TestTopLevelAnnotationClass::class, 'field')); |
315
|
|
|
|
316
|
|
|
self::assertCount(1, $annotations); |
317
|
|
|
self::assertInstanceOf(\TopLevelAnnotation::class, reset($annotations)); |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
public function testIgnoresAnnotationsNotPrefixedWithWhitespace() |
321
|
|
|
{ |
322
|
|
|
$reader = $this->getReader(); |
323
|
|
|
|
324
|
|
|
$annotation = $reader->getClassAnnotation(new \ReflectionClass(new TestIgnoresNonAnnotationsClass()), Name::class); |
325
|
|
|
self::assertInstanceOf(Name::class, $annotation); |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
private static $testResetsPhpParserAfterUseRun = false; |
329
|
|
|
|
330
|
|
|
/** |
331
|
|
|
* When getUseStatements isn't available on ReflectionClass the PhpParser has to use token_get_all(). If that |
332
|
|
|
* happens various PHP compiler globals get set, and these can have seriously bad effects on the next file to be |
333
|
|
|
* parsed. |
334
|
|
|
* Notably the doc_comment compiler global can end up containing a docblock comment. The next file to be parsed |
335
|
|
|
* on an include() will have this docblock comment attached to the first thing in the file that the compiler |
336
|
|
|
* considers to own comments. If this is a class then any later calls to getDocComment() for that class will have |
337
|
|
|
* undesirable effects. *sigh* |
338
|
|
|
*/ |
339
|
|
|
public function testResetsPhpParserAfterUse() |
340
|
|
|
{ |
341
|
|
|
// If someone has already included our main test fixture this test is invalid. It's important that our require |
342
|
|
|
// causes this file to be parsed and compiled at a certain point. |
343
|
|
|
self::assertFalse(!self::$testResetsPhpParserAfterUseRun && class_exists(\Doctrine_Tests_Annotations_Fixtures_ClassNoNamespaceNoComment::class), 'Test invalid if class has already been compiled'); |
344
|
|
|
self::$testResetsPhpParserAfterUseRun = true; |
345
|
|
|
|
346
|
|
|
$reader = $this->getReader(); |
347
|
|
|
|
348
|
|
|
// First make sure the annotation cache knows about the annotations we want to use. |
349
|
|
|
// If we don't do this then loading of annotations into the cache will cause the parser to get out of the bad |
350
|
|
|
// state we want to test. |
351
|
|
|
$class = new ReflectionClass(Fixtures\ClassWithValidAnnotationTarget::class); |
352
|
|
|
$reader->getClassAnnotations($class); |
353
|
|
|
|
354
|
|
|
// Now import an incredibly dull class which makes use of the same class level annotation that the previous class does. |
355
|
|
|
$class = new ReflectionClass(Fixtures\ClassWithClassAnnotationOnly::class); |
356
|
|
|
$annotations = $reader->getClassAnnotations($class); |
357
|
|
|
|
358
|
|
|
// This include needs to be here since we need the PHP compiler to run over it as the next thing the PHP |
359
|
|
|
// parser sees since PhpParser called token_get_all() on the intro to ClassWithClassAnnotationOnly. |
360
|
|
|
// Our test class cannot be in a namespace (some versions of PHP reset the doc_comment compiler global when |
361
|
|
|
// you hit a namespace declaration), so cannot be autoloaded. |
362
|
|
|
require_once __DIR__ . '/Fixtures/ClassNoNamespaceNoComment.php'; |
363
|
|
|
|
364
|
|
|
// So, hopefully, if all has gone OK, our class with class annotations should actually have them. |
365
|
|
|
// If this fails then something is quite badly wrong elsewhere. |
366
|
|
|
// Note that if this happens before the require it can cause other PHP files to be included, resetting the |
367
|
|
|
// compiler global state, and invalidating this test case. |
368
|
|
|
self::assertNotEmpty($annotations); |
369
|
|
|
|
370
|
|
|
$annotations = $reader->getClassAnnotations(new \ReflectionClass(new \Doctrine_Tests_Annotations_Fixtures_ClassNoNamespaceNoComment())); |
371
|
|
|
// And if our workaround for this bug is OK, our class with no doc comment should not have any class annotations. |
372
|
|
|
self::assertEmpty($annotations); |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
/** |
376
|
|
|
* @expectedException \Doctrine\Annotations\AnnotationException |
377
|
|
|
* @expectedExceptionMessage The class "Doctrine\Tests\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\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\Annotations\Fixtures\InvalidAnnotationUsageClass. |
378
|
|
|
*/ |
379
|
|
|
public function testErrorWhenInvalidAnnotationIsUsed() |
380
|
|
|
{ |
381
|
|
|
$reader = $this->getReader(); |
382
|
|
|
$ref = new \ReflectionClass(Fixtures\InvalidAnnotationUsageClass::class); |
383
|
|
|
$reader->getClassAnnotations($ref); |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
public function testInvalidAnnotationUsageButIgnoredClass() |
387
|
|
|
{ |
388
|
|
|
$reader = $this->getReader(); |
389
|
|
|
$ref = new \ReflectionClass(Fixtures\InvalidAnnotationUsageButIgnoredClass::class); |
390
|
|
|
$annots = $reader->getClassAnnotations($ref); |
391
|
|
|
|
392
|
|
|
self::assertCount(2, $annots); |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
/** |
396
|
|
|
* @group DDC-1660 |
397
|
|
|
* @group regression |
398
|
|
|
*/ |
399
|
|
|
public function testInvalidAnnotationButIgnored() |
400
|
|
|
{ |
401
|
|
|
$reader = $this->getReader(); |
402
|
|
|
$class = new \ReflectionClass(Fixtures\ClassDDC1660::class); |
403
|
|
|
|
404
|
|
|
self::assertTrue(class_exists(Fixtures\Annotation\Version::class)); |
405
|
|
|
self::assertEmpty($reader->getClassAnnotations($class)); |
406
|
|
|
self::assertEmpty($reader->getMethodAnnotations($class->getMethod('bar'))); |
407
|
|
|
self::assertEmpty($reader->getPropertyAnnotations($class->getProperty('foo'))); |
408
|
|
|
} |
409
|
|
|
|
410
|
|
|
public function testGloballyIgnoredAnnotationNotIgnored() : void |
411
|
|
|
{ |
412
|
|
|
$reader = $this->getReader(); |
413
|
|
|
$class = new \ReflectionClass(Fixtures\ClassDDC1660::class); |
414
|
|
|
|
415
|
|
|
$testLoader = static function (string $className) : bool { |
416
|
|
|
if ($className === 'since') { |
417
|
|
|
throw new \InvalidArgumentException('Globally ignored annotation names should never be passed to an autoloader.'); |
418
|
|
|
} |
419
|
|
|
|
420
|
|
|
return false; |
421
|
|
|
}; |
422
|
|
|
|
423
|
|
|
spl_autoload_register($testLoader, true, true); |
424
|
|
|
|
425
|
|
|
try { |
426
|
|
|
self::assertEmpty($reader->getClassAnnotations($class)); |
427
|
|
|
} finally { |
428
|
|
|
spl_autoload_unregister($testLoader); |
429
|
|
|
} |
430
|
|
|
} |
431
|
|
|
|
432
|
|
|
public function testAnnotationEnumeratorException() |
433
|
|
|
{ |
434
|
|
|
$reader = $this->getReader(); |
435
|
|
|
$class = new \ReflectionClass(Fixtures\ClassWithAnnotationEnum::class); |
436
|
|
|
|
437
|
|
|
self::assertCount(1, $bar = $reader->getMethodAnnotations($class->getMethod('bar'))); |
438
|
|
|
self::assertCount(1, $foo = $reader->getPropertyAnnotations($class->getProperty('foo'))); |
439
|
|
|
|
440
|
|
|
self::assertInstanceOf(Fixtures\AnnotationEnum::class, $bar[0]); |
441
|
|
|
self::assertInstanceOf(Fixtures\AnnotationEnum::class, $foo[0]); |
442
|
|
|
|
443
|
|
|
try { |
444
|
|
|
$reader->getPropertyAnnotations($class->getProperty('invalidProperty')); |
445
|
|
|
$this->fail(); |
446
|
|
|
} catch (AnnotationException $exc) { |
447
|
|
|
self::assertEquals('[Enum Error] Attribute "value" of @Doctrine\Tests\Annotations\Fixtures\AnnotationEnum declared on property Doctrine\Tests\Annotations\Fixtures\ClassWithAnnotationEnum::$invalidProperty accept only [ONE, TWO, THREE], but got FOUR.', $exc->getMessage()); |
448
|
|
|
} |
449
|
|
|
|
450
|
|
|
try { |
451
|
|
|
$reader->getMethodAnnotations($class->getMethod('invalidMethod')); |
452
|
|
|
$this->fail(); |
453
|
|
|
} catch (AnnotationException $exc) { |
454
|
|
|
self::assertEquals('[Enum Error] Attribute "value" of @Doctrine\Tests\Annotations\Fixtures\AnnotationEnum declared on method Doctrine\Tests\Annotations\Fixtures\ClassWithAnnotationEnum::invalidMethod() accept only [ONE, TWO, THREE], but got 5.', $exc->getMessage()); |
455
|
|
|
} |
456
|
|
|
} |
457
|
|
|
|
458
|
|
|
/** |
459
|
|
|
* @group DCOM-106 |
460
|
|
|
*/ |
461
|
|
|
public function testIgnoreFixMeAndUpperCaseToDo() |
462
|
|
|
{ |
463
|
|
|
$reader = $this->getReader(); |
464
|
|
|
$ref = new \ReflectionClass(DCOM106::class); |
465
|
|
|
|
466
|
|
|
self::assertEmpty($reader->getClassAnnotations($ref)); |
467
|
|
|
} |
468
|
|
|
|
469
|
|
|
/** |
470
|
|
|
* @return AnnotationReader |
471
|
|
|
*/ |
472
|
|
|
abstract protected function getReader(); |
473
|
|
|
} |
474
|
|
|
|
475
|
|
|
/** |
476
|
|
|
* @parseAnnotation("var") |
477
|
|
|
* @author Johannes M. Schmitt <[email protected]> |
478
|
|
|
* |
479
|
|
|
*/ |
480
|
|
|
class TestParseAnnotationClass |
481
|
|
|
{ |
482
|
|
|
/** |
483
|
|
|
* @var |
484
|
|
|
*/ |
485
|
|
|
private $field; |
486
|
|
|
} |
487
|
|
|
|
488
|
|
|
/** |
489
|
|
|
* @Name |
490
|
|
|
* @author Johannes M. Schmitt <[email protected]> |
491
|
|
|
*/ |
492
|
|
|
class TestIgnoresNonAnnotationsClass |
493
|
|
|
{ |
494
|
|
|
} |
495
|
|
|
|
496
|
|
|
class TestTopLevelAnnotationClass |
497
|
|
|
{ |
498
|
|
|
/** |
499
|
|
|
* @\TopLevelAnnotation |
500
|
|
|
*/ |
501
|
|
|
private $field; |
502
|
|
|
} |
503
|
|
|
|
504
|
|
|
class TestNonExistentAnnotationClass |
505
|
|
|
{ |
506
|
|
|
/** |
507
|
|
|
* @Foo\Bar\Name |
508
|
|
|
*/ |
509
|
|
|
private $field; |
510
|
|
|
} |
511
|
|
|
|
512
|
|
|
class TestAnnotationNotImportedClass |
513
|
|
|
{ |
514
|
|
|
/** |
515
|
|
|
* @NameFoo |
516
|
|
|
*/ |
517
|
|
|
private $field; |
518
|
|
|
} |
519
|
|
|
|
520
|
|
|
class TestChildClass |
521
|
|
|
{ |
522
|
|
|
/** |
523
|
|
|
* @\Doctrine\Tests\Annotations\Foo\Name(name = "foo") |
524
|
|
|
*/ |
525
|
|
|
protected $child; |
526
|
|
|
} |
527
|
|
|
|
528
|
|
|
class TestParentClass extends TestChildClass |
529
|
|
|
{ |
530
|
|
|
/** |
531
|
|
|
* @\Doctrine\Tests\Annotations\Bar\Name(name = "bar") |
532
|
|
|
*/ |
533
|
|
|
private $parent; |
534
|
|
|
} |
535
|
|
|
|
536
|
|
|
class TestImportWithConcreteAnnotation |
537
|
|
|
{ |
538
|
|
|
/** |
539
|
|
|
* @DummyAnnotation(dummyValue = "bar") |
540
|
|
|
*/ |
541
|
|
|
private $field; |
542
|
|
|
} |
543
|
|
|
|
544
|
|
|
/** |
545
|
|
|
* @ignoreAnnotation("var") |
546
|
|
|
*/ |
547
|
|
|
class DummyClass2 { |
548
|
|
|
/** |
549
|
|
|
* @DummyId @DummyColumn(type="integer") @DummyGeneratedValue |
550
|
|
|
* @var integer |
551
|
|
|
*/ |
552
|
|
|
private $id; |
553
|
|
|
} |
554
|
|
|
|
555
|
|
|
/** @Annotation */ |
556
|
|
|
class DummyId {} |
557
|
|
|
/** @Annotation */ |
558
|
|
|
class DummyColumn { |
559
|
|
|
public $type; |
560
|
|
|
} |
561
|
|
|
/** @Annotation */ |
562
|
|
|
class DummyGeneratedValue {} |
563
|
|
|
/** @Annotation */ |
564
|
|
|
class DummyAnnotation { |
565
|
|
|
public $value; |
566
|
|
|
public $dummyValue; |
567
|
|
|
} |
568
|
|
|
|
569
|
|
|
/** |
570
|
|
|
* @api |
571
|
|
|
* @Annotation |
572
|
|
|
*/ |
573
|
|
|
class DummyAnnotationWithIgnoredAnnotation { |
574
|
|
|
public $dummyValue; |
575
|
|
|
} |
576
|
|
|
|
577
|
|
|
/** @Annotation */ |
578
|
|
|
class DummyJoinColumn { |
579
|
|
|
public $name; |
580
|
|
|
public $referencedColumnName; |
581
|
|
|
} |
582
|
|
|
/** @Annotation */ |
583
|
|
|
class DummyJoinTable { |
584
|
|
|
public $name; |
585
|
|
|
public $joinColumns; |
586
|
|
|
public $inverseJoinColumns; |
587
|
|
|
} |
588
|
|
|
|
589
|
|
|
/** |
590
|
|
|
* @DummyAnnotation(dummyValue = "bar",) |
591
|
|
|
*/ |
592
|
|
|
class DummyClassWithDanglingComma |
593
|
|
|
{ |
594
|
|
|
} |
595
|
|
|
|
596
|
|
|
/** |
597
|
|
|
* @DummyAnnotation(@) |
598
|
|
|
*/ |
599
|
|
|
class DummyClassSyntaxError |
600
|
|
|
{ |
601
|
|
|
|
602
|
|
|
} |
603
|
|
|
|
604
|
|
|
class DummyClassMethodSyntaxError |
605
|
|
|
{ |
606
|
|
|
/** |
607
|
|
|
* @DummyAnnotation(@) |
608
|
|
|
*/ |
609
|
|
|
public function foo() |
610
|
|
|
{ |
611
|
|
|
|
612
|
|
|
} |
613
|
|
|
} |
614
|
|
|
|
615
|
|
|
class DummyClassPropertySyntaxError |
616
|
|
|
{ |
617
|
|
|
/** |
618
|
|
|
* @DummyAnnotation(@) |
619
|
|
|
*/ |
620
|
|
|
public $foo; |
621
|
|
|
} |
622
|
|
|
|
623
|
|
|
/** |
624
|
|
|
* @ignoreAnnotation({"since", "var"}) |
625
|
|
|
*/ |
626
|
|
|
class DummyClassNonAnnotationProblem |
627
|
|
|
{ |
628
|
|
|
/** |
629
|
|
|
* @DummyAnnotation |
630
|
|
|
* |
631
|
|
|
* @var \Test |
632
|
|
|
* @since 0.1 |
633
|
|
|
*/ |
634
|
|
|
public $foo; |
635
|
|
|
} |
636
|
|
|
|
637
|
|
|
|
638
|
|
|
/** |
639
|
|
|
* @DummyAnnotation Foo bar <[email protected]> |
640
|
|
|
*/ |
641
|
|
|
class DummyClassWithEmail |
642
|
|
|
{ |
643
|
|
|
|
644
|
|
|
} |
645
|
|
|
|
646
|
|
|
|
647
|
|
|
/** |
648
|
|
|
* @fixme public |
649
|
|
|
* @TODO |
650
|
|
|
*/ |
651
|
|
|
class DCOM106 |
652
|
|
|
{ |
653
|
|
|
|
654
|
|
|
} |
655
|
|
|
|
656
|
|
|
namespace Doctrine\Tests\Annotations\Foo; |
657
|
|
|
|
658
|
|
|
/** @Annotation */ |
659
|
|
|
class Name |
660
|
|
|
{ |
661
|
|
|
public $name; |
662
|
|
|
} |
663
|
|
|
|
664
|
|
|
namespace Doctrine\Tests\Annotations\Bar; |
665
|
|
|
|
666
|
|
|
/** @Annotation */ |
667
|
|
|
class Name |
668
|
|
|
{ |
669
|
|
|
public $name; |
670
|
|
|
} |
671
|
|
|
|