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