Skip to content

Commit

Permalink
Enhancement: Compose data into GlobalState\Modified event
Browse files Browse the repository at this point in the history
Fixes #8.
  • Loading branch information
localheinz committed Oct 9, 2020
1 parent 3b98c7d commit cb5b886
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 13 deletions.
7 changes: 7 additions & 0 deletions .psalm/baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,13 @@
<code>CapturedSubscriber</code>
</UnusedClass>
</file>
<file src="src/Event/GlobalState/Modified.php">
<PossiblyUnusedMethod occurrences="3">
<code>snapshotBefore</code>
<code>snapshotAfter</code>
<code>message</code>
</PossiblyUnusedMethod>
</file>
<file src="src/Event/GlobalState/ModifiedSubscriber.php">
<UnusedClass occurrences="1">
<code>ModifiedSubscriber</code>
Expand Down
9 changes: 7 additions & 2 deletions src/Event/DispatchingEmitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,14 @@ public function globalStateCaptured(Snapshot $snapshot): void
));
}

public function globalStateModified(): void
public function globalStateModified(Snapshot $snapshotBefore, Snapshot $snapshotAfter, string $message): void
{
$this->dispatcher->dispatch(new GlobalState\Modified($this->telemetryInfo()));
$this->dispatcher->dispatch(new GlobalState\Modified(
$this->telemetryInfo(),
$snapshotBefore,
$snapshotAfter,
$message
));
}

public function globalStateRestored(): 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 @@ -34,7 +34,7 @@ public function extensionLoaded(string $name, string $version): void;

public function globalStateCaptured(Snapshot $snapshot): void;

public function globalStateModified(): void;
public function globalStateModified(Snapshot $snapshotBefore, Snapshot $snapshotAfter, string $message): void;

public function globalStateRestored(): void;

Expand Down
44 changes: 41 additions & 3 deletions src/Event/GlobalState/Modified.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use PHPUnit\Event\Event;
use PHPUnit\Event\Telemetry;
use SebastianBergmann\GlobalState\Snapshot;

final class Modified implements Event
{
Expand All @@ -19,13 +20,50 @@ final class Modified implements Event
*/
private $telemetryInfo;

public function __construct(Telemetry\Info $telemetryInfo)
{
$this->telemetryInfo = $telemetryInfo;
/**
* @var Snapshot
*/
private $snapshotBefore;

/**
* @var Snapshot
*/
private $snapshotAfter;

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

public function __construct(
Telemetry\Info $telemetryInfo,
Snapshot $snapshotBefore,
Snapshot $snapshotAfter,
string $message
) {
$this->telemetryInfo = $telemetryInfo;
$this->snapshotBefore = $snapshotBefore;
$this->snapshotAfter = $snapshotAfter;
$this->message = $message;
}

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

public function snapshotBefore(): Snapshot
{
return $this->snapshotBefore;
}

public function snapshotAfter(): Snapshot
{
return $this->snapshotAfter;
}

public function message(): string
{
return $this->message;
}
}
11 changes: 9 additions & 2 deletions src/Framework/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2271,14 +2271,21 @@ private function restoreGlobalState(): void
}

if ($this->beStrictAboutChangesToGlobalState) {
$snapshotAfter = $this->createGlobalStateSnapshot($this->backupGlobals === true);

try {
$this->compareGlobalStateSnapshots(
$this->snapshot,
$this->createGlobalStateSnapshot($this->backupGlobals === true)
$snapshotAfter
);
} catch (RiskyTestError $rte) {
// Intentionally left empty
Event\Registry::emitter()->globalStateModified();

Event\Registry::emitter()->globalStateModified(
$this->snapshot,
$snapshotAfter,
$rte->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 @@ -40,7 +40,7 @@ public function globalStateCaptured(Snapshot $snapshot): void
{
}

public function globalStateModified(): void
public function globalStateModified(Snapshot $snapshotBefore, Snapshot $snapshotAfter, 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 @@ -258,6 +258,10 @@ public function notify(GlobalState\Captured $event): void

public function testGlobalStateModifiedDispatchesGlobalStateModifiedEvent(): void
{
$snapshotBefore = new Snapshot();
$snapshotAfter = new Snapshot();
$message = 'Hmm, who would have thought?';

$subscriber = new class extends RecordingSubscriber implements GlobalState\ModifiedSubscriber {
public function notify(GlobalState\Modified $event): void
{
Expand All @@ -278,10 +282,21 @@ public function notify(GlobalState\Modified $event): void
$telemetrySystem
);

$emitter->globalStateModified();
$emitter->globalStateModified(
$snapshotBefore,
$snapshotAfter,
$message
);

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

$event = $subscriber->lastRecordedEvent();

self::assertInstanceOf(GlobalState\Modified::class, $event);

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

public function testGlobalStateRestoredDispatchesGlobalStateRestoredEvent(): void
Expand Down
16 changes: 14 additions & 2 deletions tests/unit/Event/GlobalState/ModifiedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace PHPUnit\Event\GlobalState;

use PHPUnit\Event\AbstractEventTestCase;
use SebastianBergmann\GlobalState\Snapshot;

/**
* @covers \PHPUnit\Event\GlobalState\Modified
Expand All @@ -18,10 +19,21 @@ final class ModifiedTest extends AbstractEventTestCase
{
public function testConstructorSetsValues(): void
{
$telemetryInfo = self::createTelemetryInfo();
$telemetryInfo = self::createTelemetryInfo();
$snapshotBefore = new Snapshot();
$snapshotAfter = new Snapshot();
$message = 'Hmm, who would have thought?';

$event = new Modified($telemetryInfo);
$event = new Modified(
$telemetryInfo,
$snapshotBefore,
$snapshotAfter,
$message
);

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

0 comments on commit cb5b886

Please sign in to comment.