Skip to content

Commit 54f9dc5

Browse files
Add config to hide FQCN in the output (#5)
- Introduced `showFQCN` parameter to control the display of fully qualified class names in test output. - Updated README to include usage instructions for the new parameter. - Enhanced `ExecutionTimeExtension` to format test names based on the `showFQCN` setting. - Added unit tests to verify the functionality of the `showFQCN` parameter and its impact on test name formatting.
1 parent d46ced7 commit 54f9dc5

File tree

3 files changed

+186
-2
lines changed

3 files changed

+186
-2
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Add the extension to your `phpunit.xml.dist` or `phpunit.xml` file:
3535
<bootstrap class="Phauthentic\PHPUnit\ExecutionTiming\ExecutionTimeExtension">
3636
<parameter name="topN" value="10"/>
3737
<parameter name="showIndividualTimings" value="false"/>
38+
<parameter name="showFQCN" value="true"/>
3839
<parameter name="warningThreshold" value="1.0"/>
3940
<parameter name="dangerThreshold" value="5.0"/>
4041
</bootstrap>
@@ -46,6 +47,7 @@ Add the extension to your `phpunit.xml.dist` or `phpunit.xml` file:
4647

4748
- **`topN`** (default: `10`): Number of slowest tests to display in the summary report
4849
- **`showIndividualTimings`** (default: `false`): Whether to display timing for each test as it runs
50+
- **`showFQCN`** (default: `true`): Whether to display fully qualified class names (FQCN) or just the class name. When set to `false`, only the class name without namespace will be shown (e.g., `MyTestClass::testMethod` instead of `Phauthentic\PHPUnit\ExecutionTiming\Tests\MyTestClass::testMethod`)
4951
- **`warningThreshold`** (default: `1.0`): Time in seconds at which tests will be colored yellow (warning). Tests with execution time >= this value will be highlighted.
5052
- **`dangerThreshold`** (default: `5.0`): Time in seconds at which tests will be colored red (danger). Tests with execution time >= this value will be highlighted in red. Tests between `warningThreshold` and `dangerThreshold` will be colored yellow.
5153

@@ -122,6 +124,22 @@ This configuration will:
122124
- Show yellow for tests taking 0.5 seconds or more
123125
- Show red for tests taking 2.0 seconds or more
124126

127+
### With Short Class Names (showFQCN disabled)
128+
129+
```xml
130+
<phpunit>
131+
<extensions>
132+
<bootstrap class="Phauthentic\PHPUnit\ExecutionTiming\ExecutionTimeExtension">
133+
<parameter name="topN" value="10"/>
134+
<parameter name="showFQCN" value="false"/>
135+
</bootstrap>
136+
</extensions>
137+
</phpunit>
138+
```
139+
140+
This configuration will display only the class name without the full namespace path, making the output more compact:
141+
- `MyTestClass::testMethod` instead of `Phauthentic\PHPUnit\ExecutionTiming\Tests\MyTestClass::testMethod`
142+
125143
## How It Works
126144

127145
The extension subscribes to PHPUnit events:

src/ExecutionTimingExtension/ExecutionTimeExtension.php

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ final class ExecutionTimeExtension implements Extension
3030
private float $testStartTime = 0.0;
3131
private int $topN = 10;
3232
private bool $showIndividualTimings = false;
33+
private bool $showFQCN = true;
3334
private float $warningThreshold = 1.0;
3435
private float $dangerThreshold = 5.0;
3536

@@ -51,14 +52,15 @@ public function onTestFinished(Finished $event): void
5152
{
5253
$duration = microtime(true) - $this->testStartTime;
5354
$testName = $event->test()->id();
55+
$displayName = $this->formatTestName($testName);
5456
$this->testTimes[] = [
55-
'name' => $testName,
57+
'name' => $displayName,
5658
'time' => $duration,
5759
];
5860

5961
if ($this->showIndividualTimings) {
6062
$timeMs = round($duration * 1000, 2);
61-
printf(" ⏱ %s: %.2f ms\n", $testName, $timeMs);
63+
printf(" ⏱ %s: %.2f ms\n", $displayName, $timeMs);
6264
}
6365
}
6466

@@ -109,5 +111,34 @@ public function extractConfigurationFromParameters(ParameterCollection $paramete
109111
if ($parameters->has('dangerThreshold')) {
110112
$this->dangerThreshold = (float)$parameters->get('dangerThreshold');
111113
}
114+
115+
if ($parameters->has('showFQCN')) {
116+
$this->showFQCN = filter_var(
117+
$parameters->get('showFQCN'),
118+
FILTER_VALIDATE_BOOLEAN
119+
);
120+
}
121+
}
122+
123+
private function formatTestName(string $testName): string
124+
{
125+
if ($this->showFQCN) {
126+
return $testName;
127+
}
128+
129+
// Extract class name from format: Fully\Qualified\ClassName::methodName
130+
if (str_contains($testName, '::')) {
131+
$parts = explode('::', $testName, 2);
132+
$className = $parts[0];
133+
$methodName = $parts[1] ?? '';
134+
135+
// Get just the class name (last part of namespace)
136+
$classNameParts = explode('\\', $className);
137+
$shortClassName = end($classNameParts);
138+
139+
return $shortClassName . ($methodName !== '' ? '::' . $methodName : '');
140+
}
141+
142+
return $testName;
112143
}
113144
}

tests/Unit/ExecutionTimeExtensionTest.php

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,4 +133,139 @@ public function testOnExecutionFinishedWithNoTests(): void
133133

