/
NamePrettifier.php
194 lines (154 loc) · 5.33 KB
/
NamePrettifier.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\Util\TestDox;
use PHPUnit\Framework\TestCase;
use SebastianBergmann\Exporter\Exporter;
/**
* Prettifies class and method names for use in TestDox documentation.
*/
final class NamePrettifier
{
/**
* @var array
*/
private $strings = [];
/**
* Prettifies the name of a test class.
*/
public function prettifyTestClass(string $className): string
{
try {
$annotations = \PHPUnit\Util\Test::parseTestMethodAnnotations($className);
if (isset($annotations['class']['testdox'][0])) {
return $annotations['class']['testdox'][0];
}
} catch (\ReflectionException $e) {
}
$result = $className;
if (\substr($className, -1 * \strlen('Test')) === 'Test') {
$result = \substr($result, 0, \strripos($result, 'Test'));
}
if (\strpos($className, 'Tests') === 0) {
$result = \substr($result, \strlen('Tests'));
} elseif (\strpos($className, 'Test') === 0) {
$result = \substr($result, \strlen('Test'));
}
if ($result[0] === '\\') {
$result = \substr($result, 1);
}
return $result;
}
/**
* @throws \ReflectionException
*/
public function prettifyTestCase(TestCase $test): string
{
$annotations = $test->getAnnotations();
$annotationWithPlaceholders = false;
if (isset($annotations['method']['testdox'][0])) {
$result = $annotations['method']['testdox'][0];
if (\strpos($result, '$') !== false) {
$annotation = $annotations['method']['testdox'][0];
$providedData = $this->mapTestMethodParameterNamesToProvidedDataValues($test);
$result = \trim(
\str_replace(
\array_keys($providedData),
$providedData,
$annotation
)
);
$annotationWithPlaceholders = true;
}
} else {
$result = $this->prettifyTestMethod($test->getName(false));
}
if ($test->usesDataProvider() && !$annotationWithPlaceholders) {
$result .= $test->getDataSetAsString(false);
}
return $result;
}
/**
* Prettifies the name of a test method.
*/
public function prettifyTestMethod(string $name): string
{
$buffer = '';
if (!\is_string($name) || $name === '') {
return $buffer;
}
$string = \preg_replace('#\d+$#', '', $name, -1, $count);
if (\in_array($string, $this->strings)) {
$name = $string;
} elseif ($count === 0) {
$this->strings[] = $string;
}
if (\strpos($name, 'test_') === 0) {
$name = \substr($name, 5);
} elseif (\strpos($name, 'test') === 0) {
$name = \substr($name, 4);
}
if ($name === '') {
return $buffer;
}
$name[0] = \strtoupper($name[0]);
if (\strpos($name, '_') !== false) {
return \trim(\str_replace('_', ' ', $name));
}
$max = \strlen($name);
$wasNumeric = false;
for ($i = 0; $i < $max; $i++) {
if ($i > 0 && \ord($name[$i]) >= 65 && \ord($name[$i]) <= 90) {
$buffer .= ' ' . \strtolower($name[$i]);
} else {
$isNumeric = \is_numeric($name[$i]);
if (!$wasNumeric && $isNumeric) {
$buffer .= ' ';
$wasNumeric = true;
}
if ($wasNumeric && !$isNumeric) {
$wasNumeric = false;
}
$buffer .= $name[$i];
}
}
return $buffer;
}
/**
* @throws \ReflectionException
*/
private function mapTestMethodParameterNamesToProvidedDataValues(TestCase $test): array
{
$reflector = new \ReflectionMethod(\get_class($test), $test->getName(false));
$providedData = [];
$providedDataValues = \array_values($test->getProvidedData());
$i = 0;
foreach ($reflector->getParameters() as $parameter) {
if (!\array_key_exists($i, $providedDataValues) && $parameter->isDefaultValueAvailable()) {
$providedDataValues[$i] = $parameter->getDefaultValue();
}
$value = $providedDataValues[$i++] ?? null;
if (\is_object($value)) {
$reflector = new \ReflectionObject($value);
if ($reflector->hasMethod('__toString')) {
$value = (string) $value;
}
}
if (!\is_scalar($value)) {
$value = \gettype($value);
}
if (\is_bool($value) || \is_numeric($value)) {
$exporter = new Exporter;
$value = $exporter->export($value);
}
$providedData['$' . $parameter->getName()] = $value;
}
return $providedData;
}
}