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 setUp() 0 9 1
A testMetadata() 0 7 1
A docBlocksProvider() 0 78 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\Annotations\Parser\Visitor;
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\MetadataCollector;
11
use Doctrine\Annotations\Parser\Ast\Annotation;
12
use Doctrine\Annotations\Parser\Ast\Annotations;
13
use Doctrine\Annotations\Parser\Ast\Parameter\UnnamedParameter;
14
use Doctrine\Annotations\Parser\Ast\Parameters;
15
use Doctrine\Annotations\Parser\Ast\Reference;
16
use Doctrine\Annotations\Parser\Reference\StaticReferenceResolver;
17
use Doctrine\Annotations\Parser\Scope;
18
use Doctrine\Tests\Annotations\Assembler\Acceptor\AlwaysAcceptingAcceptor;
19
use PHPUnit\Framework\MockObject\MockObject;
20
use PHPUnit\Framework\TestCase;
21
22
final class MetadataCollectorTest extends TestCase
23
{
24
    /** @var AnnotationMetadataAssembler|MockObject */
25
    private $assembler;
26
27
    /** @var MetadataCollector */
28
    private $collector;
29
30
    protected function setUp() : void
31
    {
32
        $this->markTestIncomplete('Collector has incorrect setup');
33
34
        $this->assembler = $this->createMock(AnnotationMetadataAssembler::class);
35
        $this->collector = new MetadataCollector(
36
            $this->assembler,
37
            new AlwaysAcceptingAcceptor(),
38
            new StaticReferenceResolver()
39
        );
40
    }
41
42
    /**
43
     * @param callable(AnnotationMetadataAssembler) : void $initializer
44
     * @param callable(AnnotationMetadata[]) : void        $asserter
45
     *
46
     * @dataProvider docBlocksProvider()
47
     */
48
    public function testMetadata(Annotations $annotations, callable $initializer, callable $asserter) : void
49
    {
50
        $initializer($this->assembler);
51
52
        $this->collector->visit($annotations);
0 ignored issues
show
Bug introduced by
The method visit() does not exist on Doctrine\Annotations\Metadata\MetadataCollector. ( Ignorable by Annotation )

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

52
        $this->collector->/** @scrutinizer ignore-call */ 
53
                          visit($annotations);

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...
53
54
        $asserter(...$this->collector->collect());
0 ignored issues
show
Bug introduced by
The call to Doctrine\Annotations\Met...ataCollector::collect() has too few arguments starting with node. ( Ignorable by Annotation )

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

54
        $asserter(...$this->collector->/** @scrutinizer ignore-call */ collect());

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
Bug introduced by
Are you sure the usage of $this->collector->collect() targeting Doctrine\Annotations\Met...ataCollector::collect() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
55
    }
56
57
    public function docBlocksProvider() : iterable
58
    {
59
        yield 'single without parameters' => [
60
            new Annotations(
61
                new Annotation(
62
                    new Reference('Foo', true),
63
                    new Parameters()
64
                )
65
            ),
66
            function (AnnotationMetadataAssembler $assembler) : void {
67
                $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

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