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

AssemblerTest::testAssemblingValidExamples()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 13
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\Annotations\Assembler;
6
7
use Doctrine\Annotations\Assembler\Assembler;
8
use Doctrine\Annotations\Constructor\Constructor;
9
use Doctrine\Annotations\Constructor\Instantiator\ConstructorInstantiatorStrategy;
10
use Doctrine\Annotations\Constructor\Instantiator\Instantiator;
11
use Doctrine\Annotations\Constructor\Instantiator\PropertyInstantiatorStrategy;
12
use Doctrine\Annotations\Metadata\AnnotationMetadata;
13
use Doctrine\Annotations\Metadata\MetadataCollection;
14
use Doctrine\Annotations\Metadata\Reflection\DefaultReflectionProvider;
15
use Doctrine\Annotations\Parser\Compiler;
16
use Doctrine\Annotations\Parser\IgnoredAnnotations;
17
use Doctrine\Annotations\Parser\Imports;
18
use Doctrine\Annotations\Parser\Reference\FallbackReferenceResolver;
19
use Doctrine\Annotations\Parser\Scope;
20
use Doctrine\Annotations\PhpParser;
21
use Doctrine\Tests\Annotations\Assembler\Acceptor\AlwaysAcceptingAcceptor;
22
use Doctrine\Tests\Annotations\Fixtures\AnnotationTargetAll;
23
use Doctrine\Tests\Annotations\Fixtures\AnnotationWithConstants;
24
use Doctrine\Tests\Annotations\Fixtures\AnnotationWithVarType;
25
use Doctrine\Tests\Annotations\Fixtures\Metadata\AnnotationTargetAllMetadata;
26
use Doctrine\Tests\Annotations\Fixtures\Metadata\AnnotationWithConstantsMetadata;
27
use Doctrine\Tests\Annotations\Fixtures\Metadata\AnnotationWithVarTypeMetadata;
28
use PHPUnit\Framework\TestCase;
29
use ReflectionClass;
30
use function iterator_to_array;
31
32
class AssemblerTest extends TestCase
33
{
34
    /** @var Compiler */
35
    private $compiler;
36
37
    /** @var PhpParser */
38
    private $phpParser;
39
40
    public function setUp() : void
41
    {
42
        $this->compiler  = new Compiler();
43
        $this->phpParser = new PhpParser();
44
    }
45
46
    /**
47
     * @param AnnotationMetadata[] $metadata
48
     *
49
     * @dataProvider validExamples
50
     */
51
    public function testAssemblingValidExamples(string $class, array $metadata, callable $asserter) : void
52
    {
53
        $reflection = new ReflectionClass($class);
54
        $ast        = $this->compiler->compile($reflection->getDocComment());
0 ignored issues
show
Bug introduced by
It seems like $reflection->getDocComment() can also be of type boolean; however, parameter $docblock of Doctrine\Annotations\Parser\Compiler::compile() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

54
        $ast        = $this->compiler->compile(/** @scrutinizer ignore-type */ $reflection->getDocComment());
Loading history...
55
        $scope      = $this->createScope($reflection);
56
57
        $metadataCollection = new MetadataCollection(...$metadata);
58
59
        $assembler = $this->createAssembler($metadataCollection);
60
61
        $result = $assembler->collect($ast, $scope);
62
63
        $asserter(iterator_to_array($result));
64
    }
65
66
    public function validExamples() : iterable
67
    {
68
        yield 'fixture - ClassWithAnnotationTargetAll' => [
69
            ClassWithAnnotationTargetAll::class,
70
            [
71
                AnnotationTargetAllMetadata::get(),
72
            ],
73
            function (array $result) : void {
74
                $this->assertCount(1, $result);
75
                /** @var AnnotationTargetAll $resultAnnotation */
76
                $resultAnnotation = $result[0];
77
                $this->assertInstanceOf(AnnotationTargetAll::class, $resultAnnotation);
78
                $this->assertSame(123, $resultAnnotation->name);
79
            },
80
        ];
81
82
        yield 'fixture - ClassWithFullValidUsageOfAnnotationWithVarType' => [
83
            ClassWithFullValidUsageOfAnnotationWithVarType::class,
84
            [
85
                AnnotationTargetAllMetadata::get(),
86
                AnnotationWithVarTypeMetadata::get(),
87
            ],
88
            function (array $result) : void {
89
                $this->assertCount(1, $result);
90
                /** @var AnnotationWithVarType $resultAnnotation */
91
                $resultAnnotation = $result[0];
92
                $this->assertInstanceOf(AnnotationWithVarType::class, $resultAnnotation);
93
94
                $this->assertNull($resultAnnotation->mixed);
95
                $this->assertTrue($resultAnnotation->boolean);
96
                $this->assertFalse($resultAnnotation->bool);
97
                $this->assertSame(3.14, $resultAnnotation->float);
98
                $this->assertSame('foo', $resultAnnotation->string);
99
                $this->assertSame(42, $resultAnnotation->integer);
100
                $this->assertSame(['foo', 42, false], $resultAnnotation->array);
101
                $this->assertSame(['foo' => 'bar'], $resultAnnotation->arrayMap);
102
                $this->assertInstanceOf(AnnotationTargetAll::class, $resultAnnotation->annotation);
103
                $this->assertSame('baz', $resultAnnotation->annotation->name);
104
                $this->assertSame([1,2,3], $resultAnnotation->arrayOfIntegers);
105
                $this->assertSame(['foo', 'bar', 'baz'], $resultAnnotation->arrayOfStrings);
106
107
                $this->assertInternalType('array', $resultAnnotation->arrayOfAnnotations);
108
                $this->assertCount(2, $resultAnnotation->arrayOfAnnotations);
109
                $this->assertInstanceOf(AnnotationTargetAll::class, $resultAnnotation->arrayOfAnnotations[0]);
110
                $this->assertNull($resultAnnotation->arrayOfAnnotations[0]->name);
111
                $this->assertInstanceOf(AnnotationTargetAll::class, $resultAnnotation->arrayOfAnnotations[1]);
112
                $this->assertSame(123, $resultAnnotation->arrayOfAnnotations[1]->name);
113
            },
114
        ];
115
116
        yield 'fixture - ClassWithAnnotationWithConstants' => [
117
            ClassWithAnnotationWithConstants::class,
118
            [
119
                AnnotationWithConstantsMetadata::get(),
120
            ],
121
            function (array $result) : void {
122
                $this->assertCount(1, $result);
123
                /** @var AnnotationWithConstants $resultAnnotation */
124
                $resultAnnotation = $result[0];
125
                $this->assertInstanceOf(AnnotationWithConstants::class, $resultAnnotation);
126
127
                $this->assertSame(AnnotationWithConstants::FLOAT, $resultAnnotation->value);
128
            },
129
        ];
130
    }
131
132
    private function createScope(ReflectionClass $reflection) : Scope
133
    {
134
        return new Scope(
135
            $reflection,
136
            new Imports($this->phpParser->parseClass($reflection)),
137
            new IgnoredAnnotations()
138
        );
139
    }
140
141
    private function createAssembler(MetadataCollection $collection) : Assembler
142
    {
143
        return new Assembler(
144
            $collection,
145
            new FallbackReferenceResolver(),
146
            new Constructor(
147
                new Instantiator(
148
                    new ConstructorInstantiatorStrategy(),
149
                    new PropertyInstantiatorStrategy()
150
                )
151
            ),
152
            new DefaultReflectionProvider(),
153
            new AlwaysAcceptingAcceptor()
154
        );
155
    }
156
}
157
158
/**
159
 * @AnnotationTargetAll(name=123)
160
 */
161
class ClassWithAnnotationTargetAll
162
{
163
}
164
165
/**
166
 * @AnnotationWithConstants(value=AnnotationWithConstants::FLOAT)
167
 */
168
class ClassWithAnnotationWithConstants
169
{
170
}
171
172
/**
173
 * @AnnotationWithVarType(
174
 *     mixed=null,
175
 *     boolean=true,
176
 *     bool=false,
177
 *     float=3.14,
178
 *     string="foo",
179
 *     integer=42,
180
 *     array={"foo", 42, false},
181
 *     arrayMap={"foo": "bar"},
182
 *     annotation=@AnnotationTargetAll(name="baz"),
183
 *     arrayOfIntegers={1,2,3},
184
 *     arrayOfStrings={"foo","bar","baz"},
185
 *     arrayOfAnnotations={
186
 *         @AnnotationTargetAll,
187
 *         @AnnotationTargetAll(name=123)
188
 *     }
189
 * )
190
 */
191
class ClassWithFullValidUsageOfAnnotationWithVarType
192
{
193
}
194