1
0
mirror of https://github.com/mrclay/minify.git synced 2025-08-13 09:34:54 +02:00

Port last of min_unit_tests, remove references to it, add composer script

This commit is contained in:
Steve Clay
2015-11-22 13:55:01 -05:00
parent 9370e96fd4
commit 316a109032
85 changed files with 12 additions and 401 deletions

1
.gitattributes vendored
View File

@@ -2,5 +2,4 @@
/.gitignore export-ignore /.gitignore export-ignore
/.gitattributes export-ignore /.gitattributes export-ignore
/min_extras export-ignore /min_extras export-ignore
/min_unit_tests export-ignore
/tests export-ignore /tests export-ignore

View File

@@ -68,10 +68,8 @@ Support
Unit Testing Unit Testing
------------ ------------
1. Open the file `min_unit_tests/_inc.php`. 1. Install via Composer
1. Enable the file by commenting out the `die()` statement. 1. `composer run test`
1. Access: http://example.org/min/min_unit_tests/test_all.php (If you wish, the other test_*.php files can be run to test individual components with more verbose output.)
1. Re-disable the `_inc.php` file when you are done.
Warnings Warnings
-------- --------

View File

@@ -7,7 +7,7 @@ UPGRADING FROM 2.1.*
/min/config.php --> /min/old_config.php /min/config.php --> /min/old_config.php
/min/groupsConfig.php --> /min/old_groupsConfig.php /min/groupsConfig.php --> /min/old_groupsConfig.php
2. Overwrite all files in /min (and /min_unit_tests) with those from this zip. 2. Overwrite all files in /min with those from this zip.
3. Delete /min/groupsConfig.php 3. Delete /min/groupsConfig.php

View File

@@ -35,5 +35,8 @@
"suggest": { "suggest": {
"leafo/lessphp": "~0.4.0", "leafo/lessphp": "~0.4.0",
"meenie/javascript-packer": "~1.1" "meenie/javascript-packer": "~1.1"
},
"scripts": {
"test": "phpunit"
} }
} }

View File

@@ -1,12 +0,0 @@
# Unit Testing
0. If you haven't already, install Minify using the [UserGuide](UserGuide.wiki.md).
1. Copy the "min\_unit\_tests" directory directly into your DOCUMENT\_ROOT.
2. Browse to http://example.com/min_unit_tests/test_all.php
You should see a list of "PASS"es. You can run the individual test PHP files in http://example.com/min_unit_tests/ for more verbose output.
## Common Problems

View File

@@ -128,4 +128,4 @@ url.rewrite-once = ( "^/min/([a-z]=.*)" => "/min/index.php?$1" )
# Problems? # Problems?
See [CommonProblems](CommonProblems.wiki.md) and [Debugging](Debugging.wiki.md). You might also try [TestingMinify](TestingMinify.wiki.md) (running `test_environment.php` in particular). See [CommonProblems](CommonProblems.wiki.md) and [Debugging](Debugging.wiki.md). You might also try running `server-info.php` in particular.

View File

@@ -21,8 +21,8 @@ if (! $cli->validate()) {
if ($cli->isHelpRequest) { if ($cli->isHelpRequest) {
echo $cli->getArgumentsListing(); echo $cli->getArgumentsListing();
} }
echo "EXAMPLE: ./minify.php ../../min_unit_tests/_test_files/js/*.js\n"; echo "EXAMPLE: ./minify.php ../../tests/_test_files/js/*.js\n";
echo "EXAMPLE: ./minify.php -d../.. ../../min_unit_tests/_test_files/css/*.css\n"; echo "EXAMPLE: ./minify.php -d../.. ../../tests/_test_files/css/*.css\n";
echo "EXAMPLE: echo \"var js = 'Awesome' && /cool/;\" | ./minify.php -t js\n"; echo "EXAMPLE: echo \"var js = 'Awesome' && /cool/;\" | ./minify.php -t js\n";
echo "EXAMPLE: echo \"sel > ector { prop: 'value '; }\" | ./minify.php -t css\n"; echo "EXAMPLE: echo \"sel > ector { prop: 'value '; }\" | ./minify.php -t css\n";
echo "\n"; echo "\n";

View File

@@ -18,7 +18,7 @@ if (! $cli->validate()) {
if ($cli->isHelpRequest) { if ($cli->isHelpRequest) {
echo $cli->getArgumentsListing(); echo $cli->getArgumentsListing();
} }
echo "EXAMPLE: ./rewrite-uris.php -v -d../.. ../../min_unit_tests/_test_files/css/paths_rewrite.css ../../min_unit_tests/_test_files/css/comments.css echo "EXAMPLE: ./rewrite-uris.php -v -d../.. ../../tests/_test_files/css/paths_rewrite.css ../../tests/_test_files/css/comments.css
\n"; \n";
exit(0); exit(0);
} }

