Skip to content

Commit 944756b

Browse files
authored
Merge pull request #53 from Kanti/patch-1
[BUGFIX] sanitize filename
2 parents ee2ce2e + f3051a0 commit 944756b

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/Snapshot.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,14 @@ public function id(): string
4040

4141
public function filename(): string
4242
{
43-
return $this->id.'.'.$this->driver->extension();
43+
$file = $this->id.'.'.$this->driver->extension();
44+
// Remove anything which isn't a word, whitespace, number
45+
// or any of the following caracters -_~,;[]().
46+
$file = preg_replace("([^\w\s\d\-_~,;\[\]\(\).])", '', $file);
47+
// Remove any runs of periods
48+
$file = preg_replace("([\.]{2,})", '', $file);
49+
50+
return $file;
4451
}
4552

4653
public function exists(): bool

tests/Unit/SnapshotTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,17 @@ public function it_has_a_filename_based_on_its_id_and_its_drivers_extension()
4343

4444
$this->assertEquals('abc.php', $snapshot->filename());
4545
}
46+
47+
/** @test */
48+
public function it_has_a_filename_which_is_valid_on_all_systems()
49+
{
50+
$this->driver
51+
->expects($this->once())
52+
->method('extension')
53+
->willReturn('php');
54+
55+
$snapshot = new Snapshot('ClassTest__testOne with data set "Empty"', $this->filesystem, $this->driver);
56+
57+
$this->assertEquals('ClassTest__testOne with data set Empty.php', $snapshot->filename());
58+
}
4659
}

0 commit comments

Comments
 (0)