Skip to content

Commit

Permalink
Enhancement: Emit Test\Errored event
Browse files Browse the repository at this point in the history
Fixes theseer#28.

Co-authored-by: Andreas Möller <am@localheinz.com>
Co-authored-by: Arne Blankerts <Arne@Blankerts.de>
  • Loading branch information
localheinz and theseer committed Dec 27, 2020
1 parent 6c949a8 commit ee22609
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 8 deletions.
8 changes: 6 additions & 2 deletions src/Event/DispatchingEmitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,13 @@ public function testRunConfigured(): void
$this->dispatcher->dispatch(new Test\RunConfigured($this->telemetryInfo()));
}

public function testErrored(): void
public function testErrored(Code\Test $test, string $message): void
{
$this->dispatcher->dispatch(new Test\Errored($this->telemetryInfo()));
$this->dispatcher->dispatch(new Test\Errored(
$this->telemetryInfo(),
$test,
$message
));
}

public function testFailed(Code\Test $test, string $message): void
Expand Down
2 changes: 1 addition & 1 deletion src/Event/Emitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function globalStateRestored(Snapshot $snapshot): void;

public function testRunConfigured(): void;

public function testErrored(): void;
public function testErrored(Code\Test $test, string $message): void;

public function testFailed(Code\Test $test, string $message): void;

Expand Down
19 changes: 18 additions & 1 deletion src/Event/Test/Errored.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,37 @@
*/
namespace PHPUnit\Event\Test;

use PHPUnit\Event\Code;
use PHPUnit\Event\Event;
use PHPUnit\Event\Telemetry;

final class Errored implements Event
{
private Telemetry\Info $telemetryInfo;

public function __construct(Telemetry\Info $telemetryInfo)
private Code\Test $test;

private string $message;

public function __construct(Telemetry\Info $telemetryInfo, Code\Test $test, string $message)
{
$this->telemetryInfo = $telemetryInfo;
$this->test = $test;
$this->message = $message;
}

public function telemetryInfo(): Telemetry\Info
{
return $this->telemetryInfo;
}

public function test(): Code\Test
{
return $this->test;
}

public function message(): string
{
return $this->message;
}
}
8 changes: 8 additions & 0 deletions src/Framework/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -996,6 +996,14 @@ public function runBare(): void
} catch (Throwable $_e) {
$e = $_e;
$this->status = TestStatus::error($_e->getMessage());

$emitter->testErrored(
new Event\Code\Test(
static::class,
$this->name
),
$_e->getMessage()
);
}

$this->mockObjects = [];
Expand Down
2 changes: 1 addition & 1 deletion tests/_files/NullEmitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function testRunConfigured(): void
{
}

public function testErrored(): void
public function testErrored(Code\Test $test, string $message): void
{
}

Expand Down
19 changes: 17 additions & 2 deletions tests/unit/Event/DispatchingEmitterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,12 @@ public function notify(Test\RunConfigured $event): void

public function testTestErroredDispatchesTestErroredEvent(): void
{
$test = new Code\Test(...array_values(explode(
'::',
__METHOD__
)));
$message = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.';

$subscriber = new class extends RecordingSubscriber implements Test\ErroredSubscriber {
public function notify(Test\Errored $event): void
{
Expand All @@ -386,10 +392,19 @@ public function notify(Test\Errored $event): void
$telemetrySystem
);

$emitter->testErrored();
$emitter->testErrored(
$test,
$message
);

$this->assertSame(1, $subscriber->recordedEventCount());
$this->assertInstanceOf(Test\Errored::class, $subscriber->lastRecordedEvent());

$event = $subscriber->lastRecordedEvent();

$this->assertInstanceOf(Test\Errored::class, $event);

$this->assertSame($test, $event->test());
$this->assertSame($message, $event->message());
}

public function testTestFailedDispatchesTestFailedEvent(): void
Expand Down
14 changes: 13 additions & 1 deletion tests/unit/Event/Test/ErroredTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace PHPUnit\Event\Test;

use PHPUnit\Event\AbstractEventTestCase;
use PHPUnit\Event\Code;

/**
* @covers \PHPUnit\Event\Test\Errored
Expand All @@ -19,9 +20,20 @@ final class ErroredTest extends AbstractEventTestCase
public function testConstructorSetsValues(): void
{
$telemetryInfo = self::createTelemetryInfo();
$test = new Code\Test(...array_values(explode(
'::',
__METHOD__
)));
$message = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.';

$event = new Errored($telemetryInfo);
$event = new Errored(
$telemetryInfo,
$test,
$message
);

$this->assertSame($telemetryInfo, $event->telemetryInfo());
$this->assertSame($test, $event->test());
$this->assertSame($message, $event->message());
}
}

0 comments on commit ee22609

Please sign in to comment.