Skip to content

Commit

Permalink
Fully separated theme from printer.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bilge committed Jul 29, 2024
1 parent 5b25d0f commit efaeaa2
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 61 deletions.
19 changes: 6 additions & 13 deletions src/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@

final class Printer implements Tracer
{
private readonly array $performanceThresholds;

private int $totalTests;

private int $testCounter = 0;
Expand All @@ -40,11 +38,6 @@ final class Printer implements Tracer

public function __construct(private readonly PipConfig $config)
{
$this->performanceThresholds = [
'red' => $config->perfVslow,
'yellow' => $config->perfSlow,
'green' => 0,
];
}

public function trace(Event $event): void
Expand Down Expand Up @@ -122,19 +115,19 @@ public function trace(Event $event): void
}

$ms = round($event->telemetryInfo()->time()->duration($this->start)->asFloat() * 1_000)|0;
foreach ($this->performanceThresholds as $colour => $threshold) {
if ($ms >= $threshold) {
break;
}
}
$performance = match (true) {
$ms >= $this->config->perfVslow => TestPerformance::VerySlow,
$ms >= $this->config->perfSlow => TestPerformance::Slow,
default => TestPerformance::OK,
};

$this->config->theme->onTestFinished(new TestResult(
$id,
$this->status,
$this->totalTests,
++$this->testCounter,
$ms,
$colour,
$performance,
$this->throwable,
$this->trace,
));
Expand Down
11 changes: 11 additions & 0 deletions src/TestPerformance.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
declare(strict_types=1);

namespace ScriptFUSION\Pip;

enum TestPerformance
{
case OK;
case Slow;
case VerySlow;
}
2 changes: 1 addition & 1 deletion src/TestResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function __construct(
public readonly int $totalTests,
public readonly int $testCounter,
public readonly int $testDurationMs,
public readonly string $testDurationColour,
public readonly TestPerformance $testPerformance,
public readonly ?Throwable $throwable,
public readonly ?Trace $trace,
) {
Expand Down
41 changes: 0 additions & 41 deletions src/TestStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,45 +15,4 @@ enum TestStatus
case Notice;
case Warning;
case Deprecated;

public function getStatusCode(): string
{
return match ($this) {
self::Passed => '.',
self::Flawed => '!',
self::Failed => 'F',
self::Errored => 'E',
self::Skipped => 'S',
self::Incomplete => 'I',
self::Risky => 'R',
self::Notice => 'N',
self::Warning => 'W',
self::Deprecated => 'D',
};
}

public function getStatusColour(): string
{
return match ($this) {
self::Passed => '',
self::Flawed => 'red',
default => $this->getColour(),
};
}

public function getColour(): string
{
return match ($this) {
self::Passed,
self::Flawed => 'green,bold',
self::Failed,
self::Errored => 'red,bold',
self::Skipped => 'cyan,bold',
self::Incomplete,
self::Risky,
self::Notice,
self::Warning,
self::Deprecated, => 'yellow,bold',
};
}
}
59 changes: 53 additions & 6 deletions src/Theme/ClassicTheme.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace ScriptFUSION\Pip\Theme;

use PHPUnit\Util\Color;
use ScriptFUSION\Pip\TestPerformance;
use ScriptFUSION\Pip\TestResult;
use ScriptFUSION\Pip\TestStatus;

Expand All @@ -14,11 +15,14 @@ public function onTestFinished(TestResult $result): void
printf(
"%3d%% %s %s %s%s",
$result->calculateProgressPercentage(),
$result->status->getStatusColour() === ''
? $result->status->getStatusCode()
: Color::colorize("fg-{$result->status->getStatusColour()}", $result->status->getStatusCode()),
Color::colorize("fg-{$result->status->getColour()}", $result->id),
Color::colorize("fg-$result->testDurationColour", "($result->testDurationMs ms)"),
($statusColour = self::getStatusColour($result->status)) === ''
? self::getStatusCode($result->status)
: Color::colorize("fg-$statusColour", self::getStatusCode($result->status)),
Color::colorize("fg-" . self::getColour($result->status), $result->id),
Color::colorize(
'fg-' . self::getPeformanceColour($result->testPerformance),
"($result->testDurationMs ms)"
),
PHP_EOL,
);

Expand All @@ -44,7 +48,7 @@ public function onTestFinished(TestResult $result): void

if ($result->trace) {
printf(
Color::colorize("fg-{$result->status->getColour()}", '%s%s: %s in %s on line %s%1$s%1$s'),
Color::colorize("fg-$statusColour", '%s%s: %s in %s on line %s%1$s%1$s'),
PHP_EOL,
$result->status->name,
$result->trace->message,
Expand All @@ -53,4 +57,47 @@ public function onTestFinished(TestResult $result): void
);
}
}

private static function getStatusCode(TestStatus $status): string
{
return match ($status) {
TestStatus::Passed => '.',
TestStatus::Flawed => '!',
default => $status->name[0],
};
}

private static function getStatusColour(TestStatus $status): string
{
return match ($status) {
TestStatus::Passed => '',
TestStatus::Flawed => 'red',
default => self::getColour($status),
};
}

private static function getColour(TestStatus $status): string
{
return match ($status) {
TestStatus::Passed,
TestStatus::Flawed => 'green,bold',
TestStatus::Failed,
TestStatus::Errored => 'red,bold',
TestStatus::Skipped => 'cyan,bold',
TestStatus::Incomplete,
TestStatus::Risky,
TestStatus::Notice,
TestStatus::Warning,
TestStatus::Deprecated, => 'yellow,bold',
};
}

private static function getPeformanceColour(TestPerformance $performance): string
{
return match ($performance) {
TestPerformance::OK => 'green',
TestPerformance::Slow => 'yellow',
TestPerformance::VerySlow => 'red',
};
}
}

0 comments on commit efaeaa2

Please sign in to comment.