Skip to content

Commit 831f134

Browse files
authored
Merge pull request #126 from mathieutu/master
Allow using env var to manage snapshot creation and update
2 parents b6c9aac + c3ca9c7 commit 831f134

File tree

4 files changed

+78
-9
lines changed

4 files changed

+78
-9
lines changed

README.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ FAILURES!
156156
Tests: 1, Assertions: 1, Failures: 1.
157157
```
158158

159-
When we expect a changed value, we need to tell the test runner to update the existing snapshots instead of failing the test. This is possible by adding a`-d --update-snapshots` flag to the `phpunit` command.
159+
When we expect a changed value, we need to tell the test runner to update the existing snapshots instead of failing the test. This is possible by adding a`-d --update-snapshots` flag to the `phpunit` command, or setting the `UPDATE_SNAPSHOTS` env var to `true`.
160160

161161
```
162162
> ./vendor/bin/phpunit -d --update-snapshots
@@ -278,14 +278,27 @@ $this->assertMatchesSnapshot($something->toYaml(), new MyYamlDriver());
278278

279279
When running your tests in Continuous Integration you would possibly want to disable the creation of snapshots.
280280

281-
By using the `--without-creating-snapshots` parameter, PHPUnit will fail if the snapshots don't exist.
281+
By using the `--without-creating-snapshots` parameter or by setting the `CREATE_SNAPSHOTS` env var to `false`, PHPUnit will fail if the snapshots don't exist.
282282

283283
```bash
284284
> ./vendor/bin/phpunit -d --without-creating-snapshots
285285

286286
1) ExampleTest::test_it_matches_a_string
287287
Snapshot "ExampleTest__test_it_matches_a_string__1.txt" does not exist.
288-
You can automatically create it by removing `-d --no-create-snapshots` of PHPUnit's CLI arguments.
288+
You can automatically create it by removing the `CREATE_SNAPSHOT=false` env var, or `-d --no-create-snapshots` of PHPUnit's CLI arguments.
289+
```
290+
291+
### Usage with parallel testing
292+
293+
If you want to run your test in parallel with a tool like [Paratest](https://github.com/paratestphp/paratest), ou with the `php artisan test --parallel` command of Laravel, you will have to use the environment variables.
294+
295+
296+
```bash
297+
> CREATE_SNAPSHOTS=false php artisan test --parallel
298+
299+
1) ExampleTest::test_it_matches_a_string
300+
Snapshot "ExampleTest__test_it_matches_a_string__1.txt" does not exist.
301+
You can automatically create it by removing the `CREATE_SNAPSHOT=false` env var, or `-d --no-create-snapshots` of PHPUnit's CLI arguments.
289302
```
290303

291304
### A note for Windows users

src/MatchesSnapshots.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,23 +130,28 @@ 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+
if(in_array('--update-snapshots', $_SERVER['argv'], true)) {
138+
return true;
139+
}
140+
141+
return getenv('UPDATE_SNAPSHOTS') === 'true';
138142
}
139143

140144
/*
141145
* Determines whether or not the snapshot should be created instead of
142146
* matched.
143147
*
144148
* Override this method if you want to use a different flag or mechanism
145-
* than `-d --without-creating-snapshots`.
149+
* than `-d --without-creating-snapshots` or `CREATE_SNAPSHOTS=false` env var.
146150
*/
147151
protected function shouldCreateSnapshots(): bool
148152
{
149-
return ! in_array('--without-creating-snapshots', $_SERVER['argv'], true);
153+
return ! in_array('--without-creating-snapshots', $_SERVER['argv'], true)
154+
&& getenv('CREATE_SNAPSHOTS') !== 'false';
150155
}
151156

152157
protected function doSnapshotAssertion($actual, Driver $driver)
@@ -299,6 +304,7 @@ protected function assertSnapshotShouldBeCreated(string $snapshotFileName): void
299304
$this->fail(
300305
"Snapshot \"$snapshotFileName\" does not exist.\n".
301306
'You can automatically create it by removing '.
307+
'the `CREATE_SNAPSHOT=false` env var, or '.
302308
'`-d --without-creating-snapshots` of PHPUnit\'s CLI arguments.'
303309
);
304310
}

tests/Integration/MatchesSnapshotTest.php

Lines changed: 51 additions & 2 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
@@ -479,7 +496,7 @@ public function it_doesnt_create_a_regular_snapshot_and_mismatches_if_asked()
479496
$this->expectFail(
480497
$mockTrait,
481498
"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.'
499+
"You can automatically create it by removing the `CREATE_SNAPSHOT=false` env var, or `-d --without-creating-snapshots` of PHPUnit's CLI arguments."
483500
);
484501

485502
$mockTrait->assertMatchesSnapshot('Bar');
@@ -495,7 +512,39 @@ public function it_doesnt_create_a_file_snapshot_and_mismatches_if_asked()
495512
$this->expectFail(
496513
$mockTrait,
497514
"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.'
515+
"You can automatically create it by removing the `CREATE_SNAPSHOT=false` env var, or `-d --without-creating-snapshots` of PHPUnit's CLI arguments."
516+
);
517+
518+
$mockTrait->assertMatchesFileSnapshot(__DIR__.'/stubs/test_files/friendly_man.jpg');
519+
}
520+
521+
/** @test */
522+
public function it_doesnt_create_a_regular_snapshot_and_mismatches_if_asked_with_env_var()
523+
{
524+
putenv('CREATE_SNAPSHOTS=false');
525+
526+
$mockTrait = $this->getMatchesSnapshotMock();
527+
528+
$this->expectFail(
529+
$mockTrait,
530+
"Snapshot \"MatchesSnapshotTest__it_doesnt_create_a_regular_snapshot_and_mismatches_if_asked_with_env_var__1.txt\" does not exist.\n".
531+
"You can automatically create it by removing the `CREATE_SNAPSHOT=false` env var, or `-d --without-creating-snapshots` of PHPUnit's CLI arguments."
532+
);
533+
534+
$mockTrait->assertMatchesSnapshot('Bar');
535+
}
536+
537+
/** @test */
538+
public function it_doesnt_create_a_file_snapshot_and_mismatches_if_asked_with_env_var()
539+
{
540+
putenv('CREATE_SNAPSHOT=false');
541+
542+
$mockTrait = $this->getMatchesSnapshotMock();
543+
544+
$this->expectFail(
545+
$mockTrait,
546+
"Snapshot \"MatchesSnapshotTest__it_doesnt_create_a_file_snapshot_and_mismatches_if_asked_with_env_var__1.jpg_failed.jpg\" does not exist.\n".
547+
"You can automatically create it by removing the `CREATE_SNAPSHOT=false` env var, or `-d --without-creating-snapshots` of PHPUnit's CLI arguments."
499548
);
500549

501550
$mockTrait->assertMatchesFileSnapshot(__DIR__.'/stubs/test_files/friendly_man.jpg');
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Bar

0 commit comments

Comments
 (0)