Completed
Pull Request — master (#1)
by Harry
07:10
created

testUnableToModifyNonLocalFiles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 8
loc 8
rs 9.4285
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\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')->andReturn('text/plain; charset=utf-8 compressed-encoding=application/lzop; charset=binary; charset=binary');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 154 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
97
98
        $this->processFactory->shouldReceive('createProcess')
99
                             ->andReturn($process);
100
101
        $file = new LocalFile(static::$dir . 'unknown_compression.test');
102
        $file->put('random stuff and things 2!');
103
104
        static::assertEquals(CompressionFactory::TYPE_UNKNOWN, $this->findCompression->getCompression($file));
105
    }
106
107
    public function testWhenTheProcessFailsAnExceptionIsThrownOnFindCompression()
108
    {
109
        $process = m::mock(Process::class)->makePartial();
110
        $process->shouldReceive('isSuccessful')->andReturn(false);
111
        $process->shouldReceive('getCommandLine')->andReturn('cmd');
112
        $process->shouldReceive('getExitCode')->andReturn(1);
113
        $process->shouldReceive('getExitCodeText')->andReturn('failed');
114
        $process->shouldReceive('getWorkingDirectory')->andReturn('bla');
115
        $process->shouldReceive('isOutputDisabled')->andReturn(true);
116
        $process->shouldReceive('mustRun')->andThrow(new ProcessFailedException($process));
1 ignored issue
show
Documentation introduced by
$process is of type object<Mockery\Mock>, but the function expects a object<Symfony\Component\Process\Process>.

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