1
0
mirror of https://github.com/mrclay/minify.git synced 2025-08-09 07:36:56 +02:00

Improved/added tests, deflate preferred over gzip, improved ConditionalGet docs

This commit is contained in:
Steve Clay
2008-03-04 17:04:09 +00:00
parent f771696d40
commit e2ad73f8be
35 changed files with 398 additions and 212 deletions

View File

@@ -3,31 +3,46 @@
/** /**
* Implement conditional GET via a timestamp or hash of content * Implement conditional GET via a timestamp or hash of content
* *
* E.g. Content from DB with update time:
* <code> * <code>
* // easiest usage * list($updateTime, $content) = getDbUpdateAndContent();
* $cg = new HTTP_ConditionalGet(array( * $cg = new HTTP_ConditionalGet(array(
* 'lastModifiedTime' => filemtime(__FILE__) * 'lastModifiedTime' => $updateTime
* )); * ));
* $cg->sendHeaders(); * $cg->sendHeaders();
* if ($cg->cacheIsValid) { * if ($cg->cacheIsValid) {
* exit(); // done
* }
* // echo content
* </code>
*
*
* <code>
* // better to add content length once it's known
* $cg = new HTTP_ConditionalGet(array(
* 'lastModifiedTime' => filemtime(__FILE__)
* ));
* if ($cg->cacheIsValid) {
* $cg->sendHeaders();
* exit(); * exit();
* } * }
* $content = get_content(); * echo $content;
* $cg->setContentLength(strlen($content)); * </code>
*
* E.g. Content from DB with no update time:
* <code>
* $content = getContentFromDB();
* $cg = new HTTP_ConditionalGet(array(
* 'contentHash' => md5($content)
* ));
* $cg->sendHeaders(); * $cg->sendHeaders();
* if ($cg->cacheIsValid) {
* exit();
* }
* echo $content;
* </code>
*
* E.g. Static content with some static includes:
* <code>
* // before content
* $cg = new HTTP_ConditionalGet(array(
* 'lastUpdateTime' => max(
* filemtime(__FILE__)
* ,filemtime('/path/to/header.inc')
* ,filemtime('/path/to/footer.inc')
* )
* ));
* $cg->sendHeaders();
* if ($cg->cacheIsValid) {
* exit();
* }
* </code> * </code>
*/ */
class HTTP_ConditionalGet { class HTTP_ConditionalGet {
@@ -76,7 +91,7 @@ class HTTP_ConditionalGet {
// allow far-expires header // allow far-expires header
if (isset($spec['setExpires'])) { if (isset($spec['setExpires'])) {
if (is_numeric($spec['setExpires'])) { if (is_numeric($spec['setExpires'])) {
$spec['setExpires'] = self::gmtdate($spec['setExpires']); $spec['setExpires'] = self::gmtDate($spec['setExpires']);
} }
$this->_headers = array( $this->_headers = array(
'Cache-Control' => $scope 'Cache-Control' => $scope
@@ -159,6 +174,21 @@ class HTTP_ConditionalGet {
} }
} }
/**
* Get a GMT formatted date for use in HTTP headers
*
* <code>
* header('Expires: ' . HTTP_ConditionalGet::gmtdate($time));
* </code>
*
* @param int $time unix timestamp
*
* @return string
*/
public static function gmtDate($time) {
return gmdate('D, d M Y H:i:s \G\M\T', $time);
}
protected $_headers = array(); protected $_headers = array();
protected $_lmTime = null; protected $_lmTime = null;
protected $_etag = null; protected $_etag = null;
@@ -172,7 +202,7 @@ class HTTP_ConditionalGet {
protected function _setLastModified($time) { protected function _setLastModified($time) {
$this->_lmTime = (int)$time; $this->_lmTime = (int)$time;
$this->_headers['Last-Modified'] = self::gmtdate($time); $this->_headers['Last-Modified'] = self::gmtDate($time);
} }
/** /**
@@ -221,10 +251,6 @@ class HTTP_ConditionalGet {
// IE has tacked on extra data to this header, strip it // IE has tacked on extra data to this header, strip it
$ifModifiedSince = substr($ifModifiedSince, 0, $semicolon); $ifModifiedSince = substr($ifModifiedSince, 0, $semicolon);
} }
return ($ifModifiedSince == self::gmtdate($this->_lmTime)); return ($ifModifiedSince == self::gmtDate($this->_lmTime));
}
protected static function gmtdate($ts) {
return gmdate('D, d M Y H:i:s \G\M\T', $ts);
} }
} }

View File

@@ -52,9 +52,7 @@ to verify headers and content being sent.</p>
<dt>Safari</dt> <dt>Safari</dt>
<dd>ETag validation is unsupported, but Safari supports HTTP/1.0 validation via <dd>ETag validation is unsupported, but Safari supports HTTP/1.0 validation via
If-Modified-Since headers as long as the cache is explicitly marked If-Modified-Since headers as long as the cache is explicitly marked
&quot;public&quot; or &quot;private&quot;. ConditionalGet can send one of these "public" or "private" ("private" is default in ConditionalGet).</dd>
values determined by cookies/session data, but it's best to explicitly
set the option 'isPublic' to true or false.</dd>
</dl> </dl>
</body> </body>
</html> </html>

View File

@@ -54,15 +54,12 @@ class HTTP_Encoder {
if (isset($spec['type'])) { if (isset($spec['type'])) {
$this->_headers['Content-Type'] = $spec['type']; $this->_headers['Content-Type'] = $spec['type'];
} }
if (self::$_clientEncodeMethod === null) {
self::$_clientEncodeMethod = self::getAcceptedEncoding();
}
if (isset($spec['method']) if (isset($spec['method'])
&& in_array($spec['method'], array('gzip', 'deflate', 'compress', ''))) && in_array($spec['method'], array('gzip', 'deflate', 'compress', '')))
{ {
$this->_encodeMethod = array($spec['method'], $spec['method']); $this->_encodeMethod = array($spec['method'], $spec['method']);
} else { } else {
$this->_encodeMethod = self::$_clientEncodeMethod; $this->_encodeMethod = self::getAcceptedEncoding();
} }
} }
@@ -136,32 +133,41 @@ class HTTP_Encoder {
* this will return ('', ''), the "identity" encoding. * this will return ('', ''), the "identity" encoding.
* *
* A syntax-aware scan is done of the Accept-Encoding, so the method must * A syntax-aware scan is done of the Accept-Encoding, so the method must
* be non 0. The methods are favored in order of gzip, deflate, then * be non 0. The methods are favored in order of deflate, gzip, then
* compress. * compress. Yes, deflate is always smaller and faster!
*
* Note: this value is cached internally for the entire PHP execution
* *
* @return array two values, 1st is the actual encoding method, 2nd is the * @return array two values, 1st is the actual encoding method, 2nd is the
* alias of that method to use in the Content-Encoding header (some browsers * alias of that method to use in the Content-Encoding header (some browsers
* call gzip "x-gzip" etc.) * call gzip "x-gzip" etc.)
*/ */
public static function getAcceptedEncoding() { public static function getAcceptedEncoding() {
if (self::$_clientEncodeMethod !== null) {
return self::$_clientEncodeMethod;
}
if (! isset($_SERVER['HTTP_ACCEPT_ENCODING']) if (! isset($_SERVER['HTTP_ACCEPT_ENCODING'])
|| self::_isBuggyIe()) || self::_isBuggyIe())
{ {
return array('', ''); return array('', '');
} }
// @link http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
// test for (x-)gzip, if q is specified, can't be "0" // test for (x-)gzip, if q is specified, can't be "0"
if (preg_match('@(?:^|,)\s*((?:x-)?gzip)\s*(?:$|,|;\s*q=(?:0\.|1))@', $_SERVER['HTTP_ACCEPT_ENCODING'], $m)) { // faster test for most common "gzip, deflate"
return array('gzip', $m[1]); if (preg_match('@(?:,| )deflate$@', $_SERVER['HTTP_ACCEPT_ENCODING'])
} || preg_match(
if (preg_match('@(?:^|,)\s*deflate\s*(?:$|,|;\s*q=(?:0\.|1))@', $_SERVER['HTTP_ACCEPT_ENCODING'])) { '@(?:^|,)\\s*deflate\\s*(?:$|,|;\\s*q=(?:0\\.|1))@'
,$_SERVER['HTTP_ACCEPT_ENCODING'])
) {
return array('deflate', 'deflate'); return array('deflate', 'deflate');
} }
if (preg_match('@(?:^|,)\s*((?:x-)?compress)\s*(?:$|,|;\s*q=(?:0\.|1))@', $_SERVER['HTTP_ACCEPT_ENCODING'], $m)) { if (preg_match(
'@(?:^|,)\\s*((?:x-)?gzip)\\s*(?:$|,|;\\s*q=(?:0\\.|1))@'
,$_SERVER['HTTP_ACCEPT_ENCODING']
,$m)
) {
return array('gzip', $m[1]);
}
if (preg_match(
'@(?:^|,)\\s*((?:x-)?compress)\\s*(?:$|,|;\\s*q=(?:0\\.|1))@'
,$_SERVER['HTTP_ACCEPT_ENCODING']
,$m)
) {
return array('compress', $m[1]); return array('compress', $m[1]);
} }
return array('', ''); return array('', '');
@@ -211,21 +217,27 @@ class HTTP_Encoder {
return true; return true;
} }
protected static $_clientEncodeMethod = null; protected $_content = '';
protected $content = ''; protected $_headers = array();
protected $headers = array(); protected $_encodeMethod = array('', '');
protected $encodeMethod = array('', '');
/**
* Is the browser an IE version earlier than 6 SP2?
*/
protected static function _isBuggyIe() protected static function _isBuggyIe()
{ {
if (strstr($_SERVER['HTTP_USER_AGENT'], 'Opera') if (false !== strpos($_SERVER['HTTP_USER_AGENT'], 'Opera')
|| !preg_match('/^Mozilla\/4\.0 \(compatible; MSIE ([0-9]\.[0-9])/i', $_SERVER['HTTP_USER_AGENT'], $m)) || !preg_match(
{ '/^Mozilla\/4\.0 \(compatible; MSIE ([0-9]\.[0-9])/i'
,$_SERVER['HTTP_USER_AGENT']
,$m
)
) {
return false; return false;
} }
$version = floatval($m[1]); $version = floatval($m[1]);
if ($version < 6) return true; if ($version < 6) return true;
if ($version == 6 && !strstr($_SERVER['HTTP_USER_AGENT'], 'SV1')) { if ($version == 6 && false === strpos($_SERVER['HTTP_USER_AGENT'], 'SV1')) {
return true; return true;
} }
return false; return false;

View File

@@ -46,8 +46,7 @@ window.onload = function(){
document.getElementById("js").className = "green"; document.getElementById("js").className = "green";
}; };
'; ';
$type = 'text/javascript'; $type = 'application/x-javascript';
} }
$he = new HTTP_Encoder(array( $he = new HTTP_Encoder(array(

View File

@@ -16,7 +16,7 @@
* @package Minify * @package Minify
* @author Ryan Grove <ryan@wonko.com> * @author Ryan Grove <ryan@wonko.com>
* @author Stephen Clay <steve@mrclay.org> * @author Stephen Clay <steve@mrclay.org>
* @copyright 2007 Ryan Grove. All rights reserved. * @copyright 2008 Ryan Grove, Stephen Clay. All rights reserved.
* @license http://opensource.org/licenses/bsd-license.php New BSD License * @license http://opensource.org/licenses/bsd-license.php New BSD License
* @version 1.9.0 * @version 1.9.0
* @link http://code.google.com/p/minify/ * @link http://code.google.com/p/minify/
@@ -27,8 +27,10 @@ require_once 'Minify/Source.php';
class Minify { class Minify {
const TYPE_CSS = 'text/css'; const TYPE_CSS = 'text/css';
const TYPE_JS = 'application/x-javascript';
const TYPE_HTML = 'text/html'; const TYPE_HTML = 'text/html';
// there is some debate over the ideal JS Content-Type, but this is the
// Apache default and what Yahoo! uses..
const TYPE_JS = 'application/x-javascript';
/** /**
* @var bool Should the un-encoded version be cached? * @var bool Should the un-encoded version be cached?

View File

@@ -1,10 +1,16 @@
<?php <?php
require '_inc.php'; require_once '_inc.php';
require 'Minify/CSS.php'; require_once 'Minify/CSS.php';
function test_CSS()
{
global $thisDir;
$cssPath = dirname(__FILE__) . '/_test_files/css';
// build test file list // build test file list
$d = dir(dirname(__FILE__) . '/css'); $d = dir($cssPath);
while (false !== ($entry = $d->read())) { while (false !== ($entry = $d->read())) {
if (preg_match('/^([\w\\-]+)\.css$/', $entry, $m)) { if (preg_match('/^([\w\\-]+)\.css$/', $entry, $m)) {
$list[] = $m[1]; $list[] = $m[1];
@@ -18,15 +24,19 @@ foreach ($list as $item) {
? array('prependRelativePath' => '../') ? array('prependRelativePath' => '../')
: array(); : array();
$src = file_get_contents($thisDir . '/css/' . $item . '.css'); $src = file_get_contents($cssPath . "/{$item}.css");
$minExpected = file_get_contents($thisDir . '/css/' . $item . '.min.css'); $minExpected = file_get_contents($cssPath . "/{$item}.min.css");
$minOutput = Minify_CSS::minify($src, $options); $minOutput = Minify_CSS::minify($src, $options);
assertTrue($minExpected === $minOutput, 'Minify_CSS : ' . $item); $passed = assertTrue($minExpected === $minOutput, 'Minify_CSS : ' . $item);
if ($minExpected !== $minOutput) { if (__FILE__ === $_SERVER['SCRIPT_FILENAME']) {
echo "\n---Source\n\n{$src}"; echo "\n---Output: " .strlen($minOutput). " bytes\n\n{$minOutput}\n\n";
echo "\n\n---Expected\n\n{$minExpected}"; if (!$passed) {
echo "\n\n---Output\n\n{$minOutput}\n\n\n\n"; echo "---Expected: " .strlen($minExpected). " bytes\n\n{$minExpected}\n\n";
echo "---Source: " .strlen($src). " bytes\n\n{$src}\n\n\n";
}
}
} }
} }
test_CSS();

View File

@@ -1,12 +1,16 @@
<?php <?php
require '_inc.php'; require_once '_inc.php';
require 'Minify/HTML.php'; require_once 'Minify/HTML.php';
require 'Minify/CSS.php'; require_once 'Minify/CSS.php';
require 'Minify/Javascript.php'; require_once 'Minify/Javascript.php';
$src = file_get_contents($thisDir . '/html/before.html'); function test_HTML()
$minExpected = file_get_contents($thisDir . '/html/before.min.html'); {
global $thisDir;
$src = file_get_contents($thisDir . '/_test_files/html/before.html');
$minExpected = file_get_contents($thisDir . '/_test_files/html/before.min.html');
$minOutput = Minify_HTML::minify($src, array( $minOutput = Minify_HTML::minify($src, array(
'cssMinifier' => array('Minify_CSS', 'minify') 'cssMinifier' => array('Minify_CSS', 'minify')
@@ -15,8 +19,11 @@ $minOutput = Minify_HTML::minify($src, array(
$passed = assertTrue($minExpected === $minOutput, 'Minify_HTML'); $passed = assertTrue($minExpected === $minOutput, 'Minify_HTML');
echo "\n---Output: " .strlen($minOutput). " bytes\n\n{$minOutput}"; if (__FILE__ === $_SERVER['SCRIPT_FILENAME']) {
if (! $passed) { echo "\n---Output: " .strlen($minOutput). " bytes\n\n{$minOutput}\n\n";
echo "\n\n\n\n---Expected: " .strlen($minExpected). " bytes\n\n{$minExpected}"; echo "---Expected: " .strlen($minExpected). " bytes\n\n{$minExpected}\n\n";
echo "---Source: " .strlen($src). " bytes\n\n{$src}\n\n\n";
} }
echo "\n\n---Source: " .strlen($src). " bytes\n\n{$src}"; }
test_HTML();

View File

@@ -0,0 +1,97 @@
<?php
require_once '_inc.php';
require_once 'HTTP/Encoder.php';
function test_HTTP_Encoder()
{
global $thisDir;
$methodTests = array(
array(
'ua' => 'Any browser'
,'ae' => 'compress, x-gzip'
,'exp' => array('gzip', 'x-gzip')
,'desc' => 'recognize "x-gzip" as gzip'
)
,array(
'ua' => 'Any browser'
,'ae' => 'compress, x-gzip;q=0.5'
,'exp' => array('gzip', 'x-gzip')
,'desc' => 'gzip w/ non-zero q'
)
,array(
'ua' => 'Any browser'
,'ae' => 'compress, x-gzip;q=0'
,'exp' => array('compress', 'compress')
,'desc' => 'gzip w/ zero q'
)
,array(
'ua' => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)'
,'ae' => 'gzip, deflate'
,'exp' => array('', '')
,'desc' => 'IE6 w/o "enhanced security"'
)
,array(
'ua' => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)'
,'ae' => 'gzip, deflate'
,'exp' => array('deflate', 'deflate')
,'desc' => 'IE6 w/ "enhanced security"'
)
,array(
'ua' => 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.01)'
,'ae' => 'gzip, deflate'
,'exp' => array('', '')
,'desc' => 'IE5.5'
)
,array(
'ua' => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; en) Opera 9.25'
,'ae' => 'gzip,deflate'
,'exp' => array('deflate', 'deflate')
,'desc' => 'Opera identifying as IE6'
)
);
foreach ($methodTests as $test) {
$_SERVER['HTTP_USER_AGENT'] = $test['ua'];
$_SERVER['HTTP_ACCEPT_ENCODING'] = $test['ae'];
$exp = $test['exp'];
$ret = HTTP_Encoder::getAcceptedEncoding();
$passed = assertTrue($exp == $ret, 'HTTP_Encoder : ' . $test['desc']);
if (__FILE__ === $_SERVER['SCRIPT_FILENAME']) {
echo "\n--- AE | UA = {$test['ae']} | {$test['ua']}\n";
echo "Expected = " . preg_replace('/\\s+/', ' ', var_export($exp, 1)) . "\n";
echo "Returned = " . preg_replace('/\\s+/', ' ', var_export($ret, 1)) . "\n\n";
}
}
$variedContent = file_get_contents($thisDir . '/_test_files/html/before.html')
. file_get_contents($thisDir . '/_test_files/css/subsilver.css')
. file_get_contents($thisDir . '/../examples/1/jquery-1.2.3.js');
$encodingTests = array(
array('method' => 'gzip', 'exp' => 32174)
,array('method' => 'deflate', 'exp' => 32156)
,array('method' => 'compress', 'exp' => 32210)
);
foreach ($encodingTests as $test) {
$e = new HTTP_Encoder(array(
'content' => $variedContent
,'method' => $test['method']
));
$e->encode(9);
$ret = strlen($e->getContent());
$passed = assertTrue($ret == $test['exp']
,"HTTP_Encoder : {$test['method']} compression");
if (__FILE__ === $_SERVER['SCRIPT_FILENAME']) {
echo "\n--- {$test['method']}: expected bytes: "
, "{$test['exp']}. Returned: {$ret}\n\n";
}
}
}
test_HTTP_Encoder();

View File

@@ -1,16 +1,23 @@
<?php <?php
require '_inc.php'; require_once '_inc.php';
require 'Minify/Javascript.php'; require_once 'Minify/Javascript.php';
$src = file_get_contents($thisDir . '/js/before.js'); function test_Javascript()
$minExpected = file_get_contents($thisDir . '/js/before.min.js');; {
global $thisDir;
$src = file_get_contents($thisDir . '/_test_files/js/before.js');
$minExpected = file_get_contents($thisDir . '/_test_files/js/before.min.js');;
$minOutput = Minify_Javascript::minify($src); $minOutput = Minify_Javascript::minify($src);
$passed = assertTrue($minExpected == $minOutput, 'Minify_Javascript converts before.js to before.min.js'); $passed = assertTrue($minExpected == $minOutput, 'Minify_Javascript');
echo "\n---Output: " .strlen($minOutput). " bytes\n\n{$minOutput}"; if (__FILE__ === $_SERVER['SCRIPT_FILENAME']) {
if (! $passed) { echo "\n---Output: " .strlen($minOutput). " bytes\n\n{$minOutput}\n\n";
echo "\n\n\n\n---Expected: " .strlen($minExpected). " bytes\n\n{$minExpected}"; echo "---Expected: " .strlen($minExpected). " bytes\n\n{$minExpected}\n\n";
echo "---Source: " .strlen($src). " bytes\n\n{$src}\n\n\n";
} }
echo "\n\n---Source: " .strlen($src). " bytes\n\n{$src}"; }
test_Javascript();

View File

@@ -1,23 +1,27 @@
<?php <?php
require '_inc.php'; require_once '_inc.php';
/** /**
* Note: All Minify classes are E_STRICT except for Cache_Lite_File. * Note: All Minify classes are E_STRICT except for Cache_Lite_File.
*/ */
error_reporting(E_ALL); error_reporting(E_ALL);
require 'Minify.php'; require_once 'Minify.php';
function test_Minify()
{
global $thisDir;
$minifyTestPath = dirname(__FILE__) . '/_test_files/minify';
$tomorrow = time() + 86400; $tomorrow = time() + 86400;
$lastModified = time() - 86400; $lastModified = time() - 86400;
// Test minifying JS and serving with Expires header // Test minifying JS and serving with Expires header
$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' => preg_replace('/\\r\\n?/', "\n", file_get_contents($thisDir . '/minify/minified.js')) ,'content' => preg_replace('/\\r\\n?/', "\n", file_get_contents($minifyTestPath . '/minified.js'))
,'headers' => array ( ,'headers' => array (
'Cache-Control' => 'public', 'Cache-Control' => 'public',
'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),
@@ -26,20 +30,21 @@ $expected = array(
); );
$output = Minify::serve('Files', array( $output = Minify::serve('Files', array(
'files' => array( 'files' => array(
$thisDir . '/minify/email.js' $minifyTestPath . '/email.js'
,$thisDir . '/minify/QueryString.js' ,$minifyTestPath . '/QueryString.js'
) )
,'quiet' => true ,'quiet' => true
,'setExpires' => $tomorrow ,'setExpires' => $tomorrow
,'encodeOutput' => false ,'encodeOutput' => false
)); ));
$passed = assertTrue($expected === $output, 'Minify - JS and Expires'); $passed = assertTrue($expected === $output, 'Minify : JS and Expires');
if (__FILE__ === $_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 minifying CSS and responding with Etag/Last-Modified // Test minifying CSS and responding with Etag/Last-Modified
@@ -49,7 +54,7 @@ unset($_SERVER['HTTP_IF_NONE_MATCH'], $_SERVER['HTTP_IF_MODIFIED_SINCE']);
$expected = array( $expected = array(
'success' => true 'success' => true
,'statusCode' => 200 ,'statusCode' => 200
,'content' => file_get_contents($thisDir . '/minify/minified.css') ,'content' => file_get_contents($minifyTestPath . '/minified.css')
,'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\"",
@@ -59,19 +64,20 @@ $expected = array(
); );
$output = Minify::serve('Files', array( $output = Minify::serve('Files', array(
'files' => array( 'files' => array(
$thisDir . '/css/styles.css' $thisDir . '/_test_files/css/styles.css'
,$thisDir . '/css/subsilver.css' ,$thisDir . '/_test_files/css/subsilver.css'
) )
,'quiet' => true ,'quiet' => true
,'lastModifiedTime' => $lastModified ,'lastModifiedTime' => $lastModified
,'encodeOutput' => false ,'encodeOutput' => false
)); ));
$passed = assertTrue($expected === $output, 'Minify - CSS and Etag/Last-Modified'); $passed = assertTrue($expected === $output, 'Minify : CSS and Etag/Last-Modified');
if (__FILE__ === $_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 304 response // Test 304 response
@@ -87,14 +93,19 @@ $expected = array (
); );
$output = Minify::serve('Files', array( $output = Minify::serve('Files', array(
'files' => array( 'files' => array(
$thisDir . '/css/styles.css' $thisDir . '/_test_files/css/styles.css'
) )
,'quiet' => true ,'quiet' => true
,'lastModifiedTime' => $lastModified ,'lastModifiedTime' => $lastModified
,'encodeOutput' => false ,'encodeOutput' => false
)); ));
$passed = assertTrue($expected === $output, 'Minify - 304 response'); $passed = assertTrue($expected === $output, 'Minify : 304 response');
if (__FILE__ === $_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_Minify();

View File

@@ -1,16 +1,23 @@
<?php <?php
require '_inc.php'; require_once '_inc.php';
require 'Minify/Packer.php'; require_once 'Minify/Packer.php';
$src = file_get_contents($thisDir . '/packer/before.js'); function test_Packer()
$minExpected = file_get_contents($thisDir . '/packer/before.min.js'); {
global $thisDir;
$src = file_get_contents($thisDir . '/_test_files/packer/before.js');
$minExpected = file_get_contents($thisDir . '/_test_files/packer/before.min.js');
$minOutput = Minify_Packer::minify($src); $minOutput = Minify_Packer::minify($src);
$passed = assertTrue($minExpected === $minOutput, 'Minify_Packer'); $passed = assertTrue($minExpected === $minOutput, 'Minify_Packer');
echo "\n---Output: " .strlen($minOutput). " bytes\n\n{$minOutput}"; if (__FILE__ === $_SERVER['SCRIPT_FILENAME']) {
if (! $passed) { echo "\n---Output: " .strlen($minOutput). " bytes\n\n{$minOutput}\n\n";
echo "\n\n\n\n---Expected: " .strlen($minExpected). " bytes\n\n{$minExpected}"; echo "---Expected: " .strlen($minExpected). " bytes\n\n{$minExpected}\n\n";
echo "---Source: " .strlen($src). " bytes\n\n{$src}\n\n\n";
} }
echo "\n\n---Source: " .strlen($src). " bytes\n\n{$src}"; }
test_Packer();

10
web/test/test_all.php Normal file
View File

@@ -0,0 +1,10 @@
<?php
require 'test_Javascript.php';
require 'test_CSS.php';
require 'test_Packer.php';
require 'test_HTML.php';
require 'test_Minify.php';
require 'test_HTTP_Encoder.php';
?>