Skip to content

Commit ebbb711

Browse files
committed
Fix bug in File::class
The missing call to rewind() resulted in the file being saved with empty content if File::save() was called twice in succession.
1 parent 4592f17 commit ebbb711

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

src/File.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function save(string $filepath): void
7070
}
7171

7272
// write data
73-
$saved = @file_put_contents($filepath, $this->pointer);
73+
$saved = @file_put_contents($filepath, $this->toFilePointer());
7474
if ($saved === false) {
7575
throw new NotWritableException(
7676
sprintf("Can't write image data to path (%s).", $filepath)
@@ -85,7 +85,7 @@ public function save(string $filepath): void
8585
*/
8686
public function toString(): string
8787
{
88-
return stream_get_contents($this->pointer, offset: 0);
88+
return stream_get_contents($this->toFilePointer(), offset: 0);
8989
}
9090

9191
/**
@@ -107,7 +107,7 @@ public function toFilePointer()
107107
*/
108108
public function size(): int
109109
{
110-
$info = fstat($this->pointer);
110+
$info = fstat($this->toFilePointer());
111111

112112
return intval($info['size']);
113113
}

tests/Unit/FileTest.php

+14-4
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,21 @@ public function testFromPath(): void
4141

4242
public function testSave(): void
4343
{
44-
$filename = __DIR__ . '/file_' . strval(hrtime(true)) . '.test';
4544
$file = new File('foo');
46-
$file->save($filename);
47-
$this->assertTrue(file_exists($filename));
48-
unlink($filename);
45+
$filenames = [
46+
__DIR__ . '/01_file_' . strval(hrtime(true)) . '.test',
47+
__DIR__ . '/02_file_' . strval(hrtime(true)) . '.test',
48+
];
49+
50+
foreach ($filenames as $name) {
51+
$file->save($name);
52+
}
53+
54+
foreach ($filenames as $name) {
55+
$this->assertFileExists($name);
56+
$this->assertEquals('foo', file_get_contents($name));
57+
unlink($name);
58+
}
4959
}
5060

5161
public function testToString(): void

0 commit comments

Comments
 (0)