LocateDefinedSymbolsFromASTRootsTest   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Importance

Changes 6
Bugs 0 Features 4
Metric Value
wmc 9
eloc 52
c 6
b 0
f 4
dl 0
loc 116
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A testBasicLocateDefineCalls() 0 13 1
A testBasicLocateFunctions() 0 14 1
A testBasicLocateTrait() 0 15 1
A testBasicLocateClass() 0 15 1
A setUp() 0 5 1
A testNoRoots() 0 5 1
A testBasicDoNotLocateNamespacedDefineCalls() 0 13 1
A locate() 0 3 1
A testBasicLocateAnonymous() 0 10 1
1
<?php
2
3
namespace ComposerRequireCheckerTest\DefinedSymbolsLocator;
4
5
use ArrayObject;
6
use ComposerRequireChecker\DefinedSymbolsLocator\LocateDefinedSymbolsFromASTRoots;
7
use PhpParser\Node\Arg;
8
use PhpParser\Node\Expr\FuncCall;
9
use PhpParser\Node\Name;
10
use PhpParser\Node\Scalar\String_;
11
use PhpParser\Node\Stmt\Class_;
12
use PhpParser\Node\Stmt\Function_;
13
use PhpParser\Node\Stmt\Trait_;
14
use PHPUnit\Framework\TestCase;
15
16
/**
17
 * @covers \ComposerRequireChecker\DefinedSymbolsLocator\LocateDefinedSymbolsFromASTRoots
18
 */
19
final class LocateDefinedSymbolsFromASTRootsTest extends TestCase
20
{
21
    /** @var LocateDefinedSymbolsFromASTRoots */
22
    private $locator;
23
24
    protected function setUp(): void
25
    {
26
        parent::setUp();
27
28
        $this->locator = new LocateDefinedSymbolsFromASTRoots();
29
    }
30
31
    public function testNoRoots(): void
32
    {
33
        $symbols = $this->locate([]);
34
35
        $this->assertCount(0, $symbols);
36
    }
37
38
    public function testBasicLocateClass(): void
39
    {
40
        $roots = [
41
            new Class_('MyClassA'), new Class_('MyClassB'),
42
            new Class_('MyClassC'),
43
        ];
44
45
        $symbols = $this->locate([$roots]);
46
47
        $this->assertIsArray($symbols);
48
        $this->assertCount(3, $symbols);
49
50
        $this->assertContains('MyClassA', $symbols);
51
        $this->assertContains('MyClassB', $symbols);
52
        $this->assertContains('MyClassC', $symbols);
53
    }
54
55
    public function testBasicLocateFunctions(): void
56
    {
57
        $roots = [
58
            new Function_('myFunctionA'),
59
            new Class_('myFunctionB'),
60
        ];
61
62
        $symbols = $this->locate([$roots]);
63
64
        $this->assertIsArray($symbols);
65
        $this->assertCount(2, $symbols);
66
67
        $this->assertContains('myFunctionA', $symbols);
68
        $this->assertContains('myFunctionB', $symbols);
69
    }
70
71
    public function testBasicLocateTrait(): void
72
    {
73
        $roots = [
74
            new Trait_('MyTraitA'), new Trait_('MyTraitB'),
75
            new Trait_('MyTraitC'),
76
        ];
77
78
        $symbols = $this->locate([$roots]);
79
80
        $this->assertIsArray($symbols);
81
        $this->assertCount(3, $symbols);
82
83
        $this->assertContains('MyTraitA', $symbols);
84
        $this->assertContains('MyTraitB', $symbols);
85
        $this->assertContains('MyTraitC', $symbols);
86
    }
87
88
    public function testBasicLocateAnonymous(): void
89
    {
90
        $roots = [
91
            new Class_(null),
92
        ];
93
94
        $symbols = $this->locate([$roots]);
95
96
        $this->assertIsArray($symbols);
97
        $this->assertCount(0, $symbols);
98
    }
99
100
    public function testBasicLocateDefineCalls(): void
101
    {
102
        $roots = [
103
            new FuncCall(new Name('define'), [
104
                new Arg(new String_('CONST_NAME')),
105
                new Arg(new String_('CONST_VALUE')),
106
            ])
107
        ];
108
109
        $symbols = $this->locate([$roots]);
110
111
        $this->assertIsArray($symbols);
112
        $this->assertCount(1, $symbols);
113
114
    }
0 ignored issues
show
Coding Style introduced by
Function closing brace must go on the next line following the body; found 1 blank lines before brace
Loading history...
115
116
    public function testBasicDoNotLocateNamespacedDefineCalls(): void
117
    {
118
        $roots = [
119
            new FuncCall(new Name('define', ['namespacedName' => new Name\FullyQualified('Foo\define')]), [
120
                new Arg(new String_('NO_CONST')),
121
                new Arg(new String_('NO_SOMETHING')),
122
            ])
123
        ];
124
125
        $symbols = $this->locate([$roots]);
126
127
        $this->assertIsArray($symbols);
128
        $this->assertCount(0, $symbols);
129
130
    }
0 ignored issues
show
Coding Style introduced by
Function closing brace must go on the next line following the body; found 1 blank lines before brace
Loading history...
131
132
    private function locate(array $roots): array
133
    {
134
        return ($this->locator)(new ArrayObject($roots));
135
    }
136
}
137