Repository navigation
Expand file tree
/
Copy pathMinifyTest.php
More file actions
202 lines (169 loc) · 7.42 KB
/
Copy pathMinifyTest.php
File metadata and controls
202 lines (169 loc) · 7.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
class MinifyTest extends TestCase
{
public function test_Minify()
{
$minifyTestPath = self::$test_files . '/minify';
$_SERVER['DOCUMENT_ROOT'] = dirname(__DIR__);
$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' => self::$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',
),
);
$this->assertEquals($expected, $output, '304 response');
$this->markTestIncomplete('minifier classes aren\'t loaded for 304s');
// $this->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' => $this->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,
));
$this->assertEquals($expected, $output, 'JS and Expires');
// 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'];
$this->assertEquals($expected, $output, 'Issue 73');
// 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'];
$this->assertEquals($expected, $output, 'Issue 89 : bubbleCssImports');
$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();
$this->assertEquals(0, strpos($output, $defaultOptions['importWarning']), 'Issue 89 : detect invalid imports');
$output = $minify->serve($controller, array(
'files' => array(
$minifyTestPath . '/issue89_1.css',
),
'quiet' => true,
'encodeOutput' => false,
));
$output = $output['content'];
$this->assertFalse(strpos($output, $defaultOptions['importWarning']), 'Issue 89 : don\'t warn about valid imports');
// 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,
));
$this->assertEquals(77, $output['headers']['Content-Length'], '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' => $this->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(
self::$test_files . '/css/styles.css',
self::$test_files . '/css/comments.css',
),
'quiet' => true,
'lastModifiedTime' => $lastModified,
'encodeOutput' => false,
'maxAge' => false,
));
$this->assertEquals($expected, $output, 'CSS and Etag/Last-Modified');
}
}