Completed
Pull Request — master (#3)
by Harry
05:31
created

GzipTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 156
Duplicated Lines 71.79 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 10
c 3
b 0
f 0
lcom 1
cbo 9
dl 112
loc 156
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A testInstanceOfCompressorInterface() 0 5 1
A testFileGetsCompressedAsGzip() 0 18 1
A testFileGetsDecompressedFromGzip() 18 18 1
A testCallingGzipWithAFileThatDoesNotExistsThrowsAnException() 8 8 1
A testCallingGunzipWithAFileThatDoesNotExistsThrowsAnException() 8 8 1
A testWhenTheProcessFailsAnExceptionIsThrownOnGzip() 23 23 1
A testWhenTheProcessFailsAnExceptionIsThrownOnGunzip() 23 23 1
A testPassingTheKeepOldFileOptionWillKeepTheFile() 16 16 1
A testPassingFalseToKeepOldFileOptionWillKeepTheFile() 16 16 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\Process\ProcessFactory;
17
use Graze\DataFile\Modify\Compress\CompressionFactory;
18
use Graze\DataFile\Modify\Compress\CompressorInterface;
19
use Graze\DataFile\Modify\Compress\DeCompressorInterface;
20
use Graze\DataFile\Modify\Compress\Gzip;
21
use Graze\DataFile\Node\FileNodeInterface;
22
use Graze\DataFile\Node\LocalFile;
23
use Graze\DataFile\Test\AbstractFileTestCase;
24
use InvalidArgumentException;
25
use Mockery as m;
26
use Symfony\Component\Process\Exception\ProcessFailedException;
27
use Symfony\Component\Process\Process;
28
29
class GzipTest extends AbstractFileTestCase
30
{
31
    /**
32
     * @var Gzip
33
     */
34
    protected $gzip;
35
36
    public function setUp()
37
    {
38
        $this->gzip = new Gzip();
39
    }
40
41
    public function testInstanceOfCompressorInterface()
42
    {
43
        static::assertInstanceOf(CompressorInterface::class, $this->gzip);
44
        static::assertInstanceOf(DeCompressorInterface::class, $this->gzip);
45
    }
46
47
    public function testFileGetsCompressedAsGzip()
48
    {
49
        $file = new LocalFile(static::$dir . 'uncompressed_gz.test');
50
51
        $file->put('random stuff and things!');
52
53
        $compressedFile = $this->gzip->compress($file);
54
55
        static::assertNotNull($compressedFile);
56
        static::assertInstanceOf(FileNodeInterface::class, $compressedFile);
57
        static::assertEquals(static::$dir . 'uncompressed_gz.gz', $compressedFile->getPath());
58
        static::assertTrue($compressedFile->exists());
59
        static::assertEquals(Gzip::NAME, $compressedFile->getCompression());
60
61
        $cmd = "file {$compressedFile->getPath()} | grep " . escapeshellarg('\bgzip\b') . " | wc -l";
62
        $result = exec($cmd);
63
        static::assertEquals(1, $result, "File is not compressed as gzip");
64
    }
65
66 View Code Duplication
    public function testFileGetsDecompressedFromGzip()
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...
67
    {
68
        $file = new LocalFile(static::$dir . 'get_decompressed_uncompressed_gz.test');
69
        $file->put('random stuff and things!');
70
71
        $compressedFile = $this->gzip->compress($file);
72
        $uncompressedFile = $this->gzip->decompress($compressedFile);
73
74
        static::assertNotNull($uncompressedFile);
75
        static::assertInstanceOf(FileNodeInterface::class, $uncompressedFile);
76
        static::assertEquals(static::$dir . 'get_decompressed_uncompressed_gz', $uncompressedFile->getPath());
77
        static::assertTrue($uncompressedFile->exists());
78
        static::assertEquals(CompressionFactory::TYPE_NONE, $uncompressedFile->getCompression());
79
80
        $cmd = "file {$uncompressedFile->getPath()} | grep " . escapeshellarg('\bgzip\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 testCallingGzipWithAFileThatDoesNotExistsThrowsAnException()
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_gzip.test');
88
89
        $this->expectException(InvalidArgumentException::class);
90
91
        $this->gzip->compress($file);
92
    }
93
94 View Code Duplication
    public function testCallingGunzipWithAFileThatDoesNotExistsThrowsAnException()
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_gunzip.test');
97
98
        $this->expectException(InvalidArgumentException::class);
99
100
        $this->gzip->decompress($file);
101
    }
102
103 View Code Duplication
    public function testWhenTheProcessFailsAnExceptionIsThrownOnGzip()
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
        $processFactory = m::mock(ProcessFactory::class);
107
        $processFactory->shouldReceive('createProcess')
108
                       ->andReturn($process);
109
        $this->gzip->setProcessFactory($processFactory);
110
        $process->shouldReceive('run')->once();
111
        $process->shouldReceive('isSuccessful')->andReturn(false);
112
        $process->shouldReceive('getCommandLine')->andReturn('');
113
        $process->shouldReceive('getExitCode')->andReturn(1);
114
        $process->shouldReceive('getExitCodeText')->andReturn('bla');
115
        $process->shouldReceive('getWorkingDirectory')->andReturn('/something/');
116
        $process->shouldReceive('isOutputDisabled')->andReturn('true');
117
118
        $file = new LocalFile(static::$dir . 'failed_gzip_process.test');
119
120
        $file->put('random stuff and things 2!');
121
122
        $this->expectException(ProcessFailedException::class);
123
124
        $this->gzip->compress($file);
125
    }
126
127 View Code Duplication
    public function testWhenTheProcessFailsAnExceptionIsThrownOnGunzip()
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...
128
    {
129
        $process = m::mock(Process::class)->makePartial();
130
        $processFactory = m::mock(ProcessFactory::class);
131
        $processFactory->shouldReceive('createProcess')
132
                       ->andReturn($process);
133
        $this->gzip->setProcessFactory($processFactory);
134
        $process->shouldReceive('run')->once();
135
        $process->shouldReceive('isSuccessful')->andReturn(false);
136
        $process->shouldReceive('getCommandLine')->andReturn('');
137
        $process->shouldReceive('getExitCode')->andReturn(1);
138
        $process->shouldReceive('getExitCodeText')->andReturn('bla');
139
        $process->shouldReceive('getWorkingDirectory')->andReturn('/something/');
140
        $process->shouldReceive('isOutputDisabled')->andReturn('true');
141
142
        $file = new LocalFile(static::$dir . 'failed_gunzip_process.test');
143
144
        $file->put('random stuff and things 2!');
145
146
        $this->expectException(ProcessFailedException::class);
147
148
        $this->gzip->decompress($file);
149
    }
150
151 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...
152
    {
153
        $file = new LocalFile(static::$dir . 'keep_file_gz.test');
154
155
        $file->put('random stuff and things!');
156
157
        $compressedFile = $this->gzip->compress($file, ['keepOldFile' => true]);
158
159
        static::assertTrue($file->exists());
160
        static::assertTrue($compressedFile->exists());
161
162
        $uncompresssedFile = $this->gzip->decompress($compressedFile, ['keepOldFile' => true]);
163
164
        static::assertTrue($compressedFile->exists());
165
        static::assertTrue($uncompresssedFile->exists());
166
    }
167
168 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...
169
    {
170
        $file = new LocalFile(static::$dir . 'delete_file_gz.test');
171
172
        $file->put('random stuff and things!');
173
174
        $compressedFile = $this->gzip->compress($file, ['keepOldFile' => false]);
175
176
        static::assertFalse($file->exists());
177
        static::assertTrue($compressedFile->exists());
178
179
        $uncompresssedFile = $this->gzip->decompress($compressedFile, ['keepOldFile' => false]);
180
181
        static::assertFalse($compressedFile->exists());
182
        static::assertTrue($uncompresssedFile->exists());
183
    }
184
}
185