Completed
Push — master ( d87b02...4573bb )
by Harry
04:40
created

ZipTest::testCallingCompressWithANonLocalFileWillThrowAnException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 9
rs 9.6666
cc 1
eloc 5
nc 1
nop 0
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\Zip;
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 Mockery\MockInterface;
17
use Symfony\Component\Process\Exception\ProcessFailedException;
18
use Symfony\Component\Process\Process;
19
20
class ZipTest extends FileTestCase
21
{
22
    /**
23
     * @var Zip
24
     */
25
    protected $zip;
26
27
    /**
28
     * @var ProcessFactory|MockInterface
29
     */
30
    private $processFactory;
31
32
    public function setUp()
33
    {
34
        $this->processFactory = m::mock(ProcessFactory::class)->makePartial();
35
        $this->zip = new Zip();
36
        $this->zip->setProcessFactory($this->processFactory);
1 ignored issue
show
Documentation introduced by
$this->processFactory is of type object<Mockery\Mock>, but the function expects a object<Graze\DataFile\He...Process\ProcessFactory>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
37
    }
38
39
    public function testInstanceOfCompressorInterface()
40
    {
41
        static::assertInstanceOf(CompressorInterface::class, $this->zip);
42
        static::assertInstanceOf(DeCompressorInterface::class, $this->zip);
43
    }
44
45
    public function testFileGetsCompressedAsZip()
46
    {
47
        $file = new LocalFile(static::$dir . 'uncompressed_zip.test');
48
49
        $file->put('random stuff and things!');
50
51
        $compressedFile = $this->zip->compress($file);
52
53
        static::assertNotNull($compressedFile);
54
        static::assertInstanceOf(FileNodeInterface::class, $compressedFile);
55
        static::assertEquals(static::$dir . 'uncompressed_zip.zip', $compressedFile->getPath());
56
        static::assertTrue($compressedFile->exists());
57
        static::assertEquals(Zip::NAME, $compressedFile->getCompression());
58
59
        $cmd = "file {$compressedFile->getPath()} | grep " . escapeshellarg('\bzip\b') . " | wc -l";
60
        $result = exec($cmd);
61
        static::assertEquals(1, $result, "File is not compressed as zip");
62
    }
63
64 View Code Duplication
    public function testFileGetsDecompressedFromZip()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
65
    {
66
        $file = new LocalFile(static::$dir . 'get_decompressed_uncompressed_zip.test');
67
        $file->put('random stuff and things!');
68
69
        $compressedFile = $this->zip->compress($file);
70
71
        static::assertTrue($compressedFile->exists());
72
        $uncompressedFile = $this->zip->decompress($compressedFile);
73
74
        static::assertNotNull($uncompressedFile);
75
        static::assertInstanceOf(FileNodeInterface::class, $uncompressedFile);
76
        static::assertEquals(static::$dir . 'get_decompressed_uncompressed_zip', $uncompressedFile->getPath());
77
        static::assertTrue($uncompressedFile->exists());
78
        static::assertEquals(CompressionFactory::TYPE_NONE, $uncompressedFile->getCompression());
79
80
        $cmd = "file {$uncompressedFile->getPath()} | grep " . escapeshellarg('\bzip\b') . " | wc -l";
81
        $result = exec($cmd);
82
        static::assertEquals(0, $result, "File should not be compressed");
83
    }
84
85 View Code Duplication
    public function testCallingZipWithAFileThatDoesNotExistsThrowsAnException()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
86
    {
87
        $file = new LocalFile(static::$dir . 'invalid_zip.test');
88
89
        $this->expectException(InvalidArgumentException::class);
90
91
        $this->zip->compress($file);
92
    }
93
94 View Code Duplication
    public function testCallingUnzipWithAFileThatDoesNotExistsThrowsAnException()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
95
    {
96
        $file = new LocalFile(static::$dir . 'invalid_zip.zip');
97
98
        $this->expectException(InvalidArgumentException::class);
99
100
        $this->zip->decompress($file);
101
    }
102
103 View Code Duplication
    public function testWhenTheProcessFailsAnExceptionIsthrownOnZip()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
104
    {
105
        $process = m::mock(Process::class)->makePartial();
106
        $this->processFactory->shouldReceive('createProcess')
107
                             ->andReturn($process);
108
        $process->shouldReceive('run')->once();
109
        $process->shouldReceive('isSuccessful')->once()->andReturn(false);
110
        $process->shouldReceive('getCommandLine')->andReturn('');
111
        $process->shouldReceive('getExitCode')->andReturn(1);
112
        $process->shouldReceive('getExitCodeText')->andReturn('bla');
113
        $process->shouldReceive('getWorkingDirectory')->andReturn('/something/');
114
        $process->shouldReceive('isOutputDisabled')->andReturn('true');
115
116
        $file = new LocalFile(static::$dir . 'failed_zip_process.test');
117
118
        $file->put('random stuff and things 2!');
119
120
        $this->expectException(ProcessFailedException::class);
121
122
        $this->zip->compress($file);
123
    }
124
125 View Code Duplication
    public function testWhenTheProcessFailsAnExceptionIsthrownOnUnzip()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
126
    {
127
        $process = m::mock(Process::class)->makePartial();
128
        $this->processFactory->shouldReceive('createProcess')
129
                             ->andReturn($process);
130
        $process->shouldReceive('run')->once();
131
        $process->shouldReceive('isSuccessful')->once()->andReturn(false);
132
        $process->shouldReceive('getCommandLine')->andReturn('');
133
        $process->shouldReceive('getExitCode')->andReturn(1);
134
        $process->shouldReceive('getExitCodeText')->andReturn('bla');
135
        $process->shouldReceive('getWorkingDirectory')->andReturn('/something/');
136
        $process->shouldReceive('isOutputDisabled')->andReturn('true');
137
138
        $file = new LocalFile(static::$dir . 'failed_unzip_process.test');
139
140
        $file->put('random stuff and things 2!');
141
142
        $this->expectException(ProcessFailedException::class);
143
144
        $this->zip->decompress($file);
145
    }
146
147 View Code Duplication
    public function testPassingTheKeepOldFileOptionWillKeepTheFile()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
148
    {
149
        $file = new LocalFile(static::$dir . 'keep_file_zip.test');
150
151
        $file->put('random stuff and things!');
152
153
        $compressedFile = $this->zip->compress($file, ['keepOldFile' => true]);
154
155
        static::assertTrue($file->exists());
156
        static::assertTrue($compressedFile->exists());
157
158
        $uncompresssedFile = $this->zip->decompress($compressedFile, ['keepOldFile' => true]);
159
160
        static::assertTrue($compressedFile->exists());
161
        static::assertTrue($uncompresssedFile->exists());
162
    }
163
164 View Code Duplication
    public function testPassingFalseToKeepOldFileOptionWillKeepTheFile()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
165
    {
166
        $file = new LocalFile(static::$dir . 'delete_file_zip.test');
167
168
        $file->put('random stuff and things!');
169
170
        $compressedFile = $this->zip->compress($file, ['keepOldFile' => false]);
171
172
        static::assertFalse($file->exists());
173
        static::assertTrue($compressedFile->exists());
174
175
        $uncompresssedFile = $this->zip->decompress($compressedFile, ['keepOldFile' => false]);
176
177
        static::assertFalse($compressedFile->exists());
178
        static::assertTrue($uncompresssedFile->exists());
179
    }
180
}
181