Passed
Pull Request — master (#68)
by Björn
03:27
created

IncludeCollectorTest::provideGetIncluded()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 58
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 37
dl 0
loc 58
rs 9.328
c 0
b 0
f 0
cc 1
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace ComposerRequireCheckerTest\NodeVisitor;
4
5
use ComposerRequireChecker\NodeVisitor\IncludeCollector;
6
use PhpParser\Node;
7
use PhpParser\Node\Expr\BinaryOp\Concat;
8
use PhpParser\Node\Expr\ConstFetch;
9
use PhpParser\Node\Expr\Include_;
10
use PhpParser\Node\Expr\Variable;
11
use PhpParser\Node\Name;
12
use PhpParser\Node\Scalar\MagicConst\Dir;
13
use PhpParser\Node\Scalar\MagicConst\File;
14
use PhpParser\Node\Scalar\String_;
15
use PHPUnit\Framework\TestCase;
16
use ReflectionProperty;
17
18
class IncludeCollectorTest extends TestCase
19
{
20
    /**
21
     * yields testcase datasets
22
     * @return iterable
23
     */
24
    public function provideGetIncluded(): iterable
25
    {
26
        yield "no includes" => [__FILE__, [], []];
27
        yield "simple include" => [__FILE__, ['anywhere/here.php'], [__DIR__ . '/anywhere/here.php']];
28
        yield "simple absolute include" => [__FILE__, ['/anywhere/here.php'], ['/anywhere/here.php']];
29
        yield "simple String_ include" => [
30
            __FILE__,
31
            [new String_('anywhere/here.php')],
32
            [__DIR__ . '/anywhere/here.php']
33
        ];
34
        yield "absolute include by DIR" => [
35
            __FILE__,
36
            [
37
                new Concat(
38
                    new Dir(),
39
                    new String_('anywhere/here.php')
40
                )
41
            ],
42
            [__DIR__ . 'anywhere/here.php']
43
        ];
44
        yield "absolute include by FILE" => [
45
            __FILE__,
46
            [
47
                new Concat(
48
                    new File(),
49
                    new String_('anywhere/here.php')
50
                )
51
            ],
52
            [__FILE__ . 'anywhere/here.php']
53
        ];
54
        yield "includes with variables" => [
55
            __FILE__,
56
            [
57
                new Concat(
58
                    new ConstFetch(new Name("NAME")),
59
                    new String_('.php')
60
                )
61
            ],
62
            [
63
                __DIR__ . DIRECTORY_SEPARATOR . 'DefinedSymbolCollectorFunctionalTest.php',
64
                __DIR__ . DIRECTORY_SEPARATOR . 'DefinedSymbolCollectorTest.php',
65
                __DIR__ . DIRECTORY_SEPARATOR . 'UsedSymbolCollectorFunctionalTest.php',
66
                __DIR__ . DIRECTORY_SEPARATOR . 'UsedSymbolCollectorTest.php'
67
            ]
68
        ];
69
        yield "includes with constants" => [
70
            __FILE__,
71
            [
72
                new Concat(
73
                    new Variable(new Name('name')),
74
                    new String_('.php')
75
                )
76
            ],
77
            [
78
                __DIR__ . DIRECTORY_SEPARATOR . 'DefinedSymbolCollectorFunctionalTest.php',
79
                __DIR__ . DIRECTORY_SEPARATOR . 'DefinedSymbolCollectorTest.php',
80
                __DIR__ . DIRECTORY_SEPARATOR . 'UsedSymbolCollectorFunctionalTest.php',
81
                __DIR__ . DIRECTORY_SEPARATOR . 'UsedSymbolCollectorTest.php'
82
            ]
83
        ];
84
    }
85
86
    /**
87
     * @test
88
     * @dataProvider provideGetIncluded
89
     * @param string $file
90
     * @param array $included
91
     * @param array $result
92
     * @return void
93
     */
94
    public function testGetIncluded(string $file, array $included, array $result): void
95
    {
96
        $collector = new IncludeCollector();
97
        $reflector = new ReflectionProperty($collector, 'included');
98
        $reflector->setAccessible(true);
99
        $reflector->setValue($collector, $included);
100
        $reflector->setAccessible(false);
101
        $collected = $collector->getIncluded($file);
102
        sort($result);
103
        sort($collected);
104
        $this->assertEquals($result, $collected);
105
    }
106
107
    /**
108
     * @test
109
     * @return void
110
     */
111
    public function testBeforeTraverseEmptiesIncludes(): void
112
    {
113
        $collector = new IncludeCollector();
114
        $reflector = new ReflectionProperty($collector, 'included');
115
        $reflector->setAccessible(true);
116
        $reflector->setValue($collector, ['a', '#', 'p']);
117
        $reflector->setAccessible(false);
118
        $this->assertAttributeCount(3, 'included', $collector);
119
        $collector->beforeTraverse([]);
120
        $this->assertAttributeCount(0, 'included', $collector);
121
    }
122
123
    /**
124
     * @return iterable
125
     */
126
    public function provideEnterNode(): iterable
127
    {
128
        $expr = new String_('');
129
        yield "require" => [new Include_($expr, Include_::TYPE_REQUIRE), 1];
130
        yield "require_once" => [new Include_($expr, Include_::TYPE_REQUIRE_ONCE), 1];
131
        yield "include" => [new Include_($expr, Include_::TYPE_INCLUDE), 1];
132
        yield "include_once" => [new Include_($expr, Include_::TYPE_INCLUDE_ONCE), 1];
133
        yield "different node" => [$expr, 0];
134
    }
135
136
    /**
137
     * @test
138
     * @dataProvider provideEnterNode
139
     * @param Node $node
140
     * @param int $count
141
     * @return void
142
     */
143
    public function testEnterNode(Node $node, int $count): void
144
    {
145
        $collector = new IncludeCollector();
146
        $collector->enterNode($node);
147
        $this->assertAttributeCount($count, 'included', $collector);
148
    }
149
}
150