From 785080ab023e3bcf3f7ebfa943874d0977b4d9ac Mon Sep 17 00:00:00 2001 From: Christian Gartner Date: Fri, 27 Sep 2013 16:40:55 +0200 Subject: [PATCH 1/2] [Filesystem] introduced new Exception base classes --- .../Exception/FileNotFoundException.php | 31 +++++ .../Filesystem/Exception/IOException.php | 21 +++- .../Exception/IOExceptionInterface.php | 27 +++++ .../Component/Filesystem/Filesystem.php | 110 ++++++++++++------ .../Filesystem/Tests/ExceptionTest.php | 51 ++++++++ 5 files changed, 204 insertions(+), 36 deletions(-) create mode 100644 src/Symfony/Component/Filesystem/Exception/FileNotFoundException.php create mode 100644 src/Symfony/Component/Filesystem/Exception/IOExceptionInterface.php create mode 100644 src/Symfony/Component/Filesystem/Tests/ExceptionTest.php diff --git a/src/Symfony/Component/Filesystem/Exception/FileNotFoundException.php b/src/Symfony/Component/Filesystem/Exception/FileNotFoundException.php new file mode 100644 index 0000000000..242ff070e5 --- /dev/null +++ b/src/Symfony/Component/Filesystem/Exception/FileNotFoundException.php @@ -0,0 +1,31 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Filesystem\Exception; + +/** + * Exception class thrown when a file couldn't be found + * + * @author Christian Gärtner + */ +class FileNotFoundException extends IOException +{ + public function __construct($path, $message = null, $code = 0, \Exception $previous = null) + { + if ($message === null) { + $message = sprintf('File "%s" could not be found', $path); + } + + $this->setPath($path); + + parent::__construct($message, $code, $previous); + } +} diff --git a/src/Symfony/Component/Filesystem/Exception/IOException.php b/src/Symfony/Component/Filesystem/Exception/IOException.php index 5b27e661ee..e9f12371da 100644 --- a/src/Symfony/Component/Filesystem/Exception/IOException.php +++ b/src/Symfony/Component/Filesystem/Exception/IOException.php @@ -15,10 +15,29 @@ namespace Symfony\Component\Filesystem\Exception; * Exception class thrown when a filesystem operation failure happens * * @author Romain Neutron + * @author Christian Gärtner * * @api */ -class IOException extends \RuntimeException implements ExceptionInterface +class IOException extends \RuntimeException implements IOExceptionInterface { + private $path; + + /** + * Set the path associated with this IOException + * @param string $path The path + */ + public function setPath($path) + { + $this->path = $path; + } + + /** + * {@inheritdoc} + */ + public function getPath() + { + return $this->path; + } } diff --git a/src/Symfony/Component/Filesystem/Exception/IOExceptionInterface.php b/src/Symfony/Component/Filesystem/Exception/IOExceptionInterface.php new file mode 100644 index 0000000000..41d8d3bdee --- /dev/null +++ b/src/Symfony/Component/Filesystem/Exception/IOExceptionInterface.php @@ -0,0 +1,27 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Filesystem\Exception; + +/** + * IOException interface for file and input/output stream releated exceptions thrown by the component. + * + * @author Christian Gärtner + */ +interface IOExceptionInterface extends ExceptionInterface +{ + /** + * Returns the associated path for the exception + * + * @return string The path. + */ + public function getPath(); +} diff --git a/src/Symfony/Component/Filesystem/Filesystem.php b/src/Symfony/Component/Filesystem/Filesystem.php index 1d5109027d..f591780ff7 100644 --- a/src/Symfony/Component/Filesystem/Filesystem.php +++ b/src/Symfony/Component/Filesystem/Filesystem.php @@ -12,6 +12,7 @@ namespace Symfony\Component\Filesystem; use Symfony\Component\Filesystem\Exception\IOException; +use Symfony\Component\Filesystem\Exception\FileNotFoundException; /** * Provides basic utility to manipulate the file system. @@ -31,12 +32,13 @@ class Filesystem * @param string $targetFile The target filename * @param boolean $override Whether to override an existing file or not * - * @throws IOException When copy fails + * @throws FileNotFoundException When orginFile doesn't exist + * @throws IOException When copy fails */ public function copy($originFile, $targetFile, $override = false) { if (stream_is_local($originFile) && !is_file($originFile)) { - throw new IOException(sprintf('Failed to copy %s because file not exists', $originFile)); + throw new FileNotFoundException($originFile, sprintf('Failed to copy %s because file does not exist', $originFile)); } $this->mkdir(dirname($targetFile)); @@ -57,7 +59,9 @@ class Filesystem unset($source, $target); if (!is_file($targetFile)) { - throw new IOException(sprintf('Failed to copy %s to %s', $originFile, $targetFile)); + $e = new IOException(sprintf('Failed to copy %s to %s', $originFile, $targetFile)); + $e->setPath($originFile); + throw $e; } } } @@ -78,7 +82,9 @@ class Filesystem } if (true !== @mkdir($dir, $mode, true)) { - throw new IOException(sprintf('Failed to create %s', $dir)); + $e = new IOException(sprintf('Failed to create %s', $dir)); + $e->setPath($dir); + throw $e; } } } @@ -115,7 +121,9 @@ class Filesystem foreach ($this->toIterator($files) as $file) { $touch = $time ? @touch($file, $time, $atime) : @touch($file); if (true !== $touch) { - throw new IOException(sprintf('Failed to touch %s', $file)); + $e = new IOException(sprintf('Failed to touch %s', $file)); + $e->setPath($file); + throw $e; } } } @@ -140,17 +148,23 @@ class Filesystem $this->remove(new \FilesystemIterator($file)); if (true !== @rmdir($file)) { - throw new IOException(sprintf('Failed to remove directory %s', $file)); + $e = new IOException(sprintf('Failed to remove directory %s', $file)); + $e->setPath($file); + throw $e; } } else { // https://bugs.php.net/bug.php?id=52176 if (defined('PHP_WINDOWS_VERSION_MAJOR') && is_dir($file)) { if (true !== @rmdir($file)) { - throw new IOException(sprintf('Failed to remove file %s', $file)); + $e = new IOException(sprintf('Failed to remove file %s', $file)); + $e->setPath($file); + throw $e; } } else { if (true !== @unlink($file)) { - throw new IOException(sprintf('Failed to remove file %s', $file)); + $e = new IOException(sprintf('Failed to remove file %s', $file)); + $e->setPath($file); + throw $e; } } } @@ -174,7 +188,9 @@ class Filesystem $this->chmod(new \FilesystemIterator($file), $mode, $umask, true); } if (true !== @chmod($file, $mode & ~$umask)) { - throw new IOException(sprintf('Failed to chmod file %s', $file)); + $e = new IOException(sprintf('Failed to chmod file %s', $file)); + $e->setPath($file); + throw $e; } } } @@ -196,11 +212,15 @@ class Filesystem } if (is_link($file) && function_exists('lchown')) { if (true !== @lchown($file, $user)) { - throw new IOException(sprintf('Failed to chown file %s', $file)); + $e = new IOException(sprintf('Failed to chown file %s', $file)); + $e->setPath($file); + throw $e; } } else { if (true !== @chown($file, $user)) { - throw new IOException(sprintf('Failed to chown file %s', $file)); + $e = new IOException(sprintf('Failed to chown file %s', $file)); + $e->setPath($file); + throw $e; } } } @@ -223,11 +243,15 @@ class Filesystem } if (is_link($file) && function_exists('lchgrp')) { if (true !== @lchgrp($file, $group)) { - throw new IOException(sprintf('Failed to chgrp file %s', $file)); + $e = new IOException(sprintf('Failed to chgrp file %s', $file)); + $e->setPath($file); + throw $e; } } else { if (true !== @chgrp($file, $group)) { - throw new IOException(sprintf('Failed to chgrp file %s', $file)); + $e = new IOException(sprintf('Failed to chgrp file %s', $file)); + $e->setPath($file); + throw $e; } } } @@ -247,11 +271,15 @@ class Filesystem { // we check that target does not exist if (!$overwrite && is_readable($target)) { - throw new IOException(sprintf('Cannot rename because the target "%s" already exist.', $target)); + $e = new IOException(sprintf('Cannot rename because the target "%s" already exists.', $target)); + $e->setPath($target); + throw $e; } if (true !== @rename($origin, $target)) { - throw new IOException(sprintf('Cannot rename "%s" to "%s".', $origin, $target)); + $e = new IOException(sprintf('Cannot rename "%s" to "%s".', $origin, $target)); + $e->setPath($target); + throw $e; } } @@ -288,10 +316,14 @@ class Filesystem $report = error_get_last(); if (is_array($report)) { if (defined('PHP_WINDOWS_VERSION_MAJOR') && false !== strpos($report['message'], 'error code(1314)')) { - throw new IOException('Unable to create symlink due to error code 1314: \'A required privilege is not held by the client\'. Do you have the required Administrator-rights?'); + $e = new IOException('Unable to create symlink due to error code 1314: \'A required privilege is not held by the client\'. Do you have the required Administrator-rights?'); + $e->setPath(null); + throw $e; } } - throw new IOException(sprintf('Failed to create symbolic link from %s to %s', $originDir, $targetDir)); + $e = new IOException(sprintf('Failed to create symbolic link from %s to %s', $originDir, $targetDir)); + $e->setPath($targetDir); + throw $e; } } } @@ -389,7 +421,9 @@ class Filesystem } elseif (is_dir($file)) { $this->mkdir($target); } else { - throw new IOException(sprintf('Unable to guess "%s" file type.', $file)); + $e = new IOException(sprintf('Unable to guess "%s" file type.', $file)); + $e->setPath($file); + throw $e; } } else { if (is_link($file)) { @@ -399,7 +433,9 @@ class Filesystem } elseif (is_file($file)) { $this->copy($file, $target, isset($options['override']) ? $options['override'] : false); } else { - throw new IOException(sprintf('Unable to guess "%s" file type.', $file)); + $e = new IOException(sprintf('Unable to guess "%s" file type.', $file)); + $e->setPath($file); + throw $e; } } } @@ -427,20 +463,6 @@ class Filesystem return false; } - /** - * @param mixed $files - * - * @return \Traversable - */ - private function toIterator($files) - { - if (!$files instanceof \Traversable) { - $files = new \ArrayObject(is_array($files) ? $files : array($files)); - } - - return $files; - } - /** * Atomically dumps content into a file. * @@ -456,16 +478,34 @@ class Filesystem if (!is_dir($dir)) { $this->mkdir($dir); } elseif (!is_writable($dir)) { - throw new IOException(sprintf('Unable to write to the "%s" directory.', $dir)); + $e = new IOException(sprintf('Unable to write to the "%s" directory.', $dir)); + $e->setPath($dir); + throw $e; } $tmpFile = tempnam($dir, basename($filename)); if (false === @file_put_contents($tmpFile, $content)) { - throw new IOException(sprintf('Failed to write file "%s".', $filename)); + $e = new IOException(sprintf('Failed to write file "%s".', $filename)); + $e->setPath($filename); + throw $e; } $this->rename($tmpFile, $filename, true); $this->chmod($filename, $mode); } + + /** + * @param mixed $files + * + * @return \Traversable + */ + private function toIterator($files) + { + if (!$files instanceof \Traversable) { + $files = new \ArrayObject(is_array($files) ? $files : array($files)); + } + + return $files; + } } diff --git a/src/Symfony/Component/Filesystem/Tests/ExceptionTest.php b/src/Symfony/Component/Filesystem/Tests/ExceptionTest.php new file mode 100644 index 0000000000..ead75a2eb6 --- /dev/null +++ b/src/Symfony/Component/Filesystem/Tests/ExceptionTest.php @@ -0,0 +1,51 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Filesystem\Tests; + +use Symfony\Component\Filesystem\Exception\IOException; +use Symfony\Component\Filesystem\Exception\FileNotFoundException; + +/** + * Test class for Filesystem. + */ +class ExceptionTest extends \PHPUnit_Framework_TestCase +{ + public function testSetPath() + { + $e = new IOException(); + $e->setPath('/foo'); + $reflection = new \ReflectionProperty($e, 'path'); + $reflection->setAccessible(true); + + $this->assertEquals('/foo', $reflection->getValue($e), 'The path should get stored in the "path" property'); + } + + public function testGetPath() + { + $e = new IOException(); + $e->setPath('/foo'); + $this->assertEquals('/foo', $e->getPath(), 'The pass should be returned.'); + } + + public function testGeneratedMessage() + { + $e = new FileNotFoundException('/foo'); + $this->assertEquals('/foo', $e->getPath()); + $this->assertEquals('File "/foo" could not be found', $e->getMessage(), 'A message should be generated.'); + } + + public function testCustomMessage() + { + $e = new FileNotFoundException('/foo', 'bar'); + $this->assertEquals('bar', $e->getMessage(), 'A custom message should be possible still.'); + } +} \ No newline at end of file From c2e43d0aa4f361f034912ef72849c26f289a960e Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Fri, 27 Sep 2013 16:53:24 +0200 Subject: [PATCH 2/2] [Filesystem] removed getPath() on Exceptions and cleaned up CS and error messages --- .../Exception/ExceptionInterface.php | 1 - .../Exception/FileNotFoundException.php | 15 ++-- .../Filesystem/Exception/IOException.php | 10 +-- .../Exception/IOExceptionInterface.php | 2 +- .../Component/Filesystem/Filesystem.php | 79 +++++-------------- .../Filesystem/Tests/ExceptionTest.php | 27 +++---- 6 files changed, 46 insertions(+), 88 deletions(-) diff --git a/src/Symfony/Component/Filesystem/Exception/ExceptionInterface.php b/src/Symfony/Component/Filesystem/Exception/ExceptionInterface.php index bc9748d752..c212e664d4 100644 --- a/src/Symfony/Component/Filesystem/Exception/ExceptionInterface.php +++ b/src/Symfony/Component/Filesystem/Exception/ExceptionInterface.php @@ -20,5 +20,4 @@ namespace Symfony\Component\Filesystem\Exception; */ interface ExceptionInterface { - } diff --git a/src/Symfony/Component/Filesystem/Exception/FileNotFoundException.php b/src/Symfony/Component/Filesystem/Exception/FileNotFoundException.php index 242ff070e5..15533db408 100644 --- a/src/Symfony/Component/Filesystem/Exception/FileNotFoundException.php +++ b/src/Symfony/Component/Filesystem/Exception/FileNotFoundException.php @@ -14,18 +14,21 @@ namespace Symfony\Component\Filesystem\Exception; /** * Exception class thrown when a file couldn't be found * + * @author Fabien Potencier * @author Christian Gärtner */ class FileNotFoundException extends IOException { - public function __construct($path, $message = null, $code = 0, \Exception $previous = null) + public function __construct($message = null, $code = 0, \Exception $previous = null, $path = null) { - if ($message === null) { - $message = sprintf('File "%s" could not be found', $path); + if (null === $message) { + if (null === $path) { + $message = 'File could not be found.'; + } else { + $message = sprintf('File "%s" could not be found.', $path); + } } - $this->setPath($path); - - parent::__construct($message, $code, $previous); + parent::__construct($message, $code, $previous, $path); } } diff --git a/src/Symfony/Component/Filesystem/Exception/IOException.php b/src/Symfony/Component/Filesystem/Exception/IOException.php index e9f12371da..4b551af71b 100644 --- a/src/Symfony/Component/Filesystem/Exception/IOException.php +++ b/src/Symfony/Component/Filesystem/Exception/IOException.php @@ -16,21 +16,19 @@ namespace Symfony\Component\Filesystem\Exception; * * @author Romain Neutron * @author Christian Gärtner + * @author Fabien Potencier * * @api */ class IOException extends \RuntimeException implements IOExceptionInterface { - private $path; - /** - * Set the path associated with this IOException - * @param string $path The path - */ - public function setPath($path) + public function __construct($message, $code = 0, \Exception $previous = null, $path = null) { $this->path = $path; + + parent::__construct($message, $code, $previous); } /** diff --git a/src/Symfony/Component/Filesystem/Exception/IOExceptionInterface.php b/src/Symfony/Component/Filesystem/Exception/IOExceptionInterface.php index 41d8d3bdee..de9f3e714a 100644 --- a/src/Symfony/Component/Filesystem/Exception/IOExceptionInterface.php +++ b/src/Symfony/Component/Filesystem/Exception/IOExceptionInterface.php @@ -20,7 +20,7 @@ interface IOExceptionInterface extends ExceptionInterface { /** * Returns the associated path for the exception - * + * * @return string The path. */ public function getPath(); diff --git a/src/Symfony/Component/Filesystem/Filesystem.php b/src/Symfony/Component/Filesystem/Filesystem.php index f591780ff7..9e45d2ba3b 100644 --- a/src/Symfony/Component/Filesystem/Filesystem.php +++ b/src/Symfony/Component/Filesystem/Filesystem.php @@ -38,7 +38,7 @@ class Filesystem public function copy($originFile, $targetFile, $override = false) { if (stream_is_local($originFile) && !is_file($originFile)) { - throw new FileNotFoundException($originFile, sprintf('Failed to copy %s because file does not exist', $originFile)); + throw new FileNotFoundException(sprintf('Failed to copy "%s" because file does not exist.', $originFile), 0, null, $originFile); } $this->mkdir(dirname($targetFile)); @@ -59,9 +59,7 @@ class Filesystem unset($source, $target); if (!is_file($targetFile)) { - $e = new IOException(sprintf('Failed to copy %s to %s', $originFile, $targetFile)); - $e->setPath($originFile); - throw $e; + throw new IOException(sprintf('Failed to copy "%s" to "%s".', $originFile, $targetFile), 0, null, $originFile); } } } @@ -82,9 +80,7 @@ class Filesystem } if (true !== @mkdir($dir, $mode, true)) { - $e = new IOException(sprintf('Failed to create %s', $dir)); - $e->setPath($dir); - throw $e; + throw new IOException(sprintf('Failed to create "%s".', $dir), 0, null, $dir); } } } @@ -121,9 +117,7 @@ class Filesystem foreach ($this->toIterator($files) as $file) { $touch = $time ? @touch($file, $time, $atime) : @touch($file); if (true !== $touch) { - $e = new IOException(sprintf('Failed to touch %s', $file)); - $e->setPath($file); - throw $e; + throw new IOException(sprintf('Failed to touch "%s".', $file), 0, null, $file); } } } @@ -148,23 +142,17 @@ class Filesystem $this->remove(new \FilesystemIterator($file)); if (true !== @rmdir($file)) { - $e = new IOException(sprintf('Failed to remove directory %s', $file)); - $e->setPath($file); - throw $e; + throw new IOException(sprintf('Failed to remove directory "%s".', $file), 0, null, $file); } } else { // https://bugs.php.net/bug.php?id=52176 if (defined('PHP_WINDOWS_VERSION_MAJOR') && is_dir($file)) { if (true !== @rmdir($file)) { - $e = new IOException(sprintf('Failed to remove file %s', $file)); - $e->setPath($file); - throw $e; + throw new IOException(sprintf('Failed to remove file "%s".', $file), 0, null, $file); } } else { if (true !== @unlink($file)) { - $e = new IOException(sprintf('Failed to remove file %s', $file)); - $e->setPath($file); - throw $e; + throw new IOException(sprintf('Failed to remove file "%s".', $file), 0, null, $file); } } } @@ -188,9 +176,7 @@ class Filesystem $this->chmod(new \FilesystemIterator($file), $mode, $umask, true); } if (true !== @chmod($file, $mode & ~$umask)) { - $e = new IOException(sprintf('Failed to chmod file %s', $file)); - $e->setPath($file); - throw $e; + throw new IOException(sprintf('Failed to chmod file "%s".', $file), 0, null, $file); } } } @@ -212,15 +198,11 @@ class Filesystem } if (is_link($file) && function_exists('lchown')) { if (true !== @lchown($file, $user)) { - $e = new IOException(sprintf('Failed to chown file %s', $file)); - $e->setPath($file); - throw $e; + throw new IOException(sprintf('Failed to chown file "%s".', $file), 0, null, $file); } } else { if (true !== @chown($file, $user)) { - $e = new IOException(sprintf('Failed to chown file %s', $file)); - $e->setPath($file); - throw $e; + throw new IOException(sprintf('Failed to chown file "%s".', $file), 0, null, $file); } } } @@ -243,15 +225,11 @@ class Filesystem } if (is_link($file) && function_exists('lchgrp')) { if (true !== @lchgrp($file, $group)) { - $e = new IOException(sprintf('Failed to chgrp file %s', $file)); - $e->setPath($file); - throw $e; + throw new IOException(sprintf('Failed to chgrp file "%s".', $file), 0, null, $file); } } else { if (true !== @chgrp($file, $group)) { - $e = new IOException(sprintf('Failed to chgrp file %s', $file)); - $e->setPath($file); - throw $e; + throw new IOException(sprintf('Failed to chgrp file "%s".', $file), 0, null, $file); } } } @@ -271,15 +249,11 @@ class Filesystem { // we check that target does not exist if (!$overwrite && is_readable($target)) { - $e = new IOException(sprintf('Cannot rename because the target "%s" already exists.', $target)); - $e->setPath($target); - throw $e; + throw new IOException(sprintf('Cannot rename because the target "%s" already exists.', $target), 0, null, $target); } if (true !== @rename($origin, $target)) { - $e = new IOException(sprintf('Cannot rename "%s" to "%s".', $origin, $target)); - $e->setPath($target); - throw $e; + throw new IOException(sprintf('Cannot rename "%s" to "%s".', $origin, $target), 0, null, $target); } } @@ -316,14 +290,11 @@ class Filesystem $report = error_get_last(); if (is_array($report)) { if (defined('PHP_WINDOWS_VERSION_MAJOR') && false !== strpos($report['message'], 'error code(1314)')) { - $e = new IOException('Unable to create symlink due to error code 1314: \'A required privilege is not held by the client\'. Do you have the required Administrator-rights?'); - $e->setPath(null); - throw $e; + throw new IOException('Unable to create symlink due to error code 1314: \'A required privilege is not held by the client\'. Do you have the required Administrator-rights?'); } } - $e = new IOException(sprintf('Failed to create symbolic link from %s to %s', $originDir, $targetDir)); - $e->setPath($targetDir); - throw $e; + + throw new IOException(sprintf('Failed to create symbolic link from "%s" to "%s".', $originDir, $targetDir), 0, null, $targetDir); } } } @@ -421,9 +392,7 @@ class Filesystem } elseif (is_dir($file)) { $this->mkdir($target); } else { - $e = new IOException(sprintf('Unable to guess "%s" file type.', $file)); - $e->setPath($file); - throw $e; + throw new IOException(sprintf('Unable to guess "%s" file type.', $file), 0, null, $file); } } else { if (is_link($file)) { @@ -433,9 +402,7 @@ class Filesystem } elseif (is_file($file)) { $this->copy($file, $target, isset($options['override']) ? $options['override'] : false); } else { - $e = new IOException(sprintf('Unable to guess "%s" file type.', $file)); - $e->setPath($file); - throw $e; + throw new IOException(sprintf('Unable to guess "%s" file type.', $file), 0, null, $file); } } } @@ -478,17 +445,13 @@ class Filesystem if (!is_dir($dir)) { $this->mkdir($dir); } elseif (!is_writable($dir)) { - $e = new IOException(sprintf('Unable to write to the "%s" directory.', $dir)); - $e->setPath($dir); - throw $e; + throw new IOException(sprintf('Unable to write to the "%s" directory.', $dir), 0, null, $dir); } $tmpFile = tempnam($dir, basename($filename)); if (false === @file_put_contents($tmpFile, $content)) { - $e = new IOException(sprintf('Failed to write file "%s".', $filename)); - $e->setPath($filename); - throw $e; + throw new IOException(sprintf('Failed to write file "%s".', $filename), 0, null, $filename); } $this->rename($tmpFile, $filename, true); diff --git a/src/Symfony/Component/Filesystem/Tests/ExceptionTest.php b/src/Symfony/Component/Filesystem/Tests/ExceptionTest.php index ead75a2eb6..53bd8db76a 100644 --- a/src/Symfony/Component/Filesystem/Tests/ExceptionTest.php +++ b/src/Symfony/Component/Filesystem/Tests/ExceptionTest.php @@ -19,33 +19,28 @@ use Symfony\Component\Filesystem\Exception\FileNotFoundException; */ class ExceptionTest extends \PHPUnit_Framework_TestCase { - public function testSetPath() - { - $e = new IOException(); - $e->setPath('/foo'); - $reflection = new \ReflectionProperty($e, 'path'); - $reflection->setAccessible(true); - - $this->assertEquals('/foo', $reflection->getValue($e), 'The path should get stored in the "path" property'); - } - public function testGetPath() { - $e = new IOException(); - $e->setPath('/foo'); + $e = new IOException('', 0, null, '/foo'); $this->assertEquals('/foo', $e->getPath(), 'The pass should be returned.'); } public function testGeneratedMessage() { - $e = new FileNotFoundException('/foo'); + $e = new FileNotFoundException(null, 0, null, '/foo'); $this->assertEquals('/foo', $e->getPath()); - $this->assertEquals('File "/foo" could not be found', $e->getMessage(), 'A message should be generated.'); + $this->assertEquals('File "/foo" could not be found.', $e->getMessage(), 'A message should be generated.'); + } + + public function testGeneratedMessageWithoutPath() + { + $e = new FileNotFoundException(); + $this->assertEquals('File could not be found.', $e->getMessage(), 'A message should be generated.'); } public function testCustomMessage() { - $e = new FileNotFoundException('/foo', 'bar'); + $e = new FileNotFoundException('bar', 0, null, '/foo'); $this->assertEquals('bar', $e->getMessage(), 'A custom message should be possible still.'); } -} \ No newline at end of file +}