Skip to content

Commit 31afcd3

Browse files
committed
Allow using env var to disable snapshot creation
1 parent b6c9aac commit 31afcd3

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

src/MatchesSnapshots.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,12 @@ protected function shouldUpdateSnapshots(): bool
142142
* matched.
143143
*
144144
* Override this method if you want to use a different flag or mechanism
145-
* than `-d --without-creating-snapshots`.
145+
* than `-d --without-creating-snapshots` or `CREATE_SNAPSHOTS=false` env var.
146146
*/
147147
protected function shouldCreateSnapshots(): bool
148148
{
149-
return ! in_array('--without-creating-snapshots', $_SERVER['argv'], true);
149+
return ! in_array('--without-creating-snapshots', $_SERVER['argv'], true)
150+
&& getenv('CREATE_SNAPSHOTS') !== 'false';
150151
}
151152

152153
protected function doSnapshotAssertion($actual, Driver $driver)
@@ -299,6 +300,7 @@ protected function assertSnapshotShouldBeCreated(string $snapshotFileName): void
299300
$this->fail(
300301
"Snapshot \"$snapshotFileName\" does not exist.\n".
301302
'You can automatically create it by removing '.
303+
'the `CREATE_SNAPSHOT=false` env var, or '.
302304
'`-d --without-creating-snapshots` of PHPUnit\'s CLI arguments.'
303305
);
304306
}

tests/Integration/MatchesSnapshotTest.php

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ public function it_doesnt_create_a_regular_snapshot_and_mismatches_if_asked()
479479
$this->expectFail(
480480
$mockTrait,
481481
"Snapshot \"MatchesSnapshotTest__it_doesnt_create_a_regular_snapshot_and_mismatches_if_asked__1.txt\" does not exist.\n".
482-
'You can automatically create it by removing `-d --without-creating-snapshots` of PHPUnit\'s CLI arguments.'
482+
"You can automatically create it by removing the `CREATE_SNAPSHOT=false` env var, or `-d --without-creating-snapshots` of PHPUnit's CLI arguments."
483483
);
484484

485485
$mockTrait->assertMatchesSnapshot('Bar');
@@ -495,7 +495,39 @@ public function it_doesnt_create_a_file_snapshot_and_mismatches_if_asked()
495495
$this->expectFail(
496496
$mockTrait,
497497
"Snapshot \"MatchesSnapshotTest__it_doesnt_create_a_file_snapshot_and_mismatches_if_asked__1.jpg_failed.jpg\" does not exist.\n".
498-
'You can automatically create it by removing `-d --without-creating-snapshots` of PHPUnit\'s CLI arguments.'
498+
"You can automatically create it by removing the `CREATE_SNAPSHOT=false` env var, or `-d --without-creating-snapshots` of PHPUnit's CLI arguments."
499+
);
500+
501+
$mockTrait->assertMatchesFileSnapshot(__DIR__.'/stubs/test_files/friendly_man.jpg');
502+
}
503+
504+
/** @test */
505+
public function it_doesnt_create_a_regular_snapshot_and_mismatches_if_asked_with_env_var()
506+
{
507+
putenv('CREATE_SNAPSHOTS=false');
508+
509+
$mockTrait = $this->getMatchesSnapshotMock();
510+
511+
$this->expectFail(
512+
$mockTrait,
513+
"Snapshot \"MatchesSnapshotTest__it_doesnt_create_a_regular_snapshot_and_mismatches_if_asked_with_env_var__1.txt\" does not exist.\n".
514+
"You can automatically create it by removing the `CREATE_SNAPSHOT=false` env var, or `-d --without-creating-snapshots` of PHPUnit's CLI arguments."
515+
);
516+
517+
$mockTrait->assertMatchesSnapshot('Bar');
518+
}
519+
520+
/** @test */
521+
public function it_doesnt_create_a_file_snapshot_and_mismatches_if_asked_with_env_var()
522+
{
523+
putenv('CREATE_SNAPSHOT=false');
524+
525+
$mockTrait = $this->getMatchesSnapshotMock();
526+
527+
$this->expectFail(
528+
$mockTrait,
529+
"Snapshot \"MatchesSnapshotTest__it_doesnt_create_a_file_snapshot_and_mismatches_if_asked_with_env_var__1.jpg_failed.jpg\" does not exist.\n".
530+
"You can automatically create it by removing the `CREATE_SNAPSHOT=false` env var, or `-d --without-creating-snapshots` of PHPUnit's CLI arguments."
499531
);
500532

501533
$mockTrait->assertMatchesFileSnapshot(__DIR__.'/stubs/test_files/friendly_man.jpg');

0 commit comments

Comments
 (0)