mirror of
https://github.com/mrclay/minify.git
synced 2025-08-07 14:46:29 +02:00
min/test : first shot at an environment test.
This commit is contained in:
@@ -28,6 +28,7 @@ if ($min_documentRoot) {
|
||||
} elseif (0 === stripos(PHP_OS, 'win')) {
|
||||
Minify::setDocRoot(); // IIS may need help
|
||||
}
|
||||
$_SERVER['DOCUMENT_ROOT'] = rtrim($_SERVER['DOCUMENT_ROOT'], '/\\');
|
||||
|
||||
$min_serveOptions['minifierOptions']['text/css']['symlinks'] = $min_symlinks;
|
||||
|
||||
|
182
min/test/_inc.php
Normal file
182
min/test/_inc.php
Normal file
@@ -0,0 +1,182 @@
|
||||
<?php
|
||||
|
||||
error_reporting(E_ALL | E_STRICT);
|
||||
ini_set('display_errors', 1);
|
||||
|
||||
require dirname(__FILE__) . '/../config.php';
|
||||
|
||||
set_include_path($min_libPath . PATH_SEPARATOR . get_include_path());
|
||||
|
||||
// set cache path and doc root if configured
|
||||
$minifyCachePath = isset($min_cachePath)
|
||||
? $min_cachePath
|
||||
: '';
|
||||
|
||||
if ($min_documentRoot) {
|
||||
$_SERVER['DOCUMENT_ROOT'] = $min_documentRoot;
|
||||
} elseif (0 === stripos(PHP_OS, 'win')) {
|
||||
require_once 'Minify.php';
|
||||
Minify::setDocRoot(); // IIS may need help
|
||||
}
|
||||
$_SERVER['DOCUMENT_ROOT'] = rtrim($_SERVER['DOCUMENT_ROOT'], '/\\');
|
||||
|
||||
// default log to FirePHP
|
||||
require_once 'Minify/Logger.php';
|
||||
require_once 'FirePHP.php';
|
||||
Minify_Logger::setLogger(FirePHP::getInstance(true));
|
||||
|
||||
if (! isset($min_cachePath)) {
|
||||
$min_cachePath = '';
|
||||
}
|
||||
// code in Minify::setCache()
|
||||
if (is_string($min_cachePath)) {
|
||||
require_once 'Minify/Cache/File.php';
|
||||
$cache = new Minify_Cache_File($min_cachePath, $min_cacheFileLocking);
|
||||
} else {
|
||||
$cache = $min_cachePath;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* pTest - PHP Unit Tester
|
||||
* @param mixed $test Condition to test, evaluated as boolean
|
||||
* @param string $html Descriptive message to output upon test
|
||||
* @url http://www.sitepoint.com/blogs/2007/08/13/ptest-php-unit-tester-in-9-lines-of-code/
|
||||
* @return bool success
|
||||
*/
|
||||
function assertTrue($test, $html)
|
||||
{
|
||||
$outMode = $test ? 'PASS' : '!FAIL';
|
||||
$class = $test ? 'assert pass' : 'assert fail';
|
||||
printf("<p class='%s'><strong>%s</strong>: %s</p>", $class, $outMode, $html);
|
||||
return (bool)$test;
|
||||
}
|
||||
|
||||
function h($txt)
|
||||
{
|
||||
return htmlspecialchars($txt);
|
||||
}
|
||||
|
||||
function e($value)
|
||||
{
|
||||
return htmlspecialchars(var_export($value, 1));
|
||||
}
|
||||
|
||||
function _gzdecode($data)
|
||||
{
|
||||
$filename = $error = '';
|
||||
return _phpman_gzdecode($data, $filename, $error);
|
||||
}
|
||||
|
||||
// http://www.php.net/manual/en/function.gzdecode.php#82930
|
||||
function _phpman_gzdecode($data, &$filename='', &$error='', $maxlength=null)
|
||||
{
|
||||
$len = strlen($data);
|
||||
if ($len < 18 || strcmp(substr($data,0,2),"\x1f\x8b")) {
|
||||
$error = "Not in GZIP format.";
|
||||
return null; // Not GZIP format (See RFC 1952)
|
||||
}
|
||||
$method = ord(substr($data,2,1)); // Compression method
|
||||
$flags = ord(substr($data,3,1)); // Flags
|
||||
if ($flags & 31 != $flags) {
|
||||
$error = "Reserved bits not allowed.";
|
||||
return null;
|
||||
}
|
||||
// NOTE: $mtime may be negative (PHP integer limitations)
|
||||
$mtime = unpack("V", substr($data,4,4));
|
||||
$mtime = $mtime[1];
|
||||
$xfl = substr($data,8,1);
|
||||
$os = substr($data,8,1);
|
||||
$headerlen = 10;
|
||||
$extralen = 0;
|
||||
$extra = "";
|
||||
if ($flags & 4) {
|
||||
// 2-byte length prefixed EXTRA data in header
|
||||
if ($len - $headerlen - 2 < 8) {
|
||||
return false; // invalid
|
||||
}
|
||||
$extralen = unpack("v",substr($data,8,2));
|
||||
$extralen = $extralen[1];
|
||||
if ($len - $headerlen - 2 - $extralen < 8) {
|
||||
return false; // invalid
|
||||
}
|
||||
$extra = substr($data,10,$extralen);
|
||||
$headerlen += 2 + $extralen;
|
||||
}
|
||||
$filenamelen = 0;
|
||||
$filename = "";
|
||||
if ($flags & 8) {
|
||||
// C-style string
|
||||
if ($len - $headerlen - 1 < 8) {
|
||||
return false; // invalid
|
||||
}
|
||||
$filenamelen = strpos(substr($data,$headerlen),chr(0));
|
||||
if ($filenamelen === false || $len - $headerlen - $filenamelen - 1 < 8) {
|
||||
return false; // invalid
|
||||
}
|
||||
$filename = substr($data,$headerlen,$filenamelen);
|
||||
$headerlen += $filenamelen + 1;
|
||||
}
|
||||
$commentlen = 0;
|
||||
$comment = "";
|
||||
if ($flags & 16) {
|
||||
// C-style string COMMENT data in header
|
||||
if ($len - $headerlen - 1 < 8) {
|
||||
return false; // invalid
|
||||
}
|
||||
$commentlen = strpos(substr($data,$headerlen),chr(0));
|
||||
if ($commentlen === false || $len - $headerlen - $commentlen - 1 < 8) {
|
||||
return false; // Invalid header format
|
||||
}
|
||||
$comment = substr($data,$headerlen,$commentlen);
|
||||
$headerlen += $commentlen + 1;
|
||||
}
|
||||
$headercrc = "";
|
||||
if ($flags & 2) {
|
||||
// 2-bytes (lowest order) of CRC32 on header present
|
||||
if ($len - $headerlen - 2 < 8) {
|
||||
return false; // invalid
|
||||
}
|
||||
$calccrc = crc32(substr($data,0,$headerlen)) & 0xffff;
|
||||
$headercrc = unpack("v", substr($data,$headerlen,2));
|
||||
$headercrc = $headercrc[1];
|
||||
if ($headercrc != $calccrc) {
|
||||
$error = "Header checksum failed.";
|
||||
return false; // Bad header CRC
|
||||
}
|
||||
$headerlen += 2;
|
||||
}
|
||||
// GZIP FOOTER
|
||||
$datacrc = unpack("V",substr($data,-8,4));
|
||||
$datacrc = sprintf('%u',$datacrc[1] & 0xFFFFFFFF);
|
||||
$isize = unpack("V",substr($data,-4));
|
||||
$isize = $isize[1];
|
||||
// decompression:
|
||||
$bodylen = $len-$headerlen-8;
|
||||
if ($bodylen < 1) {
|
||||
// IMPLEMENTATION BUG!
|
||||
return null;
|
||||
}
|
||||
$body = substr($data,$headerlen,$bodylen);
|
||||
$data = "";
|
||||
if ($bodylen > 0) {
|
||||
switch ($method) {
|
||||
case 8:
|
||||
// Currently the only supported compression method:
|
||||
$data = gzinflate($body,$maxlength);
|
||||
break;
|
||||
default:
|
||||
$error = "Unknown compression method.";
|
||||
return false;
|
||||
}
|
||||
} // zero-byte body content is allowed
|
||||
// Verifiy CRC32
|
||||
$crc = sprintf("%u",crc32($data));
|
||||
$crcOK = $crc == $datacrc;
|
||||
$lenOK = $isize == strlen($data);
|
||||
if (!$lenOK || !$crcOK) {
|
||||
$error = ( $lenOK ? '' : 'Length check FAILED. ') . ( $crcOK ? '' : 'Checksum FAILED.');
|
||||
return false;
|
||||
}
|
||||
return $data;
|
||||
}
|
161
min/test/index.php
Normal file
161
min/test/index.php
Normal file
@@ -0,0 +1,161 @@
|
||||
<?php
|
||||
|
||||
if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) {
|
||||
// called directly
|
||||
if (isset($_GET['getOutputCompression'])) {
|
||||
echo (int)ini_get('zlib.output_compression');
|
||||
exit();
|
||||
}
|
||||
if (isset($_GET['hello'])) {
|
||||
// try to disable (may not work)
|
||||
ini_set('zlib.output_compression', '0');
|
||||
echo 'World!';
|
||||
exit();
|
||||
}
|
||||
}
|
||||
|
||||
$origDocRoot = $_SERVER['DOCUMENT_ROOT'];
|
||||
|
||||
require_once '_inc.php';
|
||||
|
||||
header('Content-Type: text/html; charset=utf-8');
|
||||
|
||||
?><!doctype html>
|
||||
<title>Minify server environment/config tests</title>
|
||||
<style>
|
||||
.assert {margin:1em 0 0}
|
||||
.assert + .assert {margin:0}
|
||||
.pass strong {color:#090}
|
||||
.fail strong, .warn strong {color:#c00}
|
||||
dt {margin-top:.5em; font-weight:bold}
|
||||
</style>
|
||||
<h1>Minify server environment/config tests</h1>
|
||||
|
||||
<h2>Document Root</h2>
|
||||
<dl>
|
||||
<dt>Original <code>$_SERVER['DOCUMENT_ROOT']</code> (set by PHP/env)</dt>
|
||||
<dd><code><?= e($origDocRoot) ?></code></dd>
|
||||
<dt>Current <code>$_SERVER['DOCUMENT_ROOT']</code> (altered by Minify/your config.php)</dt>
|
||||
<dd><code><?= e($_SERVER['DOCUMENT_ROOT']) ?></code></dd>
|
||||
<dt><code>realpath($_SERVER['DOCUMENT_ROOT'])</code></dt>
|
||||
<dd><code><?= e(realpath($_SERVER['DOCUMENT_ROOT'])) ?></code></dd>
|
||||
<dt><code>__FILE__</code></dt>
|
||||
<dd><code><?= e(__FILE__) ?></code></dd>
|
||||
</dl>
|
||||
<?php
|
||||
|
||||
if (isset($_SERVER['SUBDOMAIN_DOCUMENT_ROOT'])) {
|
||||
echo "<p class='assert note'><strong>!NOTE</strong>: <code>\$_SERVER['SUBDOMAIN_DOCUMENT_ROOT']</code>"
|
||||
. " is set to " . e($_SERVER['SUBDOMAIN_DOCUMENT_ROOT']) . ". You may need to set"
|
||||
. " <code>\$min_documentRoot</code> to this in config.php.</p>";
|
||||
}
|
||||
|
||||
$passed = assertTrue(
|
||||
0 === strpos(__FILE__, realpath($_SERVER['DOCUMENT_ROOT']))
|
||||
,'<code>__FILE__</code> is within realpath of docroot'
|
||||
);
|
||||
if ($passed) {
|
||||
$thisPath = str_replace(
|
||||
'\\'
|
||||
,'/'
|
||||
,substr(__FILE__, strlen(realpath($_SERVER['DOCUMENT_ROOT'])))
|
||||
);
|
||||
} else {
|
||||
// try HTTP requests anyway
|
||||
$thisPath = '/min/test/index.php';
|
||||
}
|
||||
|
||||
if ($thisPath !== '/min/test/index.php') {
|
||||
echo "<p class='assert note'><strong>!NOTE</strong>: /min/ is not directly inside DOCUMENT_ROOT.</p>";
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
<h2>HTTP Encoding</h2>
|
||||
<?php
|
||||
|
||||
$thisUrl = 'http://'
|
||||
. $_SERVER['HTTP_HOST'] // avoid redirects when SERVER_NAME doesn't match
|
||||
. ('80' === $_SERVER['SERVER_PORT'] ? '' : ":{$_SERVER['SERVER_PORT']}")
|
||||
. $thisPath;
|
||||
|
||||
$oc = @file_get_contents($thisUrl . '?getOutputCompression=1');
|
||||
|
||||
if (false === $oc || ! preg_match('/^[01]$/', $oc)) {
|
||||
echo "<p class='assert warn'><strong>!WARN</strong>: Local HTTP request failed. Testing cannot continue.</p>";
|
||||
} else {
|
||||
if ('1' === $oc) {
|
||||
echo "<p class='assert note'><strong>!NOTE</strong>: zlib.output_compression"
|
||||
. " was enabled by default, but this is OK if the next test passes.</p>";
|
||||
}
|
||||
|
||||
$fp = fopen($thisUrl . '?hello=1', 'r', false, stream_context_create(array(
|
||||
'http' => array(
|
||||
'method' => "GET",
|
||||
'header' => "Accept-Encoding: deflate, gzip\r\n"
|
||||
)
|
||||
)));
|
||||
|
||||
$meta = stream_get_meta_data($fp);
|
||||
|
||||
$passed = true;
|
||||
foreach ($meta['wrapper_data'] as $i => $header) {
|
||||
if ((preg_match('@^Content-Length: (\\d+)$@i', $header, $m) && $m[1] !== '6')
|
||||
|| preg_match('@^Content-Encoding:@i', $header, $m)
|
||||
) {
|
||||
$passed = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ($passed && stream_get_contents($fp) !== 'World!') {
|
||||
$passed = false;
|
||||
}
|
||||
assertTrue(
|
||||
$passed
|
||||
,'PHP/server can serve non-encoded content'
|
||||
);
|
||||
fclose($fp);
|
||||
|
||||
if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) {
|
||||
if (! $passed) {
|
||||
echo "<p>Returned content should be 6 bytes and not HTTP encoded.<br>"
|
||||
. "Headers returned by: <code>" . h($thisUrl . '?hello=1') . "</code></p>"
|
||||
. "<pre><code>" . h(var_export($meta['wrapper_data'], 1)) . "</code></pre>";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (assertTrue(function_exists('gzencode'), 'gzencode() exists')) {
|
||||
// test encode/decode
|
||||
$data = str_repeat(md5('testing'), 160);
|
||||
$gzipped = @gzencode($data, 6);
|
||||
assertTrue(is_string($gzipped) && strlen($gzipped) < strlen($data), 'gzip works');
|
||||
assertTrue(_gzdecode($gzipped) === $data, 'gzdecode works');
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
<h2>Cache</h2>
|
||||
<dl>
|
||||
<dt><code>$min_cachePath</code> in config.php</dt>
|
||||
<dd><code><?= e($min_cachePath) ?></code></dd>
|
||||
<dt>Resulting cache object</dt>
|
||||
<dd><pre><code><?= e($cache) ?></code></pre></dd>
|
||||
</dl>
|
||||
<?php
|
||||
|
||||
if ($min_cachePath === '') {
|
||||
echo "<p class='assert'><strong>!NOTE</strong>: Consider setting <code>\$min_cachePath</code> for best performance.</p>";
|
||||
}
|
||||
|
||||
$data = str_repeat(md5('testing'), 160);
|
||||
$id = 'Minify_test_cache';
|
||||
assertTrue(true === $cache->store($id, $data), 'Cache store');
|
||||
assertTrue(strlen($data) === $cache->getSize($id), 'Cache getSize');
|
||||
assertTrue(true === $cache->isValid($id, $_SERVER['REQUEST_TIME'] - 10), 'Cache isValid');
|
||||
ob_start();
|
||||
$cache->display($id);
|
||||
$displayed = ob_get_contents();
|
||||
ob_end_clean();
|
||||
assertTrue($data === $displayed, 'Cache display');
|
||||
assertTrue($data === $cache->fetch($id), 'Cache fetch');
|
4
min_unit_tests/README.txt
Normal file
4
min_unit_tests/README.txt
Normal file
@@ -0,0 +1,4 @@
|
||||
UNIT TESTING
|
||||
|
||||
Do not modify the "simpletest" directory as it's linked via svn:externals.
|
||||
|
20
min_unit_tests/test_simpletest.php
Normal file
20
min_unit_tests/test_simpletest.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
require_once 'simpletest/unit_tester.php';
|
||||
require_once 'simpletest/reporter.php';
|
||||
|
||||
class MinifyTestCase extends UnitTestCase {
|
||||
function __construct() {
|
||||
$this->UnitTestCase(preg_replace('/^Test_/', '', get_class($this)));
|
||||
}
|
||||
}
|
||||
|
||||
class Test_Sample extends MinifyTestCase {
|
||||
function test_Hello() {
|
||||
$this->assertTrue(true, 'Hello World!');
|
||||
}
|
||||
}
|
||||
|
||||
$test = new Grouptest('All tests');
|
||||
$test->addTestCase(new Test_Sample());
|
||||
$test->run(new HtmlReporter());
|
Reference in New Issue
Block a user