1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace ApiGen\Parser\Tests\Elements; |
4
|
|
|
|
5
|
|
|
use ApiGen\Contracts\Configuration\ConfigurationInterface; |
6
|
|
|
use ApiGen\Contracts\Parser\Elements\NamespaceSorterInterface; |
7
|
|
|
use ApiGen\Parser\Elements\Elements; |
8
|
|
|
use ApiGen\Parser\Elements\NamespaceSorter; |
9
|
|
|
use ApiGen\Tests\MethodInvoker; |
10
|
|
|
use PHPUnit\Framework\Assert; |
11
|
|
|
use PHPUnit\Framework\TestCase; |
12
|
|
|
|
13
|
|
|
final class NamespaceSorterTest extends TestCase |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var NamespaceSorterInterface |
17
|
|
|
*/ |
18
|
|
|
private $namespaceSorter; |
19
|
|
|
|
20
|
|
|
protected function setUp(): void |
21
|
|
|
{ |
22
|
|
|
$configurationMock = $this->createMock(ConfigurationInterface::class); |
23
|
|
|
|
24
|
|
|
$this->namespaceSorter = new NamespaceSorter(new Elements, $configurationMock); |
|
|
|
|
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function testSort(): void |
28
|
|
|
{ |
29
|
|
|
$groups = ['OneGroup' => [], 'OtherGroup' => [], 'OneMoreGroup' => []]; |
30
|
|
|
$sortedGroups = $this->namespaceSorter->sort($groups); |
31
|
|
|
$this->assertCount(3, $sortedGroups); |
32
|
|
|
$this->assertArrayHasKey('OneGroup', $sortedGroups); |
33
|
|
|
$this->assertArrayHasKey('OtherGroup', $sortedGroups); |
34
|
|
|
$this->assertArrayHasKey('OneMoreGroup', $sortedGroups); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function testSortNoneOnly(): void |
38
|
|
|
{ |
39
|
|
|
$groups = ['None' => []]; |
40
|
|
|
$sortedGroups = $this->namespaceSorter->sort($groups); |
41
|
|
|
$this->assertSame([], $sortedGroups); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function testIsNoneOnly(): void |
45
|
|
|
{ |
46
|
|
|
$groups['None'] = true; |
|
|
|
|
47
|
|
|
$this->assertTrue(MethodInvoker::callMethodOnObject($this->namespaceSorter, 'isNoneOnly', [$groups])); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function testAddMissingParentNamespaces(): void |
51
|
|
|
{ |
52
|
|
|
$this->assertNull(Assert::getObjectAttribute($this->namespaceSorter, 'namespaces')); |
53
|
|
|
MethodInvoker::callMethodOnObject( |
54
|
|
|
$this->namespaceSorter, 'addMissingParentNamespaces', ['Some\Group\Name'] |
55
|
|
|
); |
56
|
|
|
|
57
|
|
|
$groups = Assert::getObjectAttribute($this->namespaceSorter, 'namespaces'); |
58
|
|
|
$this->assertArrayHasKey('Some\Group\Name', $groups); |
59
|
|
|
$this->assertArrayHasKey('Some\Group', $groups); |
60
|
|
|
$this->assertArrayHasKey('Some', $groups); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function testAddMissingElementTypes(): void |
64
|
|
|
{ |
65
|
|
|
MethodInvoker::callMethodOnObject($this->namespaceSorter, 'addMissingElementTypes', ['Some\Group']); |
66
|
|
|
$groups = Assert::getObjectAttribute($this->namespaceSorter, 'namespaces'); |
67
|
|
|
$this->assertArrayHasKey('Some\Group', $groups); |
68
|
|
|
|
69
|
|
|
$someGroup = $groups['Some\Group']; |
70
|
|
|
$this->assertArrayHasKey('classes', $someGroup); |
71
|
|
|
$this->assertArrayHasKey('constants', $someGroup); |
72
|
|
|
$this->assertArrayHasKey('exceptions', $someGroup); |
73
|
|
|
$this->assertArrayHasKey('functions', $someGroup); |
74
|
|
|
$this->assertArrayHasKey('interfaces', $someGroup); |
75
|
|
|
$this->assertArrayHasKey('traits', $someGroup); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @dataProvider getCompareNamespacesData() |
80
|
|
|
*/ |
81
|
|
|
public function testCompareNamespaces(string $one, string $two, int $expected): void |
82
|
|
|
{ |
83
|
|
|
$this->assertSame( |
84
|
|
|
$expected, |
85
|
|
|
MethodInvoker::callMethodOnObject($this->namespaceSorter, 'compareNamespaceNames', [$one, $two]) |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return mixed[] |
91
|
|
|
*/ |
92
|
|
|
public function getCompareNamespacesData(): array |
93
|
|
|
{ |
94
|
|
|
return [ |
95
|
|
|
['GroupOne', 'OtherGroup', -8], |
96
|
|
|
['One', 'Two', -5], |
97
|
|
|
]; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.