/lib/minify-2.4.1b/min_unit_tests/test_Minify.php

https://github.com/martinnemitz/Mage-Minify · PHP · 213 lines · 180 code · 24 blank · 9 comment · 15 complexity · f0ca0a19dbaa948a2017ebafc3677f17 MD5 · raw file

  1. <?php
  2. // currently these only test serve() when passed the 'quiet' options
  3. require_once '_inc.php';
  4. require_once 'Minify.php';
  5. function test_Minify()
  6. {
  7. global $thisDir;
  8. $minifyTestPath = dirname(__FILE__) . '/_test_files/minify';
  9. $thisFileActive = (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME']));
  10. $tomorrow = $_SERVER['REQUEST_TIME'] + 86400;
  11. $lastModified = $_SERVER['REQUEST_TIME'] - 86400;
  12. // Test 304 response
  13. // simulate conditional headers
  14. $_SERVER['HTTP_IF_NONE_MATCH'] = "\"{$lastModified}pub\"";
  15. $_SERVER['HTTP_IF_MODIFIED_SINCE'] = gmdate('D, d M Y H:i:s \G\M\T', $lastModified);
  16. $expected = array (
  17. 'success' => true
  18. ,'statusCode' => 304
  19. ,'content' => '',
  20. 'headers' => array(
  21. 'Expires' => gmdate('D, d M Y H:i:s \G\M\T', $_SERVER['REQUEST_TIME'] + 1800),
  22. 'Vary' => 'Accept-Encoding',
  23. 'Last-Modified' => gmdate('D, d M Y H:i:s \G\M\T', $lastModified),
  24. 'ETag' => "\"pub{$lastModified}\"",
  25. 'Cache-Control' => 'max-age=1800, public',
  26. '_responseCode' => 'HTTP/1.0 304 Not Modified',
  27. )
  28. );
  29. $output = Minify::serve('Files', array(
  30. 'files' => $thisDir . '/_test_files/css/styles.css' // controller casts to array
  31. ,'quiet' => true
  32. ,'lastModifiedTime' => $lastModified
  33. ,'encodeOutput' => false
  34. ));
  35. $passed = assertTrue($expected === $output, 'Minify : 304 response');
  36. if ($thisFileActive) {
  37. echo "\nOutput: " .var_export($output, 1). "\n\n";
  38. if (! $passed) {
  39. echo "\n\n\n\n---Expected: " .var_export($expected, 1). "\n\n";
  40. }
  41. }
  42. assertTrue(
  43. ! class_exists('Minify_CSS', false)
  44. && ! class_exists('Minify_Cache', false)
  45. ,'Minify : cache, and minifier classes aren\'t loaded for 304s'
  46. );
  47. // Test minifying JS and serving with Expires header
  48. $content = preg_replace('/\\r\\n?/', "\n", file_get_contents($minifyTestPath . '/minified.js'));
  49. $lastModified = max(
  50. filemtime($minifyTestPath . '/email.js')
  51. ,filemtime($minifyTestPath . '/QueryString.js')
  52. );
  53. $expected = array(
  54. 'success' => true
  55. ,'statusCode' => 200
  56. // JSMin always converts to \n line endings
  57. ,'content' => $content
  58. ,'headers' => array (
  59. 'Expires' => gmdate('D, d M Y H:i:s \G\M\T', $tomorrow),
  60. 'Vary' => 'Accept-Encoding',
  61. 'Last-Modified' => gmdate('D, d M Y H:i:s \G\M\T', $lastModified),
  62. 'ETag' => "\"pub{$lastModified}\"",
  63. 'Cache-Control' => 'max-age=86400, public',
  64. 'Content-Length' => strlen($content),
  65. 'Content-Type' => 'application/x-javascript; charset=utf-8',
  66. )
  67. );
  68. $output = Minify::serve('Files', array(
  69. 'files' => array(
  70. $minifyTestPath . '/email.js'
  71. ,$minifyTestPath . '/QueryString.js'
  72. )
  73. ,'quiet' => true
  74. ,'maxAge' => 86400
  75. ,'encodeOutput' => false
  76. ));
  77. $passed = assertTrue($expected === $output, 'Minify : JS and Expires');
  78. if ($thisFileActive) {
  79. echo "\nOutput: " .var_export($output, 1). "\n\n";
  80. if (! $passed) {
  81. echo "\n\n\n\n---Expected: " .var_export($expected, 1). "\n\n";
  82. }
  83. }
  84. // test for Issue 73
  85. Minify::setCache(null);
  86. $expected = ";function h(){}";
  87. $output = Minify::serve('Files', array(
  88. 'files' => array(
  89. $minifyTestPath . '/issue73_1.js'
  90. ,$minifyTestPath . '/issue73_2.js'
  91. )
  92. ,'quiet' => true
  93. ,'encodeOutput' => false
  94. ));
  95. $output = $output['content'];
  96. $passed = assertTrue($expected === $output, 'Minify : Issue 73');
  97. if ($thisFileActive) {
  98. if (! $passed) {
  99. echo "\n---Output : " .var_export($output, 1). "\n";
  100. echo "---Expected: " .var_export($expected, 1). "\n\n";
  101. }
  102. }
  103. // test for Issue 89
  104. $expected = file_get_contents($minifyTestPath . '/issue89_out.min.css');
  105. $output = Minify::serve('Files', array(
  106. 'files' => array(
  107. $minifyTestPath . '/issue89_1.css'
  108. ,$minifyTestPath . '/issue89_2.css'
  109. )
  110. ,'quiet' => true
  111. ,'encodeOutput' => false
  112. ,'bubbleCssImports' => true
  113. ));
  114. $output = $output['content'];
  115. $passed = assertTrue($expected === $output, 'Minify : Issue 89 : bubbleCssImports');
  116. if ($thisFileActive) {
  117. if (! $passed) {
  118. echo "\n---Output : " .var_export($output, 1). "\n";
  119. echo "---Expected: " .var_export($expected, 1). "\n\n";
  120. }
  121. }
  122. $output = Minify::serve('Files', array(
  123. 'files' => array(
  124. $minifyTestPath . '/issue89_1.css'
  125. ,$minifyTestPath . '/issue89_2.css'
  126. )
  127. ,'quiet' => true
  128. ,'encodeOutput' => false
  129. ));
  130. $output = $output['content'];
  131. $passed = assertTrue(0 === strpos($output, Minify::$importWarning), 'Minify : Issue 89 : detect invalid imports');
  132. if ($thisFileActive) {
  133. if (! $passed) {
  134. echo "\n---Output : " .var_export($output, 1). "\n";
  135. echo "---Expected: " .var_export($expected, 1). "\n\n";
  136. }
  137. }
  138. $output = Minify::serve('Files', array(
  139. 'files' => array(
  140. $minifyTestPath . '/issue89_1.css'
  141. )
  142. ,'quiet' => true
  143. ,'encodeOutput' => false
  144. ));
  145. $output = $output['content'];
  146. $passed = assertTrue(false === strpos($output, Minify::$importWarning), 'Minify : Issue 89 : don\'t warn about valid imports');
  147. if ($thisFileActive) {
  148. if (! $passed) {
  149. echo "\n---Output : " .var_export($output, 1). "\n";
  150. echo "---Expected: " .var_export($expected, 1). "\n\n";
  151. }
  152. }
  153. // Test minifying CSS and responding with Etag/Last-Modified
  154. Minify::setCache(null);
  155. // don't allow conditional headers
  156. unset($_SERVER['HTTP_IF_NONE_MATCH'], $_SERVER['HTTP_IF_MODIFIED_SINCE']);
  157. $expectedContent = file_get_contents($minifyTestPath . '/minified.css');
  158. $expected = array(
  159. 'success' => true
  160. ,'statusCode' => 200
  161. ,'content' => $expectedContent
  162. ,'headers' => array (
  163. 'Vary' => 'Accept-Encoding',
  164. 'Last-Modified' => gmdate('D, d M Y H:i:s \G\M\T', $lastModified),
  165. 'ETag' => "\"pub{$lastModified}\"",
  166. 'Cache-Control' => 'max-age=0, public',
  167. 'Content-Length' => strlen($expectedContent),
  168. 'Content-Type' => 'text/css; charset=utf-8',
  169. )
  170. );
  171. $output = Minify::serve('Files', array(
  172. 'files' => array(
  173. $thisDir . '/_test_files/css/styles.css'
  174. ,$thisDir . '/_test_files/css/comments.css'
  175. )
  176. ,'quiet' => true
  177. ,'lastModifiedTime' => $lastModified
  178. ,'encodeOutput' => false
  179. ,'maxAge' => false
  180. ));
  181. $passed = assertTrue($expected === $output, 'Minify : CSS and Etag/Last-Modified');
  182. if ($thisFileActive) {
  183. echo "\nOutput: " .var_export($output, 1). "\n\n";
  184. if (! $passed) {
  185. echo "\n\n\n\n---Expected: " .var_export($expected, 1). "\n\n";
  186. }
  187. }
  188. }
  189. test_Minify();