Test Failed
Pull Request — master (#68)
by Björn
03:25
created

IncludeCollectorTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 61
dl 0
loc 127
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testBeforeTraverseEmptiesIncludes() 0 10 1
A provideEnterNode() 0 8 1
A testEnterNode() 0 5 1
A provideGetIncluded() 0 58 1
A testGetIncluded() 0 8 1
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
        $this->assertEquals($result, $collector->getIncluded($file));
102
    }
103
104
    /**
105
     * @test
106
     * @return void
107
     */
108
    public function testBeforeTraverseEmptiesIncludes(): void
109
    {
110
        $collector = new IncludeCollector();
111
        $reflector = new ReflectionProperty($collector, 'included');
112
        $reflector->setAccessible(true);
113
        $reflector->setValue($collector, ['a', '#', 'p']);
114
        $reflector->setAccessible(false);
115
        $this->assertAttributeCount(3, 'included', $collector);
116
        $collector->beforeTraverse([]);
117
        $this->assertAttributeCount(0, 'included', $collector);
118
    }
119
120
    /**
121
     * @return iterable
122
     */
123
    public function provideEnterNode(): iterable
124
    {
125
        $expr = new String_('');
126
        yield "require" => [new Include_($expr, Include_::TYPE_REQUIRE), 1];
127
        yield "require_once" => [new Include_($expr, Include_::TYPE_REQUIRE_ONCE), 1];
128
        yield "include" => [new Include_($expr, Include_::TYPE_INCLUDE), 1];
129
        yield "include_once" => [new Include_($expr, Include_::TYPE_INCLUDE_ONCE), 1];
130
        yield "different node" => [$expr, 0];
131
    }
132
133
    /**
134
     * @test
135
     * @dataProvider provideEnterNode
136
     * @param \ComposerRequireCheckerTest\NodeVisitor\Node $node
0 ignored issues
show
Bug introduced by
The type ComposerRequireCheckerTest\NodeVisitor\Node was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
137
     * @param int $count
138
     * @return void
139
     */
140
    public function testEnterNode(Node $node, int $count): void
141
    {
142
        $collector = new IncludeCollector();
143
        $collector->enterNode($node);
144
        $this->assertAttributeCount($count, 'included', $collector);
145
    }
146
}