Skip to content

Commit

Permalink
Added trace messages for notice, warning, deprecation and risky events.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bilge committed Jul 25, 2024
1 parent dd8bf5a commit d3774a4
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Pip makes no attempt to modify the test summary; only runtime output is changed.
composer require --dev scriptfusion/pip
```

2. Declare the printer class in your `phpunit.xml` configuration file.
2. Declare the printer extension in your `phpunit.xml` configuration file.

```xml
<extensions>
Expand Down
31 changes: 27 additions & 4 deletions src/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ final class Printer implements Tracer

private ?Throwable $throwable = null;

private ?Trace $trace = null;

private bool $flawless = true;

public function __construct(private readonly PipConfig $config)
Expand All @@ -60,15 +62,15 @@ public function trace(Event $event): void
$this->status ??= $this->flawless ? TestStatus::Passed : TestStatus::Flawed;
}
if ($event instanceof Failed) {
$this->throwable = $event->throwable();

$this->status ??= TestStatus::Failed;

$this->throwable = $event->throwable();
$this->flawless = false;
}
if ($event instanceof Errored) {
$this->throwable = $event->throwable();

$this->status ??= TestStatus::Errored;

$this->throwable = $event->throwable();
$this->flawless = false;
}
if ($event instanceof Skipped) {
Expand All @@ -82,15 +84,23 @@ public function trace(Event $event): void
if ($this->status === TestStatus::Passed || $this->status === TestStatus::Flawed) {
$this->status = TestStatus::Risky;
}

$this->trace = new Trace($event->message(), $event->test()->file(), $event->test()->line());
}
if ($event instanceof PhpNoticeTriggered) {
$this->status ??= TestStatus::Notice;

$this->trace = Trace::fromEvent($event);
}
if ($event instanceof PhpWarningTriggered) {
$this->status ??= TestStatus::Warning;

$this->trace = Trace::fromEvent($event);
}
if ($event instanceof PhpDeprecationTriggered) {
$this->status ??= TestStatus::Deprecated;

$this->trace = Trace::fromEvent($event);
}

if ($event instanceof Finished) {
Expand Down Expand Up @@ -153,6 +163,19 @@ public function trace(Event $event): void
}
}

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;
}

Expand Down
4 changes: 2 additions & 2 deletions src/TestStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ public function getStatusColour(): string
public function getColour(): string
{
return match ($this) {
self::Passed => 'green,bold',
self::Passed,
self::Flawed => 'green,bold',
self::Failed => 'red,bold',
self::Failed,
self::Errored => 'red,bold',
self::Skipped => 'cyan,bold',
self::Incomplete,
Expand Down
22 changes: 22 additions & 0 deletions src/Trace.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
declare(strict_types=1);

namespace ScriptFUSION\Pip;

use PHPUnit\Event\Test\PhpDeprecationTriggered;
use PHPUnit\Event\Test\PhpNoticeTriggered;
use PHPUnit\Event\Test\PhpWarningTriggered;

final class Trace
{
public function __construct(
public readonly string $message,
public readonly string $file,
public readonly int $line,
) {}

public static function fromEvent(PhpWarningTriggered|PhpNoticeTriggered|PhpDeprecationTriggered $event): self
{
return new self($event->message(), $event->file(), $event->line());
}
}
10 changes: 7 additions & 3 deletions test/CapabilitiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ public function testSkipped(): void
self::markTestSkipped();
}

public function testRisky(): void
public function testIncomplete(): void
{
self::markTestIncomplete();
}

public function testIncomplete(): void
public function testRisky(): void
{
self::markTestIncomplete();
}

public function testNotice(): void
Expand Down Expand Up @@ -106,10 +106,14 @@ public static function provideSuccessesAndFailures(): iterable
public function testSlow(): void
{
usleep(200_000);

self::assertTrue(true);
}

public function testGigaSlow(): void
{
sleep(1);

self::assertTrue(true);
}
}
3 changes: 3 additions & 0 deletions test/functional/deprecation.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ Runtime: %s
Configuration: %s

100% D ScriptFUSIONTest\Pip\CapabilitiesTest::testDeprecation (%d ms)

Deprecated: Serializable@anonymous implements the Serializable interface, which is deprecated. Implement __serialize() and __unserialize() instead (or in addition, if support for old PHP versions is necessary) in %s%eCapabilitiesTest.php on line %d



Time: %s
%A
Expand Down
3 changes: 3 additions & 0 deletions test/functional/notice.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ Runtime: %s
Configuration: %s

100% N ScriptFUSIONTest\Pip\CapabilitiesTest::testNotice (%d ms)

Notice: Only variables should be assigned by reference in %s%eCapabilitiesTest.php on line %d



Time: %s
%A
Expand Down
3 changes: 3 additions & 0 deletions test/functional/risky.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ Runtime: %s
Configuration: %s

100% R ScriptFUSIONTest\Pip\CapabilitiesTest::testRisky (%d ms)

Risky: This test did not perform any assertions in %s%eCapabilitiesTest.php on line %d



Time: %s
%A
Expand Down
5 changes: 2 additions & 3 deletions test/functional/slow.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ PHPUnit %s
Runtime: %s
Configuration: %s

100% [33;1mR[0m [33;1mScriptFUSIONTest\Pip\CapabilitiesTest::testSlow[0m [33m(%d ms)[0m
100% . [32;1mScriptFUSIONTest\Pip\CapabilitiesTest::testSlow[0m [33m(%d ms)[0m


Time: %s
%A
OK, but %s!
Tests: 1, Assertions: 0, Risky: 1.
OK (1 test, 1 assertion)
5 changes: 2 additions & 3 deletions test/functional/vslow.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ PHPUnit %s
Runtime: %s
Configuration: %s

100% [33;1mR[0m [33;1mScriptFUSIONTest\Pip\CapabilitiesTest::testGigaSlow[0m [31m(%d ms)[0m
100% . [32;1mScriptFUSIONTest\Pip\CapabilitiesTest::testGigaSlow[0m [31m(%d ms)[0m


Time: %s
%A
OK, but %s!
Tests: 1, Assertions: 0, Risky: 1.
OK (1 test, 1 assertion)
3 changes: 3 additions & 0 deletions test/functional/warning.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ Runtime: %s
Configuration: %s

100% W ScriptFUSIONTest\Pip\CapabilitiesTest::testWarning (%d ms)

Warning: foreach() argument must be of type array|object, int given in %s%eCapabilitiesTest.php on line %d



Time: %s
%A
Expand Down

0 comments on commit d3774a4

Please sign in to comment.