Skip to content

Commit

Permalink
fix: create temp files with 0664 permissions (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
kbond authored Feb 27, 2023
1 parent 3eba8c1 commit e5c5a5c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/TempFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public static function withName(string $name, mixed $content = null): self

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

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

@\chmod($filename, 0664);

return new self($filename);
}

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

@\chmod($file, 0664);

return $file->refresh();
}

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

@\chmod($filename, 0664);

return $filename;
}
}
14 changes: 13 additions & 1 deletion tests/TempFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public function can_create_for_spl_file(): void
$file = TempFile::for(new \SplFileInfo(__FILE__));

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

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

/**
* @test
*/
public function exists_when_created(): void
{
$this->assertFileExists(new TempFile());
$this->assertFileExists($file = new TempFile());
$this->assertSame('0664', \mb_substr(\sprintf('%o', \fileperms($file)), -4));
}

/**
Expand Down Expand Up @@ -103,6 +106,7 @@ public function can_create_for_stream(): void

$this->assertFileExists($file);
$this->assertStringEqualsFile($file, 'file contents');
$this->assertSame('0664', \mb_substr(\sprintf('%o', \fileperms($file)), -4));
}

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

$this->assertFileExists($file);
$this->assertStringEqualsFile($file, 'file contents');
$this->assertSame('0664', \mb_substr(\sprintf('%o', \fileperms($file)), -4));
}

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

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

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

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

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

TempFile::purge();

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

TempFile::purge();

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

TempFile::purge();

Expand Down

0 comments on commit e5c5a5c

Please sign in to comment.