mirror of
https://github.com/mrclay/minify.git
synced 2025-08-07 06:36:29 +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.
|
||||
*
|
||||
* @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
|
||||
* parameter is a string.
|
||||
@@ -186,7 +186,7 @@ class Minify {
|
||||
// check client cache
|
||||
require_once 'HTTP/ConditionalGet.php';
|
||||
$cgOptions = array(
|
||||
'lastModifiedTime' => self::$_options['lastModifiedTime']
|
||||
'lastModifiedTime' => self::$_options['lastModifiedTime']
|
||||
,'isPublic' => self::$_options['isPublic']
|
||||
);
|
||||
if (self::$_options['maxAge'] > 0) {
|
||||
@@ -201,7 +201,7 @@ class Minify {
|
||||
} else {
|
||||
return array(
|
||||
'success' => true
|
||||
,'statusCode' => 304
|
||||
,'statusCode' => 304
|
||||
,'content' => ''
|
||||
,'headers' => $cg->getHeaders()
|
||||
);
|
||||
@@ -309,7 +309,7 @@ class Minify {
|
||||
,'content' => $cacheIsReady
|
||||
? self::$_cache->fetch($fullCacheId)
|
||||
: $content
|
||||
,'headers' => $headers
|
||||
,'headers' => $headers
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -402,9 +402,10 @@ class Minify {
|
||||
protected static function _combineMinify() {
|
||||
$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)
|
||||
? ';'
|
||||
? "\n;"
|
||||
: '';
|
||||
// allow the user to pass a particular array of options to each
|
||||
// minifier (designated by type). source objects may still override
|
||||
@@ -474,5 +475,5 @@ class Minify {
|
||||
,self::$_options['minifierOptions']
|
||||
,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()
|
||||
{
|
||||
global $thisDir;
|
||||
|
||||
|
||||
$minifyTestPath = dirname(__FILE__) . '/_test_files/minify';
|
||||
$tomorrow = $_SERVER['REQUEST_TIME'] + 86400;
|
||||
$lastModified = $_SERVER['REQUEST_TIME'] - 86400;
|
||||
|
||||
|
||||
// Test 304 response
|
||||
|
||||
|
||||
// simulate conditional headers
|
||||
$_SERVER['HTTP_IF_NONE_MATCH'] = "\"{$lastModified}pub\"";
|
||||
$_SERVER['HTTP_IF_MODIFIED_SINCE'] = gmdate('D, d M Y H:i:s \G\M\T', $lastModified);
|
||||
|
||||
|
||||
$expected = array (
|
||||
'success' => true
|
||||
,'statusCode' => 304
|
||||
'success' => true
|
||||
,'statusCode' => 304
|
||||
,'content' => '',
|
||||
'headers' => array(
|
||||
'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),
|
||||
'ETag' => "\"{$lastModified}pub\"",
|
||||
'Cache-Control' => 'max-age=1800, public, must-revalidate',
|
||||
'_responseCode' => 'HTTP/1.0 304 Not Modified',
|
||||
'Cache-Control' => 'max-age=1800, public, must-revalidate',
|
||||
'_responseCode' => 'HTTP/1.0 304 Not Modified',
|
||||
)
|
||||
);
|
||||
$output = Minify::serve('Files', array(
|
||||
@@ -38,38 +38,37 @@ function test_Minify()
|
||||
,'encodeOutput' => false
|
||||
));
|
||||
$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";
|
||||
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(
|
||||
//! class_exists('Cache_Lite_File', false)
|
||||
! class_exists('HTTP_Encoder', false)
|
||||
&& ! class_exists('Minify_CSS', false)
|
||||
&& ! class_exists('Minify_Cache', false)
|
||||
,'Encoder.php, CSS.php, Cache.php not loaded'
|
||||
);
|
||||
|
||||
|
||||
// Test minifying JS and serving with Expires header
|
||||
|
||||
|
||||
$content = preg_replace('/\\r\\n?/', "\n", file_get_contents($minifyTestPath . '/minified.js'));
|
||||
$lastModified = max(
|
||||
filemtime($minifyTestPath . '/email.js')
|
||||
,filemtime($minifyTestPath . '/QueryString.js')
|
||||
);
|
||||
$expected = array(
|
||||
'success' => true
|
||||
'success' => true
|
||||
,'statusCode' => 200
|
||||
// Minify_Javascript always converts to \n line endings
|
||||
,'content' => $content
|
||||
,'headers' => array (
|
||||
'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),
|
||||
'ETag' => "\"{$lastModified}pub\"",
|
||||
'Cache-Control' => 'max-age=86400, public, must-revalidate',
|
||||
'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),
|
||||
'ETag' => "\"{$lastModified}pub\"",
|
||||
'Cache-Control' => 'max-age=86400, public, must-revalidate',
|
||||
'Content-Length' => strlen($content),
|
||||
'Content-Type' => 'application/x-javascript; charset=UTF-8',
|
||||
)
|
||||
@@ -83,39 +82,61 @@ function test_Minify()
|
||||
,'maxAge' => 86400
|
||||
,'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";
|
||||
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
|
||||
|
||||
|
||||
Minify::setCache();
|
||||
|
||||
|
||||
// don't allow conditional headers
|
||||
unset($_SERVER['HTTP_IF_NONE_MATCH'], $_SERVER['HTTP_IF_MODIFIED_SINCE']);
|
||||
|
||||
|
||||
$pathToWebTest = str_replace(
|
||||
DIRECTORY_SEPARATOR
|
||||
,'/'
|
||||
,substr(dirname(__FILE__), strlen(realpath($_SERVER['DOCUMENT_ROOT'])))
|
||||
);
|
||||
$expectedContent = str_replace(
|
||||
'%PATH_TO_WEB_TEST%'
|
||||
'%PATH_TO_WEB_TEST%'
|
||||
,$pathToWebTest
|
||||
,file_get_contents($minifyTestPath . '/minified.css')
|
||||
);
|
||||
|
||||
|
||||
$expected = array(
|
||||
'success' => true
|
||||
,'statusCode' => 200
|
||||
,'content' => $expectedContent
|
||||
'success' => true
|
||||
,'statusCode' => 200
|
||||
,'content' => $expectedContent
|
||||
,'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\"",
|
||||
'Cache-Control' => 'max-age=0, public, must-revalidate',
|
||||
'Content-Length' => strlen($expectedContent),
|
||||
@@ -132,12 +153,12 @@ function test_Minify()
|
||||
,'encodeOutput' => false
|
||||
,'maxAge' => false
|
||||
));
|
||||
|
||||
|
||||
$passed = assertTrue($expected === $output, 'Minify : CSS and Etag/Last-Modified');
|
||||
if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) {
|
||||
echo "\nOutput: " .var_export($output, 1). "\n\n";
|
||||
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