1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Graze\DataFile\Test\Integration\Modify\Compress; |
4
|
|
|
|
5
|
|
|
use Graze\DataFile\Helper\Process\ProcessFactory; |
6
|
|
|
use Graze\DataFile\Modify\Compress\CompressionFactory; |
7
|
|
|
use Graze\DataFile\Modify\Compress\Compressor; |
8
|
|
|
use Graze\DataFile\Modify\Compress\CompressorInterface; |
9
|
|
|
use Graze\DataFile\Modify\Compress\DeCompressorInterface; |
10
|
|
|
use Graze\DataFile\Modify\Compress\Gzip; |
11
|
|
|
use Graze\DataFile\Node\FileNodeInterface; |
12
|
|
|
use Graze\DataFile\Node\LocalFile; |
13
|
|
|
use Graze\DataFile\Test\FileTestCase; |
14
|
|
|
use InvalidArgumentException; |
15
|
|
|
use Mockery as m; |
16
|
|
|
use Symfony\Component\Process\Exception\ProcessFailedException; |
17
|
|
|
use Symfony\Component\Process\Process; |
18
|
|
|
|
19
|
|
|
class GzipTest extends FileTestCase |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var Gzip |
23
|
|
|
*/ |
24
|
|
|
protected $gzip; |
25
|
|
|
|
26
|
|
|
public function setUp() |
27
|
|
|
{ |
28
|
|
|
$this->gzip = new Gzip(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function testInstanceOfCompressorInterface() |
32
|
|
|
{ |
33
|
|
|
static::assertInstanceOf(CompressorInterface::class, $this->gzip); |
34
|
|
|
static::assertInstanceOf(DeCompressorInterface::class, $this->gzip); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function testFileGetsCompressedAsGzip() |
38
|
|
|
{ |
39
|
|
|
$file = new LocalFile(static::$dir . 'uncompressed_gz.test'); |
40
|
|
|
|
41
|
|
|
$file->put('random stuff and things!'); |
42
|
|
|
|
43
|
|
|
$compressedFile = $this->gzip->compress($file); |
44
|
|
|
|
45
|
|
|
static::assertNotNull($compressedFile); |
46
|
|
|
static::assertInstanceOf(FileNodeInterface::class, $compressedFile); |
47
|
|
|
static::assertEquals(static::$dir . 'uncompressed_gz.gz', $compressedFile->getPath()); |
48
|
|
|
static::assertTrue($compressedFile->exists()); |
49
|
|
|
static::assertEquals(Gzip::NAME, $compressedFile->getCompression()); |
50
|
|
|
|
51
|
|
|
$cmd = "file {$compressedFile->getPath()} | grep " . escapeshellarg('\bgzip\b') . " | wc -l"; |
52
|
|
|
$result = exec($cmd); |
53
|
|
|
static::assertEquals(1, $result, "File is not compressed as gzip"); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
View Code Duplication |
public function testFileGetsDecompressedFromGzip() |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
$file = new LocalFile(static::$dir . 'get_decompressed_uncompressed_gz.test'); |
59
|
|
|
$file->put('random stuff and things!'); |
60
|
|
|
|
61
|
|
|
$compressedFile = $this->gzip->compress($file); |
62
|
|
|
$uncompressedFile = $this->gzip->decompress($compressedFile); |
63
|
|
|
|
64
|
|
|
static::assertNotNull($uncompressedFile); |
65
|
|
|
static::assertInstanceOf(FileNodeInterface::class, $uncompressedFile); |
66
|
|
|
static::assertEquals(static::$dir . 'get_decompressed_uncompressed_gz', $uncompressedFile->getPath()); |
67
|
|
|
static::assertTrue($uncompressedFile->exists()); |
68
|
|
|
static::assertEquals(CompressionFactory::TYPE_NONE, $uncompressedFile->getCompression()); |
69
|
|
|
|
70
|
|
|
$cmd = "file {$uncompressedFile->getPath()} | grep " . escapeshellarg('\bgzip\b') . " | wc -l"; |
71
|
|
|
$result = exec($cmd); |
72
|
|
|
static::assertEquals(0, $result, "File should not be compressed"); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
View Code Duplication |
public function testCallingGzipWithAFileThatDoesNotExistsThrowsAnException() |
|
|
|
|
76
|
|
|
{ |
77
|
|
|
$file = new LocalFile(static::$dir . 'invalid_gzip.test'); |
78
|
|
|
|
79
|
|
|
$this->expectException(InvalidArgumentException::class); |
80
|
|
|
|
81
|
|
|
$this->gzip->compress($file); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
View Code Duplication |
public function testCallingGunzipWithAFileThatDoesNotExistsThrowsAnException() |
|
|
|
|
85
|
|
|
{ |
86
|
|
|
$file = new LocalFile(static::$dir . 'invalid_gunzip.test'); |
87
|
|
|
|
88
|
|
|
$this->expectException(InvalidArgumentException::class); |
89
|
|
|
|
90
|
|
|
$this->gzip->decompress($file); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
View Code Duplication |
public function testWhenTheProcessFailsAnExceptionIsThrownOnGzip() |
|
|
|
|
94
|
|
|
{ |
95
|
|
|
$process = m::mock(Process::class)->makePartial(); |
96
|
|
|
$processFactory = m::mock(ProcessFactory::class); |
97
|
|
|
$processFactory->shouldReceive('createProcess') |
98
|
|
|
->andReturn($process); |
99
|
|
|
$this->gzip->setProcessFactory($processFactory); |
100
|
|
|
$process->shouldReceive('run')->once(); |
101
|
|
|
$process->shouldReceive('isSuccessful')->once()->andReturn(false); |
102
|
|
|
$process->shouldReceive('getCommandLine')->andReturn(''); |
103
|
|
|
$process->shouldReceive('getExitCode')->andReturn(1); |
104
|
|
|
$process->shouldReceive('getExitCodeText')->andReturn('bla'); |
105
|
|
|
$process->shouldReceive('getWorkingDirectory')->andReturn('/something/'); |
106
|
|
|
$process->shouldReceive('isOutputDisabled')->andReturn('true'); |
107
|
|
|
|
108
|
|
|
$file = new LocalFile(static::$dir . 'failed_gzip_process.test'); |
109
|
|
|
|
110
|
|
|
$file->put('random stuff and things 2!'); |
111
|
|
|
|
112
|
|
|
$this->expectException(ProcessFailedException::class); |
113
|
|
|
|
114
|
|
|
$this->gzip->compress($file); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
View Code Duplication |
public function testWhenTheProcessFailsAnExceptionIsThrownOnGunzip() |
|
|
|
|
118
|
|
|
{ |
119
|
|
|
$process = m::mock(Process::class)->makePartial(); |
120
|
|
|
$processFactory = m::mock(ProcessFactory::class); |
121
|
|
|
$processFactory->shouldReceive('createProcess') |
122
|
|
|
->andReturn($process); |
123
|
|
|
$this->gzip->setProcessFactory($processFactory); |
124
|
|
|
$process->shouldReceive('run')->once(); |
125
|
|
|
$process->shouldReceive('isSuccessful')->once()->andReturn(false); |
126
|
|
|
$process->shouldReceive('getCommandLine')->andReturn(''); |
127
|
|
|
$process->shouldReceive('getExitCode')->andReturn(1); |
128
|
|
|
$process->shouldReceive('getExitCodeText')->andReturn('bla'); |
129
|
|
|
$process->shouldReceive('getWorkingDirectory')->andReturn('/something/'); |
130
|
|
|
$process->shouldReceive('isOutputDisabled')->andReturn('true'); |
131
|
|
|
|
132
|
|
|
$file = new LocalFile(static::$dir . 'failed_gunzip_process.test'); |
133
|
|
|
|
134
|
|
|
$file->put('random stuff and things 2!'); |
135
|
|
|
|
136
|
|
|
$this->expectException(ProcessFailedException::class); |
137
|
|
|
|
138
|
|
|
$this->gzip->decompress($file); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
View Code Duplication |
public function testPassingTheKeepOldFileOptionWillKeepTheFile() |
|
|
|
|
142
|
|
|
{ |
143
|
|
|
$file = new LocalFile(static::$dir . 'keep_file_gz.test'); |
144
|
|
|
|
145
|
|
|
$file->put('random stuff and things!'); |
146
|
|
|
|
147
|
|
|
$compressedFile = $this->gzip->compress($file, ['keepOldFile' => true]); |
148
|
|
|
|
149
|
|
|
static::assertTrue($file->exists()); |
150
|
|
|
static::assertTrue($compressedFile->exists()); |
151
|
|
|
|
152
|
|
|
$uncompresssedFile = $this->gzip->decompress($compressedFile, ['keepOldFile' => true]); |
153
|
|
|
|
154
|
|
|
static::assertTrue($compressedFile->exists()); |
155
|
|
|
static::assertTrue($uncompresssedFile->exists()); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
View Code Duplication |
public function testPassingFalseToKeepOldFileOptionWillKeepTheFile() |
|
|
|
|
159
|
|
|
{ |
160
|
|
|
$file = new LocalFile(static::$dir . 'delete_file_gz.test'); |
161
|
|
|
|
162
|
|
|
$file->put('random stuff and things!'); |
163
|
|
|
|
164
|
|
|
$compressedFile = $this->gzip->compress($file, ['keepOldFile' => false]); |
165
|
|
|
|
166
|
|
|
static::assertFalse($file->exists()); |
167
|
|
|
static::assertTrue($compressedFile->exists()); |
168
|
|
|
|
169
|
|
|
$uncompresssedFile = $this->gzip->decompress($compressedFile, ['keepOldFile' => false]); |
170
|
|
|
|
171
|
|
|
static::assertFalse($compressedFile->exists()); |
172
|
|
|
static::assertTrue($uncompresssedFile->exists()); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.