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\ClassWithAnnotationTargetAll; |
26
|
|
|
use Doctrine\Tests\Annotations\Fixtures\ClassWithAnnotationWithConstants; |
27
|
|
|
use Doctrine\Tests\Annotations\Fixtures\ClassWithFullValidUsageOfAnnotationWithVarType; |
28
|
|
|
use Doctrine\Tests\Annotations\Fixtures\Metadata\AnnotationTargetAllMetadata; |
29
|
|
|
use Doctrine\Tests\Annotations\Fixtures\Metadata\AnnotationWithConstantsMetadata; |
30
|
|
|
use Doctrine\Tests\Annotations\Fixtures\Metadata\AnnotationWithVarTypeMetadata; |
31
|
|
|
use PHPUnit\Framework\TestCase; |
32
|
|
|
use ReflectionClass; |
33
|
|
|
use function iterator_to_array; |
34
|
|
|
|
35
|
|
|
class AssemblerTest extends TestCase |
36
|
|
|
{ |
37
|
|
|
/** @var Compiler */ |
38
|
|
|
private $compiler; |
39
|
|
|
|
40
|
|
|
/** @var PhpParser */ |
41
|
|
|
private $phpParser; |
42
|
|
|
|
43
|
|
|
public function setUp() : void |
44
|
|
|
{ |
45
|
|
|
$this->compiler = new Compiler(); |
46
|
|
|
$this->phpParser = new PhpParser(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param AnnotationMetadata[] $metadata |
51
|
|
|
* |
52
|
|
|
* @dataProvider validExamples |
53
|
|
|
*/ |
54
|
|
|
public function testAssemblingValidExamples(string $class, array $metadata, callable $asserter) : void |
55
|
|
|
{ |
56
|
|
|
$reflection = new ReflectionClass($class); |
57
|
|
|
$ast = $this->compiler->compile($reflection->getDocComment()); |
|
|
|
|
58
|
|
|
$scope = $this->createScope($reflection); |
59
|
|
|
|
60
|
|
|
$metadataCollection = new MetadataCollection(...$metadata); |
61
|
|
|
|
62
|
|
|
$assembler = $this->createAssembler($metadataCollection); |
63
|
|
|
|
64
|
|
|
$result = $assembler->collect($ast, $scope); |
65
|
|
|
|
66
|
|
|
$asserter(iterator_to_array($result)); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return mixed[] |
71
|
|
|
*/ |
72
|
|
|
public function validExamples() : iterable |
73
|
|
|
{ |
74
|
|
|
yield 'fixture - ClassWithAnnotationTargetAll' => [ |
|
|
|
|
75
|
|
|
ClassWithAnnotationTargetAll::class, |
76
|
|
|
[AnnotationTargetAllMetadata::get()], |
77
|
|
|
function (array $result) : void { |
78
|
|
|
$this->assertCount(1, $result); |
79
|
|
|
/** @var AnnotationTargetAll $resultAnnotation */ |
80
|
|
|
$resultAnnotation = $result[0]; |
81
|
|
|
$this->assertInstanceOf(AnnotationTargetAll::class, $resultAnnotation); |
82
|
|
|
$this->assertSame(123, $resultAnnotation->name); |
83
|
|
|
}, |
84
|
|
|
]; |
85
|
|
|
|
86
|
|
|
yield 'fixture - ClassWithFullValidUsageOfAnnotationWithVarType' => [ |
87
|
|
|
ClassWithFullValidUsageOfAnnotationWithVarType::class, |
88
|
|
|
[ |
89
|
|
|
AnnotationTargetAllMetadata::get(), |
90
|
|
|
AnnotationWithVarTypeMetadata::get(), |
91
|
|
|
], |
92
|
|
|
function (array $result) : void { |
93
|
|
|
$this->assertCount(1, $result); |
94
|
|
|
/** @var AnnotationWithVarType $resultAnnotation */ |
95
|
|
|
$resultAnnotation = $result[0]; |
96
|
|
|
$this->assertInstanceOf(AnnotationWithVarType::class, $resultAnnotation); |
97
|
|
|
|
98
|
|
|
$this->assertNull($resultAnnotation->mixed); |
99
|
|
|
$this->assertTrue($resultAnnotation->boolean); |
100
|
|
|
$this->assertFalse($resultAnnotation->bool); |
101
|
|
|
$this->assertSame(3.14, $resultAnnotation->float); |
102
|
|
|
$this->assertSame('foo', $resultAnnotation->string); |
103
|
|
|
$this->assertSame(42, $resultAnnotation->integer); |
104
|
|
|
$this->assertSame(['foo', 42, false], $resultAnnotation->array); |
105
|
|
|
$this->assertSame(['foo' => 'bar'], $resultAnnotation->arrayMap); |
106
|
|
|
$this->assertInstanceOf(AnnotationTargetAll::class, $resultAnnotation->annotation); |
107
|
|
|
$this->assertSame('baz', $resultAnnotation->annotation->name); |
108
|
|
|
$this->assertSame([1, 2, 3], $resultAnnotation->arrayOfIntegers); |
109
|
|
|
$this->assertSame(['foo', 'bar', 'baz'], $resultAnnotation->arrayOfStrings); |
110
|
|
|
|
111
|
|
|
$this->assertInternalType('array', $resultAnnotation->arrayOfAnnotations); |
112
|
|
|
$this->assertCount(2, $resultAnnotation->arrayOfAnnotations); |
113
|
|
|
$this->assertInstanceOf(AnnotationTargetAll::class, $resultAnnotation->arrayOfAnnotations[0]); |
114
|
|
|
$this->assertNull($resultAnnotation->arrayOfAnnotations[0]->name); |
115
|
|
|
$this->assertInstanceOf(AnnotationTargetAll::class, $resultAnnotation->arrayOfAnnotations[1]); |
116
|
|
|
$this->assertSame(123, $resultAnnotation->arrayOfAnnotations[1]->name); |
117
|
|
|
}, |
118
|
|
|
]; |
119
|
|
|
|
120
|
|
|
yield 'fixture - ClassWithAnnotationWithConstants' => [ |
121
|
|
|
ClassWithAnnotationWithConstants::class, |
122
|
|
|
[AnnotationWithConstantsMetadata::get()], |
123
|
|
|
function (array $result) : void { |
124
|
|
|
$this->assertCount(1, $result); |
125
|
|
|
/** @var AnnotationWithConstants $resultAnnotation */ |
126
|
|
|
$resultAnnotation = $result[0]; |
127
|
|
|
$this->assertInstanceOf(AnnotationWithConstants::class, $resultAnnotation); |
128
|
|
|
|
129
|
|
|
$this->assertSame(AnnotationWithConstants::FLOAT, $resultAnnotation->value); |
130
|
|
|
}, |
131
|
|
|
]; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
private function createScope(ReflectionClass $reflection) : Scope |
135
|
|
|
{ |
136
|
|
|
return new Scope( |
137
|
|
|
$reflection, |
138
|
|
|
new Imports($this->phpParser->parseClass($reflection)), |
139
|
|
|
new IgnoredAnnotations() |
140
|
|
|
); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
private function createAssembler(MetadataCollection $collection) : Assembler |
144
|
|
|
{ |
145
|
|
|
return new Assembler( |
146
|
|
|
$collection, |
147
|
|
|
new FallbackReferenceResolver(), |
148
|
|
|
new Constructor( |
149
|
|
|
new Instantiator( |
150
|
|
|
new ConstructorInstantiatorStrategy(), |
151
|
|
|
new PropertyInstantiatorStrategy() |
152
|
|
|
) |
153
|
|
|
), |
154
|
|
|
new DefaultReflectionProvider(), |
155
|
|
|
new AlwaysAcceptingAcceptor() |
156
|
|
|
); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|