Skip to content

Commit

Permalink
Added theming support.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bilge committed Jul 28, 2024
1 parent 9b9997b commit 5b25d0f
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 50 deletions.
7 changes: 7 additions & 0 deletions src/PipConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@

namespace ScriptFUSION\Pip;

use ScriptFUSION\Pip\Theme\ClassicTheme;
use ScriptFUSION\Pip\Theme\Theme;

final class PipConfig
{
public int $perfSlow = 200;
public int $perfVslow = 1_000;
public bool $testDpArgs = true;
public string $testNameStrip = '';

public function __construct(public readonly Theme $theme = new ClassicTheme())
{
}
}
63 changes: 13 additions & 50 deletions src/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use PHPUnit\Event\Test\Skipped;
use PHPUnit\Event\TestRunner\ExecutionStarted;
use PHPUnit\Event\Tracer\Tracer;
use PHPUnit\Util\Color;

final class Printer implements Tracer
{
Expand Down Expand Up @@ -122,61 +121,25 @@ public function trace(Event $event): void
$id .= $data;
}

$ms = round($event->telemetryInfo()->time()->duration($this->start)->asFloat() * 1_000);
$ms = round($event->telemetryInfo()->time()->duration($this->start)->asFloat() * 1_000)|0;
foreach ($this->performanceThresholds as $colour => $threshold) {
if ($ms >= $threshold) {
break;
}
}

printf(
"%3d%% %s %s %s%s",
floor(++$this->testCounter / $this->totalTests * 100),
$this->status->getStatusColour() === ''
? $this->status->getStatusCode()
: Color::colorize("fg-{$this->status->getStatusColour()}", $this->status->getStatusCode()),
Color::colorize("fg-{$this->status->getColour()}", $id),
Color::colorize("fg-$colour", "($ms ms)"),
PHP_EOL,
);

if ($this->status === TestStatus::Failed) {
echo PHP_EOL, Color::colorize('fg-red', $this->throwable->description()), PHP_EOL,
Color::colorize('fg-red', $this->throwable->stackTrace()), PHP_EOL
;

$this->throwable = null;
}

while ($this->status === TestStatus::Errored && $this->throwable) {
echo PHP_EOL, Color::colorize('fg-white,bg-red', " {$this->throwable->className()} "), ' ',
Color::colorize('fg-red', $this->throwable->message()), PHP_EOL, PHP_EOL,
Color::colorize('fg-red', $this->throwable->stackTrace()), PHP_EOL
;

if ($this->throwable->hasPrevious()) {
echo Color::colorize('fg-red', 'Caused by');

$this->throwable = $this->throwable->previous();
} else {
$this->throwable = null;
}
}

if ($this->trace) {
printf(
Color::colorize("fg-{$this->status->getColour()}", '%s%s: %s in %s on line %s%1$s%1$s'),
PHP_EOL,
$this->status->name,
$this->trace->message,
$this->trace->file,
$this->trace->line
);

$this->trace = null;
}

$this->status = null;
$this->config->theme->onTestFinished(new TestResult(
$id,
$this->status,
$this->totalTests,
++$this->testCounter,
$ms,
$colour,
$this->throwable,
$this->trace,
));

$this->trace = $this->throwable = $this->status = null;
}

if ($event instanceof \PHPUnit\Event\TestRunner\Finished) {
Expand Down
26 changes: 26 additions & 0 deletions src/TestResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
declare(strict_types=1);

namespace ScriptFUSION\Pip;

use PHPUnit\Event\Code\Throwable;

final class TestResult
{
public function __construct(
public readonly string $id,
public readonly TestStatus $status,
public readonly int $totalTests,
public readonly int $testCounter,
public readonly int $testDurationMs,
public readonly string $testDurationColour,
public readonly ?Throwable $throwable,
public readonly ?Trace $trace,
) {
}

public function calculateProgressPercentage(): int
{
return floor($this->testCounter / $this->totalTests * 100)|0;
}
}
56 changes: 56 additions & 0 deletions src/Theme/ClassicTheme.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
declare(strict_types=1);

namespace ScriptFUSION\Pip\Theme;

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

final class ClassicTheme implements Theme
{
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)"),
PHP_EOL,
);

if ($result->status === TestStatus::Failed) {
echo PHP_EOL, Color::colorize('fg-red', $result->throwable->description()), PHP_EOL,
Color::colorize('fg-red', $result->throwable->stackTrace()), PHP_EOL
;
}

while ($result->status === TestStatus::Errored && $throwable ??= $result->throwable) {
echo PHP_EOL, Color::colorize('fg-white,bg-red', " {$throwable->className()} "), ' ',
Color::colorize('fg-red', $throwable->message()), PHP_EOL, PHP_EOL,
Color::colorize('fg-red', $throwable->stackTrace()), PHP_EOL
;

if (!$throwable->hasPrevious()) {
break;
}

echo Color::colorize('fg-red', 'Caused by');
$throwable = $throwable->previous();
}

if ($result->trace) {
printf(
Color::colorize("fg-{$result->status->getColour()}", '%s%s: %s in %s on line %s%1$s%1$s'),
PHP_EOL,
$result->status->name,
$result->trace->message,
$result->trace->file,
$result->trace->line,
);
}
}
}
11 changes: 11 additions & 0 deletions src/Theme/Theme.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
declare(strict_types=1);

namespace ScriptFUSION\Pip\Theme;

use ScriptFUSION\Pip\TestResult;

interface Theme
{
public function onTestFinished(TestResult $result): void;
}

0 comments on commit 5b25d0f

Please sign in to comment.