You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+46-8Lines changed: 46 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -169,20 +169,48 @@ FAILURES!
169
169
Tests: 1, Assertions: 1, Failures: 1.
170
170
```
171
171
172
-
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`.
172
+
When we expect a changed value, we need to tell the test runner to update the existing snapshots instead of failing the test. The package ships a wrapper binary that runs PHPUnit with the right environment variable set:
173
173
174
174
```
175
-
> ./vendor/bin/phpunit -d --update-snapshots
175
+
> ./vendor/bin/update-snapshots
176
176
177
177
OK (1 test, 1 assertion)
178
178
```
179
179
180
+
`vendor/bin/update-snapshots` accepts the same arguments as `phpunit`, so you can target a specific test:
As a result, our snapshot file returns "bar" instead of "foo".
181
187
182
188
```txt
183
189
bar
184
190
```
185
191
192
+
You can also set the `UPDATE_SNAPSHOTS` environment variable directly. This is useful in CI, or when you want to define a Composer script for your project.
193
+
194
+
```
195
+
> UPDATE_SNAPSHOTS=true ./vendor/bin/phpunit
196
+
```
197
+
198
+
A common pattern is to add an `update-snapshots` script to your project's `composer.json`:
You can then run `composer update-snapshots` (and pass extra arguments via `composer update-snapshots -- tests/OrderTest.php`).
209
+
210
+
#### Why not `phpunit -d --update-snapshots`?
211
+
212
+
Earlier versions of this package documented `phpunit -d --update-snapshots` as the way to enable updates. That syntax abuses PHPUnit's `-d` flag, which is designed to set `php.ini` values. Since [PHPUnit 12.5.12](https://github.com/sebastianbergmann/phpunit/commit/87f68b992979f9e4ad485be959b5e9d0c21597f2), failed `ini_set()` calls produce a visible `Failed to set "--update-snapshots=1"` warning, surfacing the misuse. The wrapper binary and the environment variable replace that approach. The legacy CLI argument still works for backwards compatibility, but produces the warning on PHPUnit 12.5.12 and later.
213
+
186
214
### File snapshots
187
215
188
216
The `MatchesSnapshots` trait offers two ways to assert that a file is identical to the snapshot that was made the first time the test was run:
@@ -311,29 +339,39 @@ $this->assertMatchesSnapshot($something->toYaml(), new MyYamlDriver());
311
339
312
340
### Usage in CI
313
341
314
-
When running your tests in Continuous Integration you would possibly want to disable the creation of snapshots.
342
+
When running your tests in Continuous Integration you would possibly want to disable the creation of snapshots, so missing snapshots cause the build to fail instead of being silently created.
315
343
316
-
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.
344
+
Set the `CREATE_SNAPSHOTS=false` environment variable when invoking PHPUnit:
> Earlier versions of this package documented `phpunit -d --without-creating-snapshots`. Since [PHPUnit 12.5.12](https://github.com/sebastianbergmann/phpunit/commit/87f68b992979f9e4ad485be959b5e9d0c21597f2), that syntax produces a `Failed to set "--without-creating-snapshots=1"` test runner warning because it abuses PHPUnit's `-d` flag (which is designed for `php.ini` values). The `CREATE_SNAPSHOTS=false` environment variable replaces that approach.
363
+
326
364
### Usage with parallel testing
327
365
328
-
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.
366
+
If you want to run your test in parallel with a tool like [Paratest](https://github.com/paratestphp/paratest), or with the `php artisan test --parallel` command of Laravel, you will have to use the environment variables.
329
367
330
368
331
369
```bash
332
370
> CREATE_SNAPSHOTS=false php artisan test --parallel
333
371
334
372
1) ExampleTest::test_it_matches_a_string
335
373
Snapshot "ExampleTest__test_it_matches_a_string__1.txt" does not exist.
336
-
You can automatically create it by removing the `CREATE_SNAPSHOTS=false` env var, or `-d --no-create-snapshots` of PHPUnit's CLI arguments.
374
+
You can enable snapshot creation by running `vendor/bin/phpunit` directly, or by unsetting the `CREATE_SNAPSHOTS=false` environment variable.
0 commit comments