From 8949df33ddf631eec09a1757bbda865b9d64610f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Tue, 6 Dec 2016 14:46:46 +0200 Subject: [PATCH 1/7] add testcase for js minify that produces $jscomp polyfills with recent closure compiler default input language has changed from ES3 to ES6 produces $jscomp polyfills --- tests/MinifyClosureCompilerTest.php | 12 +++++++++++- tests/TestCase.php | 19 +++++++++++++++++++ tests/_test_files/js/jscomp.polyfill.js | 7 +++++++ tests/_test_files/js/jscomp.polyfill.min.js | 1 + 4 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 tests/_test_files/js/jscomp.polyfill.js create mode 100644 tests/_test_files/js/jscomp.polyfill.min.js diff --git a/tests/MinifyClosureCompilerTest.php b/tests/MinifyClosureCompilerTest.php index ea93eea..13c88d0 100644 --- a/tests/MinifyClosureCompilerTest.php +++ b/tests/MinifyClosureCompilerTest.php @@ -80,12 +80,22 @@ 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'); } + public function testOptions() + { + $this->assertHasJar(); + + $src = $this->getDataFile('js/jscomp.polyfill.js'); + $exp = $this->getDataFile('js/jscomp.polyfill.min.js'); + $res = Minify_ClosureCompiler::minify($src); + $this->assertSame($exp, $res); + } + protected function assertHasJar() { $this->assertNotEmpty(Minify_ClosureCompiler::$jarFile); 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 From 6a330d9091a16ca62fcd38a70e6e64bceccb3314 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Tue, 6 Dec 2016 16:59:36 +0200 Subject: [PATCH 2/7] pass any option to closure compiler --- lib/Minify/ClosureCompiler.php | 40 ++++++++++++----------------- tests/MinifyClosureCompilerTest.php | 2 +- 2 files changed, 18 insertions(+), 24 deletions(-) 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/MinifyClosureCompilerTest.php b/tests/MinifyClosureCompilerTest.php index 13c88d0..15a1d5f 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'); From 8fe915f4864674e90f20343452c63c9bfdafdd7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Tue, 6 Dec 2016 17:00:58 +0200 Subject: [PATCH 3/7] test $jscomp polyfill output --- tests/MinifyClosureCompilerTest.php | 18 ++++++++++++++++-- tests/_test_files/js/jscomp.polyfilled.min.js | 3 +++ 2 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 tests/_test_files/js/jscomp.polyfilled.min.js diff --git a/tests/MinifyClosureCompilerTest.php b/tests/MinifyClosureCompilerTest.php index 15a1d5f..84dc1e3 100644 --- a/tests/MinifyClosureCompilerTest.php +++ b/tests/MinifyClosureCompilerTest.php @@ -86,13 +86,27 @@ class MinifyClosureCompilerTest extends TestCase $this->assertSame($minExpected, $minOutput, 'advanced optimizations'); } - public function testOptions() + /** + * 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'); - $res = Minify_ClosureCompiler::minify($src); + $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); } 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 Date: Tue, 6 Dec 2016 17:02:26 +0200 Subject: [PATCH 4/7] remove var_dump debug from test output --- tests/LessSourceTest.php | 2 -- tests/ScssSourceTest.php | 2 -- 2 files changed, 4 deletions(-) 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/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); From 005dc26e2226f59a59eb4be402f554cd95bf3d1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Tue, 6 Dec 2016 17:04:34 +0200 Subject: [PATCH 5/7] tests: skip rather than fail if nailgun.jar is missing --- tests/MinifyNailgunClosureCompilerTest.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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()); } From 56d55abc013950abb2d05c303ecd44ac6600774c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Tue, 6 Dec 2016 17:08:58 +0200 Subject: [PATCH 6/7] travis: install closure compiler --- .gitignore | 1 + .travis.yml | 6 ++++++ 2 files changed, 7 insertions(+) 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..06b5093 100644 --- a/.travis.yml +++ b/.travis.yml @@ -25,6 +25,12 @@ 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 From 6fc0648a0d3ca1648c7af946425152d95ac5633c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Tue, 6 Dec 2016 17:16:37 +0200 Subject: [PATCH 7/7] phpunit: verbose output of run summary --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 06b5093..359e8e2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -33,6 +33,6 @@ before_script: script: - composer validate - - phpunit + - phpunit --verbose # vim:ts=2:sw=2:et