| 1 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
require_once 'File/Archive.php';
|
|
|
4 |
require_once 'PHPUnit/Framework.php';
|
|
|
5 |
|
|
|
6 |
function var_dumped($x) { var_dump($x); return $x; }
|
|
|
7 |
|
|
|
8 |
/*
|
|
|
9 |
* Actually more to check that the syntax is OK
|
|
|
10 |
* than the actual functionnality
|
|
|
11 |
*/
|
|
|
12 |
class Test extends PHPUnit_Framework_TestCase
|
|
|
13 |
{
|
|
|
14 |
function testMemoryReader()
|
|
|
15 |
{
|
|
|
16 |
$this->assertTrue(
|
|
|
17 |
!PEAR::isError(
|
|
|
18 |
$reader = File_Archive::readMemory('ABCDEFGH', 'Memory')
|
|
|
19 |
) &&
|
|
|
20 |
$reader->next() &&
|
|
|
21 |
$reader->getFilename() == 'Memory' &&
|
|
|
22 |
$reader->getData(1) == 'A' &&
|
|
|
23 |
$reader->getData(2) == 'BC' &&
|
|
|
24 |
$reader->getData() == 'DEFGH' &&
|
|
|
25 |
$reader->next() == false
|
|
|
26 |
);
|
|
|
27 |
$reader->close();
|
|
|
28 |
}
|
|
|
29 |
function testFileReader()
|
|
|
30 |
{
|
|
|
31 |
$this->assertTrue(
|
|
|
32 |
!PEAR::isError(
|
|
|
33 |
$reader = File_Archive::read('test.php', 'test.php')
|
|
|
34 |
) &&
|
|
|
35 |
$reader->next() &&
|
|
|
36 |
$reader->getData() == file_get_contents('test.php') &&
|
|
|
37 |
$reader->next() == false
|
|
|
38 |
);
|
|
|
39 |
|
|
|
40 |
$reader->close();
|
|
|
41 |
}
|
|
|
42 |
function _testURLReader()
|
|
|
43 |
{
|
|
|
44 |
$reader = File_Archive::read("http://www.google.com", "google.html");
|
|
|
45 |
|
|
|
46 |
$this->assertFalse(PEAR::isError($reader));
|
|
|
47 |
$this->assertTrue($reader->next());
|
|
|
48 |
|
|
|
49 |
$data = $reader->getData();
|
|
|
50 |
$this->assertFalse(empty($data));
|
|
|
51 |
$reader->close();
|
|
|
52 |
}
|
|
|
53 |
function testMultiReader()
|
|
|
54 |
{
|
|
|
55 |
$reader = File_Archive::readMulti();
|
|
|
56 |
|
|
|
57 |
$reader->addSource(File_Archive::read("test.php", "test.php"));
|
|
|
58 |
$reader->addSource(File_Archive::readMemory("A", "A.txt"));
|
|
|
59 |
|
|
|
60 |
$this->assertTrue($reader->next());
|
|
|
61 |
$this->assertEquals("test.php", $reader->getFilename());
|
|
|
62 |
$this->assertTrue($reader->next());
|
|
|
63 |
$this->assertEquals("A.txt", $reader->getFilename());
|
|
|
64 |
$this->assertFalse($reader->next());
|
|
|
65 |
|
|
|
66 |
$reader->close();
|
|
|
67 |
}
|
|
|
68 |
function testConcatReader()
|
|
|
69 |
{
|
|
|
70 |
$source = File_Archive::readMulti();
|
|
|
71 |
$source->addSource(File_Archive::readMemory("ABCDE", "fo"));
|
|
|
72 |
$source->addSource(File_Archive::readMemory("FGHIJ", "ob"));
|
|
|
73 |
$source->addSource(File_Archive::readMemory("KLMNO", "ar"));
|
|
|
74 |
$reader = File_Archive::readConcat($source, "foobar");
|
|
|
75 |
|
|
|
76 |
$this->assertTrue($reader->next());
|
|
|
77 |
$this->assertEquals("ABC", $reader->getData(3));
|
|
|
78 |
$this->assertEquals("DEF", $reader->getData(3));
|
|
|
79 |
$this->assertEquals("GHIJKLMNO", $reader->getData());
|
|
|
80 |
$this->assertFalse($reader->next());
|
|
|
81 |
|
|
|
82 |
$reader->close();
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
function testFilter()
|
|
|
86 |
{
|
|
|
87 |
$reader = File_Archive::filter(File_Archive::predFalse(), File_Archive::read('.'));
|
|
|
88 |
$this->assertFalse($reader->next());
|
|
|
89 |
$reader->close();
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
function testTrue()
|
|
|
93 |
{
|
|
|
94 |
$source = File_Archive::readMemory('', 'A.txt'); $source->next();
|
|
|
95 |
$predicate = File_Archive::predTrue();
|
|
|
96 |
$this->assertTrue($predicate->isTrue($source));
|
|
|
97 |
}
|
|
|
98 |
function testFalse()
|
|
|
99 |
{
|
|
|
100 |
$source = File_Archive::readMemory('', 'A.txt'); $source->next();
|
|
|
101 |
$predicate = File_Archive::predFalse();
|
|
|
102 |
$this->assertFalse($predicate->isTrue($source));
|
|
|
103 |
}
|
|
|
104 |
function testAnd()
|
|
|
105 |
{
|
|
|
106 |
$source = File_Archive::readMemory('', 'A.txt'); $source->next();
|
|
|
107 |
$predicate = File_Archive::predAnd(
|
|
|
108 |
File_Archive::predTrue(),
|
|
|
109 |
File_Archive::predFalse(),
|
|
|
110 |
File_Archive::predTrue());
|
|
|
111 |
$this->assertFalse($predicate->isTrue($source));
|
|
|
112 |
|
|
|
113 |
$predicate = File_Archive::predAnd(
|
|
|
114 |
File_Archive::predTrue(),
|
|
|
115 |
File_Archive::predTrue());
|
|
|
116 |
$this->assertTrue($predicate->isTrue($source));
|
|
|
117 |
}
|
|
|
118 |
function testOr()
|
|
|
119 |
{
|
|
|
120 |
$source = File_Archive::readMemory('', 'A.txt'); $source->next();
|
|
|
121 |
$predicate = File_Archive::predOr(
|
|
|
122 |
File_Archive::predFalse(),
|
|
|
123 |
File_Archive::predFalse(),
|
|
|
124 |
File_Archive::predTrue());
|
|
|
125 |
$this->assertTrue($predicate->isTrue($source));
|
|
|
126 |
|
|
|
127 |
$predicate = File_Archive::predAnd(
|
|
|
128 |
File_Archive::predFalse(),
|
|
|
129 |
File_Archive::predFalse());
|
|
|
130 |
$this->assertFalse($predicate->isTrue($source));
|
|
|
131 |
}
|
|
|
132 |
function testNot()
|
|
|
133 |
{
|
|
|
134 |
$source = File_Archive::readMemory('', 'A.txt'); $source->next();
|
|
|
135 |
$predicate = File_Archive::predNot(File_Archive::predTrue());
|
|
|
136 |
$this->assertFalse($predicate->isTrue($source));
|
|
|
137 |
}
|
|
|
138 |
function testMinSize()
|
|
|
139 |
{
|
|
|
140 |
$source = File_Archive::readMemory('123456789', 'A.txt'); $source->next();
|
|
|
141 |
|
|
|
142 |
$predicate = File_Archive::predMinSize(9);
|
|
|
143 |
$this->assertTrue($predicate->isTrue($source));
|
|
|
144 |
|
|
|
145 |
$predicate = File_Archive::predMinSize(10);
|
|
|
146 |
$this->assertFalse($predicate->isTrue($source));
|
|
|
147 |
}
|
|
|
148 |
function testMinTime()
|
|
|
149 |
{
|
|
|
150 |
$source = File_Archive::read('test.php'); $source->next();
|
|
|
151 |
$predicate = File_Archive::predMinTime(filemtime('test.php')-1);
|
|
|
152 |
$this->assertTrue($predicate->isTrue($source));
|
|
|
153 |
|
|
|
154 |
$predicate = File_Archive::predMinTime(filemtime('test.php')+1);
|
|
|
155 |
$this->assertFalse($predicate->isTrue($source));
|
|
|
156 |
}
|
|
|
157 |
function testMaxDepth()
|
|
|
158 |
{
|
|
|
159 |
$source = File_Archive::readMemory('', 'A/B/C/1/A.txt'); $source->next();
|
|
|
160 |
|
|
|
161 |
$predicate = File_Archive::predMaxDepth(4);
|
|
|
162 |
$this->assertTrue($predicate->isTrue($source));
|
|
|
163 |
|
|
|
164 |
$predicate = File_Archive::predMaxDepth(3);
|
|
|
165 |
$this->assertFalse($predicate->isTrue($source));
|
|
|
166 |
}
|
|
|
167 |
function testExtension()
|
|
|
168 |
{
|
|
|
169 |
$source = File_Archive::read('test.php'); $source->next();
|
|
|
170 |
$predicate = File_Archive::predExtension(array('php', 'txt'));
|
|
|
171 |
$this->assertTrue($predicate->isTrue($source));
|
|
|
172 |
|
|
|
173 |
$predicate = File_Archive::predExtension('txt');
|
|
|
174 |
$this->assertFalse($predicate->isTrue($source));
|
|
|
175 |
}
|
|
|
176 |
function testEreg()
|
|
|
177 |
{
|
|
|
178 |
$source = File_Archive::readMemory('', 'A/B/C/1/A.txt'); $source->next();
|
|
|
179 |
|
|
|
180 |
$predicate = File_Archive::predEreg('/A');
|
|
|
181 |
$this->assertTrue($predicate->isTrue($source));
|
|
|
182 |
}
|
|
|
183 |
function testEregi()
|
|
|
184 |
{
|
|
|
185 |
$source = File_Archive::readMemory('', 'A/B/C/1/A.txt'); $source->next();
|
|
|
186 |
|
|
|
187 |
$predicate = File_Archive::predEregi('/a');
|
|
|
188 |
$this->assertTrue($predicate->isTrue($source));
|
|
|
189 |
}
|
|
|
190 |
function testCustom()
|
|
|
191 |
{
|
|
|
192 |
$source = File_Archive::readMemory('', 'A/B/C/1/A.txt'); $source->next();
|
|
|
193 |
|
|
|
194 |
$predicate = File_Archive::predCustom('return ereg("/A",$source->getFilename());');
|
|
|
195 |
$this->assertTrue($predicate->isTrue($source));
|
|
|
196 |
|
|
|
197 |
$predicate = File_Archive::predCustom('ereg("/A",$source->getFilename());');
|
|
|
198 |
$this->assertTrue($predicate->isTrue($source));
|
|
|
199 |
|
|
|
200 |
$predicate = File_Archive::predCustom('ereg("/A",$source->getFilename())');
|
|
|
201 |
$this->assertTrue($predicate->isTrue($source));
|
|
|
202 |
}
|
|
|
203 |
function testMIME()
|
|
|
204 |
{
|
|
|
205 |
$source = File_Archive::readMemory('', 'A.jpg'); $source->next();
|
|
|
206 |
|
|
|
207 |
$predicate = File_Archive::predMIME("image/jpeg");
|
|
|
208 |
$this->assertTrue($predicate->isTrue($source));
|
|
|
209 |
|
|
|
210 |
$predicate = File_Archive::predMIME("image/*");
|
|
|
211 |
$this->assertTrue($predicate->isTrue($source));
|
|
|
212 |
|
|
|
213 |
$predicate = File_Archive::predMIME("application/*");
|
|
|
214 |
$this->assertFalse($predicate->isTrue($source));
|
|
|
215 |
}
|
|
|
216 |
|
|
|
217 |
//TODO: test the toMail
|
|
|
218 |
function testToMemory()
|
|
|
219 |
{
|
|
|
220 |
$this->assertTrue(
|
|
|
221 |
!PEAR::isError(
|
|
|
222 |
File_Archive::extract(
|
|
|
223 |
File_Archive::read('test.php'),
|
|
|
224 |
$dest = File_Archive::toMemory()
|
|
|
225 |
)
|
|
|
226 |
) &&
|
|
|
227 |
file_get_contents('test.php') == $dest->getData()
|
|
|
228 |
);
|
|
|
229 |
}
|
|
|
230 |
function _testArchive($extension)
|
|
|
231 |
{
|
|
|
232 |
$filename = "test.$extension";
|
|
|
233 |
|
|
|
234 |
$this->assertTrue(
|
|
|
235 |
!PEAR::isError(
|
|
|
236 |
File_Archive::extract(
|
|
|
237 |
File_Archive::read('test.php'),
|
|
|
238 |
File_Archive::toArchive(
|
|
|
239 |
$filename,
|
|
|
240 |
$compressed = File_Archive::toMemory()
|
|
|
241 |
)
|
|
|
242 |
)
|
|
|
243 |
) &&
|
|
|
244 |
!PEAR::isError(
|
|
|
245 |
File_Archive::extract(
|
|
|
246 |
File_Archive::readSource(
|
|
|
247 |
$compressed->makeReader(), "$filename/test.php"
|
|
|
248 |
),
|
|
|
249 |
File_Archive::toVariable($uncompressed)
|
|
|
250 |
)
|
|
|
251 |
) &&
|
|
|
252 |
$uncompressed == file_get_contents('test.php')
|
|
|
253 |
);
|
|
|
254 |
}
|
|
|
255 |
function testTar() { $this->_testArchive('tar'); }
|
|
|
256 |
function testZip() { $this->_testArchive('zip'); }
|
|
|
257 |
function testAr() { $this->_testArchive('ar'); }
|
|
|
258 |
function testTgz() { $this->_testArchive('tgz'); }
|
|
|
259 |
function testTbz() { $this->_testArchive('tbz'); }
|
|
|
260 |
function _testWriteGZip2()
|
|
|
261 |
{
|
|
|
262 |
//Build the writer
|
|
|
263 |
$writer = File_Archive::toArchive('example1.tgz', File_Archive::toFiles());
|
|
|
264 |
|
|
|
265 |
//Write the list of even number in [0..999]
|
|
|
266 |
$writer->newFile("even.txt");
|
|
|
267 |
for ($i=0; $i<1000; $i+=2)
|
|
|
268 |
{
|
|
|
269 |
$writer->writeData("$i\n");
|
|
|
270 |
}
|
|
|
271 |
|
|
|
272 |
//Write the list of odd number in [0..999]
|
|
|
273 |
$writer->newFile("odd.txt");
|
|
|
274 |
for ($i=1; $i<1000; $i+=2)
|
|
|
275 |
{
|
|
|
276 |
$writer->writeData("$i\n");
|
|
|
277 |
}
|
|
|
278 |
|
|
|
279 |
//Close the writer
|
|
|
280 |
$writer->close();
|
|
|
281 |
}
|
|
|
282 |
function _testDirectories()
|
|
|
283 |
{
|
|
|
284 |
$this->assertTrue(
|
|
|
285 |
!PEAR::isError(
|
|
|
286 |
File_Archive::extract(
|
|
|
287 |
File_Archive::read('../Archive'),
|
|
|
288 |
File_Archive::toArchive('up.tbz', File_Archive::toFiles())
|
|
|
289 |
)
|
|
|
290 |
)
|
|
|
291 |
);
|
|
|
292 |
}
|
|
|
293 |
|
|
|
294 |
|
|
|
295 |
function testMultiWriter()
|
|
|
296 |
{
|
|
|
297 |
$this->assertTrue(
|
|
|
298 |
!PEAR::isError(
|
|
|
299 |
File_Archive::extract(
|
|
|
300 |
File_Archive::readMemory("ABCDEF", "A.txt"),
|
|
|
301 |
File_Archive::toMulti(
|
|
|
302 |
File_Archive::toVariable($a),
|
|
|
303 |
File_Archive::toVariable($b)
|
|
|
304 |
)
|
|
|
305 |
)
|
|
|
306 |
) &&
|
|
|
307 |
$a == 'ABCDEF' &&
|
|
|
308 |
$b == 'ABCDEF'
|
|
|
309 |
);
|
|
|
310 |
}
|
|
|
311 |
function _testReadArchive()
|
|
|
312 |
{
|
|
|
313 |
$source = File_Archive::readArchive('tar', File_Archive::read('up.tar'));
|
|
|
314 |
while ($source->next())
|
|
|
315 |
echo $source->getFilename()."\n";
|
|
|
316 |
}
|
|
|
317 |
|
|
|
318 |
function _testRemove($extension)
|
|
|
319 |
{
|
|
|
320 |
$writer = File_Archive::toArchive(
|
|
|
321 |
"test.$extension",
|
|
|
322 |
File_Archive::toVariable($x)
|
|
|
323 |
);
|
|
|
324 |
$writer->newFile('foo.txt');
|
|
|
325 |
$writer->writeData('ABCDEF');
|
|
|
326 |
$writer->newFile('bla_to_remove.txt');
|
|
|
327 |
$writer->writeData('GHIJKL');
|
|
|
328 |
$writer->newFile('bar.txt');
|
|
|
329 |
$writer->writeData('MNOP');
|
|
|
330 |
$writer->close();
|
|
|
331 |
|
|
|
332 |
File_Archive::removeFromSource(
|
|
|
333 |
File_Archive::predEreg('_to_remove'),
|
|
|
334 |
$source = File_Archive::readSource(
|
|
|
335 |
File_Archive::readMemory($x, "test.$extension"),
|
|
|
336 |
"test.$extension/")
|
|
|
337 |
);
|
|
|
338 |
$this->assertEquals(
|
|
|
339 |
"foo.txt\nbar.txt",
|
|
|
340 |
implode("\n", $source->getFileList())
|
|
|
341 |
);
|
|
|
342 |
}
|
|
|
343 |
function testRemoveTar() { return $this->_testRemove('tar'); }
|
|
|
344 |
function testRemoveTgz() { return $this->_testRemove('tbz'); }
|
|
|
345 |
function testRemoveTbz() { return $this->_testRemove('tgz'); }
|
|
|
346 |
function testRemoveAr() { return $this->_testRemove('ar'); }
|
|
|
347 |
function testRemoveZip() { return $this->_testRemove('zip'); }
|
|
|
348 |
}
|
|
|
349 |
|
|
|
350 |
//PEAR::setErrorHandling(PEAR_ERROR_PRINT);
|
|
|
351 |
|
|
|
352 |
//$test = new PHPUnit_TestSuite("Test");
|
|
|
353 |
//$result = PHPUnit::run($test);
|
|
|
354 |
//echo $result->toHTML();
|
|
|
355 |
?>
|