-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArchiveTest.php
More file actions
432 lines (368 loc) · 12.8 KB
/
Copy pathArchiveTest.php
File metadata and controls
432 lines (368 loc) · 12.8 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
<?php
/**
* @author : Jakiboy
* @package : FloatPHP
* @subpackage : Classes Filesystem Component Tests
* @version : 1.5.x
* @copyright : (c) 2018 - 2025 Jihad Sinnaour <[email protected]>
* @link : https://floatphp.com
* @license : MIT
*
* This file is a part of FloatPHP Framework.
*/
declare(strict_types=1);
namespace FloatPHP\Tests\Classes\Filesystem;
use PHPUnit\Framework\TestCase;
use FloatPHP\Classes\Filesystem\Archive;
use \ZipArchive;
/**
* Unit tests for Archive class.
*/
class ArchiveTest extends TestCase
{
/**
* @var string
*/
private $testDir;
/**
* @var string
*/
private $testFile;
/**
* @var string
*/
private $testArchive;
/**
* Set up test environment before each test.
*/
protected function setUp() : void
{
$this->testDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'floatphp_archive_test_' . uniqid();
$this->testFile = $this->testDir . DIRECTORY_SEPARATOR . 'test.txt';
$this->testArchive = $this->testDir . DIRECTORY_SEPARATOR . 'test.zip';
// Create test directory and file
if ( !is_dir($this->testDir) ) {
mkdir($this->testDir, 0755, true);
}
file_put_contents($this->testFile, 'Hello World Test Content');
}
/**
* Test compress method with file.
*/
public function testCompressFile() : void
{
if ( !extension_loaded('zip') ) {
$this->markTestSkipped('ZIP extension is not available.');
}
$result = Archive::compress($this->testFile, $this->testDir, 'test_file');
$this->assertTrue($result);
$this->assertFileExists($this->testDir . DIRECTORY_SEPARATOR . 'test_file.zip');
}
/**
* Test compress method with directory.
*/
public function testCompressDirectory() : void
{
if ( !extension_loaded('zip') ) {
$this->markTestSkipped('ZIP extension is not available.');
}
// Create subdirectory with files
$subDir = $this->testDir . DIRECTORY_SEPARATOR . 'subdir';
mkdir($subDir);
file_put_contents($subDir . DIRECTORY_SEPARATOR . 'sub_test.txt', 'Sub content');
$result = Archive::compress($this->testDir, dirname($this->testDir), 'test_dir');
$this->assertTrue($result);
$archivePath = dirname($this->testDir) . DIRECTORY_SEPARATOR . 'test_dir.zip';
$this->assertFileExists($archivePath);
// Clean up
if ( file_exists($archivePath) ) {
unlink($archivePath);
}
}
/**
* Test compress method with non-existent path.
*/
public function testCompressNonExistentPath() : void
{
$result = Archive::compress('/non/existent/path');
$this->assertFalse($result);
}
/**
* Test uncompress method.
*/
public function testUncompress() : void
{
if ( !extension_loaded('zip') ) {
$this->markTestSkipped('ZIP extension is not available.');
}
// First create an archive
$zip = new ZipArchive();
if ( $zip->open($this->testArchive, ZipArchive::CREATE) === true ) {
$zip->addFile($this->testFile, 'test.txt');
$zip->close();
$extractDir = $this->testDir . DIRECTORY_SEPARATOR . 'extracted';
mkdir($extractDir);
$result = Archive::uncompress($this->testArchive, $extractDir);
$this->assertTrue($result);
$this->assertFileExists($extractDir . DIRECTORY_SEPARATOR . 'test.txt');
} else {
$this->markTestSkipped('Could not create test archive.');
}
}
/**
* Test uncompress with remove option.
*/
public function testUncompressWithRemove() : void
{
if ( !extension_loaded('zip') ) {
$this->markTestSkipped('ZIP extension is not available.');
}
// Create a test archive
$zip = new ZipArchive();
if ( $zip->open($this->testArchive, ZipArchive::CREATE) === true ) {
$zip->addFile($this->testFile, 'test.txt');
$zip->close();
$extractDir = $this->testDir . DIRECTORY_SEPARATOR . 'extracted2';
mkdir($extractDir);
$result = Archive::uncompress($this->testArchive, $extractDir, true);
$this->assertTrue($result);
$this->assertFileExists($extractDir . DIRECTORY_SEPARATOR . 'test.txt');
$this->assertFileDoesNotExist($this->testArchive);
} else {
$this->markTestSkipped('Could not create test archive.');
}
}
/**
* Test uncompress with non-existent archive.
*/
public function testUncompressNonExistentArchive() : void
{
$result = Archive::uncompress('/non/existent/archive.zip');
$this->assertFalse($result);
}
/**
* Test isValid method with valid archive.
*/
public function testIsValidWithValidArchive() : void
{
if ( !extension_loaded('zip') ) {
$this->markTestSkipped('ZIP extension is not available.');
}
// Create a valid archive
$zip = new ZipArchive();
if ( $zip->open($this->testArchive, ZipArchive::CREATE) === true ) {
$zip->addFile($this->testFile, 'test.txt');
$zip->close();
$result = Archive::isValid($this->testArchive);
$this->assertTrue($result);
} else {
$this->markTestSkipped('Could not create test archive.');
}
}
/**
* Test isValid method with invalid archive.
*/
public function testIsValidWithInvalidArchive() : void
{
// Create a fake zip file
file_put_contents($this->testArchive, 'This is not a valid zip file');
$result = Archive::isValid($this->testArchive);
$this->assertFalse($result);
}
/**
* Test isValid method with non-existent file.
*/
public function testIsValidWithNonExistentFile() : void
{
$result = Archive::isValid('/non/existent/file.zip');
$this->assertFalse($result);
}
/**
* Test getInfo method.
*/
public function testGetInfo() : void
{
if ( !extension_loaded('zip') ) {
$this->markTestSkipped('ZIP extension is not available.');
}
// Create a test archive with multiple files
$zip = new ZipArchive();
if ( $zip->open($this->testArchive, ZipArchive::CREATE) === true ) {
$zip->addFile($this->testFile, 'test.txt');
$zip->addFromString('another.txt', 'Another file content');
$zip->setArchiveComment('Test archive comment');
$zip->close();
$info = Archive::getInfo($this->testArchive);
$this->assertIsArray($info);
$this->assertEquals('test.zip', $info['filename']);
$this->assertEquals(2, $info['numFiles']);
$this->assertEquals('Test archive comment', $info['comment']);
$this->assertCount(2, $info['files']);
// Check file details
$this->assertEquals('test.txt', $info['files'][0]['name']);
$this->assertEquals('another.txt', $info['files'][1]['name']);
} else {
$this->markTestSkipped('Could not create test archive.');
}
}
/**
* Test getInfo method with invalid archive.
*/
public function testGetInfoWithInvalidArchive() : void
{
$result = Archive::getInfo('/non/existent/file.zip');
$this->assertFalse($result);
}
/**
* Test extractFile method.
*/
public function testExtractFile() : void
{
if ( !extension_loaded('zip') ) {
$this->markTestSkipped('ZIP extension is not available.');
}
// Create a test archive
$zip = new ZipArchive();
if ( $zip->open($this->testArchive, ZipArchive::CREATE) === true ) {
$zip->addFile($this->testFile, 'test.txt');
$zip->addFromString('other.txt', 'Other content');
$zip->close();
$extractDir = $this->testDir . DIRECTORY_SEPARATOR . 'single_extract';
mkdir($extractDir);
$result = Archive::extractFile($this->testArchive, 'test.txt', $extractDir);
$this->assertTrue($result);
$this->assertFileExists($extractDir . DIRECTORY_SEPARATOR . 'test.txt');
$this->assertFileDoesNotExist($extractDir . DIRECTORY_SEPARATOR . 'other.txt');
} else {
$this->markTestSkipped('Could not create test archive.');
}
}
/**
* Test extractFile method with non-existent file.
*/
public function testExtractFileWithNonExistentFile() : void
{
if ( !extension_loaded('zip') ) {
$this->markTestSkipped('ZIP extension is not available.');
}
// Create a test archive
$zip = new ZipArchive();
if ( $zip->open($this->testArchive, ZipArchive::CREATE) === true ) {
$zip->addFile($this->testFile, 'test.txt');
$zip->close();
$result = Archive::extractFile($this->testArchive, 'non_existent.txt', $this->testDir);
$this->assertFalse($result);
} else {
$this->markTestSkipped('Could not create test archive.');
}
}
/**
* Test isGzip method with valid gzip file.
*/
public function testIsGzipWithValidFile() : void
{
if ( !extension_loaded('zlib') ) {
$this->markTestSkipped('ZLIB extension is not available.');
}
$gzipFile = $this->testDir . DIRECTORY_SEPARATOR . 'test.txt.gz';
$gz = gzopen($gzipFile, 'w9');
gzwrite($gz, 'Test gzip content');
gzclose($gz);
$result = Archive::isGzip($gzipFile);
$this->assertTrue($result);
}
/**
* Test isGzip method with invalid file.
*/
public function testIsGzipWithInvalidFile() : void
{
$fakeGzipFile = $this->testDir . DIRECTORY_SEPARATOR . 'fake.gz';
file_put_contents($fakeGzipFile, 'This is not a gzip file');
$result = Archive::isGzip($fakeGzipFile);
$this->assertFalse($result);
}
/**
* Test isGzip method with non-gz extension.
*/
public function testIsGzipWithNonGzExtension() : void
{
$result = Archive::isGzip($this->testFile);
$this->assertFalse($result);
}
/**
* Test unGzip method.
*/
public function testUnGzip() : void
{
if ( !extension_loaded('zlib') ) {
$this->markTestSkipped('ZLIB extension is not available.');
}
$gzipFile = $this->testDir . DIRECTORY_SEPARATOR . 'test.txt.gz';
$content = 'Test gzip content for extraction';
$gz = gzopen($gzipFile, 'w9');
gzwrite($gz, $content);
gzclose($gz);
$result = Archive::unGzip($gzipFile);
$this->assertTrue($result);
$extractedFile = $this->testDir . DIRECTORY_SEPARATOR . 'test.txt';
$this->assertFileExists($extractedFile);
$this->assertEquals($content, file_get_contents($extractedFile));
}
/**
* Test unGzip method with remove option.
*/
public function testUnGzipWithRemove() : void
{
if ( !extension_loaded('zlib') ) {
$this->markTestSkipped('ZLIB extension is not available.');
}
$gzipFile = $this->testDir . DIRECTORY_SEPARATOR . 'test2.txt.gz';
$content = 'Test gzip content for extraction with remove';
$gz = gzopen($gzipFile, 'w9');
gzwrite($gz, $content);
gzclose($gz);
$result = Archive::unGzip($gzipFile, 4096, true);
$this->assertTrue($result);
$extractedFile = $this->testDir . DIRECTORY_SEPARATOR . 'test2.txt';
$this->assertFileExists($extractedFile);
$this->assertFileDoesNotExist($gzipFile);
$this->assertEquals($content, file_get_contents($extractedFile));
}
/**
* Test unGzip method with non-existent file.
*/
public function testUnGzipWithNonExistentFile() : void
{
$result = Archive::unGzip('/non/existent/file.gz');
$this->assertFalse($result);
}
/**
* Clean up after each test.
*/
protected function tearDown() : void
{
// Recursively remove test directory
if ( is_dir($this->testDir) ) {
$this->removeDirectory($this->testDir);
}
}
/**
* Helper method to recursively remove directory.
*/
private function removeDirectory(string $dir) : void
{
if ( !is_dir($dir) ) {
return;
}
$files = array_diff(scandir($dir), ['.', '..']);
foreach ($files as $file) {
$path = $dir . DIRECTORY_SEPARATOR . $file;
if ( is_dir($path) ) {
$this->removeDirectory($path);
} else {
unlink($path);
}
}
rmdir($dir);
}
}