Skip to content

Commit 3f7d110

Browse files
Only mark the test incomplete after it's finished
1 parent ad2a412 commit 3f7d110

File tree

6 files changed

+104
-33
lines changed

6 files changed

+104
-33
lines changed

example/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"require": {
3-
"spatie/phpunit-snapshot-assertions": "^0.4.0",
3+
"spatie/phpunit-snapshot-assertions": "dev-master",
44
"phpunit/phpunit": "^6.0"
55
},
66
"autoload": {

example/tests/OrderSerializerTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,7 @@ public function it_can_serialize_an_order()
2121
]);
2222

2323
$this->assertMatchesJsonSnapshot($orderSerializer->serialize($order));
24+
$this->assertMatchesJsonSnapshot($orderSerializer->serialize($order));
25+
$this->assertMatchesJsonSnapshot($orderSerializer->serialize($order));
2426
}
2527
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"id": 1,
3+
"email": "[email protected]",
4+
"paid": 1,
5+
"items": [
6+
{
7+
"id": 1,
8+
"description": "Sublime Text License",
9+
"price": 210
10+
},
11+
{
12+
"id": 2,
13+
"description": "PHPStorm License",
14+
"price": 398
15+
}
16+
]
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"id": 1,
3+
"email": "[email protected]",
4+
"paid": 1,
5+
"items": [
6+
{
7+
"id": 1,
8+
"description": "Sublime Text License",
9+
"price": 210
10+
},
11+
{
12+
"id": 2,
13+
"description": "PHPStorm License",
14+
"price": 398
15+
}
16+
]
17+
}

src/MatchesSnapshots.php

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,35 @@ trait MatchesSnapshots
1414
/** @var int */
1515
protected $snapshotIncrementor;
1616

17+
/** @var string[] */
18+
protected $snapshotChanges;
19+
1720
/** @before */
1821
public function setUpSnapshotIncrementor()
1922
{
2023
$this->snapshotIncrementor = 0;
2124
}
2225

26+
/** @after */
27+
public function markTestIncompleteIfSnapshotsHaveChanged()
28+
{
29+
if (empty($this->snapshotChanges)) {
30+
return;
31+
}
32+
33+
if (count($this->snapshotChanges) === 1) {
34+
$this->markTestIncomplete($this->snapshotChanges[0]);
35+
36+
return;
37+
}
38+
39+
$formattedMessages = implode(PHP_EOL, array_map(function (string $message) {
40+
return "- {$message}";
41+
}, $this->snapshotChanges));
42+
43+
$this->markTestIncomplete($formattedMessages);
44+
}
45+
2346
public function assertMatchesSnapshot($actual, Driver $driver = null)
2447
{
2548
$this->doSnapshotAssertion($actual, $driver ?? new VarDriver());
@@ -166,7 +189,9 @@ protected function doFileSnapshotAssertion(string $filePath)
166189

167190
$fileSystem->copy($filePath, $snapshotId);
168191

169-
return $this->markTestIncomplete("File snapshot updated for {$snapshotId}");
192+
$this->registerSnapshotChange("File snapshot updated for {$snapshotId}");
193+
194+
return;
170195
}
171196

172197
$expectedExtension = pathinfo($existingSnapshotId, PATHINFO_EXTENSION);
@@ -183,14 +208,14 @@ protected function doFileSnapshotAssertion(string $filePath)
183208
if (! $fileSystem->has($snapshotId)) {
184209
$fileSystem->copy($filePath, $snapshotId);
185210

186-
$this->markTestIncomplete("File snapshot created for {$snapshotId}");
211+
$this->registerSnapshotChange("File snapshot created for {$snapshotId}");
187212
}
188213

