Completed
Branch master (53cbf4)
by Matthias
02:01
created

testWillCollectSymbolsUsedInThisFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
cc 1
eloc 15
nc 1
nop 0
1
<?php
2
3
namespace ComposerRequireCheckerTest\NodeVisitor;
4
5
use ComposerRequireChecker\NodeVisitor\UsedSymbolCollector;
6
use PhpParser\Node;
7
use PhpParser\NodeTraverser;
8
use PhpParser\NodeTraverserInterface;
9
use PhpParser\NodeVisitor\NameResolver;
10
use PhpParser\Parser;
11
use PhpParser\ParserFactory;
12
use PHPUnit\Framework\TestCase;
13
14
/**
15
 * @coversNothing
16
 *
17
 * @group functional
18
 */
19
final class UsedSymbolCollectorFunctionalTest extends TestCase
20
{
21
    /**
22
     * @var UsedSymbolCollector
23
     */
24
    private $collector;
25
26
    /**
27
     * @var Parser
28
     */
29
    private $parser;
30
31
    /**
32
     * @var NodeTraverserInterface
33
     */
34
    private $traverser;
35
36
    /**
37
     * {@inheritDoc}
38
     */
39 View Code Duplication
    protected function setUp()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
40
    {
41
        $this->collector = new UsedSymbolCollector();
42
        $this->parser    = (new ParserFactory())->create(ParserFactory::PREFER_PHP7);
43
        $this->traverser = new NodeTraverser();
44
45
        $this->traverser->addVisitor(new NameResolver());
46
        $this->traverser->addVisitor($this->collector);
47
    }
48
49
    public function testWillCollectSymbolsUsedInThisFile()
50
    {
51
        $this->traverseClassAST(self::class);
52
53
        self::assertSameCollectedSymbols(
54
            [
55
                'ComposerRequireChecker\NodeVisitor\UsedSymbolCollector',
56
                'PHPUnit\Framework\TestCase',
57
                'PhpParser\NodeTraverser',
58
                'PhpParser\ParserFactory',
59
                'file_get_contents',
60
                'ReflectionClass',
61
                'array_diff',
62
                'self',
63
                'PhpParser\NodeVisitor\NameResolver',
64
                'string',
65
                'array',
66
            ],
67
            $this->collector->getCollectedSymbols()
68
        );
69
    }
70
71
    public function testWillCollectFunctionDefinitionTypes()
72
    {
73
        $this->traverseStringAST('<?php function foo(My\ParameterType $bar, array $fooBar) {}');
74
75
        self::assertSameCollectedSymbols(
76
            [
77
                'My\ParameterType',
78
                'array',
79
            ],
80
            $this->collector->getCollectedSymbols()
81
        );
82
    }
83
84
    public function testWillCollectMethodDefinitionTypes()
85
    {
86
        $this->traverseStringAST('<?php class Foo { function foo(My\ParameterType $bar, array $fooBar) {}}');
87
88
        self::assertSameCollectedSymbols(
89
            [
90
                'My\ParameterType',
91
                'array',
92
            ],
93
            $this->collector->getCollectedSymbols()
94
        );
95
    }
96
97
    public function testWillCollectFunctionReturnTypes()
98
    {
99
        $this->traverseStringAST('<?php function foo($bar) : My\ReturnType {}');
100
101
        self::assertSameCollectedSymbols(
102
            [
103
                'My\ReturnType',
104
            ],
105
            $this->collector->getCollectedSymbols()
106
        );
107
    }
108
109
    public function testWillCollectMethodReturnTypes()
110
    {
111
        $this->traverseStringAST('<?php class Foo { function foo($bar) : My\ReturnType {}}');
112
113
        self::assertSameCollectedSymbols(
114
            [
115
                'My\ReturnType',
116
            ],
117
            $this->collector->getCollectedSymbols()
118
        );
119
    }
120
121
    public function testWillCollectSimpleFunctionReturnTypes()
122
    {
123
        $this->traverseStringAST('<?php function foo($bar) : int {}');
124
125
        self::assertSameCollectedSymbols(
126
            [
127
                'int',
128
            ],
129
            $this->collector->getCollectedSymbols()
130
        );
131
    }
132
133
    public function testWontCollectAnyUsageTypes()
134
    {
135
        $this->traverseStringAST('<?php function foo($bar) {}');
136
137
        self::assertSameCollectedSymbols(
138
            [],
139
            $this->collector->getCollectedSymbols()
140
        );
141
    }
142
143
    private function traverseStringAST(string $stringAST)
144
    {
145
        return $this->traverser->traverse(
146
            $this->parser->parse(
0 ignored issues
show
Bug introduced by
It seems like $this->parser->parse($stringAST) targeting PhpParser\Parser::parse() can also be of type null; however, PhpParser\NodeTraverserInterface::traverse() does only seem to accept array<integer,object<PhpParser\Node>>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
147
                $stringAST
148
            )
149
        );
150
    }
151
152
    private function traverseClassAST(string $className) : array
153
    {
154
        return $this->traverseStringAST(
155
                file_get_contents((new \ReflectionClass($className))->getFileName())
156
        );
157
    }
158
159
    private static function assertSameCollectedSymbols(array $expected, array $actual)
160
    {
161
        self::assertSame(array_diff($expected, $actual), array_diff($actual, $expected));
162
    }
163
}
164