1
0
mirror of https://github.com/mrclay/minify.git synced 2025-01-29 11:18:30 +01:00

port Minify_JS_ClosureCompiler test to phpunit

This commit is contained in:
Elan Ruusamäe 2015-11-18 14:08:01 +02:00 committed by Steve Clay
parent e2bb6b0565
commit 6838349e3a
4 changed files with 135 additions and 126 deletions

View File

@ -1,125 +0,0 @@
<?php
require_once '_inc.php';
function test_Minify_JS_ClosureCompiler()
{
global $thisDir;
$src = "
(function (window, undefined){
function addOne(input) {
return 1 + input;
}
window.addOne = addOne;
window.undefined = undefined;
})(window);
";
$minExpected = "(function(a,b){a.addOne=function(a){return 1+a};a.undefined=b})(window);";
$minOutput = Minify_JS_ClosureCompiler::minify($src);
if (false !== strpos($minOutput, 'Error(22): Too many compiles')) {
echo "!---: Minify_JS_ClosureCompiler : Too many recent calls to Closure Compiler API to test.\n";
return;
}
$passed = assertTrue($minExpected == $minOutput, 'Minify_JS_ClosureCompiler : Overall');
if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) {
echo "\n---Output: " .countBytes($minOutput). " bytes\n\n{$minOutput}\n\n";
echo "---Expected: " .countBytes($minExpected). " bytes\n\n{$minExpected}\n\n";
echo "---Source: " .countBytes($src). " bytes\n\n{$src}\n\n\n";
}
$src = "function blah({ return 'blah';} ";
$exc = null;
try {
$minOutput = Minify_JS_ClosureCompiler::minify($src);
} catch (Exception $e) {
$exc = $e;
}
$passed = assertTrue(
$exc instanceof Minify_JS_ClosureCompiler_Exception
, 'Minify_JS_ClosureCompiler : Throws Minify_JS_ClosureCompiler_Exception');
if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) {
echo "\n---Message: " . var_export($exc->getMessage(), 1) . "\n\n\n";
}
// Test maximum byte size check (default)
$fn = "(function() {})();";
$src = str_repeat($fn, ceil(Minify_JS_ClosureCompiler::DEFAULT_MAX_BYTES / strlen($fn)));
$exc = null;
try {
$minOutput = Minify_JS_ClosureCompiler::minify($src);
} catch (Exception $e) {
$exc = $e;
}
$passed = assertTrue(
$exc instanceof Minify_JS_ClosureCompiler_Exception
, 'Minify_JS_ClosureCompiler : Throws Minify_JS_ClosureCompiler_Exception');
assertTrue(
$exc->getMessage() === 'POST content larger than ' . Minify_JS_ClosureCompiler::DEFAULT_MAX_BYTES . ' bytes'
, 'Minify_JS_ClosureCompiler : Message must tell how big maximum byte size is');
if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) {
echo "\n---Message: " . var_export($exc->getMessage(), 1) . "\n\n\n";
}
// Test maximum byte size check (no limit)
$src = "(function(){})();";
try {
$minOutput = Minify_JS_ClosureCompiler::minify($src, array(
Minify_JS_ClosureCompiler::OPTION_MAX_BYTES => 0
));
} catch (Exception $e) {
$exc = $e;
}
$passed = assertTrue(
$src === $minOutput
, 'Minify_JS_ClosureCompiler : With no limit set, it should compile properly');
// Test maximum byte size check (custom)
$src = "(function() {})();";
$allowedBytes = 5;
$exc = null;
try {
$minOutput = Minify_JS_ClosureCompiler::minify($src, array(
Minify_JS_ClosureCompiler::OPTION_MAX_BYTES => $allowedBytes
));
} catch (Exception $e) {
$exc = $e;
}
$passed = assertTrue(
$exc instanceof Minify_JS_ClosureCompiler_Exception
, 'Minify_JS_ClosureCompiler : Throws Minify_JS_ClosureCompiler_Exception');
assertTrue(
$exc->getMessage() === 'POST content larger than ' . $allowedBytes . ' bytes'
, 'Minify_JS_ClosureCompiler : Message must tell how big maximum byte size is');
if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) {
echo "\n---Message: " . var_export($exc->getMessage(), 1) . "\n\n\n";
}
// Test additional options passed to HTTP request
$ecmascript5 = "[1,].length;";
$exc = null;
try {
$minOutput = Minify_JS_ClosureCompiler::minify($ecmascript5);
} catch (Exception $e) {
$exc = $e;
}
$passed = assertTrue(
$exc instanceof Minify_JS_ClosureCompiler_Exception
, 'Minify_JS_ClosureCompiler : Throws Minify_JS_ClosureCompiler_Exception');
if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) {
echo "\n---Message: " . var_export($exc->getMessage(), 1) . "\n\n\n";
}
$minExpected = '1;';
$minOutput = Minify_JS_ClosureCompiler::minify($ecmascript5, array(
Minify_JS_ClosureCompiler::OPTION_ADDITIONAL_OPTIONS => array(
'language' => 'ECMASCRIPT5'
)
));
$passed = assertTrue(
$minOutput === $minExpected
, 'Minify_JS_ClosureCompiler : Language option should make it compile');
}
test_Minify_JS_ClosureCompiler();

