Skip to content

Commit 17361e9

Browse files
committed
correctly fail/update the file snapshot assertion when file extensions are different
1 parent 7147881 commit 17361e9

File tree

7 files changed

+76
-0
lines changed

7 files changed

+76
-0
lines changed

src/Filesystem.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,31 @@ public function has(string $filename): bool
2727
return file_exists($this->path($filename));
2828
}
2929

30+
/**
31+
* Get all file names in this directory that have the same name
32+
* as $fileName, but have a different file extension
33+
* @param string $fileName
34+
* @return array
35+
*/
36+
public function getNamesWithDifferentExtension(string $fileName)
37+
{
38+
$extension = pathinfo($fileName, PATHINFO_EXTENSION);
39+
40+
$baseName = substr($fileName, 0, strlen($fileName) - strlen($extension) - 1);
41+
42+
$allNames = scandir($this->basePath);
43+
44+
$namesWithDifferentExtension = array_filter($allNames, function ($existingName) use ($baseName, $extension) {
45+
$existingExtension = pathinfo($existingName, PATHINFO_EXTENSION);
46+
47+
$existingBaseName = substr($existingName, 0, strlen($existingName) - strlen($existingExtension) - 1);
48+
49+
return $existingBaseName === $baseName && $existingExtension !== $extension;
50+
});
51+
52+
return array_values($namesWithDifferentExtension);
53+
}
54+
3055
public function read(string $filename): string
3156
{
3257
return file_get_contents($this->path($filename));

src/MatchesSnapshots.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,24 @@ protected function doFileSnapshotAssertion(string $filePath)
161161

162162
$snapshotId = $this->getSnapshotId().'.'.$fileExtension;
163163

164+
// If $filePath has a different file extension than the snapshot, the test should fail
165+
if ($namesWithDifferentExtension = $fileSystem->getNamesWithDifferentExtension($snapshotId)) {
166+
// There is always only one existing snapshot with a different extension
167+
$existingSnapshotId = $namesWithDifferentExtension[0];
168+
169+
if ($this->shouldUpdateSnapshots()) {
170+
$fileSystem->delete($existingSnapshotId);
171+
172+
$fileSystem->copy($filePath, $snapshotId);
173+
174+
return $this->markTestIncomplete("File snapshot updated for {$snapshotId}");
175+
}
176+
177+
$expectedExtension = pathinfo($existingSnapshotId, PATHINFO_EXTENSION);
178+
179+
return $this->fail("File did not match the snapshot file extension (expected: {$expectedExtension}, was: {$fileExtension})");
180+
}
181+
164182
$failedSnapshotId = $snapshotId.'_failed.'.$fileExtension;
165183

166184
if ($fileSystem->has($failedSnapshotId)) {

tests/Integration/MatchesSnapshotTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,16 @@ public function it_can_mismatch_a_file_snapshot()
165165
$mockTrait->assertMatchesFileSnapshot(__DIR__.'/stubs/test_files/troubled_man.jpg');
166166
}
167167

168+
/** @test */
169+
public function it_can_mismatch_a_file_snapshot_with_a_different_extension()
170+
{
171+
$mockTrait = $this->getMatchesSnapshotMock();
172+
173+
$this->expectFail($mockTrait);
174+
175+
$mockTrait->assertMatchesFileSnapshot(__DIR__.'/stubs/test_files/no_man.png');
176+
}
177+
168178
/** @test */
169179
public function it_needs_a_file_extension_to_do_a_file_snapshot_assertion()
170180
{
@@ -282,6 +292,29 @@ public function it_can_update_a_file_snapshot()
282292
);
283293
}
284294

295+
/** @test */
296+
public function it_can_update_a_file_snapshot_with_a_different_extension()
297+
{
298+
$_SERVER['argv'][] = '--update-snapshots';
299+
300+
$mockTrait = $this->getMatchesSnapshotMock();
301+
302+
$this->expectIncompleteMatchesSnapshotTest($mockTrait);
303+
304+
$oldSnapshot = __DIR__.'/__snapshots__/files/MatchesSnapshotTest__it_can_update_a_file_snapshot_with_a_different_extension__1.jpg';
305+
306+
$this->assertFileExists($oldSnapshot);
307+
308+
$mockTrait->assertMatchesFileSnapshot(__DIR__.'/stubs/test_files/no_man.png');
309+
310+
$this->assertSnapshotMatchesExample(
311+
'files/MatchesSnapshotTest__it_can_update_a_file_snapshot_with_a_different_extension__1.png',
312+
'file.png'
313+
);
314+
315+
$this->assertFileNotExists($oldSnapshot);
316+
}
317+
285318
private function expectIncompleteMatchesSnapshotTest(PHPUnit_Framework_MockObject_MockObject $matchesSnapshotMock)
286319
{
287320
$matchesSnapshotMock
7.69 KB
Loading
3.09 KB
Loading
903 Bytes
Loading
903 Bytes
Loading

0 commit comments

Comments
 (0)