Failed Conditions
Pull Request — new-parser-ast-metadata (#2)
by
unknown
02:16
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\DefaultAnnotationMetadataAssembler;
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 DefaultAnnotationMetadataAssembler(
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
            )
87
        );
88
    }
89
90
    /**
91
     * @dataProvider validExamples
92
     */
93
    public function testAssemblingValidExamples(Reference $reference, Scope $scope, callable $asserter) : void
94
    {
95
        $metadata = $this->metadataAssembler->assemble($reference, $scope);
96
97
        $asserter($metadata);
98
    }
99
100
    public function validExamples() : iterable
101
    {
102
        yield 'fixture - AnnotationTargetAll' => [
103
            new Reference(AnnotationTargetAll::class, true),
104
            new Scope(new ReflectionClass($this), new Imports([]), new IgnoredAnnotations()),
105
            function (AnnotationMetadata $metadata) : void {
106
                $this->assertEquals(AnnotationTargetAllMetadata::get(), $metadata);
107
            },
108
        ];
109
110
        yield 'fixture - AnnotationWithRequiredAttributes' => [
111
            new Reference(AnnotationWithRequiredAttributes::class, true),
112
            new Scope(new ReflectionClass($this), new Imports([]), new IgnoredAnnotations()),
113
            function (AnnotationMetadata $metadata) : void {
114
                $this->assertSame(AnnotationWithRequiredAttributes::class, $metadata->getName());
115
                $this->assertTrue($metadata->getTarget()->all(), 'Invalid target');
116
                $this->assertTrue($metadata->hasConstructor(), 'Has no constructor');
117
                $properties = $metadata->getProperties();
118
                $this->assertEmpty($properties);
119
                $this->assertNull($metadata->getDefaultProperty());
120
            },
121
        ];
122
123
        yield 'fixture - AnnotationWithRequiredAttributesWithoutConstructor' => [
124
            new Reference(AnnotationWithRequiredAttributesWithoutConstructor::class, true),
125
            new Scope(new ReflectionClass($this), new Imports([]), new IgnoredAnnotations()),
126
            function (AnnotationMetadata $metadata) : void {
127
                $this->assertSame(AnnotationWithRequiredAttributesWithoutConstructor::class, $metadata->getName());
128
                $this->assertTrue($metadata->getTarget()->all(), 'Invalid target');
129
                $this->assertFalse($metadata->hasConstructor(), 'Has constructor');
130
                $properties = $metadata->getProperties();
131
                $this->assertEquals(
132
                    [
133
                        'value' => new PropertyMetadata(
134
                            'value',
135
                            TestNullableType::fromType(new StringType()),
136
                            true
137
                        ),
138
                        'annot' => new PropertyMetadata(
139
                            'annot',
140
                            TestNullableType::fromType(new ObjectType(AnnotationTargetAnnotation::class))
141
                        ),
142
                    ],
143
                    $properties
144
                );
145
                $this->assertEquals(
146
                    new PropertyMetadata('value', TestNullableType::fromType(new StringType()), true),
147
                    $metadata->getDefaultProperty()
148
                );
149
            },
150
        ];
151
152
        yield 'fixture - AnnotationWithVarType' => [
153
            new Reference(AnnotationWithVarType::class, true),
154
            new Scope(new ReflectionClass($this), new Imports([]), new IgnoredAnnotations()),
155
            function (AnnotationMetadata $metadata) : void {
156
                $this->assertEquals(AnnotationWithVarTypeMetadata::get(), $metadata);
157
            },
158
        ];
159
160
        yield 'fixture - AnnotationWithConstants' => [
161
            new Reference(AnnotationWithConstants::class, true),
162
            new Scope(new ReflectionClass($this), new Imports([]), new IgnoredAnnotations()),
163
            function (AnnotationMetadata $metadata) : void {
164
                $this->assertEquals(AnnotationWithConstantsMetadata::get(), $metadata);
165
            },
166
        ];
167
    }
168
}
169