From ddfe262a625f7e0b1c924ecb78e809a2c5a62511 Mon Sep 17 00:00:00 2001 From: Gino Pane Date: Tue, 9 Feb 2016 15:53:15 +0300 Subject: [PATCH] Tests for filesystem operations. - Split Minify::save() to easily testable methods; - Add tests. --- src/Minify.php | 48 ++++++++++++++++++++++++++++++--------- tests/js/AbstractTest.php | 34 +++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 11 deletions(-) diff --git a/src/Minify.php b/src/Minify.php index f47d50b..a69687e 100644 --- a/src/Minify.php +++ b/src/Minify.php @@ -172,17 +172,10 @@ abstract class Minify */ protected function save($content, $path) { - // create file & open for writing - if (($handler = @fopen($path, 'w')) === false) { - throw new Exception('The file "'.$path.'" could not be opened. Check if PHP has enough permissions.'); - } + $handler = $this->openFileForWriting($path); - // write to file - if (@fwrite($handler, $content) === false) { - throw new Exception('The file "'.$path.'" could not be written to. Check if PHP has enough permissions.'); - } + $this->writeToFile($handler, $content); - // close the file @fclose($handler); } @@ -316,7 +309,7 @@ abstract class Minify * placeholder text, so we've rid all strings from characters that may be * misinterpreted. Original string content will be saved in $this->extracted * and after doing all other minifying, we can restore the original content - * via restoreStrings() + * via restoreStrings(). * * @param string[optional] $chars */ @@ -382,7 +375,7 @@ abstract class Minify /** * - * Check if the path is a regular file and can be read + * Check if the path is a regular file and can be read. * * @param string $path * @return bool @@ -390,4 +383,37 @@ abstract class Minify protected function canImportFile($path) { return (strlen($path) < PHP_MAXPATHLEN && is_file($path) && is_readable($path)); } + + /** + * + * Attempts to open file specified by $path for writing. + * + * @param string $path The path to the file. + * @return resource Specifier for the target file. + * + * @throws Exception + */ + protected function openFileForWriting($path) { + if (($handler = @fopen($path, 'w')) === false) { + throw new Exception('The file "'.$path.'" could not be opened for writing. Check if PHP has enough permissions.'); + } + + return $handler; + } + + /** + * + * Attempts to write $content to the file specified by $handler. $path is used for printing exceptions. + * + * @param resource $handler The resource to write to. + * @param string $content The content to write. + * @param string $path The path to the file (for exception printing only). + * + * @throws Exception + */ + protected function writeToFile($handler, $content, $path = '') { + if (@fwrite($handler, $content) === false) { + throw new Exception('The file "'.$path.'" could not be written to. Check your disk space and file permissions.'); + } + } } diff --git a/tests/js/AbstractTest.php b/tests/js/AbstractTest.php index e00e6ce..cde5e93 100644 --- a/tests/js/AbstractTest.php +++ b/tests/js/AbstractTest.php @@ -114,6 +114,40 @@ class CommonTest extends PHPUnit_Framework_TestCase $this->assertEquals(file_get_contents($savePath), $content); } + /** + * @test + * + * @expectedException Exception + */ + public function checkFileOpenFail() + { + $minifier = new Minify\JS(); + $wrongPath = ''; + + $object = new ReflectionObject($minifier); + $method = $object->getMethod('openFileForWriting'); + $method->setAccessible(true); + + $method->invokeArgs($minifier, array($wrongPath)); + } + + /** + * @test + * + * @expectedException Exception + */ + public function checkFileWriteFail() + { + $minifier = new Minify\JS(); + $wrongPath = ''; + + $object = new ReflectionObject($minifier); + $method = $object->getMethod('writeToFile'); + $method->setAccessible(true); + + $method->invokeArgs($minifier, array($wrongPath, '')); + } + /** * @test */