View File

@ -11,7 +11,6 @@ require 'test_Minify_Cache_ZendPlatform.php';
require 'test_Minify_CSS.php';
require 'test_Minify_CSS_UriRewriter.php';
require 'test_Minify_ClosureCompiler.php';
require 'test_Minify_JS_ClosureCompiler.php';
require 'test_Minify_YuiCSS.php';
require 'test_Minify_CommentPreserver.php';
require 'test_Minify_HTML.php';

37
phpunit.xml Normal file
View File

@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- http://phpunit.de/manual/current/en/appendixes.configuration.html -->
<phpunit
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="vendor/autoload.php">
<testsuites>
<testsuite name="Project Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<!--
<php>
<server name="KERNEL_DIR" value="/path/to/your/app/" />
</php>
-->
<!-- for code coverage -->
<filter>
<whitelist>
<directory>.</directory>
<exclude>
<directory>tests</directory>
</exclude>
</whitelist>
</filter>
</phpunit>

View File

@ -0,0 +1,98 @@
<?php
class JsClosureCompilerTest extends PHPUnit_Framework_TestCase {
public function test1() {
$src = "
(function (window, undefined){
function addOne(input) {
return 1 + input;
}
window.addOne = addOne;
window.undefined = undefined;
})(window);
";
$minExpected = "(function(a,b){a.addOne=function(a){return 1+a};a.undefined=b})(window);";
$minOutput = Minify_JS_ClosureCompiler::minify($src);
// Too many recent calls to Closure Compiler API to test.\n";
$this->assertNotContains($minOutput, 'Error(22): Too many compiles');
$this->assertSame($minExpected, $minOutput, 'Minify_JS_ClosureCompiler : Overall');
}
public function test2() {
$src = "function blah({ return 'blah';} ";
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');
}
}
// Test maximum byte size check (default)
public function test3() {
$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';
$this->assertEquals($expected, $e->getMessage(), 'Message must tell how big maximum byte size is');
}
}
// Test maximum byte size check (no limit)
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;
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';
$this->assertEquals($expected, $e->getMessage(), 'Message must tell how big maximum byte size is');
}
}
// Test additional options passed to HTTP request
public function test6() {
$ecmascript5 = "[1,].length;";
$exc = null;
try {
Minify_JS_ClosureCompiler::minify($ecmascript5);
$this->fail('Expected exception not thrown');
} catch (Exception $e) {
$this->assertInstanceOf('Minify_JS_ClosureCompiler_Exception', $e, 'Throws Minify_JS_ClosureCompiler_Exception');
}
}
public function test7() {
$ecmascript5 = "[1,].length;";
$minExpected = '1;';
$minOutput = Minify_JS_ClosureCompiler::minify($ecmascript5, array(
Minify_JS_ClosureCompiler::OPTION_ADDITIONAL_OPTIONS => array(
'language' => 'ECMASCRIPT5'
)
));
$this->assertSame($minExpected, $minOutput, 'Language option should make it compile');
}
}