Skip to content

Commit e5c5a5c

Browse files
authored
fix: create temp files with 0664 permissions (#5)
1 parent 3eba8c1 commit e5c5a5c

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/TempFile.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public static function withName(string $name, mixed $content = null): self
5656

5757
if ($content instanceof \SplFileInfo) {
5858
@\copy($content, $filename) ?: throw new \RuntimeException('Unable to copy file.');
59+
@\chmod($filename, 0664);
5960

6061
return new self($filename);
6162
}
@@ -64,6 +65,8 @@ public static function withName(string $name, mixed $content = null): self
6465
throw new \RuntimeException('Unable to write to file.');
6566
}
6667

68+
@\chmod($filename, 0664);
69+
6770
return new self($filename);
6871
}
6972

@@ -130,6 +133,8 @@ public static function image(int $width = 10, int $height = 10, string $type = '
130133
throw new \RuntimeException('Error creating temporary image.');
131134
}
132135

136+
@\chmod($file, 0664);
137+
133138
return $file->refresh();
134139
}
135140

@@ -175,6 +180,8 @@ private static function tempFile(): string
175180
throw new \RuntimeException('Failed to create temporary file.');
176181
}
177182

183+
@\chmod($filename, 0664);
184+
178185
return $filename;
179186
}
180187
}

tests/TempFileTest.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public function can_create_for_spl_file(): void
4141
$file = TempFile::for(new \SplFileInfo(__FILE__));
4242

4343
$this->assertSame(\file_get_contents(__FILE__), $file->contents());
44+
$this->assertSame('0664', \mb_substr(\sprintf('%o', \fileperms($file)), -4));
4445
}
4546

4647
/**
@@ -53,14 +54,16 @@ public function can_create_with_extension(): void
5354
$this->assertFileExists($file);
5455
$this->assertStringEndsWith('.gif', (string) $file);
5556
$this->assertFileDoesNotExist(\mb_substr($file, 0, -4));
57+
$this->assertSame('0664', \mb_substr(\sprintf('%o', \fileperms($file)), -4));
5658
}
5759

5860
/**
5961
* @test
6062
*/
6163
public function exists_when_created(): void
6264
{
63-
$this->assertFileExists(new TempFile());
65+
$this->assertFileExists($file = new TempFile());
66+
$this->assertSame('0664', \mb_substr(\sprintf('%o', \fileperms($file)), -4));
6467
}
6568

6669
/**
@@ -103,6 +106,7 @@ public function can_create_for_stream(): void
103106

104107
$this->assertFileExists($file);
105108
$this->assertStringEqualsFile($file, 'file contents');
109+
$this->assertSame('0664', \mb_substr(\sprintf('%o', \fileperms($file)), -4));
106110
}
107111

108112
/**
@@ -114,6 +118,7 @@ public function can_create_for_string(): void
114118

115119
$this->assertFileExists($file);
116120
$this->assertStringEqualsFile($file, 'file contents');
121+
$this->assertSame('0664', \mb_substr(\sprintf('%o', \fileperms($file)), -4));
117122
}
118123

119124
/**
@@ -159,6 +164,7 @@ public function default_create_image_is_jpg(): void
159164
$this->assertSame(10, $imageSize[1]);
160165
$this->assertSame('image/jpeg', $imageSize['mime']);
161166
$this->assertSame('jpg', $file->getExtension());
167+
$this->assertSame('0664', \mb_substr(\sprintf('%o', \fileperms($file)), -4));
162168
}
163169

164170
/**
@@ -173,6 +179,7 @@ public function can_create_image_for_type(string $type, string $expectedMime): v
173179
$this->assertSame(10, $imageSize[1]);
174180
$this->assertSame($expectedMime, $imageSize['mime']);
175181
$this->assertSame($type, $file->getExtension());
182+
$this->assertSame('0664', \mb_substr(\sprintf('%o', \fileperms($file)), -4));
176183
}
177184

178185
public static function imageTypeProvider(): iterable
@@ -196,6 +203,7 @@ public function can_create_image_with_dimensions(): void
196203
$this->assertSame(6, $imageSize[1]);
197204
$this->assertSame('image/png', $imageSize['mime']);
198205
$this->assertSame('png', $file->getExtension());
206+
$this->assertSame('0664', \mb_substr(\sprintf('%o', \fileperms($file)), -4));
199207
}
200208

201209
/**
@@ -211,6 +219,7 @@ public function can_create_image_with_name(): void
211219
$this->assertSame(6, $imageSize[1]);
212220
$this->assertSame('image/png', $imageSize['mime']);
213221
$this->assertSame('png', $file->getExtension());
222+
$this->assertSame('0664', \mb_substr(\sprintf('%o', \fileperms($file)), -4));
214223
}
215224

216225
/**
@@ -253,6 +262,7 @@ public function can_create_named_temp_file(): void
253262
$this->assertFileExists($file);
254263
$this->assertSame(\sys_get_temp_dir().'/some-file.txt', (string) $file);
255264
$this->assertSame('', \file_get_contents($file));
265+
$this->assertSame('0664', \mb_substr(\sprintf('%o', \fileperms($file)), -4));
256266

257267
TempFile::purge();
258268

@@ -269,6 +279,7 @@ public function can_create_named_temp_file_with_string_content(): void
269279
$this->assertFileExists($file);
270280
$this->assertSame(\sys_get_temp_dir().'/some-file.txt', (string) $file);
271281
$this->assertSame('content', \file_get_contents($file));
282+
$this->assertSame('0664', \mb_substr(\sprintf('%o', \fileperms($file)), -4));
272283

273284
TempFile::purge();
274285

@@ -285,6 +296,7 @@ public function can_create_named_temp_file_with_spl_file(): void
285296
$this->assertFileExists($file);
286297
$this->assertSame(\sys_get_temp_dir().'/some-file.txt', (string) $file);
287298
$this->assertFileEquals($file, __FILE__);
299+
$this->assertSame('0664', \mb_substr(\sprintf('%o', \fileperms($file)), -4));
288300

289301
TempFile::purge();
290302

0 commit comments

Comments
 (0)