1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\Tests\Annotations\Metadata\Assembler; |
6
|
|
|
|
7
|
|
|
use Doctrine\Annotations\Assembler\Acceptor\InternalAcceptor; |
8
|
|
|
use Doctrine\Annotations\Assembler\Assembler; |
9
|
|
|
use Doctrine\Annotations\Constructor\Constructor; |
10
|
|
|
use Doctrine\Annotations\Constructor\Instantiator\ConstructorInstantiatorStrategy; |
11
|
|
|
use Doctrine\Annotations\Constructor\Instantiator\Instantiator; |
12
|
|
|
use Doctrine\Annotations\Constructor\Instantiator\PropertyInstantiatorStrategy; |
13
|
|
|
use Doctrine\Annotations\Metadata\AnnotationMetadata; |
14
|
|
|
use Doctrine\Annotations\Metadata\Assembler\AnnotationMetadataAssembler; |
15
|
|
|
use Doctrine\Annotations\Metadata\InternalAnnotations; |
16
|
|
|
use Doctrine\Annotations\Metadata\PropertyMetadata; |
17
|
|
|
use Doctrine\Annotations\Metadata\Reflection\DefaultReflectionProvider; |
18
|
|
|
use Doctrine\Annotations\Metadata\ScopeManufacturer; |
19
|
|
|
use Doctrine\Annotations\Metadata\Type\ObjectType; |
20
|
|
|
use Doctrine\Annotations\Metadata\Type\StringType; |
21
|
|
|
use Doctrine\Annotations\Parser\Ast\Reference; |
22
|
|
|
use Doctrine\Annotations\Parser\Compiler; |
23
|
|
|
use Doctrine\Annotations\Parser\IgnoredAnnotations; |
24
|
|
|
use Doctrine\Annotations\Parser\Imports; |
25
|
|
|
use Doctrine\Annotations\Parser\Reference\FallbackReferenceResolver; |
26
|
|
|
use Doctrine\Annotations\Parser\Reference\StaticReferenceResolver; |
27
|
|
|
use Doctrine\Annotations\Parser\Scope; |
28
|
|
|
use Doctrine\Annotations\PhpParser; |
29
|
|
|
use Doctrine\Annotations\TypeParser\PHPStanTypeParser; |
30
|
|
|
use Doctrine\Tests\Annotations\Fixtures\AnnotationEnum; |
31
|
|
|
use Doctrine\Tests\Annotations\Fixtures\AnnotationTargetAll; |
32
|
|
|
use Doctrine\Tests\Annotations\Fixtures\AnnotationTargetAnnotation; |
33
|
|
|
use Doctrine\Tests\Annotations\Fixtures\AnnotationWithConstants; |
34
|
|
|
use Doctrine\Tests\Annotations\Fixtures\AnnotationWithRequiredAttributes; |
35
|
|
|
use Doctrine\Tests\Annotations\Fixtures\AnnotationWithRequiredAttributesWithoutConstructor; |
36
|
|
|
use Doctrine\Tests\Annotations\Fixtures\AnnotationWithVarType; |
37
|
|
|
use Doctrine\Tests\Annotations\Fixtures\Metadata\AnnotationEnumMetadata; |
38
|
|
|
use Doctrine\Tests\Annotations\Fixtures\Metadata\AnnotationTargetAllMetadata; |
39
|
|
|
use Doctrine\Tests\Annotations\Fixtures\Metadata\AnnotationWithConstantsMetadata; |
40
|
|
|
use Doctrine\Tests\Annotations\Fixtures\Metadata\AnnotationWithVarTypeMetadata; |
41
|
|
|
use Doctrine\Tests\Annotations\Metadata\Type\TestNullableType; |
42
|
|
|
use PHPStan\PhpDocParser\Lexer\Lexer; |
43
|
|
|
use PHPStan\PhpDocParser\Parser\ConstExprParser; |
44
|
|
|
use PHPStan\PhpDocParser\Parser\PhpDocParser; |
45
|
|
|
use PHPStan\PhpDocParser\Parser\TypeParser; |
46
|
|
|
use PHPUnit\Framework\TestCase; |
47
|
|
|
use ReflectionClass; |
48
|
|
|
|
49
|
|
|
class AnnotationMetadataAssemblerTest extends TestCase |
50
|
|
|
{ |
51
|
|
|
/** @var Compiler */ |
52
|
|
|
private $compiler; |
53
|
|
|
|
54
|
|
|
/** @var PhpParser */ |
55
|
|
|
private $phpParser; |
56
|
|
|
|
57
|
|
|
/** @var AnnotationMetadataAssembler */ |
58
|
|
|
private $metadataAssembler; |
59
|
|
|
|
60
|
|
|
/** @var Constructor */ |
61
|
|
|
private $constructor; |
62
|
|
|
|
63
|
|
|
public function setUp() : void |
64
|
|
|
{ |
65
|
|
|
$this->compiler = new Compiler(); |
66
|
|
|
$this->phpParser = new PhpParser(); |
67
|
|
|
$this->constructor = new Constructor( |
68
|
|
|
new Instantiator( |
69
|
|
|
new ConstructorInstantiatorStrategy(), |
70
|
|
|
new PropertyInstantiatorStrategy() |
71
|
|
|
) |
72
|
|
|
); |
73
|
|
|
$this->metadataAssembler = new AnnotationMetadataAssembler( |
74
|
|
|
$this->compiler, |
75
|
|
|
new FallbackReferenceResolver(), |
76
|
|
|
new DefaultReflectionProvider(), |
77
|
|
|
new PHPStanTypeParser(new Lexer(), new PhpDocParser(new TypeParser(), new ConstExprParser()), new FallbackReferenceResolver()), |
78
|
|
|
new ScopeManufacturer($this->phpParser), |
79
|
|
|
new Assembler( |
80
|
|
|
InternalAnnotations::createMetadata(), |
81
|
|
|
new StaticReferenceResolver(), |
82
|
|
|
$this->constructor, |
83
|
|
|
new DefaultReflectionProvider(), |
84
|
|
|
new InternalAcceptor( |
85
|
|
|
new StaticReferenceResolver() |
86
|
|
|
) |
87
|
|
|
) |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @dataProvider validExamples |
93
|
|
|
*/ |
94
|
|
|
public function testAssemblingValidExamples(Reference $reference, Scope $scope, callable $asserter) : void |
95
|
|
|
{ |
96
|
|
|
$metadata = $this->metadataAssembler->assemble($reference, $scope); |
97
|
|
|
|
98
|
|
|
$asserter($metadata); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return mixed[] |
103
|
|
|
*/ |
104
|
|
|
public function validExamples() : iterable |
105
|
|
|
{ |
106
|
|
|
yield 'fixture - AnnotationTargetAll' => [ |
|
|
|
|
107
|
|
|
new Reference(AnnotationTargetAll::class, true), |
108
|
|
|
new Scope(new ReflectionClass($this), new Imports([]), new IgnoredAnnotations()), |
109
|
|
|
function (AnnotationMetadata $metadata) : void { |
110
|
|
|
$this->assertEquals(AnnotationTargetAllMetadata::get(), $metadata); |
111
|
|
|
}, |
112
|
|
|
]; |
113
|
|
|
|
114
|
|
|
yield 'fixture - AnnotationWithRequiredAttributes' => [ |
115
|
|
|
new Reference(AnnotationWithRequiredAttributes::class, true), |
116
|
|
|
new Scope(new ReflectionClass($this), new Imports([]), new IgnoredAnnotations()), |
117
|
|
|
function (AnnotationMetadata $metadata) : void { |
118
|
|
|
$this->assertSame(AnnotationWithRequiredAttributes::class, $metadata->getName()); |
119
|
|
|
$this->assertTrue($metadata->getTarget()->all(), 'Invalid target'); |
120
|
|
|
$this->assertTrue($metadata->hasConstructor(), 'Has no constructor'); |
121
|
|
|
$properties = $metadata->getProperties(); |
122
|
|
|
$this->assertEmpty($properties); |
123
|
|
|
$this->assertNull($metadata->getDefaultProperty()); |
124
|
|
|
}, |
125
|
|
|
]; |
126
|
|
|
|
127
|
|
|
yield 'fixture - AnnotationWithRequiredAttributesWithoutConstructor' => [ |
128
|
|
|
new Reference(AnnotationWithRequiredAttributesWithoutConstructor::class, true), |
129
|
|
|
new Scope(new ReflectionClass($this), new Imports([]), new IgnoredAnnotations()), |
130
|
|
|
function (AnnotationMetadata $metadata) : void { |
131
|
|
|
$this->assertSame(AnnotationWithRequiredAttributesWithoutConstructor::class, $metadata->getName()); |
132
|
|
|
$this->assertTrue($metadata->getTarget()->all(), 'Invalid target'); |
133
|
|
|
$this->assertFalse($metadata->hasConstructor(), 'Has constructor'); |
134
|
|
|
$properties = $metadata->getProperties(); |
135
|
|
|
$expectedProperties = [ |
136
|
|
|
'value' => new PropertyMetadata( |
137
|
|
|
'value', |
138
|
|
|
TestNullableType::fromType(new StringType()), |
139
|
|
|
true, |
140
|
|
|
[], |
141
|
|
|
true |
142
|
|
|
), |
143
|
|
|
'annot' => new PropertyMetadata( |
144
|
|
|
'annot', |
145
|
|
|
TestNullableType::fromType(new ObjectType(AnnotationTargetAnnotation::class)), |
146
|
|
|
false, |
147
|
|
|
[], |
148
|
|
|
true |
149
|
|
|
), |
150
|
|
|
]; |
151
|
|
|
|
152
|
|
|
$this->assertEquals( |
153
|
|
|
$expectedProperties, |
154
|
|
|
$properties |
155
|
|
|
); |
156
|
|
|
$this->assertEquals( |
157
|
|
|
$expectedProperties['value'], |
158
|
|
|
$metadata->getDefaultProperty() |
159
|
|
|
); |
160
|
|
|
}, |
161
|
|
|
]; |
162
|
|
|
|
163
|
|
|
yield 'fixture - AnnotationWithVarType' => [ |
164
|
|
|
new Reference(AnnotationWithVarType::class, true), |
165
|
|
|
new Scope(new ReflectionClass($this), new Imports([]), new IgnoredAnnotations()), |
166
|
|
|
function (AnnotationMetadata $metadata) : void { |
167
|
|
|
$this->assertEquals(AnnotationWithVarTypeMetadata::get(), $metadata); |
168
|
|
|
}, |
169
|
|
|
]; |
170
|
|
|
|
171
|
|
|
yield 'fixture - AnnotationWithConstants' => [ |
172
|
|
|
new Reference(AnnotationWithConstants::class, true), |
173
|
|
|
new Scope(new ReflectionClass($this), new Imports([]), new IgnoredAnnotations()), |
174
|
|
|
function (AnnotationMetadata $metadata) : void { |
175
|
|
|
$this->assertEquals(AnnotationWithConstantsMetadata::get(), $metadata); |
176
|
|
|
}, |
177
|
|
|
]; |
178
|
|
|
|
179
|
|
|
yield 'fixture - AnnotationEnum' => [ |
180
|
|
|
new Reference(AnnotationEnum::class, true), |
181
|
|
|
new Scope(new ReflectionClass($this), new Imports([]), new IgnoredAnnotations()), |
182
|
|
|
static function (AnnotationMetadata $metadata) : void { |
183
|
|
|
self::assertEquals(AnnotationEnumMetadata::get(), $metadata); |
184
|
|
|
}, |
185
|
|
|
]; |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|