View File

@@ -1,4 +0,0 @@
<IfModule mod_env.c>
# In case AddOutputFilterByType has been added
SetEnv no-gzip
</IfModule>

View File

@@ -1,43 +0,0 @@
<?php
require __DIR__ . '/../../bootstrap.php';
// emulate regularly updating document
$every = 20;
$lastModified = round(time()/$every)*$every - $every;
$cg = new HTTP_ConditionalGet(array(
'lastModifiedTime' => $lastModified
));
if ($cg->cacheIsValid) {
$cg->sendHeaders();
// we're done
exit();
}
// generate content
$title = 'Last-Modified is known : add Content-Length';
$explain = '
<p>Here, like <a href="./">the first example</a>, we know the Last-Modified time,
but we also want to set the Content-Length to increase cacheability and allow
HTTP persistent connections. Instead of sending headers immediately, we first
generate our content, then use <code>setContentLength(strlen($content))</code>
to add the header. Then finally call <code>sendHeaders()</code> and send the
content.</p>
<p><strong>Note:</strong> This is not required if your PHP config buffers all
output and your script doesn\'t do any incremental flushing of the output
buffer. PHP will generally set Content-Length for you if it can.</p>
<p>This script emulates a document that changes every ' .$every. ' seconds.
<br>This is version: ' . date('r', $lastModified) . '</p>
';
require '_include.php';
$content = get_content(array(
'title' => $title
,'explain' => $explain
));
$cg->setContentLength(strlen($content));
$cg->sendHeaders();
send_slowly($content);

View File

@@ -1,39 +0,0 @@
<?php
require __DIR__ . '/../../bootstrap.php';
// generate content first (not ideal)
// emulate regularly updating document
$every = 20;
$lastModified = round(time()/$every)*$every - $every;
$title = 'Last-Modified is unknown : use hash of content for ETag';
$explain = '
<p>When Last-Modified is unknown, you can still use ETags, but you need a short
string that is unique for that content. In the worst case, you have to generate
all the content first, <em>then</em> instantiate HTTP_ConditionalGet, setting
the array key <code>contentHash</code> to the output of a hash function of the
content. Since we have the full content, we might as well also use
<code>setContentLength(strlen($content))</code> in the case where we need to
send it.</p>
<p>This script emulates a document that changes every ' .$every. ' seconds.
<br>This is version: ' . date('r', $lastModified) . '</p>
';
require '_include.php';
$content = get_content(array(
'title' => $title
,'explain' => $explain
));
$cg = new HTTP_ConditionalGet(array(
'contentHash' => substr(md5($content), 7)
));
if ($cg->cacheIsValid) {
$cg->sendHeaders();
// we're done
exit();
}
$cg->setContentLength(strlen($content));
$cg->sendHeaders();
send_slowly($content);

View File

