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.

Co-authored-by: Andreas Möller <am@localheinz.com>
Co-authored-by: Arne Blankerts <Arne@Blankerts.de>
  • Loading branch information
localheinz and theseer committed Jan 28, 2021
1 parent 52b4d31 commit f519947
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 13 deletions.
9 changes: 7 additions & 2 deletions src/Event/DispatchingEmitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,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
35 changes: 32 additions & 3 deletions src/Event/GlobalState/Modified.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,47 @@

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

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

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

private Snapshot $snapshotAfter;

private string $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 @@ -1864,14 +1864,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
);

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

$event = $subscriber->lastRecordedEvent();

$this->assertInstanceOf(GlobalState\Modified::class, $event);

$this->assertSame($snapshotBefore, $event->snapshotBefore());
$this->assertSame($snapshotAfter, $event->snapshotAfter());
$this->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
);

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

0 comments on commit f519947

Please sign in to comment.