Skip to content

Commit

Permalink
Enhancement: Compose data into Test\SkippedWithMessage event
Browse files Browse the repository at this point in the history
Related to #36.

Co-authored-by: Andreas Möller <am@localheinz.com>
Co-authored-by: Arne Blankerts <Arne@Blankerts.de>
  • Loading branch information
2 people authored and sebastianbergmann committed Nov 20, 2020
1 parent e2914b7 commit 547bf04
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 11 deletions.
8 changes: 6 additions & 2 deletions src/Event/DispatchingEmitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,13 @@ public function testSkippedDueToUnsatisfiedRequirements(Code\ClassMethod $testMe
));
}

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

public function testPrepared(Code\Test $test): 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 @@ -57,7 +57,7 @@ public function testAbortedWithMessage(Code\Test $test, string $message): void;

public function testSkippedDueToUnsatisfiedRequirements(Code\ClassMethod $testMethod, string ...$missingRequirements): void;

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

public function testPrepared(Code\Test $test): void;

Expand Down
25 changes: 24 additions & 1 deletion src/Event/Test/SkippedWithMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/
namespace PHPUnit\Event\Test;

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

Expand All @@ -19,13 +20,35 @@ final class SkippedWithMessage implements Event
*/
private $telemetryInfo;

public function __construct(Telemetry\Info $telemetryInfo)
/**
* @var Code\Test
*/
private $testMethod;

/**
* @var string
*/
private $message;

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

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

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

public function message(): string
{
return $this->message;
}
}
8 changes: 7 additions & 1 deletion src/Framework/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -1231,7 +1231,13 @@ public function runBare(): void
$this->status = BaseTestRunner::STATUS_SKIPPED;
$this->statusMessage = $e->getMessage();

Event\Registry::emitter()->testSkippedWithMessage();
Event\Registry::emitter()->testSkippedWithMessage(
new Event\Code\Test(
static::class,
$this->name
),
$e->getMessage()
);
} catch (Warning $e) {
$this->status = BaseTestRunner::STATUS_WARNING;
$this->statusMessage = $e->getMessage();
Expand Down
2 changes: 1 addition & 1 deletion tests/_files/NullEmitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function testSkippedDueToUnsatisfiedRequirements(Code\ClassMethod $testMe
{
}

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

Expand Down
21 changes: 18 additions & 3 deletions tests/unit/Event/DispatchingEmitterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -637,8 +637,14 @@ public function notify(Test\SkippedDueToUnsatisfiedRequirements $event): void
self::assertSame($missingRequirements, $event->missingRequirements());
}

public function testTestRunSkippedWithWarningDispatchesTestRunSkippedWithWarningEvent(): void
public function testTestSkippedMessageDispatchesTestSkippedWithMessageEvent(): 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\SkippedWithMessageSubscriber {
public function notify(Test\SkippedWithMessage $event): void
{
Expand All @@ -659,10 +665,19 @@ public function notify(Test\SkippedWithMessage $event): void
$telemetrySystem
);

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

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

$event = $subscriber->lastRecordedEvent();

self::assertInstanceOf(Test\SkippedWithMessage::class, $event);

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

public function testTestPreparedDispatchesTestPreparedEvent(): void
Expand Down
16 changes: 14 additions & 2 deletions tests/unit/Event/Test/SkippedWithMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,30 @@
namespace PHPUnit\Event\Test;

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

/**
* @covers \PHPUnit\Event\Test\SkippedWithMessage
* @covers \PHPUnit\Event\Test\SkippedByDataProvider
*/
final class SkippedWithMessageTest 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 SkippedWithMessage($telemetryInfo);
$event = new SkippedWithMessage(
$telemetryInfo,
$test,
$message
);

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

0 comments on commit 547bf04

Please sign in to comment.