@@ -1,47 +0,0 @@
<?php
require __DIR__ . '/../../bootstrap.php';
// emulate regularly updating document
$every = 20;
$lastModified = round(time()/$every)*$every - $every;
list($enc,) = HTTP_Encoder::getAcceptedEncoding();
$cg = new HTTP_ConditionalGet(array(
'lastModifiedTime' => $lastModified
,'encoding' => $enc
));
$cg->sendHeaders();
if ($cg->cacheIsValid) {
// we're done
exit();
}
// output encoded content
$title = 'ConditionalGet + Encoder';
$explain = '
<p>Using ConditionalGet and Encoder is straightforward. First impliment the
ConditionalGet, then if the cache is not valid, encode and send the content</p>
<p>This script emulates a document that changes every ' .$every. ' seconds.
<br>This is version: ' . date('r', $lastModified) . '</p>
';
require '_include.php';
$content = get_content(array(
'title' => $title
,'explain' => $explain
));
$he = new HTTP_Encoder(array(
'content' => get_content(array(
'title' => $title
,'explain' => $explain
))
));
$he->encode();
// usually you would just $he->sendAll(), but here we want to emulate slow
// connection
$he->sendHeaders();
send_slowly($he->getContent());

View File

@@ -1,26 +0,0 @@
<?php
require __DIR__ . '/../../bootstrap.php';
// far expires
$cg = new HTTP_ConditionalGet(array(
'maxAge' => 20
,'lastModifiedTime' => filemtime(__FILE__)
));
$cg->sendHeaders();
// generate, send content
$title = 'Last-Modified + Expires';
$explain = '
<p>Here we set a static "lastModifiedTime" and "maxAge" to 20. The browser
will consider this document fresh for 20 seconds, then revalidate its cache. After
the 304 response, the cache will be good for another 20 seconds. Unless you force
a reload, there will only be 304 responses for this page after the initial download.
';
require '_include.php';
echo get_content(array(
'title' => $title
,'explain' => $explain
));

View File

@@ -1,64 +0,0 @@
<?php
function send_slowly($content)
{
$half = ceil(strlen($content) / 2);
$content = str_split($content, $half);
while ($chunk = array_shift($content)) {
sleep(1);
echo $chunk;
ob_get_level() && ob_flush();
flush();
}
}
function get_content($data)
{
ob_start();
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>HTTP_ConditionalGet : <?php echo $data['title']; ?></title>
</head>
<body>
<h1>HTTP_ConditionalGet</h1>
<h2><?php echo $data['title']; ?></h2>
<?php echo $data['explain']; ?>
<ul>
<li><a href="./">Last-Modified is known : simple usage</a></li>
<li><a href="2.php">Last-Modified is known : add Content-Length</a></li>
<li><a href="3.php">Last-Modified is unknown : use hash of content for ETag</a></li>
<li><a href="4.php">ConditionalGet + Encoder</a></li>
<li><a href="5.php">Last-Modified + Expires</a></li>
</ul>
<h2>Notes</h2>
<h3>How to distinguish 200 and 304 responses</h3>
<p>For these pages all 200 responses are sent in chunks a second apart, so you
should notice that 304 responses are quicker. You can also use HTTP sniffers
like <a href="http://www.fiddlertool.com/">Fiddler (win)</a> and
<a href="http://livehttpheaders.mozdev.org/">LiveHTTPHeaders (Firefox add-on)</a>
to verify headers and content being sent.</p>
<h3>Browser notes</h3>
<dl>
<dt>Opera</dt>
<dd>Opera has a couple behaviors against the HTTP spec: Manual refreshes (F5)
prevents the ETag/If-Modified-Since headers from being sent; it only sends
them when following a link or bookmark. Also, Opera will not honor the
<code>must-revalidate</code> Cache-Control value unless <code>max-age</code>
is set. To get Opera to follow the spec, ConditionalGet will send Opera max-age=0
(if one is not already set).</dd>
<dt>Safari</dt>
<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
"public" or "private" ("private" is default in ConditionalGet).</dd>
</dl>
</body>
</html>
<?php
$content = ob_get_contents();
ob_end_clean();
return $content;
}

View File

@@ -1,35 +0,0 @@
<?php
require __DIR__ . '/../../bootstrap.php';
// emulate regularly updating document
$every = 20;
$lastModified = round(time()/$every)*$every - $every;
$cg = new HTTP_ConditionalGet(array(
'lastModifiedTime' => $lastModified
));
$cg->sendHeaders();
if ($cg->cacheIsValid) {
// we're done
exit();
}
$title = 'Last-Modified is known : simple usage';
$explain = '
<p>If your content has not changed since a certain timestamp, set this via the
the <code>lastModifiedTime</code> array key when instantiating HTTP_ConditionalGet.
You can immediately call the method <code>sendHeaders()</code> to set the
Last-Modified, ETag, and Cache-Control headers. The, if <code>cacheIsValid</code>
property is false, you echo the content.</p>
<p>This script emulates a document that changes every ' .$every. ' seconds.
<br>This is version: ' . date('r', $lastModified) . '</p>
';
require '_include.php';
send_slowly(get_content(array(
'title' => $title
,'explain' => $explain
)));

