Skip to content

Commit 8c6e03d

Browse files
committed
Add the ability to disable snapshots creation
1 parent ef86c99 commit 8c6e03d

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

src/MatchesSnapshots.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,18 @@ protected function shouldUpdateSnapshots(): bool
152152
return in_array('--update-snapshots', $_SERVER['argv'], true);
153153
}
154154

155+
/*
156+
* Determines whether or not the snapshot should be created instead of
157+
* matched.
158+
*
159+
* Override this method it you want to use a different flag or mechanism
160+
* than `-d --no-create-snapshots`.
161+
*/
162+
protected function shouldCreateSnapshots(): bool
163+
{
164+
return !in_array('--no-create-snapshots', $_SERVER['argv'], true);
165+
}
166+
155167
protected function doSnapshotAssertion($actual, Driver $driver)
156168
{
157169
$this->snapshotIncrementor++;
@@ -163,6 +175,8 @@ protected function doSnapshotAssertion($actual, Driver $driver)
163175
);
164176

165177
if (! $snapshot->exists()) {
178+
$this->assertSnapshotShouldBeCreated($snapshot->filename());
179+
166180
$this->createSnapshotAndMarkTestIncomplete($snapshot, $actual);
167181
}
168182

@@ -231,6 +245,8 @@ protected function doFileSnapshotAssertion(string $filePath): void
231245
}
232246

233247
if (! $fileSystem->has($snapshotId)) {
248+
$this->assertSnapshotShouldBeCreated($failedSnapshotId);
249+
234250
$fileSystem->copy($filePath, $snapshotId);
235251

236252
$this->registerSnapshotChange("File snapshot created for {$snapshotId}");
@@ -288,4 +304,17 @@ protected function registerSnapshotChange(string $message): void
288304
{
289305
$this->snapshotChanges[] = $message;
290306
}
307+
308+
protected function assertSnapshotShouldBeCreated(string $snapshotFileName): void
309+
{
310+
if ($this->shouldCreateSnapshots()) {
311+
return;
312+
}
313+
314+
$this->fail(
315+
"Snapshot \"$snapshotFileName\" does not exist.\n".
316+
'You can automatically create it by removing '.
317+
'`-d --no-create-snapshots` of PHPUnit\'s CLI arguments.'
318+
);
319+
}
291320
}

tests/Integration/MatchesSnapshotTest.php

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

33
namespace Spatie\Snapshots\Test\Integration;
44

5+
use PHPUnit\Framework\AssertionFailedError;
56
use PHPUnit\Framework\ExpectationFailedException;
67
use PHPUnit\Framework\MockObject\MockObject;
78
use PHPUnit\Framework\TestCase;
@@ -18,10 +19,15 @@ public function setUp(): void
1819
$this->setUpComparesSnapshotFiles();
1920

2021
$updateArgument = array_search('--update-snapshots', $_SERVER['argv']);
22+
$noCreateArgument = array_search('--no-create-snapshots', $_SERVER['argv']);
2123

2224
if ($updateArgument) {
2325
unset($_SERVER['argv'][$updateArgument]);
2426
}
27+
28+
if ($noCreateArgument) {
29+
unset($_SERVER['argv'][$noCreateArgument]);
30+
}
2531
}
2632

2733
/** @test */
@@ -459,4 +465,37 @@ private function getMatchesSnapshotMock(): MockObject
459465

460466
return $matchesSnapshotMock;
461467
}
468+
469+
470+
/** @test */
471+
public function it_doesnt_create_a_regular_snapshot_and_mismatches_if_asked()
472+
{
473+
$_SERVER['argv'][] = '--no-create-snapshots';
474+
475+
$mockTrait = $this->getMatchesSnapshotMock();
476+
477+
$this->expectFail(
478+
$mockTrait,
479+
"Snapshot \"MatchesSnapshotTest__it_doesnt_create_a_regular_snapshot_and_mismatches_if_asked__1.txt\" does not exist.\n" .
480+
'You can automatically create it by removing `-d --no-create-snapshots` of PHPUnit\'s CLI arguments.'
481+
);
482+
483+
$mockTrait->assertMatchesSnapshot('Bar');
484+
}
485+
486+
/** @test */
487+
public function it_doesnt_create_a_file_snapshot_and_mismatches_if_asked()
488+
{
489+
$_SERVER['argv'][] = '--no-create-snapshots';
490+
491+
$mockTrait = $this->getMatchesSnapshotMock();
492+
493+
$this->expectFail(
494+
$mockTrait,
495+
"Snapshot \"MatchesSnapshotTest__it_doesnt_create_a_file_snapshot_and_mismatches_if_asked__1.jpg_failed.jpg\" does not exist.\n" .
496+
'You can automatically create it by removing `-d --no-create-snapshots` of PHPUnit\'s CLI arguments.'
497+
);
498+
499+
$mockTrait->assertMatchesFileSnapshot(__DIR__.'/stubs/test_files/friendly_man.jpg');
500+
}
462501
}

0 commit comments

Comments
 (0)