Skip to content

Commit d3774a4

Browse files
committed
Added trace messages for notice, warning, deprecation and risky events.
1 parent dd8bf5a commit d3774a4

File tree

11 files changed

+75
-16
lines changed

11 files changed

+75
-16
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Pip makes no attempt to modify the test summary; only runtime output is changed.
3535
composer require --dev scriptfusion/pip
3636
```
3737

38-
2. Declare the printer class in your `phpunit.xml` configuration file.
38+
2. Declare the printer extension in your `phpunit.xml` configuration file.
3939

4040
```xml
4141
<extensions>

src/Printer.php

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ final class Printer implements Tracer
3535

3636
private ?Throwable $throwable = null;
3737

38+
private ?Trace $trace = null;
39+
3840
private bool $flawless = true;
3941

4042
public function __construct(private readonly PipConfig $config)
@@ -60,15 +62,15 @@ public function trace(Event $event): void
6062
$this->status ??= $this->flawless ? TestStatus::Passed : TestStatus::Flawed;
6163
}
6264
if ($event instanceof Failed) {
63-
$this->throwable = $event->throwable();
64-
6565
$this->status ??= TestStatus::Failed;
66+
67+
$this->throwable = $event->throwable();
6668
$this->flawless = false;
6769
}
6870
if ($event instanceof Errored) {
69-
$this->throwable = $event->throwable();
70-
7171
$this->status ??= TestStatus::Errored;
72+
73+
$this->throwable = $event->throwable();
7274
$this->flawless = false;
7375
}
7476
if ($event instanceof Skipped) {
@@ -82,15 +84,23 @@ public function trace(Event $event): void
8284
if ($this->status === TestStatus::Passed || $this->status === TestStatus::Flawed) {
8385
$this->status = TestStatus::Risky;
8486
}
87+
88+
$this->trace = new Trace($event->message(), $event->test()->file(), $event->test()->line());
8589
}
8690
if ($event instanceof PhpNoticeTriggered) {
8791
$this->status ??= TestStatus::Notice;
92+
93+
$this->trace = Trace::fromEvent($event);
8894
}
8995
if ($event instanceof PhpWarningTriggered) {
9096
$this->status ??= TestStatus::Warning;
97+
98+
$this->trace = Trace::fromEvent($event);
9199
}
92100
if ($event instanceof PhpDeprecationTriggered) {
93101
$this->status ??= TestStatus::Deprecated;
102+
103+
$this->trace = Trace::fromEvent($event);
94104
}
95105

96106
if ($event instanceof Finished) {
@@ -153,6 +163,19 @@ public function trace(Event $event): void
153163
}
154164
}
155165

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+
156179
$this->status = null;
157180
}
158181

src/TestStatus.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ public function getStatusColour(): string
4444
public function getColour(): string
4545
{
4646
return match ($this) {
47-
self::Passed => 'green,bold',
47+
self::Passed,
4848
self::Flawed => 'green,bold',
49-
self::Failed => 'red,bold',
49+
self::Failed,
5050
self::Errored => 'red,bold',
5151
self::Skipped => 'cyan,bold',
5252
self::Incomplete,

src/Trace.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace ScriptFUSION\Pip;
5+
6+
use PHPUnit\Event\Test\PhpDeprecationTriggered;
7+
use PHPUnit\Event\Test\PhpNoticeTriggered;
8+
use PHPUnit\Event\Test\PhpWarningTriggered;
9+
10+
final class Trace
11+
{
12+
public function __construct(
13+
public readonly string $message,
14+
public readonly string $file,
15+
public readonly int $line,
16+
) {}
17+
18+
public static function fromEvent(PhpWarningTriggered|PhpNoticeTriggered|PhpDeprecationTriggered $event): self
19+
{
20+
return new self($event->message(), $event->file(), $event->line());
21+
}
22+
}

test/CapabilitiesTest.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ public function testSkipped(): void
3939
self::markTestSkipped();
4040
}
4141

42-
public function testRisky(): void
42+
public function testIncomplete(): void
4343
{
44+
self::markTestIncomplete();
4445
}
4546

46-
public function testIncomplete(): void
47+
public function testRisky(): void
4748
{
48-
self::markTestIncomplete();
4949
}
5050

5151
public function testNotice(): void
@@ -106,10 +106,14 @@ public static function provideSuccessesAndFailures(): iterable
106106
public function testSlow(): void
107107
{
108108
usleep(200_000);
109+
110+
self::assertTrue(true);
109111
}
110112

111113
public function testGigaSlow(): void
112114
{
113115
sleep(1);
116+
117+
self::assertTrue(true);
114118
}
115119
}

test/functional/deprecation.phpt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ Runtime: %s
1414
Configuration: %s
1515

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

18+
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
1719

20+

1821

1922
Time: %s
2023
%A

test/functional/notice.phpt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ Runtime: %s
1414
Configuration: %s
1515

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

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

20+

1821

1922
Time: %s
2023
%A

test/functional/risky.phpt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ Runtime: %s
1414
Configuration: %s
1515

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

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

20+

1821

1922
Time: %s
2023
%A

test/functional/slow.phpt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@ PHPUnit %s
1313
Runtime: %s
1414
Configuration: %s
1515

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

1818

1919
Time: %s
2020
%A
21-
OK, but %s!
22-
Tests: 1, Assertions: 0, Risky: 1.
21+
OK (1 test, 1 assertion)

test/functional/vslow.phpt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@ PHPUnit %s
1313
Runtime: %s
1414
Configuration: %s
1515

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

1818

1919
Time: %s
2020
%A
21-
OK, but %s!
22-
Tests: 1, Assertions: 0, Risky: 1.
21+
OK (1 test, 1 assertion)

0 commit comments

Comments
 (0)