/min_unit_tests/test_Minify.php

https://github.com/splittingred/minify · PHP · 223 lines · 189 code · 24 blank · 10 comment · 18 complexity · d343640d8881c6db56953a8904ff5a95 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',
  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_File', false)
  45. ,'Minify : cache, and minifier classes aren\'t loaded for 304s'
  46. );
  47. // Test JS and Expires
  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',
  64. 'Content-Length' => countBytes($content),
  65. 'Content-Type' => 'application/x-javascript; charset=utf-8',
  66. )
  67. );
  68. unset($_SERVER['HTTP_IF_NONE_MATCH']);
  69. unset($_SERVER['HTTP_IF_MODIFIED_SINCE']);
  70. $output = Minify::serve('Files', array(
  71. 'files' => array(
  72. $minifyTestPath . '/email.js'
  73. ,$minifyTestPath . '/QueryString.js'
  74. )
  75. ,'quiet' => true
  76. ,'maxAge' => 86400
  77. ,'encodeOutput' => false
  78. ));
  79. $passed = assertTrue($expected === $output, 'Minify : JS and Expires');
  80. if ($thisFileActive) {
  81. echo "\nOutput: " .var_export($output, 1). "\n\n";
  82. if (! $passed) {
  83. echo "\n\n\n\n---Expected: " .var_export($expected, 1). "\n\n";
  84. }
  85. }
  86. // test for Issue 73
  87. Minify::setCache(null);
  88. $expected = "\n;function h(){}";
  89. $output = Minify::serve('Files', array(
  90. 'files' => array(
  91. $minifyTestPath . '/issue73_1.js'
  92. ,$minifyTestPath . '/issue73_2.js'
  93. )
  94. ,'quiet' => true
  95. ,'encodeOutput' => false
  96. ));
  97. $output = $output['content'];
  98. $passed = assertTrue($expected === $output, 'Minify : Issue 73');
  99. if ($thisFileActive) {
  100. if (! $passed) {
  101. echo "\n---Output : " .var_export($output, 1). "\n";
  102. echo "---Expected: " .var_export($expected, 1). "\n\n";
  103. }
  104. }
  105. // test for Issue 89
  106. $expected = file_get_contents($minifyTestPath . '/issue89_out.min.css');
  107. $output = Minify::serve('Files', array(
  108. 'files' => array(
  109. $minifyTestPath . '/issue89_1.css'
  110. ,$minifyTestPath . '/issue89_2.css'
  111. )
  112. ,'quiet' => true
  113. ,'encodeOutput' => false
  114. ,'bubbleCssImports' => true
  115. ));
  116. $output = $output['content'];
  117. $passed = assertTrue($expected === $output, 'Minify : Issue 89 : bubbleCssImports');
  118. if ($thisFileActive) {
  119. if (! $passed) {
  120. echo "\n---Output : " .var_export($output, 1). "\n";
  121. echo "---Expected: " .var_export($expected, 1). "\n\n";
  122. }
  123. }
  124. $output = Minify::serve('Files', array(
  125. 'files' => array(
  126. $minifyTestPath . '/issue89_1.css'
  127. ,$minifyTestPath . '/issue89_2.css'
  128. )
  129. ,'quiet' => true
  130. ,'encodeOutput' => false
  131. ));
  132. $output = $output['content'];
  133. $passed = assertTrue(0 === strpos($output, Minify::$importWarning), 'Minify : Issue 89 : detect invalid imports');
  134. if ($thisFileActive) {
  135. if (! $passed) {
  136. echo "\n---Output : " .var_export($output, 1). "\n";
  137. echo "---Expected: " .var_export($expected, 1). "\n\n";
  138. }
  139. }
  140. $output = Minify::serve('Files', array(
  141. 'files' => array(
  142. $minifyTestPath . '/issue89_1.css'
  143. )
  144. ,'quiet' => true
  145. ,'encodeOutput' => false
  146. ));
  147. $output = $output['content'];
  148. $passed = assertTrue(false === strpos($output, Minify::$importWarning), 'Minify : Issue 89 : don\'t warn about valid imports');
  149. if ($thisFileActive) {
  150. if (! $passed) {
  151. echo "\n---Output : " .var_export($output, 1). "\n";
  152. echo "---Expected: " .var_export($expected, 1). "\n\n";
  153. }
  154. }
  155. // Test Issue 132
  156. if (function_exists('mb_strlen') && ((int)ini_get('mbstring.func_overload') & 2)) {
  157. $output = Minify::serve('Files', array(
  158. 'files' => array(dirname(__FILE__) . '/_test_files/js/issue132.js')
  159. ,'quiet' => true
  160. ,'encodeOutput' => false
  161. ));
  162. $passed = assertTrue($output['headers']['Content-Length'] == 77, 'Minify : Issue 132 : mbstring.func_overload shouldn\'t cause incorrect Content-Length');
  163. }
  164. // Test minifying CSS and responding with Etag/Last-Modified
  165. // don't allow conditional headers
  166. unset($_SERVER['HTTP_IF_NONE_MATCH'], $_SERVER['HTTP_IF_MODIFIED_SINCE']);
  167. $expectedContent = file_get_contents($minifyTestPath . '/minified.css');
  168. $expected = array(
  169. 'success' => true
  170. ,'statusCode' => 200
  171. ,'content' => $expectedContent
  172. ,'headers' => array (
  173. 'Vary' => 'Accept-Encoding',
  174. 'Last-Modified' => gmdate('D, d M Y H:i:s \G\M\T', $lastModified),
  175. 'ETag' => "\"pub{$lastModified}\"",
  176. 'Cache-Control' => 'max-age=0',
  177. 'Content-Length' => countBytes($expectedContent),
  178. 'Content-Type' => 'text/css; charset=utf-8',
  179. )
  180. );
  181. $output = Minify::serve('Files', array(
  182. 'files' => array(
  183. $thisDir . '/_test_files/css/styles.css'
  184. ,$thisDir . '/_test_files/css/comments.css'
  185. )
  186. ,'quiet' => true
  187. ,'lastModifiedTime' => $lastModified
  188. ,'encodeOutput' => false
  189. ,'maxAge' => false
  190. ));
  191. $passed = assertTrue($expected === $output, 'Minify : CSS and Etag/Last-Modified');
  192. if ($thisFileActive) {
  193. echo "\nOutput: " .var_export($output, 1). "\n\n";
  194. if (! $passed) {
  195. echo "\n\n\n\n---Expected: " .var_export($expected, 1). "\n\n";
  196. }
  197. }
  198. }
  199. test_Minify();