Skip to content

Commit ddd2b8e

Browse files
Add update snapshot reminder for failing snapshots; refactored create and update handling
1 parent 4a85148 commit ddd2b8e

File tree

1 file changed

+38
-10
lines changed

1 file changed

+38
-10
lines changed

src/MatchesSnapshots.php

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PHPUnit\Framework\ExpectationFailedException;
66
use PHPUnit_Framework_ExpectationFailedException;
77
use ReflectionClass;
8+
use ReflectionObject;
89
use Spatie\Snapshots\Drivers\JsonDriver;
910
use Spatie\Snapshots\Drivers\VarDriver;
1011
use Spatie\Snapshots\Drivers\XmlDriver;
@@ -87,9 +88,7 @@ protected function doSnapshotAssertion($actual, Driver $driver)
8788
);
8889

8990
if (! $snapshot->exists()) {
90-
$snapshot->create($actual);
91-
92-
return $this->markTestIncomplete("Snapshot created for {$snapshot->id()}");
91+
$this->createSnapshotAndMarkTestIncomplete($snapshot, $actual);
9392
}
9493

9594
if ($this->shouldUpdateSnapshots()) {
@@ -99,16 +98,45 @@ protected function doSnapshotAssertion($actual, Driver $driver)
9998
// mark the test as incomplete.
10099
$snapshot->assertMatches($actual);
101100
} catch (ExpectationFailedException $exception) {
102-
$snapshot->create($actual);
103-
104-
return $this->markTestIncomplete("Snapshot updated for {$snapshot->id()}");
101+
$this->updateSnapshotAndMarkTestIncomplete($snapshot, $actual);
105102
} catch (PHPUnit_Framework_ExpectationFailedException $exception) {
106-
$snapshot->create($actual);
107-
108-
return $this->markTestIncomplete("Snapshot updated for {$snapshot->id()}");
103+
$this->updateSnapshotAndMarkTestIncomplete($snapshot, $actual);
109104
}
110105
}
111106

112-
$snapshot->assertMatches($actual);
107+
try {
108+
$snapshot->assertMatches($actual);
109+
} catch (ExpectationFailedException $exception) {
110+
$this->rethrowExpectationFailedExceptionWithUpdateSnapshotsPrompt($exception);
111+
} catch (PHPUnit_Framework_ExpectationFailedException $exception) {
112+
$this->rethrowExpectationFailedExceptionWithUpdateSnapshotsPrompt($exception);
113+
}
114+
}
115+
116+
protected function createSnapshotAndMarkTestIncomplete(Snapshot $snapshot, $actual)
117+
{
118+
$snapshot->create($actual);
119+
120+
$this->markTestIncomplete("Snapshot created for {$snapshot->id()}");
121+
}
122+
123+
protected function updateSnapshotAndMarkTestIncomplete(Snapshot $snapshot, $actual)
124+
{
125+
$snapshot->create($actual);
126+
127+
$this->markTestIncomplete("Snapshot updated for {$snapshot->id()}");
128+
}
129+
130+
protected function rethrowExpectationFailedExceptionWithUpdateSnapshotsPrompt($exception)
131+
{
132+
$newMessage = $exception->getMessage()."\n\n".'Snapshots can be updated by passing `-d --update-snapshots` through PHPUnit\'s CLI arguments.';
133+
134+
$exceptionReflection = new ReflectionObject($exception);
135+
136+
$messageReflection = $exceptionReflection->getProperty('message');
137+
$messageReflection->setAccessible(true);
138+
$messageReflection->setValue($exception, $newMessage);
139+
140+
throw $exception;
113141
}
114142
}

0 commit comments

Comments
 (0)