Binary file not shown.

Before

Width:  |  Height:  |  Size: 202 B

View File

@@ -1,57 +0,0 @@
<?php
ini_set('display_errors', 'on');
require __DIR__ . '/../../bootstrap.php';
if (!isset($_GET['test'])) {
$type = 'text/html';
ob_start();
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>HTTP_Encoder Test</title>
<style type="text/css">
@import "?test=2";
#img {background:url("?test=1");}
.green {background:#0f0;}
p span {padding:0 .5em;}
</style>
</head>
<body>
<h1>HTTP_Encoder test</h1>
<p><span class="green"> HTML </span></p>
<p><span id="css"> CSS </span></p>
<p><span id="js"> Javascript </span></p>
<p><span id="img"> image </span></p>
<script src="?test=3" type="text/javascript"></script>
</body>
</html>
<?php
$content = ob_get_contents();
ob_end_clean();
} elseif ($_GET['test'] == '1') {
$content = file_get_contents(__DIR__ . '/green.png');
$type = 'image/png';
} elseif ($_GET['test'] == '2') {
$content = '#css {background:#0f0;}';
$type = 'text/css';
} else {
$content = '
window.onload = function(){
document.getElementById("js").className = "green";
};
';
$type = 'application/x-javascript';
}
$he = new HTTP_Encoder(array(
'content' => $content
,'type' => $type
));
$he->encode();
$he->sendAll();

View File

@@ -1,62 +0,0 @@
<?php
// comment this out to run tests
die('This script is disabled.');
require __DIR__ . '/../bootstrap.php';
require __DIR__ . '/../config.php';
// set cache path and doc root if configured
$minifyCachePath = isset($min_cachePath)
? $min_cachePath
: '';
if ($min_documentRoot) {
$_SERVER['DOCUMENT_ROOT'] = $min_documentRoot;
}
// default log to FirePHP
if ($min_errorLogger && true !== $min_errorLogger) { // custom logger
Minify_Logger::setLogger($min_errorLogger);
} else {
Minify_Logger::setLogger(FirePHP::getInstance(true));
}
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 1);
header('Content-Type: text/plain;charset=utf-8');
$thisDir = __DIR__;
/**
* pTest - PHP Unit Tester
* @param mixed $test Condition to test, evaluated as boolean
* @param string $message Descriptive message to output upon test
* @url http://www.sitepoint.com/blogs/2007/08/13/ptest-php-unit-tester-in-9-lines-of-code/
*/
function assertTrue($test, $message)
{
static $count;
if (!isset($count)) $count = array('pass'=>0, 'fail'=>0, 'total'=>0);
$mode = $test ? 'pass' : 'fail';
$outMode = $test ? 'PASS' : '!FAIL';
printf("%s: %s (%d of %d tests run so far have %sed)\n",
$outMode, $message, ++$count[$mode], ++$count['total'], $mode);
return (bool)$test;
}
/**
* Get number of bytes in a string regardless of mbstring.func_overload
*
* @param string $str
* @return int
*/
function countBytes($str)
{
return (function_exists('mb_strlen') && ((int)ini_get('mbstring.func_overload') & 2))
? mb_strlen($str, '8bit')
: strlen($str);
}
ob_start();

View File

@@ -9,8 +9,8 @@ class TestCase extends PHPUnit_Framework_TestCase
public static function setupBeforeClass() public static function setupBeforeClass()
{ {
self::$document_root = __DIR__ . '/../min_unit_tests'; self::$document_root = __DIR__;
self::$test_files = self::$document_root . '/_test_files'; self::$test_files = __DIR__ . '/_test_files';
} }
/** /**