PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 3.2.5
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v3.2.5
4.7.0 4.6.1 4.6.0 4.5.5 4.5.4 4.5.3 4.5.2 3.2.20 3.2.21 3.2.22 3.2.3 3.2.5 3.2.6 3.2.7 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.7.0 3.7.1 All 111 releases
wp-optimize / vendor / matthiasmullie / minify / tests / js / AbstractTest.php

AbstractTest.php in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance 3.2.5, at vendor/matthiasmullie/minify/tests/js/AbstractTest.php

228 lines 6.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace MatthiasMullie\Minify\Test;
4
5 use MatthiasMullie\Minify;
6 use MatthiasMullie\Scrapbook\Adapters\MemoryStore;
7 use MatthiasMullie\Scrapbook\Psr6\Pool;
8 use PHPUnit\Framework\TestCase;
9 use ReflectionObject;
10
11 /**
12 * Tests common functions of abstract Minify class by using JS implementation.
13 */
14 class AbstractTest extends TestCase
15 {
16 public function testConstruct()
17 {
18 $path1 = __DIR__.'/sample/source/script1.js';
19 $path2 = __DIR__.'/sample/source/script2.js';
20 $content1 = file_get_contents($path1);
21 $content2 = file_get_contents($path2);
22
23 // 1 source in constructor
24 $minifier = new Minify\JS($content1);
25 $result = $minifier->minify();
26
27 $this->assertEquals($content1, $result);
28
29 // multiple sources in constructor
30 $minifier = new Minify\JS($content1, $content2);
31 $result = $minifier->minify();
32
33 $this->assertEquals($content1.';'.$content2, $result);
34
35 // file in constructor
36 $minifier = new Minify\JS($path1);
37 $result = $minifier->minify();
38
39 $this->assertEquals($content1, $result);
40
41 // multiple files in constructor
42 $minifier = new Minify\JS($path1, $path2);
43 $result = $minifier->minify();
44
45 $this->assertEquals($content1.';'.$content2, $result);
46 }
47
48 public function testAdd()
49 {
50 $path1 = __DIR__.'/sample/source/script1.js';
51 $path2 = __DIR__.'/sample/source/script2.js';
52 $content1 = file_get_contents($path1);
53 $content2 = file_get_contents($path2);
54 $content3 = 'var test=3';
55
56 // 1 source in add
57 $minifier = new Minify\JS();
58 $minifier->add($content1);
59 $result = $minifier->minify();
60
61 $this->assertEquals($content1, $result);
62
63 // multiple sources in add
64 $minifier = new Minify\JS();
65 $minifier->add($content1);
66 $minifier->add($content2);
67 $result = $minifier->minify();
68
69 $this->assertEquals($content1.';'.$content2, $result);
70
71 // file in add
72 $minifier = new Minify\JS();
73 $minifier->add($path1);
74 $result = $minifier->minify();
75
76 $this->assertEquals($content1, $result);
77
78 // multiple files in add
79 $minifier = new Minify\JS();
80 $minifier->add($path1);
81 $minifier->add($path2);
82 $result = $minifier->minify();
83
84 $this->assertEquals($content1.';'.$content2, $result);
85
86 // array of files in add
87 $minifier = new Minify\JS();
88 $minifier->add(array($path1, $path2));
89 $result = $minifier->minify();
90
91 $this->assertEquals($content1.';'.$content2, $result);
92
93 // array of files + overload in add
94 $minifier = new Minify\JS();
95 $minifier->add(array($path1, $path2), $content3);
96 $result = $minifier->minify();
97
98 $this->assertEquals($content1.';'.$content2.';'.$content3, $result);
99
100 $minifier = new Minify\JS();
101 $minifier->add($path1, array($path2, $content3));
102 $result = $minifier->minify();
103
104 $this->assertEquals($content1.';'.$content2.';'.$content3, $result);
105 }
106
107 public function testLoadBigString()
108 {
109 // content greater than PHP_MAXPATHLEN
110 // https://github.com/matthiasmullie/minify/issues/90
111 $content = rtrim(str_repeat('var a="b";', 500), ';');
112
113 $minifier = new Minify\JS($content);
114
115 $this->assertEquals($minifier->minify(), $content);
116 }
117
118 public function testLoadOpenBaseDirRestricted()
119 {
120 if (!function_exists('pcntl_fork') || defined('HHVM_VERSION')) {
121 $this->markTestSkipped("Can't fork, skip open_basedir test");
122 }
123
124 /*
125 * Testing open_basedir restrictions is rather annoying, since they can
126 * not be relaxed at runtime (if we tighten open_basedir for 1 test,
127 * it'll also apply for all the others)
128 * I'll run the open_basedir restriction in a separate thread instead.
129 */
130
131 $pid = pcntl_fork();
132 if ($pid === -1) {
133 // can't fork, ignore this test...
134 } elseif ($pid === 0) {
135 // https://github.com/matthiasmullie/minify/issues/111
136 ini_set('open_basedir', __DIR__.'/../..');
137
138 // instead of displaying warnings & moving to the next test, just
139 // quit with the error code; the other thread will pick it up
140 set_error_handler(function ($errno, $errstr, $errfile, $errline) {
141 // only exit if it is an error that is being reported (we don't
142 // want suppressed errors to register as failures, that happens
143 // on purpose!)
144 if (error_reporting() & $errno) {
145 exit($errno);
146 }
147 });
148
149 $minifier = new Minify\JS('/tmp/bogus/path');
150 $minifier->minify();
151
152 // don't keep executing the rest of the tests in this thread!
153 exit;
154 } else {
155 pcntl_wait($status);
156
157 $this->assertEquals($status, 0, 'open_basedir restriction caused minifier to fail');
158 }
159 }
160
161 public function testSave()
162 {
163 $path = __DIR__.'/sample/source/script1.js';
164 $content = file_get_contents($path);
165 $savePath = __DIR__.'/sample/target/script1.js';
166
167 $minifier = new Minify\JS($path);
168 $minifier->minify($savePath);
169
170 $this->assertEquals(file_get_contents($savePath), $content);
171 }
172
173 public function testCheckFileOpenFail()
174 {
175 $this->expectException('MatthiasMullie\Minify\Exceptions\IOException');
176
177 $minifier = new Minify\JS();
178 $wrongPath = '';
179
180 $object = new ReflectionObject($minifier);
181 $method = $object->getMethod('openFileForWriting');
182 $method->setAccessible(true);
183
184 $method->invokeArgs($minifier, array($wrongPath));
185 }
186
187 public function testCheckFileWriteFail()
188 {
189 $this->expectException('MatthiasMullie\Minify\Exceptions\IOException');
190
191 $minifier = new Minify\JS();
192 $wrongPath = '';
193
194 $object = new ReflectionObject($minifier);
195 $method = $object->getMethod('writeToFile');
196 $method->setAccessible(true);
197
198 $method->invokeArgs($minifier, array($wrongPath, ''));
199 }
200
201 public function testGzip()
202 {
203 $path = __DIR__.'/sample/source/script1.js';
204 $content = file_get_contents($path);
205 $savePath = __DIR__.'/sample/target/script1.js.gz';
206
207 $minifier = new Minify\JS($path);
208 $minifier->gzip($savePath, 9);
209
210 $this->assertEquals(file_get_contents($savePath), gzencode($content, 9, FORCE_GZIP));
211 }
212
213 public function testCache()
214 {
215 $path = __DIR__.'/sample/source/script1.js';
216 $content = file_get_contents($path);
217
218 $cache = new MemoryStore();
219 $pool = new Pool($cache);
220 $item = $pool->getItem('cache-script1');
221
222 $minifier = new Minify\JS($path);
223 $item = $minifier->cache($item);
224
225 $this->assertEquals($item->get(), $content);
226 }
227 }
228