testGetFileCompressionForZipFile()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 13
Ratio 100 %

Importance

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

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
                      ->andReturn($process);
116
117
        $file = new LocalFile(static::$dir . 'unknown_compression.test');
118
        $file->put('random stuff and things 2!');
119
120
        static::assertEquals(CompressionFactory::TYPE_UNKNOWN, $this->findCompression->getCompression($file));
121
    }
122
123 View Code Duplication
    public function testWhenTheProcessFailsAnExceptionIsThrownOnFindCompression()
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...
124
    {
125
        $process = m::mock(Process::class)->makePartial();
126
        $process->shouldReceive('isSuccessful')->andReturn(false);
127
        $process->shouldReceive('getCommandLine')->andReturn('cmd');
128
        $process->shouldReceive('getExitCode')->andReturn(1);
129
        $process->shouldReceive('getExitCodeText')->andReturn('failed');
130
        $process->shouldReceive('getWorkingDirectory')->andReturn('bla');
131
        $process->shouldReceive('isOutputDisabled')->andReturn(true);
132
        $process->shouldReceive('mustRun')->andThrow(new ProcessFailedException($process));
133
134
        $this->builder->shouldReceive('build')
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in Graze\DataFile\Helper\Builder\BuilderInterface.

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...
135
                      ->andReturn($process);
136
137
        $file = new LocalFile(static::$dir . 'failed_find_encoding_process.test');
138
        $file->put('random stuff and things 2!');
139
140
        $this->expectException(ProcessFailedException::class);
141
142
        $this->findCompression->getCompression($file);
143
    }
144
145 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...
146
    {
147
        $file = new LocalFile(static::$dir . 'uncompressed_file.test');
148
        $file->put('some random text');
149
150
        static::assertTrue($this->findCompression->canModify($file));
151
    }
152
153 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...
154
    {
155
        $file = m::mock(FileNodeInterface::class);
156
        static::assertFalse($this->findCompression->canModify($file));
157
158
        $this->expectException(InvalidArgumentException::class);
159
        $this->findCompression->modify($file);
160
    }
161
162 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...
163
    {
164
        $file = new LocalFile(static::$dir . 'tobegzipped_file.test');
165
        $file->put('some random text');
166
        $gzip = new Gzip();
167
        $gzipFile = $gzip->compress($file);
168
        $gzipFile->setCompression(CompressionFactory::TYPE_NONE);
169
170
        $gzipFile = $this->findCompression->modify($gzipFile);
171
        static::assertEquals(Gzip::NAME, $gzipFile->getCompression());
172
    }
173
}
174