/application/library/Thirdpart/Minify/min_unit_tests/test_Minify.php
https://gitlab.com/flyhope/Hiblog · PHP · 245 lines · 203 code · 32 blank · 10 comment · 17 complexity · 2c7d4600e00db0f26e1b2b6dbdc8b73e MD5 · raw file
- <?php
- // currently these only test serve() when passed the 'quiet' options
- require_once '_inc.php';
- function test_Minify()
- {
- global $thisDir;
- $minifyTestPath = __DIR__ . '/_test_files/minify';
- $thisFileActive = (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME']));
- $tomorrow = $_SERVER['REQUEST_TIME'] + 86400;
- $lastModified = $_SERVER['REQUEST_TIME'] - 86400;
- // Test 304 response
- // simulate conditional headers
- $_SERVER['HTTP_IF_NONE_MATCH'] = "\"{$lastModified}pub\"";
- $_SERVER['HTTP_IF_MODIFIED_SINCE'] = gmdate('D, d M Y H:i:s \G\M\T', $lastModified);
- $minify = new Minify(new Minify_Cache_Null());
- $env = new Minify_Env(array(
- 'server' => $_SERVER,
- ));
- $sourceFactory = new Minify_Source_Factory($env, array(), new Minify_Cache_Null());
- $controller = new Minify_Controller_Files($env, $sourceFactory);
- $output = $minify->serve($controller, array(
- 'files' => $thisDir . '/_test_files/css/styles.css' // controller casts to array
- ,'quiet' => true
- ,'lastModifiedTime' => $lastModified
- ,'encodeOutput' => false
- ));
- $expected = array (
- 'success' => true
- ,'statusCode' => 304
- ,'content' => '',
- 'headers' => array(
- 'Expires' => gmdate('D, d M Y H:i:s \G\M\T', $_SERVER['REQUEST_TIME'] + 1800),
- 'Vary' => 'Accept-Encoding',
- 'Last-Modified' => gmdate('D, d M Y H:i:s \G\M\T', $lastModified),
- 'ETag' => "\"pub{$lastModified}\"",
- 'Cache-Control' => 'max-age=1800',
- '_responseCode' => 'HTTP/1.0 304 Not Modified',
- )
- );
- $passed = assertTrue($expected === $output, 'Minify : 304 response');
- if ($thisFileActive) {
- echo "\nOutput: " .var_export($output, 1). "\n\n";
- if (! $passed) {
- echo "\n\n\n\n---Expected: " .var_export($expected, 1). "\n\n";
- }
- }
- assertTrue(
- ! class_exists('Minify_CSSmin', false)
- ,'Minify : minifier classes aren\'t loaded for 304s'
- );
- // Test JS and Expires
- $content = preg_replace('/\\r\\n?/', "\n", file_get_contents($minifyTestPath . '/minified.js'));
- $lastModified = max(
- filemtime($minifyTestPath . '/email.js')
- ,filemtime($minifyTestPath . '/QueryString.js')
- );
- $expected = array(
- 'success' => true
- ,'statusCode' => 200
- // JSMin always converts to \n line endings
- ,'content' => $content
- ,'headers' => array (
- 'Expires' => gmdate('D, d M Y H:i:s \G\M\T', $tomorrow),
- 'Vary' => 'Accept-Encoding',
- 'Last-Modified' => gmdate('D, d M Y H:i:s \G\M\T', $lastModified),
- 'ETag' => "\"pub{$lastModified}\"",
- 'Cache-Control' => 'max-age=86400',
- 'Content-Length' => countBytes($content),
- 'Content-Type' => 'application/x-javascript; charset=utf-8',
- )
- );
- unset($_SERVER['HTTP_IF_NONE_MATCH']);
- unset($_SERVER['HTTP_IF_MODIFIED_SINCE']);
- $env = new Minify_Env(array(
- 'server' => $_SERVER,
- ));
- $sourceFactory = new Minify_Source_Factory($env, array(), new Minify_Cache_Null());
- $controller = new Minify_Controller_Files($env, $sourceFactory);
- $output = $minify->serve($controller, array(
- 'files' => array(
- $minifyTestPath . '/email.js'
- ,$minifyTestPath . '/QueryString.js'
- )
- ,'quiet' => true
- ,'maxAge' => 86400
- ,'encodeOutput' => false
- ));
-
- $passed = assertTrue($expected === $output, 'Minify : JS and Expires');
- if ($thisFileActive) {
- echo "\nOutput: " .var_export($output, 1). "\n\n";
- if (! $passed) {
- echo "\n\n\n\n---Expected: " .var_export($expected, 1). "\n\n";
- }
- }
-
- // test for Issue 73
- $expected = ";function h(){}";
- $output = $minify->serve($controller, array(
- 'files' => array(
- $minifyTestPath . '/issue73_1.js'
- ,$minifyTestPath . '/issue73_2.js'
- )
- ,'quiet' => true
- ,'encodeOutput' => false
- ));
- $output = $output['content'];
-
- $passed = assertTrue($expected === $output, 'Minify : Issue 73');
- if ($thisFileActive) {
- if (! $passed) {
- echo "\n---Output : " .var_export($output, 1). "\n";
- echo "---Expected: " .var_export($expected, 1). "\n\n";
- }
- }
-
- // test for Issue 89
- $expected = file_get_contents($minifyTestPath . '/issue89_out.min.css');
- $output = $minify->serve($controller, array(
- 'files' => array(
- $minifyTestPath . '/issue89_1.css'
- ,$minifyTestPath . '/issue89_2.css'
- )
- ,'quiet' => true
- ,'encodeOutput' => false
- ,'bubbleCssImports' => true
- ));
- $output = $output['content'];
- $passed = assertTrue($expected === $output, 'Minify : Issue 89 : bubbleCssImports');
- if ($thisFileActive) {
- if (! $passed) {
- echo "\n---Output : " .var_export($output, 1). "\n";
- echo "---Expected: " .var_export($expected, 1). "\n\n";
- }
- }
-
- $output = $minify->serve($controller, array(
- 'files' => array(
- $minifyTestPath . '/issue89_1.css'
- ,$minifyTestPath . '/issue89_2.css'
- )
- ,'quiet' => true
- ,'encodeOutput' => false
- ));
- $output = $output['content'];
- $defaultOptions = $minify->getDefaultOptions();
- $passed = assertTrue(0 === strpos($output, $defaultOptions['importWarning']), 'Minify : Issue 89 : detect invalid imports');
- if ($thisFileActive) {
- if (! $passed) {
- echo "\n---Output : " .var_export($output, 1). "\n";
- echo "---Expected: " .var_export($expected, 1). "\n\n";
- }
- }
-
- $output = $minify->serve($controller, array(
- 'files' => array(
- $minifyTestPath . '/issue89_1.css'
- )
- ,'quiet' => true
- ,'encodeOutput' => false
- ));
- $output = $output['content'];
- $passed = assertTrue(false === strpos($output, $defaultOptions['importWarning']), 'Minify : Issue 89 : don\'t warn about valid imports');
- if ($thisFileActive) {
- if (! $passed) {
- echo "\n---Output : " .var_export($output, 1). "\n";
- echo "---Expected: " .var_export($expected, 1). "\n\n";
- }
- }
- // Test Issue 132
- if (function_exists('mb_strlen') && ((int)ini_get('mbstring.func_overload') & 2)) {
- $output = $minify->serve($controller, array(
- 'files' => array(__DIR__ . '/_test_files/js/issue132.js')
- ,'quiet' => true
- ,'encodeOutput' => false
- ));
- $passed = assertTrue($output['headers']['Content-Length'] == 77, 'Minify : Issue 132 : mbstring.func_overload shouldn\'t cause incorrect Content-Length');
- }
- // Test minifying CSS and responding with Etag/Last-Modified
- // don't allow conditional headers
- unset($_SERVER['HTTP_IF_NONE_MATCH'], $_SERVER['HTTP_IF_MODIFIED_SINCE']);
- $expectedContent = file_get_contents($minifyTestPath . '/minified.css');
- $expected = array(
- 'success' => true
- ,'statusCode' => 200
- ,'content' => $expectedContent
- ,'headers' => array (
- 'Vary' => 'Accept-Encoding',
- 'Last-Modified' => gmdate('D, d M Y H:i:s \G\M\T', $lastModified),
- 'ETag' => "\"pub{$lastModified}\"",
- 'Cache-Control' => 'max-age=0',
- 'Content-Length' => countBytes($expectedContent),
- 'Content-Type' => 'text/css; charset=utf-8',
- )
- );
- $env = new Minify_Env(array(
- 'server' => $_SERVER,
- ));
- $sourceFactory = new Minify_Source_Factory($env, array(), new Minify_Cache_Null());
- $controller = new Minify_Controller_Files($env, $sourceFactory);
- $output = $minify->serve($controller, array(
- 'files' => array(
- $thisDir . '/_test_files/css/styles.css'
- ,$thisDir . '/_test_files/css/comments.css'
- )
- ,'quiet' => true
- ,'lastModifiedTime' => $lastModified
- ,'encodeOutput' => false
- ,'maxAge' => false
- ));
- $passed = assertTrue($expected === $output, 'Minify : CSS and Etag/Last-Modified');
- if ($thisFileActive) {
- echo "\nOutput: " .var_export($output, 1). "\n\n";
- if (! $passed) {
- echo "\n\n\n\n---Expected: " .var_export($expected, 1). "\n\n";
- }
- }
- }
- test_Minify();