Skip to content

Commit

Permalink
Enhancement: Emit Test\PassedButRisky event
Browse files Browse the repository at this point in the history
Fixes #32.

Co-authored-by: Andreas Möller <[email protected]>
Co-authored-by: Arne Blankerts <[email protected]>
  • Loading branch information
localheinz and theseer committed Feb 1, 2021
1 parent bcea212 commit e97ee74
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 7 deletions.
6 changes: 6 additions & 0 deletions .psalm/baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,12 @@
<code>$this instanceof PhptTestCase</code>
</RedundantCondition>
</file>
<file src="src/Framework/TestResult.php">
<MissingThrowsDocblock occurrences="2">
<code>getName</code>
<code>getName</code>
</MissingThrowsDocblock>
</file>
<file src="src/Framework/TestRunner.php">
<MissingThrowsDocblock occurrences="14">
<code>CodeCoverage::instance()</code>
Expand Down
8 changes: 6 additions & 2 deletions src/Event/DispatchingEmitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,13 @@ public function testPassedWithWarning(Code\Test $test, string $message): void
));
}

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

public function testSkippedByDataProvider(Code\ClassMethod $testMethod, 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 @@ -51,7 +51,7 @@ public function testPassed(Code\Test $test): void;

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

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

public function testSkippedByDataProvider(Code\ClassMethod $testMethod, string $message): void;

Expand Down
19 changes: 18 additions & 1 deletion src/Event/Test/PassedButRisky.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 PassedButRisky 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;
}
}
10 changes: 10 additions & 0 deletions src/Framework/TestResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use function get_class;
use Countable;
use Error;
use PHPUnit\Event;
use PHPUnit\Util\Printer;
use Throwable;

Expand Down Expand Up @@ -189,6 +190,15 @@ public function addFailure(Test $test, AssertionFailedError $e, float $time): vo

if ($test instanceof TestCase) {
$test->markAsRisky();

Event\Facade::emitter()->testPassedButRisky(
new Event\Code\Test(
get_class($test),
$test->getName(false),
$test->getName(true),
),
$e->getMessage()
);
}

if ($this->stopOnRisky || $this->stopOnDefect) {
Expand Down
2 changes: 1 addition & 1 deletion tests/_files/NullEmitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function testPassedWithWarning(Code\Test $test, string $message): void
{
}

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

Expand Down
17 changes: 16 additions & 1 deletion tests/unit/Event/DispatchingEmitterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,13 @@ public function notify(Test\PassedWithWarning $event): void

public function testTestPassedButRiskyDispatchesTestPassedButRiskyEvent(): void
{
$test = new Code\Test(
self::class,
'foo',
'foo with data set #123'
);
$message = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.';

$subscriber = new class extends RecordingSubscriber implements Test\PassedButRiskySubscriber {
public function notify(Test\PassedButRisky $event): void
{
Expand All @@ -569,10 +576,18 @@ public function notify(Test\PassedButRisky $event): void
$telemetrySystem
);

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

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

$event = $subscriber->lastRecordedEvent();

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

public function testTestSkippedByDataProviderDispatchesTestSkippedByDataProviderEvent(): void
Expand Down
15 changes: 14 additions & 1 deletion tests/unit/Event/Test/PassedButRiskyTest.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\PassedButRisky
Expand All @@ -19,9 +20,21 @@ final class PassedButRiskyTest extends AbstractEventTestCase
public function testConstructorSetsValues(): void
{
$telemetryInfo = self::createTelemetryInfo();
$test = new Code\Test(
self::class,
'foo',
'foo with data set #123'
);
$message = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.';

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

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

0 comments on commit e97ee74

Please sign in to comment.