1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of graze/data-file |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
* @license https://github.com/graze/data-file/blob/master/LICENSE.md |
11
|
|
|
* @link https://github.com/graze/data-file |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Graze\DataFile\Test\Integration\Modify\Compress; |
15
|
|
|
|
16
|
|
|
use Graze\DataFile\Helper\Builder\Builder; |
17
|
|
|
use Graze\DataFile\Helper\Builder\BuilderInterface; |
18
|
|
|
use Graze\DataFile\Modify\Compress\CompressionFactory; |
19
|
|
|
use Graze\DataFile\Modify\Compress\CompressorInterface; |
20
|
|
|
use Graze\DataFile\Modify\Compress\DeCompressorInterface; |
21
|
|
|
use Graze\DataFile\Modify\Compress\Zip; |
22
|
|
|
use Graze\DataFile\Node\FileNodeInterface; |
23
|
|
|
use Graze\DataFile\Node\LocalFile; |
24
|
|
|
use Graze\DataFile\Test\AbstractFileTestCase; |
25
|
|
|
use InvalidArgumentException; |
26
|
|
|
use Mockery as m; |
27
|
|
|
use Mockery\MockInterface; |
28
|
|
|
use Symfony\Component\Process\Exception\ProcessFailedException; |
29
|
|
|
use Symfony\Component\Process\Process; |
30
|
|
|
|
31
|
|
|
class ZipTest extends AbstractFileTestCase |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @var Zip |
35
|
|
|
*/ |
36
|
|
|
protected $zip; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var BuilderInterface|MockInterface |
40
|
|
|
*/ |
41
|
|
|
private $builder; |
42
|
|
|
|
43
|
|
|
public function setUp() |
44
|
|
|
{ |
45
|
|
|
$this->builder = m::mock(Builder::class)->makePartial(); |
46
|
|
|
$this->zip = new Zip(); |
47
|
|
|
$this->zip->setBuilder($this->builder); |
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function testInstanceOfCompressorInterface() |
51
|
|
|
{ |
52
|
|
|
static::assertInstanceOf(CompressorInterface::class, $this->zip); |
53
|
|
|
static::assertInstanceOf(DeCompressorInterface::class, $this->zip); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function testFileGetsCompressedAsZip() |
57
|
|
|
{ |
58
|
|
|
$file = new LocalFile(static::$dir . 'uncompressed_zip.test'); |
59
|
|
|
|
60
|
|
|
$file->put('random stuff and things!'); |
61
|
|
|
|
62
|
|
|
$compressedFile = $this->zip->compress($file); |
63
|
|
|
|
64
|
|
|
static::assertNotNull($compressedFile); |
65
|
|
|
static::assertInstanceOf(FileNodeInterface::class, $compressedFile); |
66
|
|
|
static::assertEquals(static::$dir . 'uncompressed_zip.zip', $compressedFile->getPath()); |
67
|
|
|
static::assertTrue($compressedFile->exists()); |
68
|
|
|
static::assertEquals(Zip::NAME, $compressedFile->getCompression()); |
69
|
|
|
|
70
|
|
|
$cmd = "file {$compressedFile->getPath()} | grep " . escapeshellarg('\bzip\b') . " | wc -l"; |
71
|
|
|
$result = exec($cmd); |
72
|
|
|
static::assertEquals(1, $result, "File is not compressed as zip"); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
View Code Duplication |
public function testFileGetsDecompressedFromZip() |
|
|
|
|
76
|
|
|
{ |
77
|
|
|
$file = new LocalFile(static::$dir . 'get_decompressed_uncompressed_zip.test'); |
78
|
|
|
$file->put('random stuff and things!'); |
79
|
|
|
|
80
|
|
|
$compressedFile = $this->zip->compress($file); |
81
|
|
|
|
82
|
|
|
static::assertTrue($compressedFile->exists()); |
83
|
|
|
$uncompressedFile = $this->zip->decompress($compressedFile); |
84
|
|
|
|
85
|
|
|
static::assertNotNull($uncompressedFile); |
86
|
|
|
static::assertInstanceOf(FileNodeInterface::class, $uncompressedFile); |
87
|
|
|
static::assertEquals(static::$dir . 'get_decompressed_uncompressed_zip', $uncompressedFile->getPath()); |
88
|
|
|
static::assertTrue($uncompressedFile->exists()); |
89
|
|
|
static::assertEquals(CompressionFactory::TYPE_NONE, $uncompressedFile->getCompression()); |
90
|
|
|
|
91
|
|
|
$cmd = "file {$uncompressedFile->getPath()} | grep " . escapeshellarg('\bzip\b') . " | wc -l"; |
92
|
|
|
$result = exec($cmd); |
93
|
|
|
static::assertEquals(0, $result, "File should not be compressed"); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
View Code Duplication |
public function testCallingZipWithAFileThatDoesNotExistsThrowsAnException() |
|
|
|
|
97
|
|
|
{ |
98
|
|
|
$file = new LocalFile(static::$dir . 'invalid_zip.test'); |
99
|
|
|
|
100
|
|
|
$this->expectException(InvalidArgumentException::class); |
101
|
|
|
|
102
|
|
|
$this->zip->compress($file); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
View Code Duplication |
public function testCallingUnzipWithAFileThatDoesNotExistsThrowsAnException() |
|
|
|
|
106
|
|
|
{ |
107
|
|
|
$file = new LocalFile(static::$dir . 'invalid_zip.zip'); |
108
|
|
|
|
109
|
|
|
$this->expectException(InvalidArgumentException::class); |
110
|
|
|
|
111
|
|
|
$this->zip->decompress($file); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function testWhenTheProcessFailsAnExceptionIsthrownOnZip() |
115
|
|
|
{ |
116
|
|
|
$process = m::mock(Process::class)->makePartial(); |
117
|
|
|
$this->builder->shouldReceive('build') |
|
|
|
|
118
|
|
|
->andReturn($process); |
119
|
|
|
$process->shouldReceive('run')->once(); |
120
|
|
|
$process->shouldReceive('isSuccessful')->andReturn(false); |
121
|
|
|
$process->shouldReceive('getCommandLine')->andReturn(''); |
122
|
|
|
$process->shouldReceive('getExitCode')->andReturn(1); |
123
|
|
|
$process->shouldReceive('getExitCodeText')->andReturn('bla'); |
124
|
|
|
$process->shouldReceive('getWorkingDirectory')->andReturn('/something/'); |
125
|
|
|
$process->shouldReceive('isOutputDisabled')->andReturn('true'); |
126
|
|
|
|
127
|
|
|
$file = new LocalFile(static::$dir . 'failed_zip_process.test'); |
128
|
|
|
|
129
|
|
|
$file->put('random stuff and things 2!'); |
130
|
|
|
|
131
|
|
|
$this->expectException(ProcessFailedException::class); |
132
|
|
|
|
133
|
|
|
$this->zip->compress($file); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function testWhenTheProcessFailsAnExceptionIsthrownOnUnzip() |
137
|
|
|
{ |
138
|
|
|
$process = m::mock(Process::class)->makePartial(); |
139
|
|
|
$this->builder->shouldReceive('build') |
|
|
|
|
140
|
|
|
->andReturn($process); |
141
|
|
|
$process->shouldReceive('run')->once(); |
142
|
|
|
$process->shouldReceive('isSuccessful')->andReturn(false); |
143
|
|
|
$process->shouldReceive('getCommandLine')->andReturn(''); |
144
|
|
|
$process->shouldReceive('getExitCode')->andReturn(1); |
145
|
|
|
$process->shouldReceive('getExitCodeText')->andReturn('bla'); |
146
|
|
|
$process->shouldReceive('getWorkingDirectory')->andReturn('/something/'); |
147
|
|
|
$process->shouldReceive('isOutputDisabled')->andReturn('true'); |
148
|
|
|
|
149
|
|
|
$file = new LocalFile(static::$dir . 'failed_unzip_process.test'); |
150
|
|
|
|
151
|
|
|
$file->put('random stuff and things 2!'); |
152
|
|
|
|
153
|
|
|
$this->expectException(ProcessFailedException::class); |
154
|
|
|
|
155
|
|
|
$this->zip->decompress($file); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
View Code Duplication |
public function testPassingTheKeepOldFileOptionWillKeepTheFile() |
|
|
|
|
159
|
|
|
{ |
160
|
|
|
$file = new LocalFile(static::$dir . 'keep_file_zip.test'); |
161
|
|
|
|
162
|
|
|
$file->put('random stuff and things!'); |
163
|
|
|
|
164
|
|
|
$compressedFile = $this->zip->compress($file, ['keepOldFile' => true]); |
165
|
|
|
|
166
|
|
|
static::assertTrue($file->exists()); |
167
|
|
|
static::assertTrue($compressedFile->exists()); |
168
|
|
|
|
169
|
|
|
$uncompresssedFile = $this->zip->decompress($compressedFile, ['keepOldFile' => true]); |
170
|
|
|
|
171
|
|
|
static::assertTrue($compressedFile->exists()); |
172
|
|
|
static::assertTrue($uncompresssedFile->exists()); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
View Code Duplication |
public function testPassingFalseToKeepOldFileOptionWillKeepTheFile() |
|
|
|
|
176
|
|
|
{ |
177
|
|
|
$file = new LocalFile(static::$dir . 'delete_file_zip.test'); |
178
|
|
|
|
179
|
|
|
$file->put('random stuff and things!'); |
180
|
|
|
|
181
|
|
|
$compressedFile = $this->zip->compress($file, ['keepOldFile' => false]); |
182
|
|
|
|
183
|
|
|
static::assertFalse($file->exists()); |
184
|
|
|
static::assertTrue($compressedFile->exists()); |
185
|
|
|
|
186
|
|
|
$uncompresssedFile = $this->zip->decompress($compressedFile, ['keepOldFile' => false]); |
187
|
|
|
|
188
|
|
|
static::assertFalse($compressedFile->exists()); |
189
|
|
|
static::assertTrue($uncompresssedFile->exists()); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: