From b2e579a811db4a941846acc3141edb7974c52683 Mon Sep 17 00:00:00 2001 From: Kotlyar Maksim Date: Thu, 5 Jun 2014 14:14:17 +0000 Subject: [PATCH] [stream] throws exception when content cannot be read. --- Binary/Loader/StreamLoader.php | 14 ++++++++++++-- Tests/Binary/Loader/StreamLoaderTest.php | 8 ++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/Binary/Loader/StreamLoader.php b/Binary/Loader/StreamLoader.php index d5d81d30b..d1ae5abc2 100644 --- a/Binary/Loader/StreamLoader.php +++ b/Binary/Loader/StreamLoader.php @@ -52,12 +52,22 @@ public function find($path) * file_exists() is not used as not all wrappers support stat() to actually check for existing resources. */ if (($this->context && !$resource = @fopen($name, 'r', null, $this->context)) || !$resource = @fopen($name, 'r')) { - throw new NotLoadableException('Source image not found.'); + throw new NotLoadableException(sprintf('Source image %s not found.', $name)); } // Closing the opened stream to avoid locking of the resource to find. fclose($resource); - return file_get_contents($name, null, $this->context); + try { + $content = file_get_contents($name, null, $this->context); + } catch (\Exception $e) { + throw new NotLoadableException(sprintf('Source image %s could not be loaded.', $name, $e)); + } + + if (false === $content) { + throw new NotLoadableException(sprintf('Source image %s could not be loaded.', $name)); + } + + return $content; } } diff --git a/Tests/Binary/Loader/StreamLoaderTest.php b/Tests/Binary/Loader/StreamLoaderTest.php index 57216fabc..f3412ebb7 100644 --- a/Tests/Binary/Loader/StreamLoaderTest.php +++ b/Tests/Binary/Loader/StreamLoaderTest.php @@ -14,10 +14,14 @@ public function testThrowsIfInvalidPathGivenOnFind() { $loader = new StreamLoader('file://'); + $path = $this->tempDir.'/invalid.jpeg'; + $this->setExpectedException( - 'Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException' + 'Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException', + 'Source image file://'.$path.' not found.' ); - $loader->find($this->tempDir.'/invalid.jpeg'); + + $loader->find($path); } public function testReturnImageContentOnFind()