diff --git a/.gitignore b/.gitignore index ca2c1a9..f3f3bdf 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ /composer.lock /vendor /.php_cs.cache +/tests/compiler.jar diff --git a/.travis.yml b/.travis.yml index e1a2519..359e8e2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -25,8 +25,14 @@ cache: install: - composer update --no-interaction --prefer-source +before_script: + - wget -c https://dl.google.com/closure-compiler/compiler-latest.zip -O vendor/compiler-latest.zip + - unzip -fd vendor/closure-compiler vendor/compiler-latest.zip + - ln -sfn ../$(echo vendor/closure-compiler/closure-compiler-*.jar) tests/compiler.jar + - java -jar tests/compiler.jar --version + script: - composer validate - - phpunit + - phpunit --verbose # vim:ts=2:sw=2:et diff --git a/lib/Minify/ClosureCompiler.php b/lib/Minify/ClosureCompiler.php index 487cafe..26cbc67 100644 --- a/lib/Minify/ClosureCompiler.php +++ b/lib/Minify/ClosureCompiler.php @@ -24,19 +24,12 @@ * * * - * @todo unit tests, $options docs - * @todo more options support (or should just passthru them all?) - * * @package Minify * @author Stephen Clay * @author Elan Ruusamäe */ class Minify_ClosureCompiler { - const OPTION_CHARSET = 'charset'; - const OPTION_COMPILATION_LEVEL = 'compilation_level'; - const OPTION_WARNING_LEVEL = 'warning_level'; - public static $isDebug = false; /** @@ -61,6 +54,17 @@ class Minify_ClosureCompiler */ public static $javaExecutable = 'java'; + /** + * Default command line options passed to closure-compiler + * + * @var array + */ + public static $defaultOptions = array( + 'charset' => 'utf-8', + 'compilation_level' => 'SIMPLE_OPTIMIZATIONS', + 'warning_level' => 'QUIET', + ); + /** * Minify a Javascript string * @@ -137,7 +141,8 @@ class Minify_ClosureCompiler $this->checkJar(self::$jarFile); $server = array( self::$javaExecutable, - '-jar', escapeshellarg(self::$jarFile) + '-jar', + escapeshellarg(self::$jarFile) ); return $server; @@ -151,24 +156,13 @@ class Minify_ClosureCompiler { $args = array(); - $o = array_merge( - array( - self::OPTION_CHARSET => 'utf-8', - self::OPTION_COMPILATION_LEVEL => 'SIMPLE_OPTIMIZATIONS', - self::OPTION_WARNING_LEVEL => 'QUIET', - ), + $options = array_merge( + static::$defaultOptions, $userOptions ); - $charsetOption = $o[self::OPTION_CHARSET]; - if (preg_match('/^[\\da-zA-Z0-9\\-]+$/', $charsetOption)) { - $args[] = "--charset {$charsetOption}"; - } - - foreach (array(self::OPTION_COMPILATION_LEVEL, self::OPTION_WARNING_LEVEL) as $opt) { - if ($o[$opt]) { - $args[] = "--{$opt} " . escapeshellarg($o[$opt]); - } + foreach ($options as $key => $value) { + $args[] = "--{$key} " . escapeshellarg($value); } return $args; diff --git a/tests/LessSourceTest.php b/tests/LessSourceTest.php index f61045c..a2b5158 100644 --- a/tests/LessSourceTest.php +++ b/tests/LessSourceTest.php @@ -24,9 +24,7 @@ class LessSourceTest extends TestCase touch($includedLess); $mtime1 = filemtime($mainLess); - var_dump($mtime1); $mtime2 = filemtime($includedLess); - var_dump($mtime2); $max = max($mtime1, $mtime2); diff --git a/tests/MinifyClosureCompilerTest.php b/tests/MinifyClosureCompilerTest.php index ea93eea..84dc1e3 100644 --- a/tests/MinifyClosureCompilerTest.php +++ b/tests/MinifyClosureCompilerTest.php @@ -63,7 +63,7 @@ class MinifyClosureCompilerTest extends TestCase $src = "function unused() {};"; $minExpected = ''; $options = array( - Minify_ClosureCompiler::OPTION_COMPILATION_LEVEL => 'ADVANCED_OPTIMIZATIONS' + 'compilation_level' => 'ADVANCED_OPTIMIZATIONS' ); $minOutput = Minify_ClosureCompiler::minify($src, $options); $this->assertSame($minExpected, $minOutput, 'advanced optimizations'); @@ -80,12 +80,36 @@ class MinifyClosureCompilerTest extends TestCase { $this->assertHasJar(); - $src = file_get_contents(self::$test_files . '/bug-513.js'); + $src = $this->getDataFile('bug-513.js'); $minExpected = 'var a=4;'; $minOutput = Minify_ClosureCompiler::minify($src); $this->assertSame($minExpected, $minOutput, 'advanced optimizations'); } + /** + * Test that language_in parameter has effect. + */ + public function testLanguageOptions() + { + $this->assertHasJar(); + + $src = $this->getDataFile('js/jscomp.polyfill.js'); + $exp = $this->getDataFile('js/jscomp.polyfill.min.js'); + $options = array( + 'language_in' => 'ECMASCRIPT3', + ); + + $res = Minify_ClosureCompiler::minify($src, $options); + $this->assertSame($exp, $res); + + $options = array( + 'language_in' => 'ECMASCRIPT6', + ); + $exp = $this->getDataFile('js/jscomp.polyfilled.min.js'); + $res = Minify_ClosureCompiler::minify($src, $options); + $this->assertSame($exp, $res); + } + protected function assertHasJar() { $this->assertNotEmpty(Minify_ClosureCompiler::$jarFile); diff --git a/tests/MinifyNailgunClosureCompilerTest.php b/tests/MinifyNailgunClosureCompilerTest.php index caeeb70..572ed5f 100644 --- a/tests/MinifyNailgunClosureCompilerTest.php +++ b/tests/MinifyNailgunClosureCompilerTest.php @@ -20,7 +20,7 @@ class MinifyNailgunClosureCompilerTest extends TestCase /** * Test minimisation with the minimum necessary settings */ - public function test2() + public function test1() { $this->assertHasJar(); $src = " @@ -40,8 +40,10 @@ class MinifyNailgunClosureCompilerTest extends TestCase protected function assertHasJar() { $this->assertNotEmpty(Minify_ClosureCompiler::$jarFile); + $this->assertNotEmpty(Minify_NailgunClosureCompiler::$ngJarFile); try { $this->assertFileExists(Minify_ClosureCompiler::$jarFile, "Have closure compiler compiler.jar"); + $this->assertFileExists(Minify_NailgunClosureCompiler::$ngJarFile, "Have nailgun.jar"); } catch (Exception $e) { $this->markTestSkipped($e->getMessage()); } diff --git a/tests/ScssSourceTest.php b/tests/ScssSourceTest.php index a1581ef..c37ca36 100644 --- a/tests/ScssSourceTest.php +++ b/tests/ScssSourceTest.php @@ -24,9 +24,7 @@ class ScssSourceTest extends TestCase touch($includedLess); $mtime1 = filemtime($mainLess); - var_dump($mtime1); $mtime2 = filemtime($includedLess); - var_dump($mtime2); $max = max($mtime1, $mtime2); diff --git a/tests/TestCase.php b/tests/TestCase.php index a871461..f763412 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -47,4 +47,23 @@ class TestCase extends PHPUnit_Framework_TestCase $this->assertSame($data, $displayed, "$id display"); $this->assertEquals($data, $cache->fetch($id), "$id fetch"); } + + /** + * Read data file, assert that it exists and is not empty. + * As a side effect calls trim() to fight against different Editors that insert or strip final newline. + * + * @param string $filename + * @return string + */ + protected function getDataFile($filename) + { + $path = self::$test_files . '/' . $filename; + $this->assertFileExists($path); + $contents = file_get_contents($path); + $this->assertNotEmpty($contents); + $contents = trim($contents); + $this->assertNotEmpty($contents); + + return $contents; + } } \ No newline at end of file diff --git a/tests/_test_files/js/jscomp.polyfill.js b/tests/_test_files/js/jscomp.polyfill.js new file mode 100644 index 0000000..f1da735 --- /dev/null +++ b/tests/_test_files/js/jscomp.polyfill.js @@ -0,0 +1,7 @@ +(function() { + /** + * @type {string} + */ + var $array = jQuery.find('#div'); + print($array.find('a')); +})(); \ No newline at end of file diff --git a/tests/_test_files/js/jscomp.polyfill.min.js b/tests/_test_files/js/jscomp.polyfill.min.js new file mode 100644 index 0000000..6146151 --- /dev/null +++ b/tests/_test_files/js/jscomp.polyfill.min.js @@ -0,0 +1 @@ +(function(){var a=jQuery.find("#div");print(a.find("a"))})(); \ No newline at end of file diff --git a/tests/_test_files/js/jscomp.polyfilled.min.js b/tests/_test_files/js/jscomp.polyfilled.min.js new file mode 100644 index 0000000..faa4d04 --- /dev/null +++ b/tests/_test_files/js/jscomp.polyfilled.min.js @@ -0,0 +1,3 @@ +var $jscomp={scope:{},findInternal:function(a,c,b){a instanceof String&&(a=String(a));for(var d=a.length,e=0;e