Skip to content

Commit 60e62c4

Browse files
Readme
1 parent 23586ab commit 60e62c4

File tree

2 files changed

+161
-7
lines changed

2 files changed

+161
-7
lines changed

CHANGELOG.md

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

33
All notable changes to `phpunit-snapshot-assertions` will be documented in this file
44

5-
## 1.0.0 - 201X-XX-XX
5+
## 0.4.0 - 2017-03-27
66

7-
- initial release
7+
- Initial release

README.md

Lines changed: 159 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,22 @@ class OrderSerializerTest
2222
}
2323
}
2424
```
25+
```
26+
> ./vendor/bin/phpunit
27+
28+
There was 1 incomplete test:
29+
30+
1) ExampleTest::test_it_matches_a_string
31+
Snapshot created for OrderSerializerTest__test_it_serializes_an_order_json
32+
33+
OK, but incomplete, skipped, or risky tests!
34+
Tests: 1, Assertions: 0, Incomplete: 1.
35+
```
36+
```
37+
> ./vendor/bin/phpunit
38+
39+
OK (1 test, 1 assertion)
40+
```
2541

2642
## Postcardware
2743

@@ -35,22 +51,160 @@ The best postcards are published [on our website](https://spatie.be/en/opensourc
3551

3652
You can install the package via composer:
3753

38-
``` bash
54+
```bash
3955
composer require spatie/phpunit-snapshot-assertions
4056
```
4157

4258
## Usage
4359

44-
*Todo*
60+
To make snapshot assertions, use the `Spatie\Snapshots\MatchesSnapshots` trait in your test case class. This adds three assertion methods to the class:
61+
62+
- `assertMatchesSnapshot($actual)`
63+
- `assertMatchesJsonSnapshot($actual)`
64+
- `assertMatchesXmlSnapshot($actual)`
65+
66+
### Snapshot Testing 101
67+
68+
Let's do a snapshot assertion for a simple string, "foo".
69+
70+
```php
71+
public function test_it_is_foo() {
72+
$this->assertMatchesSnapshot('foo');
73+
}
74+
```
75+
76+
The first time the assertion runs, it doesn't have a snapshot to compare the string with. The test runner generates a new snapshot and marks the test as incomplete.
77+
78+
```
79+
> ./vendor/bin/phpunit
80+
81+
There was 1 incomplete test:
4582
83+
1) ExampleTest::test_it_matches_a_string
84+
Snapshot created for ExampleTest__test_it_matches_a_string
85+
86+
OK, but incomplete, skipped, or risky tests!
87+
Tests: 1, Assertions: 0, Incomplete: 1.
88+
```
89+
90+
Snapshot ids are generated based on the test and testcase's names. Basic snapshots return a `var_export` of the actual value.
91+
92+
```php
93+
<?php return 'foo';
94+
```
95+
96+
Let's rerun the test. The test runner will see that there's already a snapshot for the assertion and do a comparison.
97+
98+
```
99+
> ./vendor/bin/phpunit
100+
101+
OK (1 test, 1 assertion)
102+
```
103+
104+
If we change actual value to "bar", the test will fail because the snapshot still returns "foo".
105+
106+
```php
107+
public function test_it_is_foo() {
108+
$this->assertMatchesSnapshot('bar');
109+
}
46110
```
47-
assertMatchesSnapshot
48-
assertMatchesJsonSnapshot
49-
assertMatchesXmlSnapshot
111+
```
112+
> ./vendor/bin/phpunit
113+
114+
1) ExampleTest::test_it_matches_a_string
115+
Failed asserting that two strings are equal.
116+
--- Expected
117+
+++ Actual
118+
@@ @@
119+
-'foo'
120+
+'bar'
121+
122+
FAILURES!
123+
Tests: 1, Assertions: 1, Failures: 1.
124+
```
125+
126+
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.
127+
128+
```
129+
> ./vendor/bin/phpunit -d --update-snapshots
130+
131+
OK (1 test, 1 assertion)
132+
```
133+
134+
As a result, our snapshot file returns "bar" instead of "foo".
135+
136+
```php
137+
<?php return 'bar';
138+
```
139+
140+
### Customizing Snapshot Ids and Directories
141+
142+
Snapshot ids are generated via the `getSnapshotId` method on the `MatchesSnapshot` trait. Override the method to customize the id. By default, a snapshot id exists of the test name, the test case name and an incrementing value, e.g. `Test__my_test_case__1`.
143+
144+
#### Example: Replacing the `__` Delimiter With `--`
145+
146+
```php
147+
protected function getSnapshotId(): string
148+
{
149+
return (new ReflectionClass($this))->getShortName().'--'.
150+
$this->getName().'--'.
151+
$this->snapshotIncrementor;
152+
}
153+
```
154+
155+
By default, snapshots are stored in a `__snapshots__` directory relative to the test class. This can be changed by overriding the `getSnapshotDirectory` method.
156+
157+
#### Example: Renaming the `__snapshots__` directory to `snapshots`
158+
159+
```php
160+
protected function getSnapshotDirectory(): string
161+
{
162+
return dirname((new ReflectionClass($this))->getFileName()).
163+
DIRECTORY_SEPARATOR.
164+
'snapshots';
165+
}
50166
```
51167

52168
### Writing Custom Drivers
53169

170+
Drivers ensure that different types of data can be serialized and matched in their own way. A driver is a class that implements the `Spatie\Snapshots\Driver` interface, which requires three method implementations: `serialize`, `extension` and `match`.
171+
172+
Let's take a quick quick look at the `JsonDriver`.
173+
174+
```php
175+
namespace Spatie\Snapshots\Drivers;
176+
177+
use PHPUnit\Framework\Assert;
178+
use Spatie\Snapshots\Driver;
179+
use Spatie\Snapshots\Exceptions\CantBeSerialized;
180+
181+
class JsonDriver implements Driver
182+
{
183+
public function serialize($data): string
184+
{
185+
if (! is_string($data)) {
186+
throw new CantBeSerialized('Only strings can be serialized to json');
187+
}
188+
189+
return json_encode(json_decode($data), JSON_PRETTY_PRINT).PHP_EOL;
190+
}
191+
192+
public function extension(): string
193+
{
194+
return 'json';
195+
}
196+
197+
public function match($expected, $actual)
198+
{
199+
Assert::assertJsonStringEqualsJsonString($actual, $expected);
200+
}
201+
}
202+
```
203+
204+
- The `serialize` method returns a string which will be written to the snapshot file. In the `JsonDriver`, we'll decode and re-encode the json string to ensure the snapshot has pretty printing.
205+
- We want to save json snapshots as json files, so we'll use `json` as their file extension.
206+
- When matching the expected data with the actual data, we want to use PHPUnit's built in json assertions, so we'll call the specific `assertJsonStringEqualsJsonString` method.
207+
54208
## Changelog
55209

56210
Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

0 commit comments

Comments
 (0)