Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@
</testsuite>
</testsuites>

<extensions>
<bootstrap class="Phauthentic\PHPUnit\ExecutionTiming\ExecutionTimeExtension">
<parameter name="topN" value="10"/>
<parameter name="showIndividualTimings" value="false"/>
<parameter name="warningThreshold" value="1.0"/>
<parameter name="dangerThreshold" value="5.0"/>
</bootstrap>
</extensions>

<source>
<include>
<directory suffix=".php">src</directory>
Expand Down
42 changes: 35 additions & 7 deletions src/ExecutionTimingExtension/ExecutionTimeReportPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private function printTestLines(array $topTests): void

/**
* @param array<int, array{name: string, time: float}> $topTests
* @return array{rank: int, name: int}
* @return array{rank: int, name: int, timeMs: int, timeSec: int}
*/
private function calculateColumnWidths(array $topTests): array
{
Expand All @@ -100,35 +100,53 @@ private function calculateColumnWidths(array $topTests): array
)
);

$maxTimeMsWidth = max(
array_map(
static fn(array $test): int => strlen(sprintf('%.2f ms', round($test['time'] * 1000, 2))),
$topTests
)
);

$maxTimeSecWidth = max(
array_map(
static fn(array $test): int => strlen(sprintf('(%.3f s)', round($test['time'], 3))),
$topTests
)
);

return [
'rank' => $maxRankWidth,
'name' => $maxNameWidth,
'timeMs' => $maxTimeMsWidth,
'timeSec' => $maxTimeSecWidth,
];
}

/**
* @param array{name: string, time: float} $test
* @param array{rank: int, name: int} $columnWidths
* @param array{rank: int, name: int, timeMs: int, timeSec: int} $columnWidths
*/
private function printTestLine(array $test, int $rank, array $columnWidths): void
{
$timeMs = round($test['time'] * 1000, 2);
$timeSec = round($test['time'], 3);
$rankFormatted = $this->formatRank($rank, $columnWidths['rank']);
$nameFormatted = $this->formatTestName($test['name'], $columnWidths['name']);
$timeMsFormatted = $this->formatTimeMs($timeMs, $columnWidths['timeMs']);
$timeSecFormatted = $this->formatTimeSec($timeSec, $columnWidths['timeSec']);

$color = $this->determineColor($test['time']);

$nameDisplay = $color !== '' ? Color::colorize($color, $nameFormatted) : $nameFormatted;
$timeMsDisplay = $color !== '' ? Color::colorize($color, sprintf('%.2f ms', $timeMs)) : sprintf('%.2f ms', $timeMs);
$timeSecDisplay = $color !== '' ? Color::colorize($color, sprintf('(%.3f s)', $timeSec)) : sprintf('(%.3f s)', $timeSec);
$timeMsDisplay = $color !== '' ? Color::colorize($color, $timeMsFormatted) : $timeMsFormatted;
$timeSecDisplay = $color !== '' ? Color::colorize($color, $timeSecFormatted) : $timeSecFormatted;

printf(
" %s. %s : %s %s" . PHP_EOL,
" %s. ⏱ %s %s %s" . PHP_EOL,
$rankFormatted,
$nameDisplay,
$timeMsDisplay,
$timeSecDisplay
$timeSecDisplay,
$nameDisplay
);
}

Expand Down Expand Up @@ -159,4 +177,14 @@ private function formatTestName(string $name, int $width): string
{
return str_pad($name, $width, ' ');
}

private function formatTimeMs(float $timeMs, int $width): string
{
return str_pad(sprintf('%.2f ms', $timeMs), $width, ' ', STR_PAD_LEFT);
}

private function formatTimeSec(float $timeSec, int $width): string
{
return str_pad(sprintf('(%.3f s)', $timeSec), $width, ' ', STR_PAD_LEFT);
}
}
46 changes: 23 additions & 23 deletions tests/Unit/ExecutionTimeReportPrinterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,12 @@ public function testPrintAlignsColumns(): void
$firstLine = $testLines[0];
$secondLine = $testLines[1];

$firstColonPos = strpos($firstLine, ':');
$secondColonPos = strpos($secondLine, ':');
$firstClockPos = strpos($firstLine, '');
$secondClockPos = strpos($secondLine, '');

$this->assertNotFalse($firstColonPos);
$this->assertNotFalse($secondColonPos);
$this->assertEquals($firstColonPos, $secondColonPos, 'Columns should be aligned');
$this->assertNotFalse($firstClockPos);
$this->assertNotFalse($secondClockPos);
$this->assertEquals($firstClockPos, $secondClockPos, 'Columns should be aligned');
}
}

Expand Down Expand Up @@ -220,22 +220,16 @@ public function testPrintColorsCorrectlyWithMultipleThresholds(): void
$output = ob_get_clean() ?: '';

// FastTest should not be colored
$fastTestPos = strpos($output, 'FastTest');
$this->assertNotFalse($fastTestPos);
$fastTestLine = substr($output, $fastTestPos, 100);
$fastTestLine = $this->extractLineContaining($output, 'FastTest');
$this->assertStringNotContainsString("\x1b[", $fastTestLine);

// WarningTest should be yellow
$warningTestPos = strpos($output, 'WarningTest');
$this->assertNotFalse($warningTestPos);
$warningTestLine = substr($output, $warningTestPos, 200);
$warningTestLine = $this->extractLineContaining($output, 'WarningTest');
$this->assertStringContainsString("\x1b[33m", $warningTestLine);
$this->assertStringNotContainsString("\x1b[31m", $warningTestLine);

// DangerTest should be red
$dangerTestPos = strpos($output, 'DangerTest');
$this->assertNotFalse($dangerTestPos);
$dangerTestLine = substr($output, $dangerTestPos, 200);
$dangerTestLine = $this->extractLineContaining($output, 'DangerTest');
$this->assertStringContainsString("\x1b[31m", $dangerTestLine);
}

Expand All @@ -254,21 +248,27 @@ public function testPrintRespectsThresholdConfiguration(): void
$output = ob_get_clean() ?: '';

// Test1 (0.8s) should not be colored
$test1Pos = strpos($output, 'Test1');
$this->assertNotFalse($test1Pos);
$test1Line = substr($output, $test1Pos, 100);
$test1Line = $this->extractLineContaining($output, 'Test1');
$this->assertStringNotContainsString("\x1b[", $test1Line);

// Test2 (1.2s) should be yellow (>= 1.0 but < 2.0)
$test2Pos = strpos($output, 'Test2');
$this->assertNotFalse($test2Pos);
$test2Line = substr($output, $test2Pos, 200);
$test2Line = $this->extractLineContaining($output, 'Test2');
$this->assertStringContainsString("\x1b[33m", $test2Line);

// Test3 (3.0s) should be red (>= 2.0)
$test3Pos = strpos($output, 'Test3');
$this->assertNotFalse($test3Pos);
$test3Line = substr($output, $test3Pos, 200);
$test3Line = $this->extractLineContaining($output, 'Test3');
$this->assertStringContainsString("\x1b[31m", $test3Line);
}

private function extractLineContaining(string $output, string $search): string
{
$lines = explode("\n", $output);
foreach ($lines as $line) {
if (str_contains($line, $search)) {
return $line;
}
}

return '';
}
}