|
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\Assembler\Constant\ReflectionConstantResolver; |
|
10
|
|
|
use Doctrine\Annotations\Constructor\Constructor; |
|
11
|
|
|
use Doctrine\Annotations\Constructor\Instantiator\ConstructorInstantiatorStrategy; |
|
12
|
|
|
use Doctrine\Annotations\Constructor\Instantiator\Instantiator; |
|
13
|
|
|
use Doctrine\Annotations\Constructor\Instantiator\PropertyInstantiatorStrategy; |
|
14
|
|
|
use Doctrine\Annotations\Metadata\AnnotationMetadata; |
|
15
|
|
|
use Doctrine\Annotations\Metadata\Assembler\AnnotationMetadataAssembler; |
|
16
|
|
|
use Doctrine\Annotations\Metadata\InternalAnnotations; |
|
17
|
|
|
use Doctrine\Annotations\Metadata\PropertyMetadata; |
|
18
|
|
|
use Doctrine\Annotations\Metadata\Reflection\DefaultReflectionProvider; |
|
19
|
|
|
use Doctrine\Annotations\Metadata\ScopeManufacturer; |
|
20
|
|
|
use Doctrine\Annotations\Metadata\Type\ObjectType; |
|
21
|
|
|
use Doctrine\Annotations\Metadata\Type\StringType; |
|
22
|
|
|
use Doctrine\Annotations\Parser\Ast\Reference; |
|
23
|
|
|
use Doctrine\Annotations\Parser\Compiler; |
|
24
|
|
|
use Doctrine\Annotations\Parser\IgnoredAnnotations; |
|
25
|
|
|
use Doctrine\Annotations\Parser\Imports; |
|
26
|
|
|
use Doctrine\Annotations\Parser\Reference\FallbackReferenceResolver; |
|
27
|
|
|
use Doctrine\Annotations\Parser\Reference\StaticReferenceResolver; |
|
28
|
|
|
use Doctrine\Annotations\Parser\Scope; |
|
29
|
|
|
use Doctrine\Annotations\PhpParser; |
|
30
|
|
|
use Doctrine\Annotations\TypeParser\PHPStanTypeParser; |
|
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\AnnotationTargetAllMetadata; |
|
38
|
|
|
use Doctrine\Tests\Annotations\Fixtures\Metadata\AnnotationWithConstantsMetadata; |
|
39
|
|
|
use Doctrine\Tests\Annotations\Fixtures\Metadata\AnnotationWithVarTypeMetadata; |
|
40
|
|
|
use Doctrine\Tests\Annotations\Metadata\Type\TestNullableType; |
|
41
|
|
|
use PHPStan\PhpDocParser\Lexer\Lexer; |
|
42
|
|
|
use PHPStan\PhpDocParser\Parser\ConstExprParser; |
|
43
|
|
|
use PHPStan\PhpDocParser\Parser\PhpDocParser; |
|
44
|
|
|
use PHPStan\PhpDocParser\Parser\TypeParser; |
|
45
|
|
|
use PHPUnit\Framework\TestCase; |
|
46
|
|
|
use ReflectionClass; |
|
47
|
|
|
|
|
48
|
|
|
class AnnotationMetadataAssemblerTest extends TestCase |
|
49
|
|
|
{ |
|
50
|
|
|
/** @var Compiler */ |
|
51
|
|
|
private $compiler; |
|
52
|
|
|
|
|
53
|
|
|
/** @var PhpParser */ |
|
54
|
|
|
private $phpParser; |
|
55
|
|
|
|
|
56
|
|
|
/** @var AnnotationMetadataAssembler */ |
|
57
|
|
|
private $metadataAssembler; |
|
58
|
|
|
|
|
59
|
|
|
/** @var Constructor */ |
|
60
|
|
|
private $constructor; |
|
61
|
|
|
|
|
62
|
|
|
public function setUp() : void |
|
63
|
|
|
{ |
|
64
|
|
|
$this->compiler = new Compiler(); |
|
65
|
|
|
$this->phpParser = new PhpParser(); |
|
66
|
|
|
$this->constructor = new Constructor( |
|
67
|
|
|
new Instantiator( |
|
68
|
|
|
new ConstructorInstantiatorStrategy(), |
|
69
|
|
|
new PropertyInstantiatorStrategy() |
|
70
|
|
|
) |
|
71
|
|
|
); |
|
72
|
|
|
$this->metadataAssembler = new AnnotationMetadataAssembler( |
|
73
|
|
|
$this->compiler, |
|
74
|
|
|
new FallbackReferenceResolver(), |
|
75
|
|
|
new DefaultReflectionProvider(), |
|
76
|
|
|
new PHPStanTypeParser(new Lexer(), new PhpDocParser(new TypeParser(), new ConstExprParser()), new FallbackReferenceResolver()), |
|
77
|
|
|
new ScopeManufacturer($this->phpParser), |
|
78
|
|
|
new Assembler( |
|
79
|
|
|
InternalAnnotations::createMetadata(), |
|
80
|
|
|
new StaticReferenceResolver(), |
|
81
|
|
|
$this->constructor, |
|
82
|
|
|
new DefaultReflectionProvider(), |
|
83
|
|
|
new InternalAcceptor( |
|
84
|
|
|
new StaticReferenceResolver() |
|
85
|
|
|
), |
|
86
|
|
|
new ReflectionConstantResolver(new DefaultReflectionProvider()) |
|
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
|
|
|
$this->assertEquals( |
|
136
|
|
|
[ |
|
137
|
|
|
'value' => new PropertyMetadata( |
|
138
|
|
|
'value', |
|
139
|
|
|
TestNullableType::fromType(new StringType()), |
|
140
|
|
|
true |
|
141
|
|
|
), |
|
142
|
|
|
'annot' => new PropertyMetadata( |
|
143
|
|
|
'annot', |
|
144
|
|
|
TestNullableType::fromType(new ObjectType(AnnotationTargetAnnotation::class)) |
|
145
|
|
|
), |
|
146
|
|
|
], |
|
147
|
|
|
$properties |
|
148
|
|
|
); |
|
149
|
|
|
$this->assertEquals( |
|
150
|
|
|
new PropertyMetadata('value', TestNullableType::fromType(new StringType()), true), |
|
151
|
|
|
$metadata->getDefaultProperty() |
|
152
|
|
|
); |
|
153
|
|
|
}, |
|
154
|
|
|
]; |
|
155
|
|
|
|
|
156
|
|
|
yield 'fixture - AnnotationWithVarType' => [ |
|
157
|
|
|
new Reference(AnnotationWithVarType::class, true), |
|
158
|
|
|
new Scope(new ReflectionClass($this), new Imports([]), new IgnoredAnnotations()), |
|
159
|
|
|
function (AnnotationMetadata $metadata) : void { |
|
160
|
|
|
$this->assertEquals(AnnotationWithVarTypeMetadata::get(), $metadata); |
|
161
|
|
|
}, |
|
162
|
|
|
]; |
|
163
|
|
|
|
|
164
|
|
|
yield 'fixture - AnnotationWithConstants' => [ |
|
165
|
|
|
new Reference(AnnotationWithConstants::class, true), |
|
166
|
|
|
new Scope(new ReflectionClass($this), new Imports([]), new IgnoredAnnotations()), |
|
167
|
|
|
function (AnnotationMetadata $metadata) : void { |
|
168
|
|
|
$this->assertEquals(AnnotationWithConstantsMetadata::get(), $metadata); |
|
169
|
|
|
}, |
|
170
|
|
|
]; |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
|