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