[TemporaryFile][TESTS] Throw on attempt to write to null resource and fix tests

This commit is contained in:
Hugo Sales 2021-08-08 01:05:45 +00:00
parent c24c32334d
commit 809bf00aa9
No known key found for this signature in database
GPG Key ID: 7D0C7EAFC9D835A0
2 changed files with 25 additions and 3 deletions

View File

@ -61,8 +61,10 @@ class TemporaryFile extends \SplFileInfo
} }
} }
if ($this->resource === false) { if ($this->resource === false) {
// @codeCoverageIgnoreStart
$this->cleanup(); $this->cleanup();
throw new TemporaryFileException('Could not open file: ' . $filepath); throw new TemporaryFileException('Could not open file: ' . $filepath);
// @codeCoverageIgnoreEnd
} }
parent::__construct($filepath); parent::__construct($filepath);
@ -81,14 +83,18 @@ class TemporaryFile extends \SplFileInfo
* *
* @param string $data The string that is to be written. * @param string $data The string that is to be written.
* *
* @return null|false|int the number of bytes written, false on error, null on null resource/stream * @throws ServerException when the resource is null
*
* @return false|int the number of bytes written, false on error
*/ */
public function write(string $data): int | false | null public function write(string $data): int | false
{ {
if (!is_null($this->resource)) { if (!is_null($this->resource)) {
return fwrite($this->resource, $data); return fwrite($this->resource, $data);
} else { } else {
return null; // @codeCoverageIgnoreStart
throw new TemporaryFileException(_m('Temporary file attempted to write to a null resource'));
// @codeCoverageIgnoreEnd
} }
} }
@ -152,10 +158,14 @@ class TemporaryFile extends \SplFileInfo
{ {
if (!is_dir($directory)) { if (!is_dir($directory)) {
if (false === @mkdir($directory, $dirmode, true) && !is_dir($directory)) { if (false === @mkdir($directory, $dirmode, true) && !is_dir($directory)) {
// @codeCoverageIgnoreStart
throw new TemporaryFileException(sprintf('Unable to create the "%s" directory.', $directory)); throw new TemporaryFileException(sprintf('Unable to create the "%s" directory.', $directory));
// @codeCoverageIgnoreEnd
} }
} elseif (!is_writable($directory)) { } elseif (!is_writable($directory)) {
// @codeCoverageIgnoreStart
throw new TemporaryFileException(sprintf('Unable to write in the "%s" directory.', $directory)); throw new TemporaryFileException(sprintf('Unable to write in the "%s" directory.', $directory));
// @codeCoverageIgnoreEnd
} }
$destpath = rtrim($directory, '/\\') . DIRECTORY_SEPARATOR . $this->getName($filename); $destpath = rtrim($directory, '/\\') . DIRECTORY_SEPARATOR . $this->getName($filename);
@ -200,7 +210,9 @@ class TemporaryFile extends \SplFileInfo
if (!$renamed || !$chmoded) { if (!$renamed || !$chmoded) {
if (!$existed && file_exists($destpath)) { if (!$existed && file_exists($destpath)) {
// If the file wasn't there, clean it up in case of a later failure // If the file wasn't there, clean it up in case of a later failure
// @codeCoverageIgnoreStart
unlink($destpath); unlink($destpath);
// @codeCoverageIgnoreEnd
} }
throw new TemporaryFileException(sprintf('Could not move the file "%s" to "%s" (%s).', $this->getPathname(), $destpath, strip_tags($error))); throw new TemporaryFileException(sprintf('Could not move the file "%s" to "%s" (%s).', $this->getPathname(), $destpath, strip_tags($error)));
} }
@ -220,9 +232,11 @@ class TemporaryFile extends \SplFileInfo
*/ */
public function getMimeType() public function getMimeType()
{ {
// @codeCoverageIgnoreStart
if (!class_exists(MimeTypes::class)) { if (!class_exists(MimeTypes::class)) {
throw new \LogicException('You cannot guess the mime type as the Mime component is not installed. Try running "composer require symfony/mime".'); throw new \LogicException('You cannot guess the mime type as the Mime component is not installed. Try running "composer require symfony/mime".');
} }
// @codeCoverageIgnoreEnd
return MimeTypes::getDefault()->guessMimeType($this->getPathname()); return MimeTypes::getDefault()->guessMimeType($this->getPathname());
} }

View File

@ -45,4 +45,12 @@ class TemporaryFileTest extends WebTestCase
static::assertThrows(TemporaryFileException::class, fn () => $temp->commit($filepath)); static::assertThrows(TemporaryFileException::class, fn () => $temp->commit($filepath));
static::assertThrows(TemporaryFileException::class, fn () => $temp->commit('/root/cannot_write_here')); static::assertThrows(TemporaryFileException::class, fn () => $temp->commit('/root/cannot_write_here'));
} }
public function testCreateDirectory()
{
$temp = new TemporaryFile();
@unlink('/tmp/social-test-folder/test');
$temp->move('/tmp/social-test-folder/', 'test');
static::assertTrue(file_exists('/tmp/social-test-folder/test'));
}
} }