1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\Tests\Annotations\Assembler; |
6
|
|
|
|
7
|
|
|
use Doctrine\Annotations\Assembler\Assembler; |
8
|
|
|
use Doctrine\Annotations\Constructor\Constructor; |
9
|
|
|
use Doctrine\Annotations\Constructor\Instantiator\ConstructorInstantiatorStrategy; |
10
|
|
|
use Doctrine\Annotations\Constructor\Instantiator\Instantiator; |
11
|
|
|
use Doctrine\Annotations\Constructor\Instantiator\PropertyInstantiatorStrategy; |
12
|
|
|
use Doctrine\Annotations\Metadata\AnnotationMetadata; |
13
|
|
|
use Doctrine\Annotations\Metadata\MetadataCollection; |
14
|
|
|
use Doctrine\Annotations\Metadata\Reflection\DefaultReflectionProvider; |
15
|
|
|
use Doctrine\Annotations\Parser\Compiler; |
16
|
|
|
use Doctrine\Annotations\Parser\IgnoredAnnotations; |
17
|
|
|
use Doctrine\Annotations\Parser\Imports; |
18
|
|
|
use Doctrine\Annotations\Parser\Reference\FallbackReferenceResolver; |
19
|
|
|
use Doctrine\Annotations\Parser\Scope; |
20
|
|
|
use Doctrine\Annotations\PhpParser; |
21
|
|
|
use Doctrine\Tests\Annotations\Assembler\Acceptor\AlwaysAcceptingAcceptor; |
22
|
|
|
use Doctrine\Tests\Annotations\Fixtures\AnnotationTargetAll; |
23
|
|
|
use Doctrine\Tests\Annotations\Fixtures\AnnotationWithConstants; |
24
|
|
|
use Doctrine\Tests\Annotations\Fixtures\AnnotationWithVarType; |
25
|
|
|
use Doctrine\Tests\Annotations\Fixtures\Metadata\AnnotationTargetAllMetadata; |
26
|
|
|
use Doctrine\Tests\Annotations\Fixtures\Metadata\AnnotationWithConstantsMetadata; |
27
|
|
|
use Doctrine\Tests\Annotations\Fixtures\Metadata\AnnotationWithVarTypeMetadata; |
28
|
|
|
use PHPUnit\Framework\TestCase; |
29
|
|
|
use ReflectionClass; |
30
|
|
|
use function iterator_to_array; |
31
|
|
|
|
32
|
|
|
class AssemblerTest extends TestCase |
33
|
|
|
{ |
34
|
|
|
/** @var Compiler */ |
35
|
|
|
private $compiler; |
36
|
|
|
|
37
|
|
|
/** @var PhpParser */ |
38
|
|
|
private $phpParser; |
39
|
|
|
|
40
|
|
|
public function setUp() : void |
41
|
|
|
{ |
42
|
|
|
$this->compiler = new Compiler(); |
43
|
|
|
$this->phpParser = new PhpParser(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param AnnotationMetadata[] $metadata |
48
|
|
|
* |
49
|
|
|
* @dataProvider validExamples |
50
|
|
|
*/ |
51
|
|
|
public function testAssemblingValidExamples(string $class, array $metadata, callable $asserter) : void |
52
|
|
|
{ |
53
|
|
|
$reflection = new ReflectionClass($class); |
54
|
|
|
$ast = $this->compiler->compile($reflection->getDocComment()); |
|
|
|
|
55
|
|
|
$scope = $this->createScope($reflection); |
56
|
|
|
|
57
|
|
|
$metadataCollection = new MetadataCollection(...$metadata); |
58
|
|
|
|
59
|
|
|
$assembler = $this->createAssembler($metadataCollection); |
60
|
|
|
|
61
|
|
|
$result = $assembler->collect($ast, $scope); |
62
|
|
|
|
63
|
|
|
$asserter(iterator_to_array($result)); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function validExamples() : iterable |
67
|
|
|
{ |
68
|
|
|
yield 'fixture - ClassWithAnnotationTargetAll' => [ |
69
|
|
|
ClassWithAnnotationTargetAll::class, |
70
|
|
|
[AnnotationTargetAllMetadata::get()], |
71
|
|
|
function (array $result) : void { |
72
|
|
|
$this->assertCount(1, $result); |
73
|
|
|
/** @var AnnotationTargetAll $resultAnnotation */ |
74
|
|
|
$resultAnnotation = $result[0]; |
75
|
|
|
$this->assertInstanceOf(AnnotationTargetAll::class, $resultAnnotation); |
76
|
|
|
$this->assertSame(123, $resultAnnotation->name); |
77
|
|
|
}, |
78
|
|
|
]; |
79
|
|
|
|
80
|
|
|
yield 'fixture - ClassWithFullValidUsageOfAnnotationWithVarType' => [ |
81
|
|
|
ClassWithFullValidUsageOfAnnotationWithVarType::class, |
82
|
|
|
[ |
83
|
|
|
AnnotationTargetAllMetadata::get(), |
84
|
|
|
AnnotationWithVarTypeMetadata::get(), |
85
|
|
|
], |
86
|
|
|
function (array $result) : void { |
87
|
|
|
$this->assertCount(1, $result); |
88
|
|
|
/** @var AnnotationWithVarType $resultAnnotation */ |
89
|
|
|
$resultAnnotation = $result[0]; |
90
|
|
|
$this->assertInstanceOf(AnnotationWithVarType::class, $resultAnnotation); |
91
|
|
|
|
92
|
|
|
$this->assertNull($resultAnnotation->mixed); |
93
|
|
|
$this->assertTrue($resultAnnotation->boolean); |
94
|
|
|
$this->assertFalse($resultAnnotation->bool); |
95
|
|
|
$this->assertSame(3.14, $resultAnnotation->float); |
96
|
|
|
$this->assertSame('foo', $resultAnnotation->string); |
97
|
|
|
$this->assertSame(42, $resultAnnotation->integer); |
98
|
|
|
$this->assertSame(['foo', 42, false], $resultAnnotation->array); |
99
|
|
|
$this->assertSame(['foo' => 'bar'], $resultAnnotation->arrayMap); |
100
|
|
|
$this->assertInstanceOf(AnnotationTargetAll::class, $resultAnnotation->annotation); |
101
|
|
|
$this->assertSame('baz', $resultAnnotation->annotation->name); |
102
|
|
|
$this->assertSame([1, 2, 3], $resultAnnotation->arrayOfIntegers); |
103
|
|
|
$this->assertSame(['foo', 'bar', 'baz'], $resultAnnotation->arrayOfStrings); |
104
|
|
|
|
105
|
|
|
$this->assertInternalType('array', $resultAnnotation->arrayOfAnnotations); |
106
|
|
|
$this->assertCount(2, $resultAnnotation->arrayOfAnnotations); |
107
|
|
|
$this->assertInstanceOf(AnnotationTargetAll::class, $resultAnnotation->arrayOfAnnotations[0]); |
108
|
|
|
$this->assertNull($resultAnnotation->arrayOfAnnotations[0]->name); |
109
|
|
|
$this->assertInstanceOf(AnnotationTargetAll::class, $resultAnnotation->arrayOfAnnotations[1]); |
110
|
|
|
$this->assertSame(123, $resultAnnotation->arrayOfAnnotations[1]->name); |
111
|
|
|
}, |
112
|
|
|
]; |
113
|
|
|
|
114
|
|
|
yield 'fixture - ClassWithAnnotationWithConstants' => [ |
115
|
|
|
ClassWithAnnotationWithConstants::class, |
116
|
|
|
[AnnotationWithConstantsMetadata::get()], |
117
|
|
|
function (array $result) : void { |
118
|
|
|
$this->assertCount(1, $result); |
119
|
|
|
/** @var AnnotationWithConstants $resultAnnotation */ |
120
|
|
|
$resultAnnotation = $result[0]; |
121
|
|
|
$this->assertInstanceOf(AnnotationWithConstants::class, $resultAnnotation); |
122
|
|
|
|
123
|
|
|
$this->assertSame(AnnotationWithConstants::FLOAT, $resultAnnotation->value); |
124
|
|
|
}, |
125
|
|
|
]; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
private function createScope(ReflectionClass $reflection) : Scope |
129
|
|
|
{ |
130
|
|
|
return new Scope( |
131
|
|
|
$reflection, |
132
|
|
|
new Imports($this->phpParser->parseClass($reflection)), |
133
|
|
|
new IgnoredAnnotations() |
134
|
|
|
); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
private function createAssembler(MetadataCollection $collection) : Assembler |
138
|
|
|
{ |
139
|
|
|
return new Assembler( |
140
|
|
|
$collection, |
141
|
|
|
new FallbackReferenceResolver(), |
142
|
|
|
new Constructor( |
143
|
|
|
new Instantiator( |
144
|
|
|
new ConstructorInstantiatorStrategy(), |
145
|
|
|
new PropertyInstantiatorStrategy() |
146
|
|
|
) |
147
|
|
|
), |
148
|
|
|
new DefaultReflectionProvider(), |
149
|
|
|
new AlwaysAcceptingAcceptor() |
150
|
|
|
); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @AnnotationTargetAll(name=123) |
156
|
|
|
*/ |
157
|
|
|
class ClassWithAnnotationTargetAll |
158
|
|
|
{ |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @AnnotationWithConstants(value=AnnotationWithConstants::FLOAT) |
163
|
|
|
*/ |
164
|
|
|
class ClassWithAnnotationWithConstants |
165
|
|
|
{ |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @AnnotationWithVarType( |
170
|
|
|
* mixed=null, |
171
|
|
|
* boolean=true, |
172
|
|
|
* bool=false, |
173
|
|
|
* float=3.14, |
174
|
|
|
* string="foo", |
175
|
|
|
* integer=42, |
176
|
|
|
* array={"foo", 42, false}, |
177
|
|
|
* arrayMap={"foo": "bar"}, |
178
|
|
|
* annotation=@AnnotationTargetAll(name="baz"), |
179
|
|
|
* arrayOfIntegers={1,2,3}, |
180
|
|
|
* arrayOfStrings={"foo","bar","baz"}, |
181
|
|
|
* arrayOfAnnotations={ |
182
|
|
|
* @AnnotationTargetAll, |
183
|
|
|
* @AnnotationTargetAll(name=123) |
184
|
|
|
* } |
185
|
|
|
* ) |
186
|
|
|
*/ |
187
|
|
|
class ClassWithFullValidUsageOfAnnotationWithVarType |
188
|
|
|
{ |
189
|
|
|
} |
190
|
|
|
|