mirror of
https://github.com/mrclay/minify.git
synced 2025-08-12 17:14:24 +02:00
closure compiler: handle better when api is limited
This commit is contained in:
committed by
Steve Clay
parent
badb6ce191
commit
316b384b1f
@@ -1,8 +1,10 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
class JsClosureCompilerTest extends PHPUnit_Framework_TestCase {
|
class JsClosureCompilerTest extends PHPUnit_Framework_TestCase
|
||||||
public function test1() {
|
{
|
||||||
$src = "
|
public function test1()
|
||||||
|
{
|
||||||
|
$src = "
|
||||||
(function (window, undefined){
|
(function (window, undefined){
|
||||||
function addOne(input) {
|
function addOne(input) {
|
||||||
return 1 + input;
|
return 1 + input;
|
||||||
@@ -11,88 +13,114 @@ class JsClosureCompilerTest extends PHPUnit_Framework_TestCase {
|
|||||||
window.undefined = undefined;
|
window.undefined = undefined;
|
||||||
})(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->assertSame($minExpected, $minOutput, 'Minify_JS_ClosureCompiler : Overall');
|
||||||
$this->assertNotContains('Error(22): Too many compiles', $minOutput);
|
}
|
||||||
|
|
||||||
$this->assertSame($minExpected, $minOutput, 'Minify_JS_ClosureCompiler : Overall');
|
public function test2()
|
||||||
}
|
{
|
||||||
|
$src = "function blah({ return 'blah';} ";
|
||||||
|
$e = null;
|
||||||
|
try {
|
||||||
|
$this->compile($src);
|
||||||
|
} catch (Minify_JS_ClosureCompiler_Exception $e) {
|
||||||
|
}
|
||||||
|
$this->assertInstanceOf('Minify_JS_ClosureCompiler_Exception', $e, 'Throws Minify_JS_ClosureCompiler_Exception');
|
||||||
|
}
|
||||||
|
|
||||||
public function test2() {
|
// Test maximum byte size check (default)
|
||||||
$src = "function blah({ return 'blah';} ";
|
public function test3()
|
||||||
try {
|
{
|
||||||
Minify_JS_ClosureCompiler::minify($src);
|
$fn = "(function() {})();";
|
||||||
$this->fail('Expected exception not thrown');
|
$src = str_repeat($fn, ceil(Minify_JS_ClosureCompiler::DEFAULT_MAX_BYTES / strlen($fn)));
|
||||||
} catch (Exception $e) {
|
$e = null;
|
||||||
$this->assertInstanceOf('Minify_JS_ClosureCompiler_Exception', $e, 'Throws Minify_JS_ClosureCompiler_Exception');
|
try {
|
||||||
}
|
$this->compile($src);
|
||||||
}
|
} catch (Minify_JS_ClosureCompiler_Exception $e) {
|
||||||
|
}
|
||||||
|
$this->assertInstanceOf('Minify_JS_ClosureCompiler_Exception', $e, 'Throws Minify_JS_ClosureCompiler_Exception');
|
||||||
|
|
||||||
// Test maximum byte size check (default)
|
$expected = 'POST content larger than ' . Minify_JS_ClosureCompiler::DEFAULT_MAX_BYTES . ' bytes';
|
||||||
public function test3() {
|
$this->assertEquals($expected, $e->getMessage(), 'Message must tell how big maximum byte size is');
|
||||||
$fn = "(function() {})();";
|
}
|
||||||
$src = str_repeat($fn, ceil(Minify_JS_ClosureCompiler::DEFAULT_MAX_BYTES / strlen($fn)));
|
|
||||||
try {
|
|
||||||
Minify_JS_ClosureCompiler::minify($src);
|
|
||||||
$this->fail('Expected exception not thrown');
|
|
||||||
} catch (Exception $e) {
|
|
||||||
$this->assertInstanceOf('Minify_JS_ClosureCompiler_Exception', $e, 'Throws Minify_JS_ClosureCompiler_Exception');
|
|
||||||
|
|
||||||
$expected = 'POST content larger than ' . Minify_JS_ClosureCompiler::DEFAULT_MAX_BYTES . ' bytes';
|
// Test maximum byte size check (no limit)
|
||||||
$this->assertEquals($expected, $e->getMessage(), 'Message must tell how big maximum byte size is');
|
public function test4()
|
||||||
}
|
{
|
||||||
}
|
$src = "(function(){})();";
|
||||||
|
$minOutput = $this->compile($src, array(
|
||||||
|
Minify_JS_ClosureCompiler::OPTION_MAX_BYTES => 0
|
||||||
|
));
|
||||||
|
|
||||||
// Test maximum byte size check (no limit)
|
$this->assertSame($src, $minOutput, 'With no limit set, it should compile properly');
|
||||||
public function test4() {
|
}
|
||||||
$src = "(function(){})();";
|
|
||||||
$minOutput = Minify_JS_ClosureCompiler::minify($src, array(
|
|
||||||
Minify_JS_ClosureCompiler::OPTION_MAX_BYTES => 0
|
|
||||||
));
|
|
||||||
|
|
||||||
$this->assertSame($src, $minOutput, 'With no limit set, it should compile properly');
|
// Test maximum byte size check (custom)
|
||||||
}
|
public function test5()
|
||||||
|
{
|
||||||
|
$src = "(function() {})();";
|
||||||
|
$allowedBytes = 5;
|
||||||
|
$e = null;
|
||||||
|
try {
|
||||||
|
$this->compile($src, array(
|
||||||
|
Minify_JS_ClosureCompiler::OPTION_MAX_BYTES => $allowedBytes
|
||||||
|
));
|
||||||
|
} catch (Minify_JS_ClosureCompiler_Exception $e) {
|
||||||
|
}
|
||||||
|
$this->assertInstanceOf('Minify_JS_ClosureCompiler_Exception', $e, 'Throws Minify_JS_ClosureCompiler_Exception');
|
||||||
|
|
||||||
// Test maximum byte size check (custom)
|
$expected = 'POST content larger than ' . $allowedBytes . ' bytes';
|
||||||
public function test5() {
|
$this->assertEquals($expected, $e->getMessage(), 'Message must tell how big maximum byte size is');
|
||||||
$src = "(function() {})();";
|
}
|
||||||
$allowedBytes = 5;
|
|
||||||
try {
|
|
||||||
Minify_JS_ClosureCompiler::minify($src, array(
|
|
||||||
Minify_JS_ClosureCompiler::OPTION_MAX_BYTES => $allowedBytes
|
|
||||||
));
|
|
||||||
} catch (Exception $e) {
|
|
||||||
$this->assertInstanceOf('Minify_JS_ClosureCompiler_Exception', $e, 'Throws Minify_JS_ClosureCompiler_Exception');
|
|
||||||
|
|
||||||
$expected = 'POST content larger than ' . $allowedBytes . ' bytes';
|
// Test additional options passed to HTTP request
|
||||||
$this->assertEquals($expected, $e->getMessage(), 'Message must tell how big maximum byte size is');
|
public function test6()
|
||||||
}
|
{
|
||||||
}
|
$ecmascript5 = "[1,].length;";
|
||||||
|
$e = null;
|
||||||
|
try {
|
||||||
|
$this->compile($ecmascript5);
|
||||||
|
} catch (Minify_JS_ClosureCompiler_Exception $e) {
|
||||||
|
}
|
||||||
|
$this->assertInstanceOf('Minify_JS_ClosureCompiler_Exception', $e, 'Throws Minify_JS_ClosureCompiler_Exception');
|
||||||
|
}
|
||||||
|
|
||||||
// Test additional options passed to HTTP request
|
public function test7()
|
||||||
public function test6() {
|
{
|
||||||
$ecmascript5 = "[1,].length;";
|
$ecmascript5 = "[1,].length;";
|
||||||
$exc = null;
|
|
||||||
try {
|
|
||||||
Minify_JS_ClosureCompiler::minify($ecmascript5);
|
|
||||||
$this->fail('Expected exception not thrown');
|
|
||||||
|
|
||||||
} catch (Exception $e) {
|
$minExpected = '1;';
|
||||||
$this->assertInstanceOf('Minify_JS_ClosureCompiler_Exception', $e, 'Throws Minify_JS_ClosureCompiler_Exception');
|
$minOutput = $this->compile($ecmascript5, array(
|
||||||
}
|
Minify_JS_ClosureCompiler::OPTION_ADDITIONAL_OPTIONS => array(
|
||||||
}
|
'language' => 'ECMASCRIPT5'
|
||||||
|
)
|
||||||
|
));
|
||||||
|
$this->assertSame($minExpected, $minOutput, 'Language option should make it compile');
|
||||||
|
}
|
||||||
|
|
||||||
public function test7() {
|
/**
|
||||||
$ecmascript5 = "[1,].length;";
|
* 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);
|
||||||
|
|
||||||
$minExpected = '1;';
|
// output may contain an error message, and original source:
|
||||||
$minOutput = Minify_JS_ClosureCompiler::minify($ecmascript5, array(
|
// /* Received errors from Closure Compiler API:
|
||||||
Minify_JS_ClosureCompiler::OPTION_ADDITIONAL_OPTIONS => array(
|
// Error(22): Too many compiles performed recently. Try again later.
|
||||||
'language' => 'ECMASCRIPT5'
|
// (Using fallback minifier)
|
||||||
)
|
// */
|
||||||
));
|
// (function(window,undefined){function addOne(input){return 1+input;}
|
||||||
$this->assertSame($minExpected, $minOutput, 'Language option should make it compile');
|
// window.addOne=addOne;window.undefined=undefined;})(window);
|
||||||
}
|
|
||||||
|
$this->assertNotContains('Error(22): Too many compiles', $result);
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user