Skip to content

Commit 654aaec

Browse files
Merge pull request #43 from spatie/mark-incomplete-after-test
Mark incomplete after test
2 parents ad2a412 + fb87ba1 commit 654aaec

File tree

4 files changed

+68
-36
lines changed

4 files changed

+68
-36
lines changed

.styleci.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
preset: laravel
22

3-
linting: true
4-
53
enabled:
64
- alpha_ordered_imports
75

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": "^1.0",
44
"phpunit/phpunit": "^6.0"
55
},
66
"autoload": {

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 & 28 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,9 @@ public function it_can_update_a_string_snapshot()
232232

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

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

239239
$this->assertSnapshotMatchesExample(
240240
'MatchesSnapshotTest__it_can_update_a_string_snapshot__1.php',
@@ -249,9 +249,9 @@ public function it_can_update_a_xml_snapshot()
249249

250250
$mockTrait = $this->getMatchesSnapshotMock();
251251

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

256256
$this->assertSnapshotMatchesExample(
257257
'MatchesSnapshotTest__it_can_update_a_xml_snapshot__1.xml',
@@ -266,9 +266,9 @@ public function it_can_update_a_json_snapshot()
266266

267267
$mockTrait = $this->getMatchesSnapshotMock();
268268

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

273273
$this->assertSnapshotMatchesExample(
274274
'MatchesSnapshotTest__it_can_update_a_json_snapshot__1.json',
@@ -283,9 +283,9 @@ public function it_can_update_a_file_snapshot()
283283

284284
$mockTrait = $this->getMatchesSnapshotMock();
285285

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

290290
$this->assertSnapshotMatchesExample(
291291
'files/MatchesSnapshotTest__it_can_update_a_file_snapshot__1.jpg',
@@ -300,13 +300,13 @@ public function it_can_update_a_file_snapshot_with_a_different_extension()
300300

301301
$mockTrait = $this->getMatchesSnapshotMock();
302302

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

307305
$this->assertFileExists($oldSnapshot);
308306

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

311311
$this->assertSnapshotMatchesExample(
312312
'files/MatchesSnapshotTest__it_can_update_a_file_snapshot_with_a_different_extension__1.png',
@@ -316,11 +316,15 @@ public function it_can_update_a_file_snapshot_with_a_different_extension()
316316
$this->assertFileNotExists($oldSnapshot);
317317
}
318318

319-
private function expectIncompleteMatchesSnapshotTest(PHPUnit_Framework_MockObject_MockObject $matchesSnapshotMock)
319+
private function expectIncompleteMatchesSnapshotTest(PHPUnit_Framework_MockObject_MockObject $matchesSnapshotMock, callable $assertions)
320320
{
321321
$matchesSnapshotMock
322322
->expects($this->once())
323323
->method('markTestIncomplete');
324+
325+
$assertions($matchesSnapshotMock);
326+
327+
$matchesSnapshotMock->markTestIncompleteIfSnapshotsHaveChanged();
324328
}
325329

326330
private function expectFail(PHPUnit_Framework_MockObject_MockObject $matchesSnapshotMock)

0 commit comments

Comments
 (0)