Completed
Pull Request — master (#1)
by Harry
03:50
created

testGetFileCompressionForNonCompressedFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 11
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 11
loc 11
rs 9.4285
cc 1
eloc 7
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\FindCompression;
8
use Graze\DataFile\Modify\Compress\Gzip;
9
use Graze\DataFile\Modify\Compress\Zip;
10
use Graze\DataFile\Node\FileNodeInterface;
11
use Graze\DataFile\Node\LocalFile;
12
use Graze\DataFile\Test\FileTestCase;
13
use InvalidArgumentException;
14
use Mockery as m;
15
use Mockery\MockInterface;
16
use Symfony\Component\Process\Exception\ProcessFailedException;
17
use Symfony\Component\Process\Process;
18
19
class FindCompressionTest extends FileTestCase
20
{
21
    /**
22
     * @var FindCompression
23
     */
24
    protected $findCompression;
25
26
    /**
27
     * @var ProcessFactory|MockInterface
28
     */
29
    protected $processFactory;
30
31
    /**
32
     * @var CompressionFactory|MockInterface
33
     */
34
    protected $compressionFactory;
35
36
    public function setUp()
37
    {
38
        $this->processFactory = m::mock(ProcessFactory::class)->makePartial();
39
        $this->compressionFactory = m::mock(CompressionFactory::class);
40
        $this->compressionFactory->shouldReceive('isCompression')
41
                                 ->with('gzip')
42
                                 ->andReturn(true);
43
        $this->compressionFactory->shouldReceive('isCompression')
44
                                 ->with('zip')
45
                                 ->andReturn(true);
46
        $this->compressionFactory->shouldReceive('isCompression')
47
                                 ->andReturn(false);
48
        $this->findCompression = new FindCompression($this->compressionFactory);
49
        $this->findCompression->setProcessFactory($this->processFactory);
50
    }
51
52 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...
53
    {
54
        $file = new LocalFile(static::$dir . 'uncompressed_file.test');
55
        $file->put('some random text');
56
57
        static::assertEquals(
58
            $file->getCompression(),
59
            $this->findCompression->getCompression($file)
60
        );
61
        static::assertEquals(CompressionFactory::TYPE_NONE, $file->getCompression());
62
    }
63
64 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...
65
    {
66
        $file = new LocalFile(static::$dir . 'tobegzipped_file.test');
67
        $file->put('some random text');
68
        $gzip = new Gzip();
69
        $gzipFile = $gzip->compress($file);
70
71
        static::assertEquals(
72
            $gzipFile->getCompression(),
73
            $this->findCompression->getCompression($gzipFile)
74
        );
75
        static::assertEquals(Gzip::NAME, $gzipFile->getCompression());
76
    }
77
78 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...
79
    {
80
        $file = new LocalFile(static::$dir . 'tobezipped.test');
81
        $file->put('some random text');
82
        $zip = new Zip();
83
        $zipFile = $zip->compress($file);
84
85
        static::assertEquals(
86
            $zipFile->getCompression(),
87
            $this->findCompression->getCompression($zipFile)
88
        );
89
        static::assertEquals(Zip::NAME, $zipFile->getCompression());
90
    }
91
92 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...
93
    {
94
        $process = m::mock(Process::class)->makePartial();
95
        $process->shouldReceive('mustRun');
96
        $process->shouldReceive('getOutput')
97
                ->andReturn(
98
                    'text/plain; charset=utf-8 compressed-encoding=application/lzop; charset=binary; charset=binary'
99
                );
100
101
        $this->processFactory->shouldReceive('createProcess')
102
                             ->andReturn($process);
103
104
        $file = new LocalFile(static::$dir . 'unknown_compression.test');
105
        $file->put('random stuff and things 2!');
106
107
        static::assertEquals(CompressionFactory::TYPE_UNKNOWN, $this->findCompression->getCompression($file));
108
    }
109
110
    public function testWhenTheProcessFailsAnExceptionIsThrownOnFindCompression()
111
    {
112
        /** @var Process|MockInterface $process */
113
        $process = m::mock(Process::class)->makePartial();
114
        $process->shouldReceive('isSuccessful')->andReturn(false);
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in Symfony\Component\Process\Process.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
115
        $process->shouldReceive('getCommandLine')->andReturn('cmd');
116
        $process->shouldReceive('getExitCode')->andReturn(1);
117
        $process->shouldReceive('getExitCodeText')->andReturn('failed');
118
        $process->shouldReceive('getWorkingDirectory')->andReturn('bla');
119
        $process->shouldReceive('isOutputDisabled')->andReturn(true);
120
        $process->shouldReceive('mustRun')->andThrow(new ProcessFailedException($process));
0 ignored issues
show
Bug introduced by
It seems like $process defined by \Mockery::mock(\Symfony\...::class)->makePartial() on line 113 can also be of type object<Mockery\MockInterface>; however, Symfony\Component\Proces...xception::__construct() does only seem to accept object<Symfony\Component\Process\Process>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
121
122
        $this->processFactory->shouldReceive('createProcess')
123
                             ->andReturn($process);
124
125
        $file = new LocalFile(static::$dir . 'failed_find_encoding_process.test');
126
        $file->put('random stuff and things 2!');
127
128
        $this->expectException(ProcessFailedException::class);
129
130
        $this->findCompression->getCompression($file);
131
    }
132
133 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...
134
    {
135
        $file = new LocalFile(static::$dir . 'uncompressed_file.test');
136
        $file->put('some random text');
137
138
        static::assertTrue($this->findCompression->canModify($file));
139
    }
140
141 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...
142
    {
143
        $file = m::mock(FileNodeInterface::class);
144
        static::assertFalse($this->findCompression->canModify($file));
145
146
        $this->expectException(InvalidArgumentException::class);
147
        $this->findCompression->modify($file);
148
    }
149
150 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...
151
    {
152
        $file = new LocalFile(static::$dir . 'tobegzipped_file.test');
153
        $file->put('some random text');
154
        $gzip = new Gzip();
155
        $gzipFile = $gzip->compress($file);
156
        $gzipFile->setCompression(CompressionFactory::TYPE_NONE);
157
158
        $gzipFile = $this->findCompression->modify($gzipFile);
159
        static::assertEquals(Gzip::NAME, $gzipFile->getCompression());
160
    }
161
}
162