2
2
3
3
namespace Spatie \Snapshots \Test \Integration ;
4
4
5
+ use PHPUnit \Framework \AssertionFailedError ;
5
6
use PHPUnit \Framework \ExpectationFailedException ;
6
7
use PHPUnit \Framework \MockObject \MockObject ;
7
8
use PHPUnit \Framework \TestCase ;
@@ -18,10 +19,15 @@ public function setUp(): void
18
19
$ this ->setUpComparesSnapshotFiles ();
19
20
20
21
$ updateArgument = array_search ('--update-snapshots ' , $ _SERVER ['argv ' ]);
22
+ $ withoutCreatingArgument = array_search ('--without-creating-snapshots ' , $ _SERVER ['argv ' ]);
21
23
22
24
if ($ updateArgument ) {
23
25
unset($ _SERVER ['argv ' ][$ updateArgument ]);
24
26
}
27
+
28
+ if ($ withoutCreatingArgument ) {
29
+ unset($ _SERVER ['argv ' ][$ withoutCreatingArgument ]);
30
+ }
25
31
}
26
32
27
33
/** @test */
@@ -209,7 +215,10 @@ public function it_can_mismatch_a_file_snapshot()
209
215
{
210
216
$ mockTrait = $ this ->getMatchesSnapshotMock ();
211
217
212
- $ this ->expectFail ($ mockTrait );
218
+ $ this ->expectFail (
219
+ $ mockTrait ,
220
+ 'File did not match snapshot (MatchesSnapshotTest__it_can_mismatch_a_file_snapshot__1.jpg) '
221
+ );
213
222
214
223
$ mockTrait ->assertMatchesFileSnapshot (__DIR__ .'/stubs/test_files/troubled_man.jpg ' );
215
224
}
@@ -219,7 +228,10 @@ public function it_can_mismatch_a_file_snapshot_with_a_different_extension()
219
228
{
220
229
$ mockTrait = $ this ->getMatchesSnapshotMock ();
221
230
222
- $ this ->expectFail ($ mockTrait );
231
+ $ this ->expectFail (
232
+ $ mockTrait ,
233
+ 'File did not match the snapshot file extension (expected: jpg, was: png) '
234
+ );
223
235
224
236
$ mockTrait ->assertMatchesFileSnapshot (__DIR__ .'/stubs/test_files/no_man.png ' );
225
237
}
@@ -229,10 +241,14 @@ public function it_needs_a_file_extension_to_do_a_file_snapshot_assertion()
229
241
{
230
242
$ mockTrait = $ this ->getMatchesSnapshotMock ();
231
243
232
- $ this ->expectFail ($ mockTrait );
233
-
234
244
$ filePath = __DIR__ .'/stubs/test_files/file_without_extension ' ;
235
245
246
+ $ this ->expectFail (
247
+ $ mockTrait ,
248
+ 'Unable to make a file snapshot, file does not have a file extension ' .
249
+ "( $ filePath) "
250
+ );
251
+
236
252
$ this ->assertFileExists ($ filePath );
237
253
238
254
$ mockTrait ->assertMatchesFileSnapshot ($ filePath );
@@ -243,7 +259,10 @@ public function it_persists_the_failed_file_after_mismatching_a_file_snapshot()
243
259
{
244
260
$ mockTrait = $ this ->getMatchesSnapshotMock ();
245
261
246
- $ this ->expectFail ($ mockTrait );
262
+ $ this ->expectFail (
263
+ $ mockTrait ,
264
+ 'File did not match snapshot (MatchesSnapshotTest__it_persists_the_failed_file_after_mismatching_a_file_snapshot__1.jpg) '
265
+ );
247
266
248
267
$ mismatchedFile = __DIR__ .'/stubs/test_files/troubled_man.jpg ' ;
249
268
@@ -270,7 +289,7 @@ public function it_deletes_the_persisted_failed_file_before_a_file_snapshot_asse
270
289
271
290
$ mockTrait ->assertMatchesFileSnapshot (__DIR__ .'/stubs/test_files/friendly_man.jpg ' );
272
291
273
- $ this ->assertFileNotExists ($ persistedFailedFile );
292
+ $ this ->assertFileDoesNotExist ($ persistedFailedFile );
274
293
}
275
294
276
295
/** @test */
@@ -378,7 +397,7 @@ public function it_can_update_a_file_snapshot_with_a_different_extension()
378
397
'file.png '
379
398
);
380
399
381
- $ this ->assertFileNotExists ($ oldSnapshot );
400
+ $ this ->assertFileDoesNotExist ($ oldSnapshot );
382
401
}
383
402
384
403
private function expectIncompleteMatchesSnapshotTest (MockObject $ matchesSnapshotMock , callable $ assertions )
@@ -392,11 +411,15 @@ private function expectIncompleteMatchesSnapshotTest(MockObject $matchesSnapshot
392
411
$ matchesSnapshotMock ->markTestIncompleteIfSnapshotsHaveChanged ();
393
412
}
394
413
395
- private function expectFail (MockObject $ matchesSnapshotMock )
414
+ private function expectFail (MockObject $ matchesSnapshotMock, string $ message )
396
415
{
416
+ $ this ->expectException (AssertionFailedError::class);
417
+
397
418
$ matchesSnapshotMock
398
419
->expects ($ this ->once ())
399
- ->method ('fail ' );
420
+ ->method ('fail ' )
421
+ ->with ($ message )
422
+ ->willThrowException (new AssertionFailedError ());
400
423
}
401
424
402
425
private function expectFailedMatchesSnapshotTest ()
@@ -441,4 +464,36 @@ private function getMatchesSnapshotMock(): MockObject
441
464
442
465
return $ matchesSnapshotMock ;
443
466
}
467
+
468
+ /** @test */
469
+ public function it_doesnt_create_a_regular_snapshot_and_mismatches_if_asked ()
470
+ {
471
+ $ _SERVER ['argv ' ][] = '--without-creating-snapshots ' ;
472
+
473
+ $ mockTrait = $ this ->getMatchesSnapshotMock ();
474
+
475
+ $ this ->expectFail (
476
+ $ mockTrait ,
477
+ "Snapshot \"MatchesSnapshotTest__it_doesnt_create_a_regular_snapshot_and_mismatches_if_asked__1.txt \" does not exist. \n" .
478
+ 'You can automatically create it by removing `-d --without-creating-snapshots` of PHPUnit \'s CLI arguments. '
479
+ );
480
+
481
+ $ mockTrait ->assertMatchesSnapshot ('Bar ' );
482
+ }
483
+
484
+ /** @test */
485
+ public function it_doesnt_create_a_file_snapshot_and_mismatches_if_asked ()
486
+ {
487
+ $ _SERVER ['argv ' ][] = '--without-creating-snapshots ' ;
488
+
489
+ $ mockTrait = $ this ->getMatchesSnapshotMock ();
490
+
491
+ $ this ->expectFail (
492
+ $ mockTrait ,
493
+ "Snapshot \"MatchesSnapshotTest__it_doesnt_create_a_file_snapshot_and_mismatches_if_asked__1.jpg_failed.jpg \" does not exist. \n" .
494
+ 'You can automatically create it by removing `-d --without-creating-snapshots` of PHPUnit \'s CLI arguments. '
495
+ );
496
+
497
+ $ mockTrait ->assertMatchesFileSnapshot (__DIR__ .'/stubs/test_files/friendly_man.jpg ' );
498
+ }
444
499
}
0 commit comments