| 1 |
<?php |
| 2 |
|
| 3 |
namespace Minify\Test; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use Minify_ClosureCompiler; |
| 7 |
|
| 8 |
class MinifyClosureCompilerTest extends TestCase |
| 9 |
{ |
| 10 |
public static function setupBeforeClass() |
| 11 |
{ |
| 12 |
parent::setupBeforeClass(); |
| 13 |
Minify_ClosureCompiler::$isDebug = true; |
| 14 |
|
| 15 |
// To test more functionality, download a compiler.jar from |
| 16 |
// https://github.com/google/closure-compiler#getting-started, |
| 17 |
// put it under tests dir as 'compiler.jar' |
| 18 |
|
| 19 |
// set minimum necessary settings |
| 20 |
Minify_ClosureCompiler::$jarFile = __DIR__ . DIRECTORY_SEPARATOR . 'compiler.jar'; |
| 21 |
Minify_ClosureCompiler::$tempDir = sys_get_temp_dir(); |
| 22 |
} |
| 23 |
|
| 24 |
/* |
| 25 |
* Test minimisation w/o setting the necessary settings |
| 26 |
*/ |
| 27 |
public function test1() |
| 28 |
{ |
| 29 |
// clear params |
| 30 |
Minify_ClosureCompiler::$jarFile = null; |
| 31 |
Minify_ClosureCompiler::$tempDir = null; |
| 32 |
try { |
| 33 |
Minify_ClosureCompiler::minify(''); |
| 34 |
$this->fail(); |
| 35 |
} catch (Exception $e) { |
| 36 |
$this->assertInstanceOf('Minify_ClosureCompiler_Exception', $e); |
| 37 |
} |
| 38 |
// redo init to make other tests pass |
| 39 |
self::setupBeforeClass(); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Test minimisation with the minimum necessary settings |
| 44 |
*/ |
| 45 |
public function test2() |
| 46 |
{ |
| 47 |
$this->assertHasJar(); |
| 48 |
$src = " |
| 49 |
(function (window, undefined){ |
| 50 |
function addOne(input) { |
| 51 |
return 1 + input; |
| 52 |
} |
| 53 |
window.addOne = addOne; |
| 54 |
window.undefined = undefined; |
| 55 |
})(window); |
| 56 |
"; |
| 57 |
$minExpected = "(function(a,b){a.addOne=function(a){return 1+a};a.undefined=b})(window);"; |
| 58 |
$minOutput = Minify_ClosureCompiler::minify($src); |
| 59 |
$this->assertSame($minExpected, $minOutput, 'minimum necessary settings'); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Test minimisation with advanced compilation level |
| 64 |
*/ |
| 65 |
public function test3() |
| 66 |
{ |
| 67 |
$this->assertHasJar(); |
| 68 |
$src = "function unused() {};"; |
| 69 |
$minExpected = ''; |
| 70 |
$options = array( |
| 71 |
'compilation_level' => 'ADVANCED_OPTIMIZATIONS' |
| 72 |
); |
| 73 |
$minOutput = Minify_ClosureCompiler::minify($src, $options); |
| 74 |
$this->assertSame($minExpected, $minOutput, 'advanced optimizations'); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Test that closure compiler does not produce unneeded noise |
| 79 |
* |
| 80 |
* @see https://code.google.com/p/closure-compiler/issues/detail?id=513 |
| 81 |
* |
| 82 |
* NOTE: this test does not actually cover it, result is manually verified. |
| 83 |
*/ |
| 84 |
public function test4() |
| 85 |
{ |
| 86 |
$this->assertHasJar(); |
| 87 |
|
| 88 |
$src = $this->getDataFile('bug-513.js'); |
| 89 |
$minExpected = 'var a=4;'; |
| 90 |
$minOutput = Minify_ClosureCompiler::minify($src); |
| 91 |
$this->assertSame($minExpected, $minOutput, 'advanced optimizations'); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Test that language_in parameter has effect. |
| 96 |
*/ |
| 97 |
public function testLanguageOptions() |
| 98 |
{ |
| 99 |
$this->assertHasJar(); |
| 100 |
|
| 101 |
$src = $this->getDataFile('js/jscomp.polyfill.js'); |
| 102 |
$exp = $this->getDataFile('js/jscomp.polyfill.min.js'); |
| 103 |
$options = array( |
| 104 |
'language_in' => 'ECMASCRIPT3', |
| 105 |
); |
| 106 |
|
| 107 |
$res = Minify_ClosureCompiler::minify($src, $options); |
| 108 |
$this->assertSame($exp, $res); |
| 109 |
|
| 110 |
$options = array( |
| 111 |
'language_in' => 'ECMASCRIPT6', |
| 112 |
); |
| 113 |
$exp = $this->getDataFile('js/jscomp.polyfilled.min.js'); |
| 114 |
$res = Minify_ClosureCompiler::minify($src, $options); |
| 115 |
$this->assertSame($exp, $res); |
| 116 |
} |
| 117 |
|
| 118 |
protected function assertHasJar() |
| 119 |
{ |
| 120 |
$this->assertNotEmpty(Minify_ClosureCompiler::$jarFile); |
| 121 |
try { |
| 122 |
$this->assertFileExists(Minify_ClosureCompiler::$jarFile, "Have closure compiler compiler.jar"); |
| 123 |
} catch (Exception $e) { |
| 124 |
$this->markTestSkipped($e->getMessage()); |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
|