Skip to content

Commit da29aab

Browse files
committed
clean filenames on MatchesFileSnapshot
1 parent 61a196e commit da29aab

File tree

6 files changed

+61
-13
lines changed

6 files changed

+61
-13
lines changed

src/Filename.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Spatie\Snapshots;
4+
5+
class Filename
6+
{
7+
static public function cleanFilename(string $raw): string
8+
{
9+
// Remove anything which isn't a word, whitespace, number
10+
// or any of the following caracters -_~,;[]().
11+
$file = preg_replace("([^\w\s\d\-_~,;\[\]\(\).])", '', $raw);
12+
13+
// Remove any runs of periods
14+
$file = preg_replace("([\.]{2,})", '', $file);
15+
16+
return $file;
17+
}
18+
}

src/MatchesSnapshots.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ protected function doFileSnapshotAssertion(string $filePath): void
207207
$this->snapshotIncrementor++;
208208

209209
$snapshotId = $this->getSnapshotId().'.'.$fileExtension;
210+
$snapshotId = Filename::cleanFilename($snapshotId);
210211

211212
// If $filePath has a different file extension than the snapshot, the test should fail
212213
if ($namesWithDifferentExtension = $fileSystem->getNamesWithDifferentExtension($snapshotId)) {

src/Snapshot.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,7 @@ public function filename(): string
4242
{
4343
$file = $this->id.'.'.$this->driver->extension();
4444

45-
// Remove anything which isn't a word, whitespace, number
46-
// or any of the following caracters -_~,;[]().
47-
$file = preg_replace("([^\w\s\d\-_~,;\[\]\(\).])", '', $file);
48-
49-
// Remove any runs of periods
50-
$file = preg_replace("([\.]{2,})", '', $file);
51-
52-
return $file;
45+
return Filename::cleanFilename($file);
5346
}
5447

5548
public function exists(): bool

tests/Integration/MatchesSnapshotTest.php

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,23 @@ public function it_deletes_the_persisted_failed_file_before_a_file_snapshot_asse
292292
$this->assertFileDoesNotExist($persistedFailedFile);
293293
}
294294

295+
/** @test */
296+
public function it_cleans_filenames_on_file_snapshot()
297+
{
298+
$mockTrait = $this->getMatchesSnapshotMock(false);
299+
$mockTrait
300+
->expects($this->any())
301+
->method('getSnapshotId')
302+
->willReturn('MatchesSnapshotTest__it_cleans_filenames_on_file_snapshot with dataset "Empty"__1');
303+
304+
$this->expectFail(
305+
$mockTrait,
306+
'File did not match snapshot (MatchesSnapshotTest__it_cleans_filenames_on_file_snapshot with dataset Empty__1.jpg)'
307+
);
308+
309+
$mockTrait->assertMatchesFileSnapshot(__DIR__.'/stubs/test_files/troubled_man.jpg');
310+
}
311+
295312
/** @test */
296313
public function it_can_update_a_string_snapshot()
297314
{
@@ -447,7 +464,7 @@ private function expectFailedMatchesSnapshotTest()
447464
/**
448465
* @return \PHPUnit\Framework\MockObject\MockObject|\Spatie\Snapshots\MatchesSnapshots
449466
*/
450-
private function getMatchesSnapshotMock(): MockObject
467+
private function getMatchesSnapshotMock(bool $mockGetSnapshotId = true): MockObject
451468
{
452469
$mockMethods = [
453470
'markTestIncomplete',
@@ -468,10 +485,12 @@ private function getMatchesSnapshotMock(): MockObject
468485
$mockMethods
469486
);
470487

471-
$matchesSnapshotMock
472-
->expects($this->any())
473-
->method('getSnapshotId')
474-
->willReturn('MatchesSnapshotTest__'.$this->getName().'__1');
488+
if ($mockGetSnapshotId) {
489+
$matchesSnapshotMock
490+
->expects($this->any())
491+
->method('getSnapshotId')
492+
->willReturn('MatchesSnapshotTest__'.$this->getName().'__1');
493+
}
475494

476495
$matchesSnapshotMock
477496
->expects($this->any())
7.69 KB
Loading

tests/Unit/FilenameTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Spatie\Snapshots\Test\Unit;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Spatie\Snapshots\Filename;
7+
8+
class FilenameTest extends TestCase
9+
{
10+
/** @test */
11+
public function it_creates_a_filename_which_is_valid_on_all_systems()
12+
{
13+
$name = 'ClassTest__testOne with... data set "Empty".php';
14+
15+
$this->assertEquals('ClassTest__testOne with data set Empty.php', Filename::cleanFilename($name));
16+
}
17+
}

0 commit comments

Comments
 (0)