1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace ApiGen\Parser\Tests\Elements; |
4
|
|
|
|
5
|
|
|
use ApiGen\Contracts\Parser\Reflection\Behavior\InClassInterface; |
6
|
|
|
use ApiGen\Contracts\Parser\Reflection\ClassReflectionInterface; |
7
|
|
|
use ApiGen\Contracts\Parser\Reflection\ConstantReflectionInterface; |
8
|
|
|
use ApiGen\Contracts\Parser\Reflection\FunctionReflectionInterface; |
9
|
|
|
use ApiGen\Parser\Elements\ElementSorter; |
10
|
|
|
use ApiGen\Parser\Reflection\TokenReflection\ReflectionInterface; |
11
|
|
|
use PHPUnit\Framework\TestCase; |
12
|
|
|
|
13
|
|
|
final class ElementSorterTest extends TestCase |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var ElementSorter |
17
|
|
|
*/ |
18
|
|
|
private $elementSorter; |
19
|
|
|
|
20
|
|
|
protected function setUp(): void |
21
|
|
|
{ |
22
|
|
|
$this->elementSorter = new ElementSorter; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
View Code Duplication |
public function testSortElementsByFqnConstants(): void |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
$constantReflectionMock = $this->createMock(ConstantReflectionInterface::class); |
28
|
|
|
$constantReflectionMock->method('getDeclaringClassName') |
29
|
|
|
->willReturn('B'); |
30
|
|
|
$constantReflectionMock->method('getName') |
31
|
|
|
->willReturn('C'); |
32
|
|
|
|
33
|
|
|
$constantReflectionMock2 = $this->createMock(ConstantReflectionInterface::class); |
34
|
|
|
$constantReflectionMock2->method('getDeclaringClassName') |
35
|
|
|
->willReturn('A'); |
36
|
|
|
$constantReflectionMock2->method('getName') |
37
|
|
|
->willReturn('D'); |
38
|
|
|
|
39
|
|
|
$elements = [$constantReflectionMock, $constantReflectionMock2]; |
40
|
|
|
|
41
|
|
|
$sortedElements = $this->elementSorter->sortElementsByFqn($elements); |
42
|
|
|
$this->assertNotSame($elements, $sortedElements); |
43
|
|
|
$this->assertSame($constantReflectionMock2, $sortedElements[0]); |
44
|
|
|
$this->assertSame($constantReflectionMock, $sortedElements[1]); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
View Code Duplication |
public function testSortElementsByFqnFunctions(): void |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
$reflectionFunctionMock = $this->createMock(FunctionReflectionInterface::class); |
50
|
|
|
$reflectionFunctionMock->method('getNamespaceName') |
51
|
|
|
->willReturn('B'); |
52
|
|
|
$reflectionFunctionMock->method('getName') |
53
|
|
|
->willReturn('C'); |
54
|
|
|
|
55
|
|
|
$reflectionFunctionMock2 = $this->createMock(FunctionReflectionInterface::class); |
56
|
|
|
$reflectionFunctionMock2->method('getNamespaceName') |
57
|
|
|
->willReturn('A'); |
58
|
|
|
$reflectionFunctionMock2->method('getName') |
59
|
|
|
->willReturn('D'); |
60
|
|
|
|
61
|
|
|
$elements = [$reflectionFunctionMock, $reflectionFunctionMock2]; |
62
|
|
|
|
63
|
|
|
$sortedElements = $this->elementSorter->sortElementsByFqn($elements); |
64
|
|
|
$this->assertNotSame($elements, $sortedElements); |
65
|
|
|
$this->assertSame($reflectionFunctionMock2, $sortedElements[0]); |
66
|
|
|
$this->assertSame($reflectionFunctionMock, $sortedElements[1]); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
View Code Duplication |
public function testSortElementsByFqnMethod(): void |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
$reflectionMethodMock = $this->createMock( |
72
|
|
|
[ReflectionInterface::class, InClassInterface::class] |
|
|
|
|
73
|
|
|
); |
74
|
|
|
$reflectionMethodMock->method('getDeclaringClassName') |
75
|
|
|
->willReturn('B'); |
76
|
|
|
$reflectionMethodMock->method('getName') |
77
|
|
|
->willReturn('C'); |
78
|
|
|
|
79
|
|
|
$reflectionMethodMock2 = $this->createMock( |
80
|
|
|
[ReflectionInterface::class, InClassInterface::class] |
|
|
|
|
81
|
|
|
); |
82
|
|
|
$reflectionMethodMock2->method('getDeclaringClassName') |
83
|
|
|
->willReturn('A'); |
84
|
|
|
$reflectionMethodMock2->method('getName') |
85
|
|
|
->willReturn('D'); |
86
|
|
|
|
87
|
|
|
$elements = [$reflectionMethodMock, $reflectionMethodMock2]; |
88
|
|
|
|
89
|
|
|
$sortedElements = $this->elementSorter->sortElementsByFqn($elements); |
90
|
|
|
$this->assertNotSame($elements, $sortedElements); |
91
|
|
|
$this->assertSame($reflectionMethodMock2, $sortedElements[0]); |
92
|
|
|
$this->assertSame($reflectionMethodMock, $sortedElements[1]); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
View Code Duplication |
public function testSortElementsByFqnProperties(): void |
|
|
|
|
96
|
|
|
{ |
97
|
|
|
$reflectionMethodMock = $this->createMock([ReflectionInterface::class, InClassInterface::class]); |
|
|
|
|
98
|
|
|
$reflectionMethodMock->method('getDeclaringClassName') |
99
|
|
|
->willReturn('B'); |
100
|
|
|
$reflectionMethodMock->method('getName') |
101
|
|
|
->willReturn('C'); |
102
|
|
|
|
103
|
|
|
$reflectionMethodMock2 = $this->createMock([ReflectionInterface::class, InClassInterface::class]); |
|
|
|
|
104
|
|
|
$reflectionMethodMock2->method('getDeclaringClassName') |
105
|
|
|
->willReturn('A'); |
106
|
|
|
$reflectionMethodMock2->method('getName') |
107
|
|
|
->willReturn('D'); |
108
|
|
|
|
109
|
|
|
$elements = [$reflectionMethodMock, $reflectionMethodMock2]; |
110
|
|
|
|
111
|
|
|
$sortedElements = $this->elementSorter->sortElementsByFqn($elements); |
112
|
|
|
$this->assertNotSame($elements, $sortedElements); |
113
|
|
|
$this->assertSame($reflectionMethodMock2, $sortedElements[0]); |
114
|
|
|
$this->assertSame($reflectionMethodMock, $sortedElements[1]); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function testSortElementsByFqnNonSupportedType(): void |
118
|
|
|
{ |
119
|
|
|
$reflectionClassMock = $this->createMock(ClassReflectionInterface::class); |
120
|
|
|
$sortedElements = $this->elementSorter->sortElementsByFqn([$reflectionClassMock]); |
121
|
|
|
$this->assertSame([$reflectionClassMock], $sortedElements); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function testSortElementsByFqnWithEmptyArray(): void |
125
|
|
|
{ |
126
|
|
|
$this->assertSame([], $this->elementSorter->sortElementsByFqn([])); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
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.