Skip to content

Commit 0d7fb2d

Browse files
committed
Allow using env var to enable snapshot update
1 parent 31afcd3 commit 0d7fb2d

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

src/MatchesSnapshots.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,12 @@ protected function getFileSnapshotDirectory(): string
130130
* matched.
131131
*
132132
* Override this method it you want to use a different flag or mechanism
133-
* than `-d --update-snapshots`.
133+
* than `-d --update-snapshots` or `UPDATE_SNAPSHOTS=true` env var.
134134
*/
135135
protected function shouldUpdateSnapshots(): bool
136136
{
137-
return in_array('--update-snapshots', $_SERVER['argv'], true);
137+
return in_array('--update-snapshots', $_SERVER['argv'], true)
138+
|| getenv('UPDATE_SNAPSHOTS') === 'true';
138139
}
139140

140141
/*

tests/Integration/MatchesSnapshotTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,23 @@ public function it_can_update_a_file_snapshot_with_a_different_extension()
400400
$this->assertFileDoesNotExist($oldSnapshot);
401401
}
402402

403+
/** @test */
404+
public function it_can_update_a_snapshot_with_env_var()
405+
{
406+
putenv('UPDATE_SNAPSHOTS=true');
407+
408+
$mockTrait = $this->getMatchesSnapshotMock();
409+
410+
$this->expectIncompleteMatchesSnapshotTest($mockTrait, function ($mockTrait) {
411+
$mockTrait->assertMatchesSnapshot('Foo');
412+
});
413+
414+
$this->assertSnapshotMatchesExample(
415+
'MatchesSnapshotTest__it_can_update_a_snapshot_with_env_var__1.txt',
416+
'string_snapshot.txt'
417+
);
418+
}
419+
403420
private function expectIncompleteMatchesSnapshotTest(MockObject $matchesSnapshotMock, callable $assertions)
404421
{
405422
$matchesSnapshotMock
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Bar

0 commit comments

Comments
 (0)