1
0
mirror of https://github.com/mrclay/minify.git synced 2025-08-11 00:24:11 +02:00

port MinifyClosureCompilerTest to phpunit

This commit is contained in:
Elan Ruusamäe
2015-11-18 17:33:01 +02:00
committed by Steve Clay
parent a856756705
commit 4f344b5d21
3 changed files with 72 additions and 76 deletions

View File

@@ -1,75 +0,0 @@
<?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();

View File

@@ -9,7 +9,6 @@ require 'test_Minify_Cache_WinCache.php';
require 'test_Minify_Cache_ZendPlatform.php';
require 'test_Minify_CSS.php';
require 'test_Minify_CSS_UriRewriter.php';
require 'test_Minify_ClosureCompiler.php';
require 'test_Minify_YuiCSS.php';
require 'test_Minify_HTML.php';
require 'test_Minify_ImportProcessor.php';

View File

@@ -0,0 +1,72 @@
<?php
class MinifyClosureCompilerTest extends TestCase
{
public static function setupBeforeClass()
{
parent::setupBeforeClass();
Minify_ClosureCompiler::$isDebug = true;
// To test more functionality, download a compiler.jar from
// https://code.google.com/p/closure-compiler/wiki/BinaryDownloads,
// put it under tests dir as 'compiler.jar'
// set minimum necessary settings
Minify_ClosureCompiler::$jarFile = __DIR__ . DIRECTORY_SEPARATOR . 'compiler.jar';
Minify_ClosureCompiler::$tempDir = sys_get_temp_dir();
}
/*
* Test minification w/o setting the necessary settings
*/
public function test1()
{
Minify_ClosureCompiler::$jarFile = null;
Minify_ClosureCompiler::$tempDir = null;
try {
Minify_ClosureCompiler::minify('');
$this->fail();
} catch (Exception $e) {
$this->assertInstanceOf('Minify_ClosureCompiler_Exception', $e);
}
}
/**
* Test minification with the minimum necessary settings
*/
public function test2()
{
$this->assertHasCompilerJar();
$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);
$this->assertSame($minExpected, $minOutput, 'minimum necessary settings');
}
/**
* Test minification with advanced compilation level
*/
public function test3()
{
$this->assertHasCompilerJar();
$src = "function unused() {};";
$minExpected = '';
$minOutput = Minify_ClosureCompiler::minify($src, array(
Minify_ClosureCompiler::OPTION_COMPILATION_LEVEL => 'ADVANCED_OPTIMIZATIONS'
));
$this->assertSame($minExpected, $minOutput, 'advanced optimizations');
}
protected function assertHasCompilerJar()
{
$this->assertFileExists(Minify_ClosureCompiler::$jarFile, "Have closure compiler compiler.jar");
}
}