Skip to content

Commit 7957748

Browse files
Allow updates
1 parent 7b4d2db commit 7957748

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

src/MatchesSnapshots.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Spatie\Snapshots;
44

5+
use PHPUnit_Framework_ExpectationFailedException;
6+
57
trait MatchesSnapshots
68
{
79
public function assertMatchesSnapshot($actual, $type = 'var', $methodTrace = null)
@@ -17,6 +19,26 @@ public function assertMatchesSnapshot($actual, $type = 'var', $methodTrace = nul
1719
return $this->markTestIncomplete("Snapshot created for {$snapshot->id()}");
1820
}
1921

22+
if (getenv('update_snapshots') == 1) {
23+
return $this->updateSnapshot($type, $snapshot, $actual);
24+
}
25+
26+
$this->doSnapShotAssertion($type, $snapshot, $actual);
27+
}
28+
29+
protected function updateSnapshot($type, Snapshot $snapshot, $actual)
30+
{
31+
try {
32+
$this->doSnapShotAssertion($type, $snapshot, $actual);
33+
} catch (PHPUnit_Framework_ExpectationFailedException $exception) {
34+
$snapshot->update($actual);
35+
36+
$this->markTestIncomplete("Snapshot updated for {$snapshot->id()}");
37+
}
38+
}
39+
40+
protected function doSnapShotAssertion($type, Snapshot $snapshot, $actual)
41+
{
2042
if ($type === 'xml') {
2143
return $this->assertXmlStringEqualsXmlString($snapshot->get(), $actual);
2244
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"foo": "foo",
3+
"bar": "bar",
4+
"baz": "baz"
5+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0"?>
2+
<foo>
3+
<bar>Baz</bar>
4+
</foo>

0 commit comments

Comments
 (0)