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

MetadataCollectorTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 61
dl 0
loc 113
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A docBlocksProvider() 0 78 4
A setUp() 0 7 1
A testMetadata() 0 9 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\Annotations\Metadata;
6
7
use Doctrine\Annotations\Metadata\AnnotationMetadata;
8
use Doctrine\Annotations\Metadata\AnnotationTarget;
9
use Doctrine\Annotations\Metadata\Assembler\AnnotationMetadataAssembler;
10
use Doctrine\Annotations\Metadata\MetadataCollection;
11
use Doctrine\Annotations\Metadata\MetadataCollector;
12
use Doctrine\Annotations\Parser\Ast\Annotation;
13
use Doctrine\Annotations\Parser\Ast\Annotations;
14
use Doctrine\Annotations\Parser\Ast\Parameter\UnnamedParameter;
15
use Doctrine\Annotations\Parser\Ast\Parameters;
16
use Doctrine\Annotations\Parser\Ast\Reference;
17
use Doctrine\Annotations\Parser\Reference\StaticReferenceResolver;
18
use Doctrine\Annotations\Parser\Scope;
19
use Doctrine\Tests\Annotations\Annotation\Parser\Reference\IdentifierPassingReferenceResolver;
20
use Doctrine\Tests\Annotations\Annotation\Parser\ScopeMother;
21
use Doctrine\Tests\Annotations\Assembler\Acceptor\AlwaysAcceptingAcceptor;
22
use PHPUnit\Framework\MockObject\MockObject;
23
use PHPUnit\Framework\TestCase;
24
25
final class MetadataCollectorTest extends TestCase
26
{
27
    /** @var AnnotationMetadataAssembler|MockObject */
28
    private $assembler;
29
30
    /** @var MetadataCollector */
31
    private $collector;
32
33
    protected function setUp() : void
34
    {
35
        $this->assembler = $this->createMock(AnnotationMetadataAssembler::class);
36
        $this->collector = new MetadataCollector(
37
            $this->assembler,
38
            new AlwaysAcceptingAcceptor(),
39
            new IdentifierPassingReferenceResolver()
40
        );
41
    }
42
43
    /**
44
     * @param callable(AnnotationMetadataAssembler) : void $initializer
45
     * @param callable(AnnotationMetadata[]) : void        $asserter
46
     *
47
     * @dataProvider docBlocksProvider()
48
     */
49
    public function testMetadata(Annotations $annotations, callable $initializer, callable $asserter) : void
50
    {
51
        $initializer($this->assembler);
52
53
        $collection = new MetadataCollection();
54
55
        $this->collector->collect($annotations, ScopeMother::example(), $collection);
56
57
        $asserter($collection);
58
    }
59
60
    public function docBlocksProvider() : iterable
61
    {
62
        yield 'single without parameters' => [
63
            new Annotations(
64
                new Annotation(
65
                    new Reference('Foo', true),
66
                    new Parameters()
67
                )
68
            ),
69
            function (AnnotationMetadataAssembler $assembler) : void {
70
                $assembler->method('assemble')
0 ignored issues
show
Bug introduced by
The method method() does not exist on Doctrine\Annotations\Met...tationMetadataAssembler. ( Ignorable by Annotation )

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

70
                $assembler->/** @scrutinizer ignore-call */ 
71
                            method('assemble')

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
71
                    ->with(
72
                        $this->callback(static function (Reference $reference) : bool {
73
                            return $reference->getIdentifier() === 'Foo' && $reference->isFullyQualified() === true;
74
                        }),
75
                        $this->isInstanceOf(Scope::class)
76
                    )
77
                    ->willReturn(new AnnotationMetadata(
78
                        'Foo',
79
                        new AnnotationTarget(AnnotationTarget::TARGET_ALL),
80
                        false,
81
                        []
82
                    ));
83
            },
84
            static function (MetadataCollection $collection) : void {
85
                self::assertCount(1, $collection);
86
                self::assertSame('Foo', $collection['Foo']->getName());
87
            },
88
        ];
89
        yield 'nested' => [
90
            new Annotations(
91
                new Annotation(
92
                    new Reference('Foo', true),
93
                    new Parameters(
94
                        new UnnamedParameter(
95
                            new Annotation(
96
                                new Reference('Bar', false),
97
                                new Parameters()
98
                            )
99
                        )
100
                    )
101
                )
102
            ),
103
            function (AnnotationMetadataAssembler $assembler) : void {
104
                $assembler->method('assemble')
105
                    ->withConsecutive(
106
                        [
107
                            $this->callback(static function (Reference $reference) : bool {
108
                                return $reference->getIdentifier() === 'Bar' && $reference->isFullyQualified() === false;
109
                            }),
110
                            $this->isInstanceOf(Scope::class),
111
                        ],
112
                        [
113
                            $this->callback(static function (Reference $reference) : bool {
114
                                return $reference->getIdentifier() === 'Foo' && $reference->isFullyQualified() === true;
115
                            }),
116
                            $this->isInstanceOf(Scope::class),
117
                        ]
118
                    )
119
                    ->willReturnOnConsecutiveCalls(
120
                        new AnnotationMetadata(
121
                            'Bar',
122
                            new AnnotationTarget(AnnotationTarget::TARGET_ALL),
123
                            false,
124
                            []
125
                        ),
126
                        new AnnotationMetadata(
127
                            'Foo',
128
                            new AnnotationTarget(AnnotationTarget::TARGET_ALL),
129
                            false,
130
                            []
131
                        )
132
                    );
133
            },
134
            static function (MetadataCollection $collection) : void {
135
                self::assertCount(2, $collection);
136
                self::assertSame('Bar', $collection['Bar']->getName());
137
                self::assertSame('Foo', $collection['Foo']->getName());
138
            },
139
        ];
140
    }
141
}
142