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

closure compiler: handle better when api is limited

This commit is contained in:
Elan Ruusamäe
2015-11-18 18:00:26 +02:00
committed by Steve Clay
parent badb6ce191
commit 316b384b1f

View File

@@ -1,7 +1,9 @@
<?php <?php
class JsClosureCompilerTest extends PHPUnit_Framework_TestCase { class JsClosureCompilerTest extends PHPUnit_Framework_TestCase
public function test1() { {
public function test1()
{
$src = " $src = "
(function (window, undefined){ (function (window, undefined){
function addOne(input) { function addOne(input) {
@@ -12,43 +14,43 @@ class JsClosureCompilerTest extends PHPUnit_Framework_TestCase {
})(window); })(window);
"; ";
$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 = $this->compile($src);
// Too many recent calls to Closure Compiler API to test.\n";
$this->assertNotContains('Error(22): Too many compiles', $minOutput);
$this->assertSame($minExpected, $minOutput, 'Minify_JS_ClosureCompiler : Overall'); $this->assertSame($minExpected, $minOutput, 'Minify_JS_ClosureCompiler : Overall');
} }
public function test2() { public function test2()
{
$src = "function blah({ return 'blah';} "; $src = "function blah({ return 'blah';} ";
$e = null;
try { try {
Minify_JS_ClosureCompiler::minify($src); $this->compile($src);
$this->fail('Expected exception not thrown'); } catch (Minify_JS_ClosureCompiler_Exception $e) {
} catch (Exception $e) {
$this->assertInstanceOf('Minify_JS_ClosureCompiler_Exception', $e, 'Throws Minify_JS_ClosureCompiler_Exception');
} }
$this->assertInstanceOf('Minify_JS_ClosureCompiler_Exception', $e, 'Throws Minify_JS_ClosureCompiler_Exception');
} }
// Test maximum byte size check (default) // Test maximum byte size check (default)
public function test3() { public function test3()
{
$fn = "(function() {})();"; $fn = "(function() {})();";
$src = str_repeat($fn, ceil(Minify_JS_ClosureCompiler::DEFAULT_MAX_BYTES / strlen($fn))); $src = str_repeat($fn, ceil(Minify_JS_ClosureCompiler::DEFAULT_MAX_BYTES / strlen($fn)));
$e = null;
try { try {
Minify_JS_ClosureCompiler::minify($src); $this->compile($src);
$this->fail('Expected exception not thrown'); } catch (Minify_JS_ClosureCompiler_Exception $e) {
} catch (Exception $e) { }
$this->assertInstanceOf('Minify_JS_ClosureCompiler_Exception', $e, 'Throws Minify_JS_ClosureCompiler_Exception'); $this->assertInstanceOf('Minify_JS_ClosureCompiler_Exception', $e, 'Throws Minify_JS_ClosureCompiler_Exception');
$expected = 'POST content larger than ' . Minify_JS_ClosureCompiler::DEFAULT_MAX_BYTES . ' bytes'; $expected = 'POST content larger than ' . Minify_JS_ClosureCompiler::DEFAULT_MAX_BYTES . ' bytes';
$this->assertEquals($expected, $e->getMessage(), 'Message must tell how big maximum byte size is'); $this->assertEquals($expected, $e->getMessage(), 'Message must tell how big maximum byte size is');
} }
}
// Test maximum byte size check (no limit) // Test maximum byte size check (no limit)
public function test4() { public function test4()
{
$src = "(function(){})();"; $src = "(function(){})();";
$minOutput = Minify_JS_ClosureCompiler::minify($src, array( $minOutput = $this->compile($src, array(
Minify_JS_ClosureCompiler::OPTION_MAX_BYTES => 0 Minify_JS_ClosureCompiler::OPTION_MAX_BYTES => 0
)); ));
@@ -56,43 +58,69 @@ class JsClosureCompilerTest extends PHPUnit_Framework_TestCase {
} }
// Test maximum byte size check (custom) // Test maximum byte size check (custom)
public function test5() { public function test5()
{
$src = "(function() {})();"; $src = "(function() {})();";
$allowedBytes = 5; $allowedBytes = 5;
$e = null;
try { try {
Minify_JS_ClosureCompiler::minify($src, array( $this->compile($src, array(
Minify_JS_ClosureCompiler::OPTION_MAX_BYTES => $allowedBytes Minify_JS_ClosureCompiler::OPTION_MAX_BYTES => $allowedBytes
)); ));
} catch (Exception $e) { } catch (Minify_JS_ClosureCompiler_Exception $e) {
}
$this->assertInstanceOf('Minify_JS_ClosureCompiler_Exception', $e, 'Throws Minify_JS_ClosureCompiler_Exception'); $this->assertInstanceOf('Minify_JS_ClosureCompiler_Exception', $e, 'Throws Minify_JS_ClosureCompiler_Exception');
$expected = 'POST content larger than ' . $allowedBytes . ' bytes'; $expected = 'POST content larger than ' . $allowedBytes . ' bytes';
$this->assertEquals($expected, $e->getMessage(), 'Message must tell how big maximum byte size is'); $this->assertEquals($expected, $e->getMessage(), 'Message must tell how big maximum byte size is');
} }
}
// Test additional options passed to HTTP request // Test additional options passed to HTTP request
public function test6() { public function test6()
{
$ecmascript5 = "[1,].length;"; $ecmascript5 = "[1,].length;";
$exc = null; $e = null;
try { try {
Minify_JS_ClosureCompiler::minify($ecmascript5); $this->compile($ecmascript5);
$this->fail('Expected exception not thrown'); } catch (Minify_JS_ClosureCompiler_Exception $e) {
}
} catch (Exception $e) {
$this->assertInstanceOf('Minify_JS_ClosureCompiler_Exception', $e, 'Throws Minify_JS_ClosureCompiler_Exception'); $this->assertInstanceOf('Minify_JS_ClosureCompiler_Exception', $e, 'Throws Minify_JS_ClosureCompiler_Exception');
} }
}
public function test7() { public function test7()
{
$ecmascript5 = "[1,].length;"; $ecmascript5 = "[1,].length;";
$minExpected = '1;'; $minExpected = '1;';
$minOutput = Minify_JS_ClosureCompiler::minify($ecmascript5, array( $minOutput = $this->compile($ecmascript5, array(
Minify_JS_ClosureCompiler::OPTION_ADDITIONAL_OPTIONS => array( Minify_JS_ClosureCompiler::OPTION_ADDITIONAL_OPTIONS => array(
'language' => 'ECMASCRIPT5' 'language' => 'ECMASCRIPT5'
) )
)); ));
$this->assertSame($minExpected, $minOutput, 'Language option should make it compile'); $this->assertSame($minExpected, $minOutput, 'Language option should make it compile');
} }
/**
* Call closure compiler, but intercept API limit errors.
*
* @param string $script
* @param array $options
* @return string
*/
private function compile($script, $options = array())
{
$result = Minify_JS_ClosureCompiler::minify($script, $options);
// output may contain an error message, and original source:
// /* Received errors from Closure Compiler API:
// Error(22): Too many compiles performed recently. Try again later.
// (Using fallback minifier)
// */
// (function(window,undefined){function addOne(input){return 1+input;}
// window.addOne=addOne;window.undefined=undefined;})(window);
$this->assertNotContains('Error(22): Too many compiles', $result);
return $result;
}
} }