Completed
Push — master ( 1a19ee...d67a52 )
by Harry
8s
created

FindCompressionTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 142
Duplicated Lines 54.23 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 9
c 3
b 1
f 0
lcom 1
cbo 11
dl 77
loc 142
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 15 1
A testGetFileCompressionForNonCompressedFile() 11 11 1
A testGetFileCompressionForGzipFile() 13 13 1
A testGetFileCompressionForZipFile() 13 13 1
A testWhenTheProcessReturnsAnUnknownCompressionUnknownTypeIsReturned() 14 17 1
A testWhenTheProcessFailsAnExceptionIsThrownOnFindCompression() 0 21 1
A testCanModifyCanModifyLocalFiles() 7 7 1
A testUnableToModifyNonLocalFiles() 8 8 1
A testModifyWillSetTheCompression() 11 11 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\FindCompression;
19
use Graze\DataFile\Modify\Compress\Gzip;
20
use Graze\DataFile\Modify\Compress\Zip;
21
use Graze\DataFile\Node\FileNodeInterface;
22
use Graze\DataFile\Node\LocalFile;
23
use Graze\DataFile\Test\FileTestCase;
24
use InvalidArgumentException;
25
use Mockery as m;
26
use Mockery\MockInterface;
27
use Symfony\Component\Process\Exception\ProcessFailedException;
28
use Symfony\Component\Process\Process;
29
30
class FindCompressionTest extends FileTestCase
31
{
32
    /**
33
     * @var FindCompression
34
     */
35
    protected $findCompression;
36
37
    /**
38
     * @var ProcessFactory|MockInterface
39
     */
40
    protected $processFactory;
41
42
    /**
43
     * @var CompressionFactory|MockInterface
44
     */
45
    protected $compressionFactory;
46
47
    public function setUp()
48
    {
49
        $this->processFactory = m::mock(ProcessFactory::class)->makePartial();
50
        $this->compressionFactory = m::mock(CompressionFactory::class);
51
        $this->compressionFactory->shouldReceive('isCompression')
52
                                 ->with('gzip')
53
                                 ->andReturn(true);
54
        $this->compressionFactory->shouldReceive('isCompression')
55
                                 ->with('zip')
56
                                 ->andReturn(true);
57
        $this->compressionFactory->shouldReceive('isCompression')
58
                                 ->andReturn(false);
59
        $this->findCompression = new FindCompression($this->compressionFactory);
60
        $this->findCompression->setProcessFactory($this->processFactory);
61
    }
62
63 View Code Duplication
    public function testGetFileCompressionForNonCompressedFile()
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...
64
    {
65
        $file = new LocalFile(static::$dir . 'uncompressed_file.test');
66
        $file->put('some random text');
67
68
        static::assertEquals(
69
            $file->getCompression(),
70
            $this->findCompression->getCompression($file)
71
        );
72
        static::assertEquals(CompressionFactory::TYPE_NONE, $file->getCompression());
73
    }
74
75 View Code Duplication
    public function testGetFileCompressionForGzipFile()
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...
76
    {
77
        $file = new LocalFile(static::$dir . 'tobegzipped_file.test');
78
        $file->put('some random text');
79
        $gzip = new Gzip();
80
        $gzipFile = $gzip->compress($file);
81
82
        static::assertEquals(
83
            $gzipFile->getCompression(),
84
            $this->findCompression->getCompression($gzipFile)
85
        );
86
        static::assertEquals(Gzip::NAME, $gzipFile->getCompression());
87
    }
88
89 View Code Duplication
    public function testGetFileCompressionForZipFile()
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...
90
    {
91
        $file = new LocalFile(static::$dir . 'tobezipped.test');
92
        $file->put('some random text');
93
        $zip = new Zip();
94
        $zipFile = $zip->compress($file);
95
96
        static::assertEquals(
97
            $zipFile->getCompression(),
98
            $this->findCompression->getCompression($zipFile)
99
        );
100
        static::assertEquals(Zip::NAME, $zipFile->getCompression());
101
    }
102
103 View Code Duplication
    public function testWhenTheProcessReturnsAnUnknownCompressionUnknownTypeIsReturned()
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
        $process->shouldReceive('mustRun');
107
        $process->shouldReceive('getOutput')
108
                ->andReturn(
109
                    'text/plain; charset=utf-8 compressed-encoding=application/lzop; charset=binary; charset=binary'
110
                );
111
112
        $this->processFactory->shouldReceive('createProcess')
113
                             ->andReturn($process);
114
115
        $file = new LocalFile(static::$dir . 'unknown_compression.test');
116
        $file->put('random stuff and things 2!');
117
118
        static::assertEquals(CompressionFactory::TYPE_UNKNOWN, $this->findCompression->getCompression($file));
119
    }
120
121
    public function testWhenTheProcessFailsAnExceptionIsThrownOnFindCompression()
122
    {
123
        $process = m::mock(Process::class)->makePartial();
124
        $process->shouldReceive('isSuccessful')->andReturn(false);
125
        $process->shouldReceive('getCommandLine')->andReturn('cmd');
126
        $process->shouldReceive('getExitCode')->andReturn(1);
127
        $process->shouldReceive('getExitCodeText')->andReturn('failed');
128
        $process->shouldReceive('getWorkingDirectory')->andReturn('bla');
129
        $process->shouldReceive('isOutputDisabled')->andReturn(true);
130
        $process->shouldReceive('mustRun')->andThrow(new ProcessFailedException($process));
131
132
        $this->processFactory->shouldReceive('createProcess')
133
                             ->andReturn($process);
134
135
        $file = new LocalFile(static::$dir . 'failed_find_encoding_process.test');
136
        $file->put('random stuff and things 2!');
137
138
        $this->expectException(ProcessFailedException::class);
139
140
        $this->findCompression->getCompression($file);
141
    }
142
143 View Code Duplication
    public function testCanModifyCanModifyLocalFiles()
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...
144
    {
145
        $file = new LocalFile(static::$dir . 'uncompressed_file.test');
146
        $file->put('some random text');
147
148
        static::assertTrue($this->findCompression->canModify($file));
149
    }
150
151 View Code Duplication
    public function testUnableToModifyNonLocalFiles()
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 = m::mock(FileNodeInterface::class);
154
        static::assertFalse($this->findCompression->canModify($file));
155
156
        $this->expectException(InvalidArgumentException::class);
157
        $this->findCompression->modify($file);
158
    }
159
160 View Code Duplication
    public function testModifyWillSetTheCompression()
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...
161
    {
162
        $file = new LocalFile(static::$dir . 'tobegzipped_file.test');
163
        $file->put('some random text');
164
        $gzip = new Gzip();
165
        $gzipFile = $gzip->compress($file);
166
        $gzipFile->setCompression(CompressionFactory::TYPE_NONE);
167
168
        $gzipFile = $this->findCompression->modify($gzipFile);
169
        static::assertEquals(Gzip::NAME, $gzipFile->getCompression());
170
    }
171
}
172