mirror of
https://github.com/mrclay/minify.git
synced 2025-01-18 05:38:16 +01:00
Merge branch 'Jimdo-add-tests-closure-compiler'
This commit is contained in:
commit
ea7fe90554
@ -33,6 +33,11 @@
|
|||||||
*/
|
*/
|
||||||
class Minify_ClosureCompiler {
|
class Minify_ClosureCompiler {
|
||||||
|
|
||||||
|
const OPTION_CHARSET = 'charset';
|
||||||
|
const OPTION_COMPILATION_LEVEL = 'compilation_level';
|
||||||
|
|
||||||
|
public static $isDebug = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Filepath of the Closure Compiler jar file. This must be set before
|
* Filepath of the Closure Compiler jar file. This must be set before
|
||||||
* calling minifyJs().
|
* calling minifyJs().
|
||||||
@ -65,18 +70,28 @@ class Minify_ClosureCompiler {
|
|||||||
* @see https://code.google.com/p/closure-compiler/source/browse/trunk/README
|
* @see https://code.google.com/p/closure-compiler/source/browse/trunk/README
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
|
*
|
||||||
|
* @throws Minify_ClosureCompiler_Exception
|
||||||
*/
|
*/
|
||||||
public static function minify($js, $options = array())
|
public static function minify($js, $options = array())
|
||||||
{
|
{
|
||||||
self::_prepare();
|
self::_prepare();
|
||||||
if (! ($tmpFile = tempnam(self::$tempDir, 'cc_'))) {
|
if (! ($tmpFile = tempnam(self::$tempDir, 'cc_'))) {
|
||||||
throw new Exception('Minify_ClosureCompiler : could not create temp file in "'.self::$tempDir.'".');
|
throw new Minify_ClosureCompiler_Exception('Minify_ClosureCompiler : could not create temp file in "'.self::$tempDir.'".');
|
||||||
}
|
}
|
||||||
file_put_contents($tmpFile, $js);
|
file_put_contents($tmpFile, $js);
|
||||||
exec(self::_getCmd($options, $tmpFile), $output, $result_code);
|
$cmd = self::_getCmd($options, $tmpFile);
|
||||||
|
exec($cmd, $output, $result_code);
|
||||||
unlink($tmpFile);
|
unlink($tmpFile);
|
||||||
if ($result_code != 0) {
|
if ($result_code != 0) {
|
||||||
throw new Exception('Minify_ClosureCompiler : Closure Compiler execution failed.');
|
$message = 'Minify_ClosureCompiler : Closure Compiler execution failed.';
|
||||||
|
if (self::$isDebug) {
|
||||||
|
exec($cmd . ' 2>&1', $error);
|
||||||
|
if ($error) {
|
||||||
|
$message .= "\nReason:\n" . join("\n", $error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw new Minify_ClosureCompiler_Exception($message);
|
||||||
}
|
}
|
||||||
return implode("\n", $output);
|
return implode("\n", $output);
|
||||||
}
|
}
|
||||||
@ -85,17 +100,18 @@ class Minify_ClosureCompiler {
|
|||||||
{
|
{
|
||||||
$o = array_merge(
|
$o = array_merge(
|
||||||
array(
|
array(
|
||||||
'charset' => 'utf-8',
|
self::OPTION_CHARSET => 'utf-8',
|
||||||
'compilation_level' => 'SIMPLE_OPTIMIZATIONS',
|
self::OPTION_COMPILATION_LEVEL => 'SIMPLE_OPTIMIZATIONS',
|
||||||
),
|
),
|
||||||
$userOptions
|
$userOptions
|
||||||
);
|
);
|
||||||
|
$charsetOption = $o[self::OPTION_CHARSET];
|
||||||
$cmd = self::$javaExecutable . ' -jar ' . escapeshellarg(self::$jarFile)
|
$cmd = self::$javaExecutable . ' -jar ' . escapeshellarg(self::$jarFile)
|
||||||
. (preg_match('/^[\\da-zA-Z0-9\\-]+$/', $o['charset'])
|
. (preg_match('/^[\\da-zA-Z0-9\\-]+$/', $charsetOption)
|
||||||
? " --charset {$o['charset']}"
|
? " --charset {$charsetOption}"
|
||||||
: '');
|
: '');
|
||||||
|
|
||||||
foreach (array('compilation_level') as $opt) {
|
foreach (array(self::OPTION_COMPILATION_LEVEL) as $opt) {
|
||||||
if ($o[$opt]) {
|
if ($o[$opt]) {
|
||||||
$cmd .= " --{$opt} ". escapeshellarg($o[$opt]);
|
$cmd .= " --{$opt} ". escapeshellarg($o[$opt]);
|
||||||
}
|
}
|
||||||
@ -106,18 +122,18 @@ class Minify_ClosureCompiler {
|
|||||||
private static function _prepare()
|
private static function _prepare()
|
||||||
{
|
{
|
||||||
if (! is_file(self::$jarFile)) {
|
if (! is_file(self::$jarFile)) {
|
||||||
throw new Exception('Minify_ClosureCompiler : $jarFile('.self::$jarFile.') is not a valid file.');
|
throw new Minify_ClosureCompiler_Exception('Minify_ClosureCompiler : $jarFile('.self::$jarFile.') is not a valid file.');
|
||||||
}
|
}
|
||||||
if (! is_readable(self::$jarFile)) {
|
if (! is_readable(self::$jarFile)) {
|
||||||
throw new Exception('Minify_ClosureCompiler : $jarFile('.self::$jarFile.') is not readable.');
|
throw new Minify_ClosureCompiler_Exception('Minify_ClosureCompiler : $jarFile('.self::$jarFile.') is not readable.');
|
||||||
}
|
}
|
||||||
if (! is_dir(self::$tempDir)) {
|
if (! is_dir(self::$tempDir)) {
|
||||||
throw new Exception('Minify_ClosureCompiler : $tempDir('.self::$tempDir.') is not a valid direcotry.');
|
throw new Minify_ClosureCompiler_Exception('Minify_ClosureCompiler : $tempDir('.self::$tempDir.') is not a valid direcotry.');
|
||||||
}
|
}
|
||||||
if (! is_writable(self::$tempDir)) {
|
if (! is_writable(self::$tempDir)) {
|
||||||
throw new Exception('Minify_ClosureCompiler : $tempDir('.self::$tempDir.') is not writable.');
|
throw new Minify_ClosureCompiler_Exception('Minify_ClosureCompiler : $tempDir('.self::$tempDir.') is not writable.');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* vim:ts=4:sw=4:et */
|
class Minify_ClosureCompiler_Exception extends Exception {}
|
||||||
|
@ -9,7 +9,7 @@ function test_Minify_Cache_File()
|
|||||||
|
|
||||||
$cache = new Minify_Cache_File();
|
$cache = new Minify_Cache_File();
|
||||||
|
|
||||||
echo "NOTE: Minify_Cache_File : path is set to: '" . $cache->getPath() . "'.\n";
|
echo " Minify_Cache_File : path is set to: '" . $cache->getPath() . "'.\n";
|
||||||
|
|
||||||
assertTrue(true === $cache->store($id, $data), $prefix . 'store');
|
assertTrue(true === $cache->store($id, $data), $prefix . 'store');
|
||||||
|
|
||||||
|
@ -8,14 +8,14 @@ function test_Minify_Cache_Memcache()
|
|||||||
|
|
||||||
if (! function_exists('memcache_set')) {
|
if (! function_exists('memcache_set')) {
|
||||||
if ($thisFileActive) {
|
if ($thisFileActive) {
|
||||||
echo "NOTE: {$prefix}PHP lacks memcache support\n";
|
echo " {$prefix}To test this component, install memcache in PHP\n";
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$mc = new Memcache;
|
$mc = new Memcache;
|
||||||
if (! @$mc->connect('localhost', 11211)) {
|
if (! @$mc->connect('localhost', 11211)) {
|
||||||
if ($thisFileActive) {
|
if ($thisFileActive) {
|
||||||
echo "NOTE: {$prefix}Could not connect to localhost:11211\n";
|
echo " {$prefix}Memcache server not found on localhost:11211\n";
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
75
min_unit_tests/test_Minify_ClosureCompiler.php
Normal file
75
min_unit_tests/test_Minify_ClosureCompiler.php
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
<?php
|
||||||
|
require_once '_inc.php';
|
||||||
|
|
||||||
|
function test_Minify_ClosureCompiler()
|
||||||
|
{
|
||||||
|
global $thisDir;
|
||||||
|
Minify_ClosureCompiler::$isDebug = true;
|
||||||
|
|
||||||
|
|
||||||
|
// --- Test minification w/o setting the necessary settings ---
|
||||||
|
try {
|
||||||
|
Minify_ClosureCompiler::minify('');
|
||||||
|
} catch (Exception $e) {
|
||||||
|
$exc = $e;
|
||||||
|
}
|
||||||
|
$passed = assertTrue(
|
||||||
|
$exc instanceof Minify_ClosureCompiler_Exception
|
||||||
|
, 'Minify_ClosureCompiler : Throws Minify_ClosureCompiler_Exception');
|
||||||
|
|
||||||
|
if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) {
|
||||||
|
echo "\n---Message: " . var_export($exc->getMessage(), 1) . "\n\n\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
$compiler_jar_path = __DIR__ . DIRECTORY_SEPARATOR . 'compiler.jar';
|
||||||
|
if (is_file($compiler_jar_path)) {
|
||||||
|
|
||||||
|
// set minimum necessary settings
|
||||||
|
Minify_ClosureCompiler::$jarFile = $compiler_jar_path;
|
||||||
|
Minify_ClosureCompiler::$tempDir = sys_get_temp_dir();
|
||||||
|
|
||||||
|
|
||||||
|
// --- Test minification with the minimum necessary settings ---
|
||||||
|
|
||||||
|
$src = "
|
||||||
|
(function (window, undefined){
|
||||||
|
function addOne(input) {
|
||||||
|
return 1 + input;
|
||||||
|
}
|
||||||
|
window.addOne = addOne;
|
||||||
|
window.undefined = undefined;
|
||||||
|
})(window);
|
||||||
|
";
|
||||||
|
$minExpected = "(function(a,b){a.addOne=function(a){return 1+a};a.undefined=b})(window);";
|
||||||
|
$minOutput = Minify_ClosureCompiler::minify($src);
|
||||||
|
$passed = assertTrue($minExpected == $minOutput, 'Minify_ClosureCompiler : minimum necessary settings');
|
||||||
|
if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) {
|
||||||
|
echo "\n---Output: " .countBytes($minOutput). " bytes\n\n{$minOutput}\n\n";
|
||||||
|
echo "---Expected: " .countBytes($minExpected). " bytes\n\n{$minExpected}\n\n";
|
||||||
|
echo "---Source: " .countBytes($src). " bytes\n\n{$src}\n\n\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// --- Test minification with advanced compilation level ---
|
||||||
|
|
||||||
|
$src = "function unused() {};";
|
||||||
|
$minExpected = '';
|
||||||
|
$minOutput = Minify_ClosureCompiler::minify($src, array(
|
||||||
|
Minify_ClosureCompiler::OPTION_COMPILATION_LEVEL => 'ADVANCED_OPTIMIZATIONS'
|
||||||
|
));
|
||||||
|
$passed = assertTrue($minExpected == $minOutput, 'Minify_ClosureCompiler : advanced optimizations');
|
||||||
|
if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) {
|
||||||
|
echo "\n---Output: " .countBytes($minOutput). " bytes\n\n{$minOutput}\n\n";
|
||||||
|
echo "---Expected: " .countBytes($minExpected). " bytes\n\n{$minExpected}\n\n";
|
||||||
|
echo "---Source: " .countBytes($src). " bytes\n\n{$src}\n\n\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
echo " Minify_ClosureCompiler : To test more functionality, download a compiler.jar from\n";
|
||||||
|
echo " https://code.google.com/p/closure-compiler/wiki/BinaryDownloads,\n";
|
||||||
|
echo " put it under '${compiler_jar_path}',\n";
|
||||||
|
echo " and make it readable by your webserver\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
test_Minify_ClosureCompiler();
|
@ -17,7 +17,7 @@ function test_Minify_JS_ClosureCompiler()
|
|||||||
$minExpected = "(function(a,b){a.addOne=function(a){return 1+a};a.undefined=b})(window);";
|
$minExpected = "(function(a,b){a.addOne=function(a){return 1+a};a.undefined=b})(window);";
|
||||||
$minOutput = Minify_JS_ClosureCompiler::minify($src);
|
$minOutput = Minify_JS_ClosureCompiler::minify($src);
|
||||||
if (false !== strpos($minOutput, 'Error(22): Too many compiles')) {
|
if (false !== strpos($minOutput, 'Error(22): Too many compiles')) {
|
||||||
echo "!NOTE: Too many recent calls to Closure Compiler API to test.\n";
|
echo "!---: Minify_JS_ClosureCompiler : Too many recent calls to Closure Compiler API to test.\n";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,10 +1,20 @@
|
|||||||
<?php
|
<?php
|
||||||
require_once '_inc.php';
|
require_once '_inc.php';
|
||||||
|
|
||||||
|
// customize this as needed
|
||||||
|
define('MINIFY_TESTS_YUICOMPRESSOR_JAR_FILE', '/usr/share/java/yuicompressor.jar');
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function test_Minify_YuiCSS()
|
function test_Minify_YuiCSS()
|
||||||
{
|
{
|
||||||
// XXX this may need customizing
|
if (!is_file(MINIFY_TESTS_YUICOMPRESSOR_JAR_FILE)) {
|
||||||
Minify_YUICompressor::$jarFile = '/usr/share/java/yuicompressor.jar';
|
echo " Minify_YUICompressor : To test this, install the .jar file and customize the constant in:\n";
|
||||||
|
echo " " . __FILE__ . "\n";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Minify_YUICompressor::$jarFile = MINIFY_TESTS_YUICOMPRESSOR_JAR_FILE;
|
||||||
Minify_YUICompressor::$tempDir = sys_get_temp_dir();
|
Minify_YUICompressor::$tempDir = sys_get_temp_dir();
|
||||||
|
|
||||||
$src = "/* stack overflow test */
|
$src = "/* stack overflow test */
|
||||||
@ -20,9 +30,9 @@ function test_Minify_YuiCSS()
|
|||||||
// unfortunately error output is not caught from yui, so have to guess
|
// unfortunately error output is not caught from yui, so have to guess
|
||||||
try {
|
try {
|
||||||
$minOutput = Minify_YUICompressor::minifyCss($src);
|
$minOutput = Minify_YUICompressor::minifyCss($src);
|
||||||
assertTrue(false, "Expected exception Not thrown");
|
echo " Minify_YUICompressor : Correctly handles input which caused stack overflow in 2.4.6\n";
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
assertTrue($e->getMessage() == 'Minify_YUICompressor : YUI compressor execution failed.', 'got expected Exception');
|
assertTrue($e->getMessage() == 'Minify_YUICompressor : YUI compressor execution failed.', 'Minify_YUICompressor : got expected Exception');
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -9,6 +9,7 @@ require 'test_Minify_Cache_Memcache.php';
|
|||||||
require 'test_Minify_Cache_ZendPlatform.php';
|
require 'test_Minify_Cache_ZendPlatform.php';
|
||||||
require 'test_Minify_CSS.php';
|
require 'test_Minify_CSS.php';
|
||||||
require 'test_Minify_CSS_UriRewriter.php';
|
require 'test_Minify_CSS_UriRewriter.php';
|
||||||
|
require 'test_Minify_ClosureCompiler.php';
|
||||||
require 'test_Minify_JS_ClosureCompiler.php';
|
require 'test_Minify_JS_ClosureCompiler.php';
|
||||||
require 'test_Minify_YuiCSS.php';
|
require 'test_Minify_YuiCSS.php';
|
||||||
require 'test_Minify_CommentPreserver.php';
|
require 'test_Minify_CommentPreserver.php';
|
||||||
|
@ -43,11 +43,11 @@ function test_environment()
|
|||||||
. "cannot modify this, consider setting \$min_documentRoot in config.php\n\n";
|
. "cannot modify this, consider setting \$min_documentRoot in config.php\n\n";
|
||||||
}
|
}
|
||||||
if (isset($_SERVER['SUBDOMAIN_DOCUMENT_ROOT'])) {
|
if (isset($_SERVER['SUBDOMAIN_DOCUMENT_ROOT'])) {
|
||||||
echo "\n!NOTE: environment : \$_SERVER['SUBDOMAIN_DOCUMENT_ROOT'] is set. "
|
echo "\n environment : \$_SERVER['SUBDOMAIN_DOCUMENT_ROOT'] is set. "
|
||||||
. "You may need to set \$min_documentRoot to this in config.php\n";
|
. "You may need to set \$min_documentRoot to this in config.php\n";
|
||||||
}
|
}
|
||||||
if (realpath(__FILE__) !== realpath($_SERVER['DOCUMENT_ROOT'] . '/min_unit_tests/test_environment.php')) {
|
if (realpath(__FILE__) !== realpath($_SERVER['DOCUMENT_ROOT'] . '/min_unit_tests/test_environment.php')) {
|
||||||
echo "!NOTE: environment : /min_unit_tests/ is not directly inside DOCUMENT_ROOT\n";
|
echo " environment : /min_unit_tests/ is not directly inside DOCUMENT_ROOT\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
$thisUrl = 'http://'
|
$thisUrl = 'http://'
|
||||||
@ -59,11 +59,11 @@ function test_environment()
|
|||||||
$oc = @file_get_contents($thisUrl . '?getOutputCompression=1');
|
$oc = @file_get_contents($thisUrl . '?getOutputCompression=1');
|
||||||
|
|
||||||
if (false === $oc || ! preg_match('/^[01]$/', $oc)) {
|
if (false === $oc || ! preg_match('/^[01]$/', $oc)) {
|
||||||
echo "!WARN: environment : Local HTTP request failed. Testing cannot continue.\n";
|
echo "!---: environment : Local HTTP request failed. Testing cannot continue.\n";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if ('1' === $oc) {
|
if ('1' === $oc) {
|
||||||
echo "!WARN: environment : zlib.output_compression is enabled in php.ini"
|
echo "!---: environment : zlib.output_compression is enabled in php.ini"
|
||||||
. " or .htaccess.\n";
|
. " or .htaccess.\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -88,9 +88,9 @@ function test_environment()
|
|||||||
if (! $passed) {
|
if (! $passed) {
|
||||||
$testFake = _test_environment_getHello($thisUrl . '?hello=faketype');
|
$testFake = _test_environment_getHello($thisUrl . '?hello=faketype');
|
||||||
if ($testFake['length'] == 6) {
|
if ($testFake['length'] == 6) {
|
||||||
echo "!NOTE: environment : Server does not auto-encode arbitrary types. This\n"
|
echo " environment : Server does not auto-encode arbitrary types. This\n"
|
||||||
. " may indicate that the auto-encoding is caused by Apache's \n"
|
. " may indicate that the auto-encoding is caused by Apache's\n"
|
||||||
. " AddOutputFilterByType.";
|
. " AddOutputFilterByType.";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user