| 1 |
<?php |
| 2 |
|
| 3 |
namespace Minify\Test; |
| 4 |
|
| 5 |
use Minify_JS_ClosureCompiler; |
| 6 |
use Minify_JS_ClosureCompiler_Exception; |
| 7 |
|
| 8 |
class JsClosureCompilerTest extends TestCase |
| 9 |
{ |
| 10 |
public function test1() |
| 11 |
{ |
| 12 |
$src = " |
| 13 |
(function (window, undefined){ |
| 14 |
function addOne(input) { |
| 15 |
return 1 + input; |
| 16 |
} |
| 17 |
window.addOne = addOne; |
| 18 |
window.undefined = undefined; |
| 19 |
})(window); |
| 20 |
"; |
| 21 |
$minExpected = "(function(a,b){a.addOne=function(a){return 1+a};a.undefined=b})(window);"; |
| 22 |
$minOutput = $this->compile($src); |
| 23 |
|
| 24 |
$this->assertSame($minExpected, $minOutput, 'Minify_JS_ClosureCompiler : Overall'); |
| 25 |
} |
| 26 |
|
| 27 |
public function test2() |
| 28 |
{ |
| 29 |
$src = "function blah({ return 'blah';} "; |
| 30 |
$e = null; |
| 31 |
try { |
| 32 |
$this->compile($src); |
| 33 |
} catch (Minify_JS_ClosureCompiler_Exception $e) { |
| 34 |
} |
| 35 |
$this->assertInstanceOf( |
| 36 |
'Minify_JS_ClosureCompiler_Exception', |
| 37 |
$e, |
| 38 |
'Throws Minify_JS_ClosureCompiler_Exception' |
| 39 |
); |
| 40 |
} |
| 41 |
|
| 42 |
// Test maximum byte size check (default) |
| 43 |
public function test3() |
| 44 |
{ |
| 45 |
$fn = "(function() {})();"; |
| 46 |
$src = str_repeat($fn, ceil(Minify_JS_ClosureCompiler::DEFAULT_MAX_BYTES / strlen($fn))); |
| 47 |
$e = null; |
| 48 |
try { |
| 49 |
$this->compile($src); |
| 50 |
} catch (Minify_JS_ClosureCompiler_Exception $e) { |
| 51 |
} |
| 52 |
$this->assertInstanceOf( |
| 53 |
'Minify_JS_ClosureCompiler_Exception', |
| 54 |
$e, |
| 55 |
'Throws Minify_JS_ClosureCompiler_Exception' |
| 56 |
); |
| 57 |
|
| 58 |
$expected = 'POST content larger than ' . Minify_JS_ClosureCompiler::DEFAULT_MAX_BYTES . ' bytes'; |
| 59 |
$this->assertEquals($expected, $e->getMessage(), 'Message must tell how big maximum byte size is'); |
| 60 |
} |
| 61 |
|
| 62 |
// Test maximum byte size check (no limit) |
| 63 |
public function test4() |
| 64 |
{ |
| 65 |
$src = "(function(){})();"; |
| 66 |
$minOutput = $this->compile($src, array( |
| 67 |
Minify_JS_ClosureCompiler::OPTION_MAX_BYTES => 0, |
| 68 |
)); |
| 69 |
|
| 70 |
$this->assertSame($src, $minOutput, 'With no limit set, it should compile properly'); |
| 71 |
} |
| 72 |
|
| 73 |
// Test maximum byte size check (custom) |
| 74 |
public function test5() |
| 75 |
{ |
| 76 |
$src = "(function() {})();"; |
| 77 |
$allowedBytes = 5; |
| 78 |
$e = null; |
| 79 |
try { |
| 80 |
$this->compile($src, array( |
| 81 |
Minify_JS_ClosureCompiler::OPTION_MAX_BYTES => $allowedBytes, |
| 82 |
)); |
| 83 |
} catch (Minify_JS_ClosureCompiler_Exception $e) { |
| 84 |
} |
| 85 |
$this->assertInstanceOf( |
| 86 |
'Minify_JS_ClosureCompiler_Exception', |
| 87 |
$e, |
| 88 |
'Throws Minify_JS_ClosureCompiler_Exception' |
| 89 |
); |
| 90 |
|
| 91 |
$expected = 'POST content larger than ' . $allowedBytes . ' bytes'; |
| 92 |
$this->assertEquals($expected, $e->getMessage(), 'Message must tell how big maximum byte size is'); |
| 93 |
} |
| 94 |
|
| 95 |
// Test additional options passed to HTTP request |
| 96 |
public function test6() |
| 97 |
{ |
| 98 |
$ecmascript3 = "[1,].length;"; |
| 99 |
$e = null; |
| 100 |
try { |
| 101 |
$this->compile($ecmascript3, array( |
| 102 |
Minify_JS_ClosureCompiler::OPTION_ADDITIONAL_OPTIONS => array( |
| 103 |
'language' => 'ECMASCRIPT3', |
| 104 |
), |
| 105 |
)); |
| 106 |
} catch (Minify_JS_ClosureCompiler_Exception $e) { |
| 107 |
} |
| 108 |
$this->assertInstanceOf( |
| 109 |
'Minify_JS_ClosureCompiler_Exception', |
| 110 |
$e, |
| 111 |
'Throws Minify_JS_ClosureCompiler_Exception' |
| 112 |
); |
| 113 |
} |
| 114 |
|
| 115 |
public function test7() |
| 116 |
{ |
| 117 |
$ecmascript5 = "[1,].length;"; |
| 118 |
|
| 119 |
$minExpected = '1;'; |
| 120 |
$minOutput = $this->compile($ecmascript5, array( |
| 121 |
Minify_JS_ClosureCompiler::OPTION_ADDITIONAL_OPTIONS => array( |
| 122 |
'language' => 'ECMASCRIPT5', |
| 123 |
), |
| 124 |
)); |
| 125 |
$this->assertSame($minExpected, $minOutput, 'Language option should make it compile'); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Call closure compiler, but intercept API limit errors. |
| 130 |
* |
| 131 |
* @param string $script |
| 132 |
* @param array $options |
| 133 |
* @return string |
| 134 |
*/ |
| 135 |
private function compile($script, $options = array()) |
| 136 |
{ |
| 137 |
$result = Minify_JS_ClosureCompiler::minify($script, $options); |
| 138 |
|
| 139 |
// output may contain an error message, and original source: |
| 140 |
// /* Received errors from Closure Compiler API: |
| 141 |
// Error(22): Too many compiles performed recently. Try again later. |
| 142 |
// (Using fallback minifier) |
| 143 |
// */ |
| 144 |
// (function(window,undefined){function addOne(input){return 1+input;} |
| 145 |
// window.addOne=addOne;window.undefined=undefined;})(window); |
| 146 |
|
| 147 |
$this->assertNotContains('Error(22): Too many compiles', $result); |
| 148 |
|
| 149 |
return $result; |
| 150 |
} |
| 151 |
} |
| 152 |
|