189214
if (! $fileSystem->fileEquals($filePath, $snapshotId)) {
190215
if ($this->shouldUpdateSnapshots()) {
191216
$fileSystem->copy($filePath, $snapshotId);
192217

193-
$this->markTestIncomplete("File snapshot updated for {$snapshotId}");
218+
$this->registerSnapshotChange("File snapshot updated for {$snapshotId}");
194219
}
195220

196221
$fileSystem->copy($filePath, $failedSnapshotId);
@@ -205,14 +230,14 @@ protected function createSnapshotAndMarkTestIncomplete(Snapshot $snapshot, $actu
205230
{
206231
$snapshot->create($actual);
207232

208-
$this->markTestIncomplete("Snapshot created for {$snapshot->id()}");
233+
$this->registerSnapshotChange("Snapshot created for {$snapshot->id()}");
209234
}
210235

211236
protected function updateSnapshotAndMarkTestIncomplete(Snapshot $snapshot, $actual)
212237
{
213238
$snapshot->create($actual);
214239

215-
$this->markTestIncomplete("Snapshot updated for {$snapshot->id()}");
240+
$this->registerSnapshotChange("Snapshot updated for {$snapshot->id()}");
216241
}
217242

218243
protected function rethrowExpectationFailedExceptionWithUpdateSnapshotsPrompt($exception)
@@ -229,4 +254,9 @@ protected function rethrowExpectationFailedExceptionWithUpdateSnapshotsPrompt($e
229254

230255
throw $exception;
231256
}
257+
258+
protected function registerSnapshotChange(string $message)
259+
{
260+
$this->snapshotChanges[] = $message;
261+
}
232262
}

tests/Integration/MatchesSnapshotTest.php

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ public function it_can_create_a_snapshot_from_a_string()
2929
{
3030
$mockTrait = $this->getMatchesSnapshotMock();
3131

32-
$this->expectIncompleteMatchesSnapshotTest($mockTrait);
33-
34-
$mockTrait->assertMatchesSnapshot('Foo');
32+
$this->expectIncompleteMatchesSnapshotTest($mockTrait, function ($mockTrait) {
33+
$mockTrait->assertMatchesSnapshot('Foo');
34+
});
3535

3636
$this->assertSnapshotMatchesExample(
3737
'MatchesSnapshotTest__it_can_match_an_existing_string_snapshot__1.php',
@@ -44,9 +44,9 @@ public function it_can_create_a_snapshot_from_xml()
4444
{
4545
$mockTrait = $this->getMatchesSnapshotMock();
4646

47-
$this->expectIncompleteMatchesSnapshotTest($mockTrait);
48-
49-
$mockTrait->assertMatchesXmlSnapshot('<foo><bar>Baz</bar></foo>');
47+
$this->expectIncompleteMatchesSnapshotTest($mockTrait, function ($mockTrait) {
48+
$mockTrait->assertMatchesXmlSnapshot('<foo><bar>Baz</bar></foo>');
49+
});
5050

5151
$this->assertSnapshotMatchesExample(
5252
'MatchesSnapshotTest__it_can_create_a_snapshot_from_xml__1.xml',
@@ -59,9 +59,9 @@ public function it_can_create_a_snapshot_from_json()
5959
{
6060
$mockTrait = $this->getMatchesSnapshotMock();
6161

62-
$this->expectIncompleteMatchesSnapshotTest($mockTrait);
63-
64-
$mockTrait->assertMatchesJsonSnapshot('{"foo":"foo","bar":"bar","baz":"baz"}');
62+
$this->expectIncompleteMatchesSnapshotTest($mockTrait, function ($mockTrait) {
63+
$mockTrait->assertMatchesJsonSnapshot('{"foo":"foo","bar":"bar","baz":"baz"}');
64+
});
6565

6666
$this->assertSnapshotMatchesExample(
6767
'MatchesSnapshotTest__it_can_create_a_snapshot_from_json__1.json',
@@ -74,9 +74,9 @@ public function it_can_create_a_snapshot_from_a_file()
7474
{
7575
$mockTrait = $this->getMatchesSnapshotMock();
7676

77-
$this->expectIncompleteMatchesSnapshotTest($mockTrait);
78-
79-
$mockTrait->assertMatchesFileSnapshot(__DIR__.'/stubs/test_files/friendly_man.jpg');
77+
$this->expectIncompleteMatchesSnapshotTest($mockTrait, function ($mockTrait) {
78+
$mockTrait->assertMatchesFileSnapshot(__DIR__.'/stubs/test_files/friendly_man.jpg');
79+
});
8080

8181
$this->assertSnapshotMatchesExample(
8282
'files/MatchesSnapshotTest__it_can_create_a_snapshot_from_a_file__1.jpg',
@@ -232,9 +232,10 @@ public function it_can_update_a_string_snapshot()
232232

233233
$mockTrait = $this->getMatchesSnapshotMock();
234234

235-
$this->expectIncompleteMatchesSnapshotTest($mockTrait);
235+
$this->expectIncompleteMatchesSnapshotTest($mockTrait, function ($mockTrait) {
236+
$mockTrait->assertMatchesSnapshot('Foo');
237+
});
236238

237-
$mockTrait->assertMatchesSnapshot('Foo');
238239

239240
$this->assertSnapshotMatchesExample(
240241
'MatchesSnapshotTest__it_can_update_a_string_snapshot__1.php',
@@ -249,9 +250,9 @@ public function it_can_update_a_xml_snapshot()
249250

250251
$mockTrait = $this->getMatchesSnapshotMock();
251252

252-
$this->expectIncompleteMatchesSnapshotTest($mockTrait);
253-
254-
$mockTrait->assertMatchesXmlSnapshot('<foo><bar>Baz</bar></foo>');
253+
$this->expectIncompleteMatchesSnapshotTest($mockTrait, function ($mockTrait) {
254+
$mockTrait->assertMatchesXmlSnapshot('<foo><bar>Baz</bar></foo>');
255+
});
255256

256257
$this->assertSnapshotMatchesExample(
257258
'MatchesSnapshotTest__it_can_update_a_xml_snapshot__1.xml',
@@ -266,9 +267,9 @@ public function it_can_update_a_json_snapshot()
266267

267268
$mockTrait = $this->getMatchesSnapshotMock();
268269

269-
$this->expectIncompleteMatchesSnapshotTest($mockTrait);
270-
271-
$mockTrait->assertMatchesJsonSnapshot('{"foo":"foo","bar":"bar","baz":"baz"}');
270+
$this->expectIncompleteMatchesSnapshotTest($mockTrait, function ($mockTrait) {
271+
$mockTrait->assertMatchesJsonSnapshot('{"foo":"foo","bar":"bar","baz":"baz"}');
272+
});
272273

273274
$this->assertSnapshotMatchesExample(
274275
'MatchesSnapshotTest__it_can_update_a_json_snapshot__1.json',
@@ -283,9 +284,9 @@ public function it_can_update_a_file_snapshot()
283284

284285
$mockTrait = $this->getMatchesSnapshotMock();
285286

286-
$this->expectIncompleteMatchesSnapshotTest($mockTrait);
287-
288-
$mockTrait->assertMatchesFileSnapshot(__DIR__.'/stubs/test_files/friendly_man.jpg');
287+
$this->expectIncompleteMatchesSnapshotTest($mockTrait, function ($mockTrait) {
288+
$mockTrait->assertMatchesFileSnapshot(__DIR__.'/stubs/test_files/friendly_man.jpg');
289+
});
289290

290291
$this->assertSnapshotMatchesExample(
291292
'files/MatchesSnapshotTest__it_can_update_a_file_snapshot__1.jpg',
@@ -300,13 +301,13 @@ public function it_can_update_a_file_snapshot_with_a_different_extension()
300301

301302
$mockTrait = $this->getMatchesSnapshotMock();
302303

303-
$this->expectIncompleteMatchesSnapshotTest($mockTrait);
304-
305304
$oldSnapshot = __DIR__.'/__snapshots__/files/MatchesSnapshotTest__it_can_update_a_file_snapshot_with_a_different_extension__1.jpg';
306305

307306
$this->assertFileExists($oldSnapshot);
308307

309-
$mockTrait->assertMatchesFileSnapshot(__DIR__.'/stubs/test_files/no_man.png');
308+
$this->expectIncompleteMatchesSnapshotTest($mockTrait, function ($mockTrait) {
309+
$mockTrait->assertMatchesFileSnapshot(__DIR__.'/stubs/test_files/no_man.png');
310+
});
310311

311312
$this->assertSnapshotMatchesExample(
312313
'files/MatchesSnapshotTest__it_can_update_a_file_snapshot_with_a_different_extension__1.png',
@@ -316,11 +317,15 @@ public function it_can_update_a_file_snapshot_with_a_different_extension()
316317
$this->assertFileNotExists($oldSnapshot);
317318
}
318319

319-
private function expectIncompleteMatchesSnapshotTest(PHPUnit_Framework_MockObject_MockObject $matchesSnapshotMock)
320+
private function expectIncompleteMatchesSnapshotTest(PHPUnit_Framework_MockObject_MockObject $matchesSnapshotMock, callable $assertions)
320321
{
321322
$matchesSnapshotMock
322323
->expects($this->once())
323324
->method('markTestIncomplete');
325+
326+
$assertions($matchesSnapshotMock);
327+
328+
$matchesSnapshotMock->markTestIncompleteIfSnapshotsHaveChanged();
324329
}
325330

326331
private function expectFail(PHPUnit_Framework_MockObject_MockObject $matchesSnapshotMock)

0 commit comments

Comments
 (0)