|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Velikonja\LabbyBundle\Test\Util; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
6
|
|
|
use Velikonja\LabbyBundle\Util\ZipArchive; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class ZipArchiveTest |
|
10
|
|
|
* |
|
11
|
|
|
* @package Velikonja\LabbyBundle\Test\Util |
|
12
|
|
|
*/ |
|
13
|
|
|
class ZipArchiveTest extends \PHPUnit_Framework_TestCase |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
private $tmpDir; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Create temporary directory, if exists removes it first. |
|
22
|
|
|
*/ |
|
23
|
|
|
public function setUp() |
|
24
|
|
|
{ |
|
25
|
|
|
$this->tmpDir = sys_get_temp_dir() . '/tmp'; //TODO: change path |
|
26
|
|
|
|
|
27
|
|
|
$fs = new Filesystem(); |
|
28
|
|
|
$fs->remove($this->tmpDir); |
|
29
|
|
|
$fs->mkdir($this->tmpDir); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Remove temporary directory. |
|
34
|
|
|
*/ |
|
35
|
|
|
public function tearDown() |
|
36
|
|
|
{ |
|
37
|
|
|
$fs = new Filesystem(); |
|
38
|
|
|
$fs->remove($this->tmpDir); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Tests if current file content is zipped. |
|
43
|
|
|
* |
|
44
|
|
|
* @throws \Exception |
|
45
|
|
|
*/ |
|
46
|
|
|
public function testSimpleZipOfContent() |
|
47
|
|
|
{ |
|
48
|
|
|
$tmpFile = $this->tmpDir . '/archive.zip'; |
|
49
|
|
|
|
|
50
|
|
|
$zip = new ZipArchive($this->tmpDir); |
|
51
|
|
|
$zip->zip($tmpFile, file_get_contents(__FILE__)); |
|
52
|
|
|
|
|
53
|
|
|
$this->assertTrue( |
|
54
|
|
|
file_exists($tmpFile), |
|
55
|
|
|
"File `$tmpFile` not created." |
|
56
|
|
|
); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @expectedException \InvalidArgumentException |
|
61
|
|
|
*/ |
|
62
|
|
|
public function testIfExceptionIsThrownWhenCompressedFileIsNotFound() |
|
63
|
|
|
{ |
|
64
|
|
|
$tmpZipFile = $this->tmpDir . '/test.zip'; |
|
65
|
|
|
|
|
66
|
|
|
$zip = new \ZipArchive(); |
|
67
|
|
|
$zip->open($tmpZipFile, \ZipArchive::CREATE); |
|
68
|
|
|
$zip->addFromString('foo.bar', ''); |
|
69
|
|
|
$zip->close(); |
|
70
|
|
|
|
|
71
|
|
|
$zip = new ZipArchive($this->tmpDir); |
|
72
|
|
|
$zip->unzip($tmpZipFile); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* ZipArchive should throw exception if directory is not writable. |
|
77
|
|
|
* |
|
78
|
|
|
* @expectedException \Exception |
|
79
|
|
|
*/ |
|
80
|
|
|
public function testThrowExceptionIfTempDirIsNotWritable() |
|
81
|
|
|
{ |
|
82
|
|
|
$unwritableDir = $this->tmpDir . '/unwritable'; |
|
83
|
|
|
|
|
84
|
|
|
mkdir($unwritableDir, 0444); // write only dir |
|
85
|
|
|
|
|
86
|
|
|
new ZipArchive($unwritableDir); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Should return false if file is not zip. |
|
91
|
|
|
*/ |
|
92
|
|
|
public function testShouldReturnFalseIfFileIsNotZip() |
|
93
|
|
|
{ |
|
94
|
|
|
$zip = new ZipArchive(); |
|
95
|
|
|
|
|
96
|
|
|
$this->assertFalse( |
|
97
|
|
|
$zip->isZipped(__FILE__), |
|
98
|
|
|
basename(__FILE__) . ' is not zipped file, but ZipArchive reports it is.' |
|
99
|
|
|
); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|