134134
$this->assertEmpty($output);
135135
}
136+
137+
public function testDefaultShowFQCNIsTrue(): void
138+
{
139+
$reflection = new \ReflectionClass($this->extension);
140+
$property = $reflection->getProperty('showFQCN');
141+
$property->setAccessible(true);
142+
143+
$this->assertTrue($property->getValue($this->extension));
144+
}
145+
146+
public function testExtractConfigurationFromParametersWithShowFQCNTrue(): void
147+
{
148+
$parameters = ParameterCollection::fromArray([
149+
'showFQCN' => 'true',
150+
]);
151+
152+
$reflection = new \ReflectionClass($this->extension);
153+
$method = $reflection->getMethod('extractConfigurationFromParameters');
154+
$method->setAccessible(true);
155+
$method->invoke($this->extension, $parameters);
156+
157+
$property = $reflection->getProperty('showFQCN');
158+
$property->setAccessible(true);
159+
160+
$this->assertTrue($property->getValue($this->extension));
161+
}
162+
163+
public function testExtractConfigurationFromParametersWithShowFQCNFalse(): void
164+
{
165+
$parameters = ParameterCollection::fromArray([
166+
'showFQCN' => 'false',
167+
]);
168+
169+
$reflection = new \ReflectionClass($this->extension);
170+
$method = $reflection->getMethod('extractConfigurationFromParameters');
171+
$method->setAccessible(true);
172+
$method->invoke($this->extension, $parameters);
173+
174+
$property = $reflection->getProperty('showFQCN');
175+
$property->setAccessible(true);
176+
177+
$this->assertFalse($property->getValue($this->extension));
178+
}
179+
180+
public function testFormatTestNameWithFQCNEnabled(): void
181+
{
182+
$reflection = new \ReflectionClass($this->extension);
183+
$method = $reflection->getMethod('formatTestName');
184+
$method->setAccessible(true);
185+
186+
$testName = 'Phauthentic\\PHPUnit\\ExecutionTiming\\Tests\\Unit\\MyTestClass::testMethod';
187+
$result = $method->invoke($this->extension, $testName);
188+
189+
$this->assertEquals('Phauthentic\\PHPUnit\\ExecutionTiming\\Tests\\Unit\\MyTestClass::testMethod', $result);
190+
}
191+
192+
public function testFormatTestNameWithFQCNDisabled(): void
193+
{
194+
$parameters = ParameterCollection::fromArray([
195+
'showFQCN' => 'false',
196+
]);
197+
198+
$reflection = new \ReflectionClass($this->extension);
199+
$extractMethod = $reflection->getMethod('extractConfigurationFromParameters');
200+
$extractMethod->setAccessible(true);
201+
$extractMethod->invoke($this->extension, $parameters);
202+
203+
$formatMethod = $reflection->getMethod('formatTestName');
204+
$formatMethod->setAccessible(true);
205+
206+
$testName = 'Phauthentic\\PHPUnit\\ExecutionTiming\\Tests\\Unit\\MyTestClass::testMethod';
207+
$result = $formatMethod->invoke($this->extension, $testName);
208+
209+
$this->assertEquals('MyTestClass::testMethod', $result);
210+
}
211+
212+
public function testFormatTestNameWithFQCNDisabledAndNoNamespace(): void
213+
{
214+
$parameters = ParameterCollection::fromArray([
215+
'showFQCN' => 'false',
216+
]);
217+
218+
$reflection = new \ReflectionClass($this->extension);
219+
$extractMethod = $reflection->getMethod('extractConfigurationFromParameters');
220+
$extractMethod->setAccessible(true);
221+
$extractMethod->invoke($this->extension, $parameters);
222+
223+
$formatMethod = $reflection->getMethod('formatTestName');
224+
$formatMethod->setAccessible(true);
225+
226+
$testName = 'MyTestClass::testMethod';
227+
$result = $formatMethod->invoke($this->extension, $testName);
228+
229+
$this->assertEquals('MyTestClass::testMethod', $result);
230+
}
231+
232+
public function testFormatTestNameWithFQCNDisabledAndNoMethodName(): void
233+
{
234+
$parameters = ParameterCollection::fromArray([
235+
'showFQCN' => 'false',
236+
]);
237+
238+
$reflection = new \ReflectionClass($this->extension);
239+
$extractMethod = $reflection->getMethod('extractConfigurationFromParameters');
240+
$extractMethod->setAccessible(true);
241+
$extractMethod->invoke($this->extension, $parameters);
242+
243+
$formatMethod = $reflection->getMethod('formatTestName');
244+
$formatMethod->setAccessible(true);
245+
246+
$testName = 'Phauthentic\\PHPUnit\\ExecutionTiming\\Tests\\Unit\\MyTestClass';
247+
$result = $formatMethod->invoke($this->extension, $testName);
248+
249+
$this->assertEquals('Phauthentic\\PHPUnit\\ExecutionTiming\\Tests\\Unit\\MyTestClass', $result);
250+
}
251+
252+
public function testFormatTestNameWithFQCNDisabledAndSingleNamespaceLevel(): void
253+
{
254+
$parameters = ParameterCollection::fromArray([
255+
'showFQCN' => 'false',
256+
]);
257+
258+
$reflection = new \ReflectionClass($this->extension);
259+
$extractMethod = $reflection->getMethod('extractConfigurationFromParameters');
260+
$extractMethod->setAccessible(true);
261+
$extractMethod->invoke($this->extension, $parameters);
262+
263+
$formatMethod = $reflection->getMethod('formatTestName');
264+
$formatMethod->setAccessible(true);
265+
266+
$testName = 'MyNamespace\\MyTestClass::testMethod';
267+
$result = $formatMethod->invoke($this->extension, $testName);
268+
269+
$this->assertEquals('MyTestClass::testMethod', $result);
270+
}
136271
}

0 commit comments

Comments
 (0)