Skip to content

Commit 5b25d0f

Browse files
committed
Added theming support.
1 parent 9b9997b commit 5b25d0f

File tree

5 files changed

+113
-50
lines changed

5 files changed

+113
-50
lines changed

src/PipConfig.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,17 @@
33

44
namespace ScriptFUSION\Pip;
55

6+
use ScriptFUSION\Pip\Theme\ClassicTheme;
7+
use ScriptFUSION\Pip\Theme\Theme;
8+
69
final class PipConfig
710
{
811
public int $perfSlow = 200;
912
public int $perfVslow = 1_000;
1013
public bool $testDpArgs = true;
1114
public string $testNameStrip = '';
15+
16+
public function __construct(public readonly Theme $theme = new ClassicTheme())
17+
{
18+
}
1219
}

src/Printer.php

Lines changed: 13 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
use PHPUnit\Event\Test\Skipped;
2020
use PHPUnit\Event\TestRunner\ExecutionStarted;
2121
use PHPUnit\Event\Tracer\Tracer;
22-
use PHPUnit\Util\Color;
2322

2423
final class Printer implements Tracer
2524
{
@@ -122,61 +121,25 @@ public function trace(Event $event): void
122121
$id .= $data;
123122
}
124123

125-
$ms = round($event->telemetryInfo()->time()->duration($this->start)->asFloat() * 1_000);
124+
$ms = round($event->telemetryInfo()->time()->duration($this->start)->asFloat() * 1_000)|0;
126125
foreach ($this->performanceThresholds as $colour => $threshold) {
127126
if ($ms >= $threshold) {
128127
break;
129128
}
130129
}
131130

132-
printf(
133-
"%3d%% %s %s %s%s",
134-
floor(++$this->testCounter / $this->totalTests * 100),
135-
$this->status->getStatusColour() === ''
136-
? $this->status->getStatusCode()
137-
: Color::colorize("fg-{$this->status->getStatusColour()}", $this->status->getStatusCode()),
138-
Color::colorize("fg-{$this->status->getColour()}", $id),
139-
Color::colorize("fg-$colour", "($ms ms)"),
140-
PHP_EOL,
141-
);
142-
143-
if ($this->status === TestStatus::Failed) {
144-
echo PHP_EOL, Color::colorize('fg-red', $this->throwable->description()), PHP_EOL,
145-
Color::colorize('fg-red', $this->throwable->stackTrace()), PHP_EOL
146-
;
147-
148-
$this->throwable = null;
149-
}
150-
151-
while ($this->status === TestStatus::Errored && $this->throwable) {
152-
echo PHP_EOL, Color::colorize('fg-white,bg-red', " {$this->throwable->className()} "), ' ',
153-
Color::colorize('fg-red', $this->throwable->message()), PHP_EOL, PHP_EOL,
154-
Color::colorize('fg-red', $this->throwable->stackTrace()), PHP_EOL
155-
;
156-
157-
if ($this->throwable->hasPrevious()) {
158-
echo Color::colorize('fg-red', 'Caused by');
159-
160-
$this->throwable = $this->throwable->previous();
161-
} else {
162-
$this->throwable = null;
163-
}
164-
}
165-
166-
if ($this->trace) {
167-
printf(
168-
Color::colorize("fg-{$this->status->getColour()}", '%s%s: %s in %s on line %s%1$s%1$s'),
169-
PHP_EOL,
170-
$this->status->name,
171-
$this->trace->message,
172-
$this->trace->file,
173-
$this->trace->line
174-
);
175-
176-
$this->trace = null;
177-
}
178-
179-
$this->status = null;
131+
$this->config->theme->onTestFinished(new TestResult(
132+
$id,
133+
$this->status,
134+
$this->totalTests,
135+
++$this->testCounter,
136+
$ms,
137+
$colour,
138+
$this->throwable,
139+
$this->trace,
140+
));
141+
142+
$this->trace = $this->throwable = $this->status = null;
180143
}
181144

182145
if ($event instanceof \PHPUnit\Event\TestRunner\Finished) {

src/TestResult.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace ScriptFUSION\Pip;
5+
6+
use PHPUnit\Event\Code\Throwable;
7+
8+
final class TestResult
9+
{
10+
public function __construct(
11+
public readonly string $id,
12+
public readonly TestStatus $status,
13+
public readonly int $totalTests,
14+
public readonly int $testCounter,
15+
public readonly int $testDurationMs,
16+
public readonly string $testDurationColour,
17+
public readonly ?Throwable $throwable,
18+
public readonly ?Trace $trace,
19+
) {
20+
}
21+
22+
public function calculateProgressPercentage(): int
23+
{
24+
return floor($this->testCounter / $this->totalTests * 100)|0;
25+
}
26+
}

src/Theme/ClassicTheme.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace ScriptFUSION\Pip\Theme;
5+
6+
use PHPUnit\Util\Color;
7+
use ScriptFUSION\Pip\TestResult;
8+
use ScriptFUSION\Pip\TestStatus;
9+
10+
final class ClassicTheme implements Theme
11+
{
12+
public function onTestFinished(TestResult $result): void
13+
{
14+
printf(
15+
"%3d%% %s %s %s%s",
16+
$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)"),
22+
PHP_EOL,
23+
);
24+
25+
if ($result->status === TestStatus::Failed) {
26+
echo PHP_EOL, Color::colorize('fg-red', $result->throwable->description()), PHP_EOL,
27+
Color::colorize('fg-red', $result->throwable->stackTrace()), PHP_EOL
28+
;
29+
}
30+
31+
while ($result->status === TestStatus::Errored && $throwable ??= $result->throwable) {
32+
echo PHP_EOL, Color::colorize('fg-white,bg-red', " {$throwable->className()} "), ' ',
33+
Color::colorize('fg-red', $throwable->message()), PHP_EOL, PHP_EOL,
34+
Color::colorize('fg-red', $throwable->stackTrace()), PHP_EOL
35+
;
36+
37+
if (!$throwable->hasPrevious()) {
38+
break;
39+
}
40+
41+
echo Color::colorize('fg-red', 'Caused by');
42+
$throwable = $throwable->previous();
43+
}
44+
45+
if ($result->trace) {
46+
printf(
47+
Color::colorize("fg-{$result->status->getColour()}", '%s%s: %s in %s on line %s%1$s%1$s'),
48+
PHP_EOL,
49+
$result->status->name,
50+
$result->trace->message,
51+
$result->trace->file,
52+
$result->trace->line,
53+
);
54+
}
55+
}
56+
}

src/Theme/Theme.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\Theme;
5+
6+
use ScriptFUSION\Pip\TestResult;
7+
8+
interface Theme
9+
{
10+
public function onTestFinished(TestResult $result): void;
11+
}

0 commit comments

Comments
 (0)