Failed Conditions
Pull Request — new-parser-ast-metadata (#3)
by
unknown
01:53
created

AnnotationMetadataAssemblerTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

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