Skip to content

Commit efaeaa2

Browse files
committed
Fully separated theme from printer.
1 parent 5b25d0f commit efaeaa2

File tree

5 files changed

+71
-61
lines changed

5 files changed

+71
-61
lines changed

src/Printer.php

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222

2323
final class Printer implements Tracer
2424
{
25-
private readonly array $performanceThresholds;
26-
2725
private int $totalTests;
2826

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

4139
public function __construct(private readonly PipConfig $config)
4240
{
43-
$this->performanceThresholds = [
44-
'red' => $config->perfVslow,
45-
'yellow' => $config->perfSlow,
46-
'green' => 0,
47-
];
4841
}
4942

5043
public function trace(Event $event): void
@@ -122,19 +115,19 @@ public function trace(Event $event): void
122115
}
123116

124117
$ms = round($event->telemetryInfo()->time()->duration($this->start)->asFloat() * 1_000)|0;
125-
foreach ($this->performanceThresholds as $colour => $threshold) {
126-
if ($ms >= $threshold) {
127-
break;
128-
}
129-
}
118+
$performance = match (true) {
119+
$ms >= $this->config->perfVslow => TestPerformance::VerySlow,
120+
$ms >= $this->config->perfSlow => TestPerformance::Slow,
121+
default => TestPerformance::OK,
122+
};
130123

131124
$this->config->theme->onTestFinished(new TestResult(
132125
$id,
133126
$this->status,
134127
$this->totalTests,
135128
++$this->testCounter,
136129
$ms,
137-
$colour,
130+
$performance,
138131
$this->throwable,
139132
$this->trace,
140133
));

src/TestPerformance.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace ScriptFUSION\Pip;
5+
6+
enum TestPerformance
7+
{
8+
case OK;
9+
case Slow;
10+
case VerySlow;
11+
}

src/TestResult.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public function __construct(
1313
public readonly int $totalTests,
1414
public readonly int $testCounter,
1515
public readonly int $testDurationMs,
16-
public readonly string $testDurationColour,
16+
public readonly TestPerformance $testPerformance,
1717
public readonly ?Throwable $throwable,
1818
public readonly ?Trace $trace,
1919
) {

src/TestStatus.php

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -15,45 +15,4 @@ enum TestStatus
1515
case Notice;
1616
case Warning;
1717
case Deprecated;
18-
19-
public function getStatusCode(): string
20-
{
21-
return match ($this) {
22-
self::Passed => '.',
23-
self::Flawed => '!',
24-
self::Failed => 'F',
25-
self::Errored => 'E',
26-
self::Skipped => 'S',
27-
self::Incomplete => 'I',
28-
self::Risky => 'R',
29-
self::Notice => 'N',
30-
self::Warning => 'W',
31-
self::Deprecated => 'D',
32-
};
33-
}
34-
35-
public function getStatusColour(): string
36-
{
37-
return match ($this) {
38-
self::Passed => '',
39-
self::Flawed => 'red',
40-
default => $this->getColour(),
41-
};
42-
}
43-
44-
public function getColour(): string
45-
{
46-
return match ($this) {
47-
self::Passed,
48-
self::Flawed => 'green,bold',
49-
self::Failed,
50-
self::Errored => 'red,bold',
51-
self::Skipped => 'cyan,bold',
52-
self::Incomplete,
53-
self::Risky,
54-
self::Notice,
55-
self::Warning,
56-
self::Deprecated, => 'yellow,bold',
57-
};
58-
}
5918
}

src/Theme/ClassicTheme.php

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
namespace ScriptFUSION\Pip\Theme;
55

66
use PHPUnit\Util\Color;
7+
use ScriptFUSION\Pip\TestPerformance;
78
use ScriptFUSION\Pip\TestResult;
89
use ScriptFUSION\Pip\TestStatus;
910

@@ -14,11 +15,14 @@ public function onTestFinished(TestResult $result): void
1415
printf(
1516
"%3d%% %s %s %s%s",
1617
$result->calculateProgressPercentage(),
17-
$result->status->getStatusColour() === ''
18-
? $result->status->getStatusCode()
19-
: Color::colorize("fg-{$result->status->getStatusColour()}", $result->status->getStatusCode()),
20-
Color::colorize("fg-{$result->status->getColour()}", $result->id),
21-
Color::colorize("fg-$result->testDurationColour", "($result->testDurationMs ms)"),
18+
($statusColour = self::getStatusColour($result->status)) === ''
19+
? self::getStatusCode($result->status)
20+
: Color::colorize("fg-$statusColour", self::getStatusCode($result->status)),
21+
Color::colorize("fg-" . self::getColour($result->status), $result->id),
22+
Color::colorize(
23+
'fg-' . self::getPeformanceColour($result->testPerformance),
24+
"($result->testDurationMs ms)"
25+
),
2226
PHP_EOL,
2327
);
2428

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

4549
if ($result->trace) {
4650
printf(
47-
Color::colorize("fg-{$result->status->getColour()}", '%s%s: %s in %s on line %s%1$s%1$s'),
51+
Color::colorize("fg-$statusColour", '%s%s: %s in %s on line %s%1$s%1$s'),
4852
PHP_EOL,
4953
$result->status->name,
5054
$result->trace->message,
@@ -53,4 +57,47 @@ public function onTestFinished(TestResult $result): void
5357
);
5458
}
5559
}
60+
61+
private static function getStatusCode(TestStatus $status): string
62+
{
63+
return match ($status) {
64+
TestStatus::Passed => '.',
65+
TestStatus::Flawed => '!',
66+
default => $status->name[0],
67+
};
68+
}
69+
70+
private static function getStatusColour(TestStatus $status): string
71+
{
72+
return match ($status) {
73+
TestStatus::Passed => '',
74+
TestStatus::Flawed => 'red',
75+
default => self::getColour($status),
76+
};
77+
}
78+
79+
private static function getColour(TestStatus $status): string
80+
{
81+
return match ($status) {
82+
TestStatus::Passed,
83+
TestStatus::Flawed => 'green,bold',
84+
TestStatus::Failed,
85+
TestStatus::Errored => 'red,bold',
86+
TestStatus::Skipped => 'cyan,bold',
87+
TestStatus::Incomplete,
88+
TestStatus::Risky,
89+
TestStatus::Notice,
90+
TestStatus::Warning,
91+
TestStatus::Deprecated, => 'yellow,bold',
92+
};
93+
}
94+
95+
private static function getPeformanceColour(TestPerformance $performance): string
96+
{
97+
return match ($performance) {
98+
TestPerformance::OK => 'green',
99+
TestPerformance::Slow => 'yellow',
100+
TestPerformance::VerySlow => 'red',
101+
};
102+
}
56103
}

0 commit comments

Comments
 (0)