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
We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).
73
+
We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).
74
74
75
75
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).
To make snapshot assertions, use the `Spatie\Snapshots\MatchesSnapshots` trait in your test case class. This adds five assertion methods to the class:
87
+
To make snapshot assertions, use the `Spatie\Snapshots\MatchesSnapshots` trait in your test case class. This adds a set of assertion methods to the class:
88
88
89
89
-`assertMatchesSnapshot($actual)`
90
+
-`assertMatchesFileHashSnapshot($actual)`
91
+
-`assertMatchesFileSnapshot($actual)`
92
+
-`assertMatchesHtmlSnapshot($actual)`
90
93
-`assertMatchesJsonSnapshot($actual)`
94
+
-`assertMatchesObjectSnapshot($actual)`
95
+
-`assertMatchesTextSnapshot($actual)`
91
96
-`assertMatchesXmlSnapshot($actual)`
92
-
-`assertMatchesHtmlSnapshot($actual)`
93
-
-`assertMatchesFileSnapshot($filePath)`
94
-
-`assertMatchesFileHashSnapshot($filePath)`
97
+
-`assertMatchesYamlSnapshot($actual)`
95
98
96
99
### Snapshot Testing 101
97
100
@@ -117,10 +120,10 @@ OK, but incomplete, skipped, or risky tests!
117
120
Tests: 1, Assertions: 0, Incomplete: 1.
118
121
```
119
122
120
-
Snapshot ids are generated based on the test and testcase's names. Basic snapshots return a `var_export` of the actual value.
123
+
Snapshot ids are generated based on the test and testcase's names. Basic snapshots return a plain text or YAML representation of the actual value.
121
124
122
-
```php
123
-
<?php return 'foo';
125
+
```txt
126
+
foo
124
127
```
125
128
126
129
Let's rerun the test. The test runner will see that there's already a snapshot for the assertion and do a comparison.
@@ -163,16 +166,16 @@ OK (1 test, 1 assertion)
163
166
164
167
As a result, our snapshot file returns "bar" instead of "foo".
165
168
166
-
```php
167
-
<?php return 'bar';
169
+
```txt
170
+
bar
168
171
```
169
172
170
173
### File snapshots
171
174
172
175
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:
173
176
174
-
The `assertMatchesFileHashSnapshot($filePath)` assertion asserts that the hash of the file passed into the function and the hash saved in the snapshot match. This assertion is fast and uses very little disk space. The downside of this assertion is that there is no easy way to see how the two files differ if the test fails.
175
-
177
+
The `assertMatchesFileHashSnapshot($filePath)` assertion asserts that the hash of the file passed into the function and the hash saved in the snapshot match. This assertion is fast and uses very little disk space. The downside of this assertion is that there is no easy way to see how the two files differ if the test fails.
178
+
176
179
The `assertMatchesFileSnapshot($filePath)` assertion works almost the same way as the file hash assertion, except that it actually saves the whole file in the snapshots directory. If the assertion fails, it places the failed file next to the snapshot file so they can easily be manually compared. The persisted failed file is automatically deleted when the test passes. This assertion is most useful when working with binary files that should be manually compared like images or pdfs.
177
180
178
181
### Customizing Snapshot Ids and Directories
@@ -209,18 +212,18 @@ The driver used to serialize the data can be specificied as second argument of t
209
212
`assertMatchesSnapshot` method, so you can pick one that better suits your needs:
210
213
211
214
```php
212
-
use Spatie\Snapshots\Drivers\ObjectDriver;
215
+
use Spatie\Snapshots\Drivers\JsonDriver;
213
216
use Spatie\Snapshots\MatchesSnapshots;
214
217
215
218
class OrderTest
216
219
{
217
220
use MatchesSnapshots;
218
221
219
-
public function test_snapshot_with_object_driver()
222
+
public function test_snapshot_with_json_driver()
220
223
{
221
224
$order = new Order(1);
222
225
223
-
$this->assertMatchesSnapshot($order, new ObjectDriver());
226
+
$this->assertMatchesSnapshot($order->toJson(), new JsonDriver());
0 commit comments