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

testTraitAdaptionDefinition()   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\DefinedSymbolCollector;
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 DefinedSymbolCollectorFunctionalTest extends TestCase
19
{
20
    /**
21
     * @var DefinedSymbolCollector
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 DefinedSymbolCollector();
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 testWillCollectSymbolsDefinedInThisFile()
49
    {
50
        $this->traverseClassAST(self::class);
51
52
        self::assertSameCollectedSymbols(
53
            ['ComposerRequireCheckerTest\NodeVisitor\DefinedSymbolCollectorFunctionalTest'],
54
            $this->collector->getDefinedSymbols()
55
        );
56
    }
57
58
    public function testWillCollectFunctionDefinition()
59
    {
60
        $this->traverseStringAST('function foo() {}');
61
62
        self::assertSameCollectedSymbols(
63
            ['foo'],
64
            $this->collector->getDefinedSymbols()
65
        );
66
    }
67
68
    public function testWillCollectNamespacedFunctionDefinition()
69
    {
70
        $this->traverseStringAST('namespace Foo; function foo() {}');
71
72
        self::assertSameCollectedSymbols(
73
            ['Foo\foo'],
74
            $this->collector->getDefinedSymbols()
75
        );
76
    }
77
78
    public function testWillCollectConstDefinition()
79
    {
80
        $this->traverseStringAST('const foo = "bar", baz = "tab";');
81
82
        self::assertSameCollectedSymbols(
83
            ['foo', 'baz'],
84
            $this->collector->getDefinedSymbols()
85
        );
86
    }
87
88
    public function testWillCollectNamespacedConstDefinition()
89
    {
90
        $this->traverseStringAST('namespace Foo; const foo = "bar", baz = "tab";');
91
92
        self::assertSameCollectedSymbols(
93
            ['Foo\foo', 'Foo\baz'],
94
            $this->collector->getDefinedSymbols()
95
        );
96
    }
97
98
    public function testTraitAdaptionDefinition()
99
    {
100
        $this->traverseStringAST('namespace Foo; trait BarTrait { protected function test(){}} class UseTrait { use BarTrait {test as public;} }');
101
102
        self::assertSameCollectedSymbols(
103
            ['Foo\BarTrait', 'Foo\UseTrait'],
104
            $this->collector->getDefinedSymbols()
105
        );
106
    }
107
108
109
    private function traverseStringAST(string $phpSource): array
110
    {
111
        return $this->traverser->traverse($this->parser->parse('<?php ' . $phpSource));
112
    }
113
114
    private function traverseClassAST(string $className): array
115
    {
116
        return $this->traverser->traverse(
117
            $this->parser->parse(
118
                file_get_contents((new \ReflectionClass($className))->getFileName())
119
            )
120
        );
121
    }
122
123
    private static function assertSameCollectedSymbols(array $expected, array $actual)
124
    {
125
        self::assertSame(array_diff($expected, $actual), array_diff($actual, $expected));
126
    }
127
}
128