1
0
mirror of https://github.com/mrclay/minify.git synced 2025-08-05 21:57:53 +02:00

port JsMinTest to phpunit

This commit is contained in:
Elan Ruusamäe
2015-11-18 17:13:21 +02:00
committed by Steve Clay
parent 6838349e3a
commit 08070aecdb
2 changed files with 133 additions and 110 deletions

View File

@@ -1,110 +0,0 @@
<?php
require_once '_inc.php';
use JSMin\JSMin;
function test_JSMin()
{
global $thisDir;
$src = file_get_contents($thisDir . '/_test_files/js/before.js');
$minExpected = file_get_contents($thisDir . '/_test_files/js/before.min.js');
$minOutput = JSMin::minify($src);
assertTrue($minExpected == $minOutput, 'JSMin : 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 = file_get_contents($thisDir . '/_test_files/js/issue144.js');
$minExpected = file_get_contents($thisDir . '/_test_files/js/issue144.min.js');
$minOutput = JSMin::minify($src);
assertTrue($minExpected == $minOutput, 'JSMin : Handle "+ ++a" syntax (Issue 144)');
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 = file_get_contents($thisDir . '/_test_files/js/issue256.js');
$minExpected = file_get_contents($thisDir . '/_test_files/js/issue256.min.js');
$minOutput = JSMin::minify($src);
assertTrue($minExpected == $minOutput, 'JSMin : Handle \n!function()... (Issue 256)');
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";
}
if (function_exists('mb_strlen') && ((int)ini_get('mbstring.func_overload') & 2)) {
$src = file_get_contents($thisDir . '/_test_files/js/issue132.js');
$minExpected = file_get_contents($thisDir . '/_test_files/js/issue132.min.js');
$minOutput = JSMin::minify($src);
assertTrue($minExpected == $minOutput, 'JSMin : mbstring.func_overload shouldn\'t cause failure (Issue 132)');
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 = file_get_contents($thisDir . '/_test_files/js/regexes.js');
$minExpected = file_get_contents($thisDir . '/_test_files/js/regexes.min.js');
$minOutput = JSMin::minify($src);
assertTrue($minExpected == $minOutput, 'JSMin : Identify RegExp literals');
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";
}
test_JSMin_exception('"Hello'
,'Unterminated String'
,'JSMin\UnterminatedStringException'
,"JSMin: Unterminated String at byte 5: \"Hello");
test_JSMin_exception("return /regexp\n}"
,'Unterminated RegExp'
,'JSMin\UnterminatedRegExpException'
,"JSMin: Unterminated RegExp at byte 14: /regexp\n");
test_JSMin_exception("return/regexp\n}"
,'Unterminated RegExp'
,'JSMin\UnterminatedRegExpException'
,"JSMin: Unterminated RegExp at byte 13: /regexp\n");
test_JSMin_exception(";return/regexp\n}"
,'Unterminated RegExp'
,'JSMin\UnterminatedRegExpException'
,"JSMin: Unterminated RegExp at byte 14: /regexp\n");
test_JSMin_exception(";return /regexp\n}"
,'Unterminated RegExp'
,'JSMin\UnterminatedRegExpException'
,"JSMin: Unterminated RegExp at byte 15: /regexp\n");
test_JSMin_exception("typeof/regexp\n}"
,'Unterminated RegExp'
,'JSMin\UnterminatedRegExpException'
,"JSMin: Unterminated RegExp at byte 13: /regexp\n");
test_JSMin_exception("/* Comment "
,'Unterminated Comment'
,'JSMin\UnterminatedCommentException'
,"JSMin: Unterminated comment at byte 11: /* Comment ");
}
function test_JSMin_exception($js, $label, $expClass, $expMessage) {
$eClass = $eMsg = '';
try {
JSMin::minify($js);
} catch (Exception $e) {
$eClass = get_class($e);
$eMsg = $e->getMessage();
}
$passed = assertTrue($eClass === $expClass && $eMsg === $expMessage,
'JSMin : throw on ' . $label);
if (! $passed && isset($e) && (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME']))) {
echo "\n ---" , $e, "\n\n";
}
}
test_JSMin();

133
tests/JSMinTest.php Normal file
View File

@@ -0,0 +1,133 @@
<?php
use JSMin\JSMin;
class JsMinTest extends PHPUnit_Framework_TestCase
{
/** @var string */
static $test_files;
public static function setupBeforeClass()
{
self::$test_files = __DIR__ . '/../min_unit_tests/_test_files';
}
public function test1()
{
$src = file_get_contents(self::$test_files . '/js/before.js');
$minExpected = file_get_contents(self::$test_files . '/js/before.min.js');
$minOutput = JSMin::minify($src);
$this->assertSame($minExpected, $minOutput, 'Overall');
}
public function test2()
{
$src = file_get_contents(self::$test_files . '/js/issue144.js');
$minExpected = file_get_contents(self::$test_files . '/js/issue144.min.js');
$minOutput = JSMin::minify($src);
$this->assertSame($minExpected, $minOutput, 'Handle "+ ++a" syntax (Issue 144)');
}
public function test3()
{
$src = file_get_contents(self::$test_files . '/js/issue256.js');
$minExpected = file_get_contents(self::$test_files . '/js/issue256.min.js');
$minOutput = JSMin::minify($src);
$this->assertSame($minExpected, $minOutput, 'Handle \n!function()... (Issue 256)');
}
public function test4()
{
$have_overload = function_exists('mb_strlen') && ((int)ini_get('mbstring.func_overload') & 2);
if (!$have_overload) {
$this->markTestSkipped();
}
$src = file_get_contents(self::$test_files . '/js/issue132.js');
$minExpected = file_get_contents(self::$test_files . '/js/issue132.min.js');
$minOutput = JSMin::minify($src);
$this->assertSame($minExpected, $minOutput, 'mbstring.func_overload shouldn\'t cause failure (Issue 132)');
}
public function test5()
{
$src = file_get_contents(self::$test_files . '/js/regexes.js');
$minExpected = file_get_contents(self::$test_files . '/js/regexes.min.js');
$minOutput = JSMin::minify($src);
$this->assertSame($minExpected, $minOutput, 'Identify RegExp literals');
}
/**
* @param string $js
* @param string $label
* @param string $expClass
* @param string $expMessage
* @dataProvider testJSMinExceptionData
*/
public function testJSMinException($js, $label, $expClass, $expMessage)
{
$eClass = $eMsg = '';
try {
JSMin::minify($js);
} catch (Exception $e) {
$eClass = get_class($e);
$eMsg = $e->getMessage();
}
$this->assertTrue($eClass === $expClass && $eMsg === $expMessage, 'Throw on ' . $label);
}
public function testJSMinExceptionData()
{
// $js, $label, $expClass, $expMessage
return array(
array(
'"Hello',
'Unterminated String',
'JSMin\UnterminatedStringException',
"JSMin: Unterminated String at byte 5: \"Hello",
),
array(
"return /regexp\n}",
'Unterminated RegExp',
'JSMin\UnterminatedRegExpException',
"JSMin: Unterminated RegExp at byte 14: /regexp\n",
),
array(
"return/regexp\n}",
'Unterminated RegExp',
'JSMin\UnterminatedRegExpException',
"JSMin: Unterminated RegExp at byte 13: /regexp\n",
),
array(
";return/regexp\n}",
'Unterminated RegExp',
'JSMin\UnterminatedRegExpException',
"JSMin: Unterminated RegExp at byte 14: /regexp\n",
),
array(
";return /regexp\n}",
'Unterminated RegExp',
'JSMin\UnterminatedRegExpException',
"JSMin: Unterminated RegExp at byte 15: /regexp\n",
),
array(
"typeof/regexp\n}",
'Unterminated RegExp',
'JSMin\UnterminatedRegExpException',
"JSMin: Unterminated RegExp at byte 13: /regexp\n",
),
array(
"/* Comment ",
'Unterminated Comment',
'JSMin\UnterminatedCommentException',
"JSMin: Unterminated comment at byte 11: /* Comment ",
),
);
}
}