Completed
Pull Request — master (#45)
by Matthias
03:25
created

testUseTraitAdaptionAlias()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace ComposerRequireCheckerTest\NodeVisitor;
4
5
use ComposerRequireChecker\NodeVisitor\UsedSymbolCollector;
6
use PhpParser\NodeTraverser;
7
use PhpParser\NodeTraverserInterface;
8
use PhpParser\NodeVisitor\NameResolver;
9
use PhpParser\Parser;
10
use PhpParser\ParserFactory;
11
use PHPUnit\Framework\TestCase;
12
13
/**
14
 * @coversNothing
15
 *
16
 * @group functional
17
 */
18
final class UsedSymbolCollectorFunctionalTest extends TestCase
19
{
20
    /**
21
     * @var UsedSymbolCollector
22
     */
23
    private $collector;
24
25
    /**
26
     * @var Parser
27
     */
28
    private $parser;
29
30
    /**
31
     * @var NodeTraverserInterface
32
     */
33
    private $traverser;
34
35
    /**
36
     * {@inheritDoc}
37
     */
38 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...
39
    {
40
        $this->collector = new UsedSymbolCollector();
41
        $this->parser    = (new ParserFactory())->create(ParserFactory::PREFER_PHP7);
42
        $this->traverser = new NodeTraverser();
43
44
        $this->traverser->addVisitor(new NameResolver());
45
        $this->traverser->addVisitor($this->collector);
46
    }
47
48
    public function testWillCollectSymbolsUsedInThisFile()
49
    {
50
        $this->traverseClassAST(self::class);
51
52
        self::assertSameCollectedSymbols(
53
            [
54
                'ComposerRequireChecker\NodeVisitor\UsedSymbolCollector',
55
                'PHPUnit\Framework\TestCase',
56
                'PhpParser\NodeTraverser',
57
                'PhpParser\ParserFactory',
58
                'file_get_contents',
59
                'ReflectionClass',
60
                'array_diff',
61
                'self',
62
                'PhpParser\NodeVisitor\NameResolver',
63
                'string',
64
                'array',
65
            ],
66
            $this->collector->getCollectedSymbols()
67
        );
68
    }
69
70
    public function testWillCollectFunctionDefinitionTypes()
71
    {
72
        $this->traverseStringAST('<?php function foo(My\ParameterType $bar, array $fooBar) {}');
73
74
        self::assertSameCollectedSymbols(
75
            [
76
                'My\ParameterType',
77
                'array',
78
            ],
79
            $this->collector->getCollectedSymbols()
80
        );
81
    }
82
83
    public function testWillCollectMethodDefinitionTypes()
84
    {
85
        $this->traverseStringAST('<?php class Foo { function foo(My\ParameterType $bar, array $fooBar) {}}');
86
87
        self::assertSameCollectedSymbols(
88
            [
89
                'My\ParameterType',
90
                'array',
91
            ],
92
            $this->collector->getCollectedSymbols()
93
        );
94
    }
95
96
    public function testWillCollectFunctionReturnTypes()
97
    {
98
        $this->traverseStringAST('<?php function foo($bar) : My\ReturnType {}');
99
100
        self::assertSameCollectedSymbols(
101
            [
102
                'My\ReturnType',
103
            ],
104
            $this->collector->getCollectedSymbols()
105
        );
106
    }
107
108
    public function testWillCollectMethodReturnTypes()
109
    {
110
        $this->traverseStringAST('<?php class Foo { function foo($bar) : My\ReturnType {}}');
111
112
        self::assertSameCollectedSymbols(
113
            [
114
                'My\ReturnType',
115
            ],
116
            $this->collector->getCollectedSymbols()
117
        );
118
    }
119
120
    public function testWillCollectSimpleFunctionReturnTypes()
121
    {
122
        $this->traverseStringAST('<?php function foo($bar) : int {}');
123
124
        self::assertSameCollectedSymbols(
125
            [
126
                'int',
127
            ],
128
            $this->collector->getCollectedSymbols()
129
        );
130
    }
131
132
    public function testWontCollectAnyUsageTypes()
133
    {
134
        $this->traverseStringAST('<?php function foo($bar) {}');
135
136
        self::assertSameCollectedSymbols(
137
            [],
138
            $this->collector->getCollectedSymbols()
139
        );
140
    }
141
142
    public function testUseTraitAdaptionAlias()
143
    {
144
        $this->traverseStringAST('<?php namespace Foo; trait BarTrait { protected function test(){}} class UseTrait { use BarTrait {test as public;} }');
145
146
        self::assertSameCollectedSymbols(
147
            ['Foo\BarTrait'],
148
            $this->collector->getCollectedSymbols()
149
        );
150
    }
151
152
    private function traverseStringAST(string $stringAST)
153
    {
154
        return $this->traverser->traverse(
155
            $this->parser->parse(
156
                $stringAST
157
            )
158
        );
159
    }
160
161
    private function traverseClassAST(string $className): array
162
    {
163
        return $this->traverseStringAST(
164
            file_get_contents((new \ReflectionClass($className))->getFileName())
165
        );
166
    }
167
168
    private static function assertSameCollectedSymbols(array $expected, array $actual)
169
    {
170
        self::assertSame(array_diff($expected, $actual), array_diff($actual, $expected));
171
    }
172
}
173