mirror of
https://github.com/matthiasmullie/minify.git
synced 2025-02-22 20:32:46 +01:00
Merge branch 'feature/test-exceptions'
This commit is contained in:
commit
08b5c43285
@ -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.');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
*/
|
||||
|
Loading…
x
Reference in New Issue
Block a user