mirror of
https://github.com/mrclay/minify.git
synced 2025-08-11 08:34:19 +02:00
Minify.php : fix for Issue 73 and test
This commit is contained in:
@@ -56,7 +56,7 @@ class Minify {
|
|||||||
* need to recombine files, minify and encode the output.
|
* need to recombine files, minify and encode the output.
|
||||||
*
|
*
|
||||||
* @param mixed $cache object with identical interface as Minify_Cache_File or
|
* @param mixed $cache object with identical interface as Minify_Cache_File or
|
||||||
* a directory path. (default = '')
|
* a directory path, or null to disable caching. (default = '')
|
||||||
*
|
*
|
||||||
* @param bool $fileLocking (default = true) This only applies if the first
|
* @param bool $fileLocking (default = true) This only applies if the first
|
||||||
* parameter is a string.
|
* parameter is a string.
|
||||||
@@ -186,7 +186,7 @@ class Minify {
|
|||||||
// check client cache
|
// check client cache
|
||||||
require_once 'HTTP/ConditionalGet.php';
|
require_once 'HTTP/ConditionalGet.php';
|
||||||
$cgOptions = array(
|
$cgOptions = array(
|
||||||
'lastModifiedTime' => self::$_options['lastModifiedTime']
|
'lastModifiedTime' => self::$_options['lastModifiedTime']
|
||||||
,'isPublic' => self::$_options['isPublic']
|
,'isPublic' => self::$_options['isPublic']
|
||||||
);
|
);
|
||||||
if (self::$_options['maxAge'] > 0) {
|
if (self::$_options['maxAge'] > 0) {
|
||||||
@@ -201,7 +201,7 @@ class Minify {
|
|||||||
} else {
|
} else {
|
||||||
return array(
|
return array(
|
||||||
'success' => true
|
'success' => true
|
||||||
,'statusCode' => 304
|
,'statusCode' => 304
|
||||||
,'content' => ''
|
,'content' => ''
|
||||||
,'headers' => $cg->getHeaders()
|
,'headers' => $cg->getHeaders()
|
||||||
);
|
);
|
||||||
@@ -309,7 +309,7 @@ class Minify {
|
|||||||
,'content' => $cacheIsReady
|
,'content' => $cacheIsReady
|
||||||
? self::$_cache->fetch($fullCacheId)
|
? self::$_cache->fetch($fullCacheId)
|
||||||
: $content
|
: $content
|
||||||
,'headers' => $headers
|
,'headers' => $headers
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -402,9 +402,10 @@ class Minify {
|
|||||||
protected static function _combineMinify() {
|
protected static function _combineMinify() {
|
||||||
$type = self::$_options['contentType']; // ease readability
|
$type = self::$_options['contentType']; // ease readability
|
||||||
|
|
||||||
// when combining scripts, make sure all statements separated
|
// when combining scripts, make sure all statements separated and
|
||||||
|
// trailing single line comment is terminated
|
||||||
$implodeSeparator = ($type === self::TYPE_JS)
|
$implodeSeparator = ($type === self::TYPE_JS)
|
||||||
? ';'
|
? "\n;"
|
||||||
: '';
|
: '';
|
||||||
// allow the user to pass a particular array of options to each
|
// allow the user to pass a particular array of options to each
|
||||||
// minifier (designated by type). source objects may still override
|
// minifier (designated by type). source objects may still override
|
||||||
@@ -474,5 +475,5 @@ class Minify {
|
|||||||
,self::$_options['minifierOptions']
|
,self::$_options['minifierOptions']
|
||||||
,self::$_options['postprocessor']
|
,self::$_options['postprocessor']
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
2
min_unit_tests/_test_files/minify/issue73_1.js
Normal file
2
min_unit_tests/_test_files/minify/issue73_1.js
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
|
||||||
|
// end in comment
|
3
min_unit_tests/_test_files/minify/issue73_2.js
Normal file
3
min_unit_tests/_test_files/minify/issue73_2.js
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
function h() {
|
||||||
|
|
||||||
|
}
|
@@ -8,27 +8,27 @@ require_once 'Minify.php';
|
|||||||
function test_Minify()
|
function test_Minify()
|
||||||
{
|
{
|
||||||
global $thisDir;
|
global $thisDir;
|
||||||
|
|
||||||
$minifyTestPath = dirname(__FILE__) . '/_test_files/minify';
|
$minifyTestPath = dirname(__FILE__) . '/_test_files/minify';
|
||||||
$tomorrow = $_SERVER['REQUEST_TIME'] + 86400;
|
$tomorrow = $_SERVER['REQUEST_TIME'] + 86400;
|
||||||
$lastModified = $_SERVER['REQUEST_TIME'] - 86400;
|
$lastModified = $_SERVER['REQUEST_TIME'] - 86400;
|
||||||
|
|
||||||
// Test 304 response
|
// Test 304 response
|
||||||
|
|
||||||
// simulate conditional headers
|
// simulate conditional headers
|
||||||
$_SERVER['HTTP_IF_NONE_MATCH'] = "\"{$lastModified}pub\"";
|
$_SERVER['HTTP_IF_NONE_MATCH'] = "\"{$lastModified}pub\"";
|
||||||
$_SERVER['HTTP_IF_MODIFIED_SINCE'] = gmdate('D, d M Y H:i:s \G\M\T', $lastModified);
|
$_SERVER['HTTP_IF_MODIFIED_SINCE'] = gmdate('D, d M Y H:i:s \G\M\T', $lastModified);
|
||||||
|
|
||||||
$expected = array (
|
$expected = array (
|
||||||
'success' => true
|
'success' => true
|
||||||
,'statusCode' => 304
|
,'statusCode' => 304
|
||||||
,'content' => '',
|
,'content' => '',
|
||||||
'headers' => array(
|
'headers' => array(
|
||||||
'Expires' => gmdate('D, d M Y H:i:s \G\M\T', $_SERVER['REQUEST_TIME'] + 1800),
|
'Expires' => gmdate('D, d M Y H:i:s \G\M\T', $_SERVER['REQUEST_TIME'] + 1800),
|
||||||
'Last-Modified' => gmdate('D, d M Y H:i:s \G\M\T', $lastModified),
|
'Last-Modified' => gmdate('D, d M Y H:i:s \G\M\T', $lastModified),
|
||||||
'ETag' => "\"{$lastModified}pub\"",
|
'ETag' => "\"{$lastModified}pub\"",
|
||||||
'Cache-Control' => 'max-age=1800, public, must-revalidate',
|
'Cache-Control' => 'max-age=1800, public, must-revalidate',
|
||||||
'_responseCode' => 'HTTP/1.0 304 Not Modified',
|
'_responseCode' => 'HTTP/1.0 304 Not Modified',
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
$output = Minify::serve('Files', array(
|
$output = Minify::serve('Files', array(
|
||||||
@@ -38,38 +38,37 @@ function test_Minify()
|
|||||||
,'encodeOutput' => false
|
,'encodeOutput' => false
|
||||||
));
|
));
|
||||||
$passed = assertTrue($expected === $output, 'Minify : 304 response');
|
$passed = assertTrue($expected === $output, 'Minify : 304 response');
|
||||||
if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) {
|
if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) {
|
||||||
echo "\nOutput: " .var_export($output, 1). "\n\n";
|
echo "\nOutput: " .var_export($output, 1). "\n\n";
|
||||||
if (! $passed) {
|
if (! $passed) {
|
||||||
echo "\n\n\n\n---Expected: " .var_export($expected, 1). "\n\n";
|
echo "\n\n\n\n---Expected: " .var_export($expected, 1). "\n\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
assertTrue(
|
assertTrue(
|
||||||
//! class_exists('Cache_Lite_File', false)
|
|
||||||
! class_exists('HTTP_Encoder', false)
|
! class_exists('HTTP_Encoder', false)
|
||||||
&& ! class_exists('Minify_CSS', false)
|
&& ! class_exists('Minify_CSS', false)
|
||||||
&& ! class_exists('Minify_Cache', false)
|
&& ! class_exists('Minify_Cache', false)
|
||||||
,'Encoder.php, CSS.php, Cache.php not loaded'
|
,'Encoder.php, CSS.php, Cache.php not loaded'
|
||||||
);
|
);
|
||||||
|
|
||||||
// Test minifying JS and serving with Expires header
|
// Test minifying JS and serving with Expires header
|
||||||
|
|
||||||
$content = preg_replace('/\\r\\n?/', "\n", file_get_contents($minifyTestPath . '/minified.js'));
|
$content = preg_replace('/\\r\\n?/', "\n", file_get_contents($minifyTestPath . '/minified.js'));
|
||||||
$lastModified = max(
|
$lastModified = max(
|
||||||
filemtime($minifyTestPath . '/email.js')
|
filemtime($minifyTestPath . '/email.js')
|
||||||
,filemtime($minifyTestPath . '/QueryString.js')
|
,filemtime($minifyTestPath . '/QueryString.js')
|
||||||
);
|
);
|
||||||
$expected = array(
|
$expected = array(
|
||||||
'success' => true
|
'success' => true
|
||||||
,'statusCode' => 200
|
,'statusCode' => 200
|
||||||
// Minify_Javascript always converts to \n line endings
|
// Minify_Javascript always converts to \n line endings
|
||||||
,'content' => $content
|
,'content' => $content
|
||||||
,'headers' => array (
|
,'headers' => array (
|
||||||
'Expires' => gmdate('D, d M Y H:i:s \G\M\T', $tomorrow),
|
'Expires' => gmdate('D, d M Y H:i:s \G\M\T', $tomorrow),
|
||||||
'Last-Modified' => gmdate('D, d M Y H:i:s \G\M\T', $lastModified),
|
'Last-Modified' => gmdate('D, d M Y H:i:s \G\M\T', $lastModified),
|
||||||
'ETag' => "\"{$lastModified}pub\"",
|
'ETag' => "\"{$lastModified}pub\"",
|
||||||
'Cache-Control' => 'max-age=86400, public, must-revalidate',
|
'Cache-Control' => 'max-age=86400, public, must-revalidate',
|
||||||
'Content-Length' => strlen($content),
|
'Content-Length' => strlen($content),
|
||||||
'Content-Type' => 'application/x-javascript; charset=UTF-8',
|
'Content-Type' => 'application/x-javascript; charset=UTF-8',
|
||||||
)
|
)
|
||||||
@@ -83,39 +82,61 @@ function test_Minify()
|
|||||||
,'maxAge' => 86400
|
,'maxAge' => 86400
|
||||||
,'encodeOutput' => false
|
,'encodeOutput' => false
|
||||||
));
|
));
|
||||||
$passed = assertTrue($expected === $output, 'Minify : JS and Expires');
|
|
||||||
|
|
||||||
if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) {
|
$passed = assertTrue($expected === $output, 'Minify : JS and Expires');
|
||||||
|
if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) {
|
||||||
echo "\nOutput: " .var_export($output, 1). "\n\n";
|
echo "\nOutput: " .var_export($output, 1). "\n\n";
|
||||||
if (! $passed) {
|
if (! $passed) {
|
||||||
echo "\n\n\n\n---Expected: " .var_export($expected, 1). "\n\n";
|
echo "\n\n\n\n---Expected: " .var_export($expected, 1). "\n\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// test for Issue 73
|
||||||
|
Minify::setCache(null);
|
||||||
|
|
||||||
|
$expected = ";function h(){}";
|
||||||
|
$output = Minify::serve('Files', array(
|
||||||
|
'files' => array(
|
||||||
|
$minifyTestPath . '/issue73_1.js'
|
||||||
|
,$minifyTestPath . '/issue73_2.js'
|
||||||
|
)
|
||||||
|
,'quiet' => true
|
||||||
|
,'encodeOutput' => false
|
||||||
|
));
|
||||||
|
$output = $output['content'];
|
||||||
|
|
||||||
|
$passed = assertTrue($expected === $output, 'Minify : Issue 73');
|
||||||
|
if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) {
|
||||||
|
if (! $passed) {
|
||||||
|
echo "\n---Output : " .var_export($output, 1). "\n";
|
||||||
|
echo "---Expected: " .var_export($expected, 1). "\n\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Test minifying CSS and responding with Etag/Last-Modified
|
// Test minifying CSS and responding with Etag/Last-Modified
|
||||||
|
|
||||||
Minify::setCache();
|
Minify::setCache();
|
||||||
|
|
||||||
// don't allow conditional headers
|
// don't allow conditional headers
|
||||||
unset($_SERVER['HTTP_IF_NONE_MATCH'], $_SERVER['HTTP_IF_MODIFIED_SINCE']);
|
unset($_SERVER['HTTP_IF_NONE_MATCH'], $_SERVER['HTTP_IF_MODIFIED_SINCE']);
|
||||||
|
|
||||||
$pathToWebTest = str_replace(
|
$pathToWebTest = str_replace(
|
||||||
DIRECTORY_SEPARATOR
|
DIRECTORY_SEPARATOR
|
||||||
,'/'
|
,'/'
|
||||||
,substr(dirname(__FILE__), strlen(realpath($_SERVER['DOCUMENT_ROOT'])))
|
,substr(dirname(__FILE__), strlen(realpath($_SERVER['DOCUMENT_ROOT'])))
|
||||||
);
|
);
|
||||||
$expectedContent = str_replace(
|
$expectedContent = str_replace(
|
||||||
'%PATH_TO_WEB_TEST%'
|
'%PATH_TO_WEB_TEST%'
|
||||||
,$pathToWebTest
|
,$pathToWebTest
|
||||||
,file_get_contents($minifyTestPath . '/minified.css')
|
,file_get_contents($minifyTestPath . '/minified.css')
|
||||||
);
|
);
|
||||||
|
|
||||||
$expected = array(
|
$expected = array(
|
||||||
'success' => true
|
'success' => true
|
||||||
,'statusCode' => 200
|
,'statusCode' => 200
|
||||||
,'content' => $expectedContent
|
,'content' => $expectedContent
|
||||||
,'headers' => array (
|
,'headers' => array (
|
||||||
'Last-Modified' => gmdate('D, d M Y H:i:s \G\M\T', $lastModified),
|
'Last-Modified' => gmdate('D, d M Y H:i:s \G\M\T', $lastModified),
|
||||||
'ETag' => "\"{$lastModified}pub\"",
|
'ETag' => "\"{$lastModified}pub\"",
|
||||||
'Cache-Control' => 'max-age=0, public, must-revalidate',
|
'Cache-Control' => 'max-age=0, public, must-revalidate',
|
||||||
'Content-Length' => strlen($expectedContent),
|
'Content-Length' => strlen($expectedContent),
|
||||||
@@ -132,12 +153,12 @@ function test_Minify()
|
|||||||
,'encodeOutput' => false
|
,'encodeOutput' => false
|
||||||
,'maxAge' => false
|
,'maxAge' => false
|
||||||
));
|
));
|
||||||
|
|
||||||
$passed = assertTrue($expected === $output, 'Minify : CSS and Etag/Last-Modified');
|
$passed = assertTrue($expected === $output, 'Minify : CSS and Etag/Last-Modified');
|
||||||
if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) {
|
if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) {
|
||||||
echo "\nOutput: " .var_export($output, 1). "\n\n";
|
echo "\nOutput: " .var_export($output, 1). "\n\n";
|
||||||
if (! $passed) {
|
if (! $passed) {
|
||||||
echo "\n\n\n\n---Expected: " .var_export($expected, 1). "\n\n";
|
echo "\n\n\n\n---Expected: " .var_export($expected, 1). "\n\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user