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

testWhenTheProcessFailsAnExceptionIsThrownOnFindCompression()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 21
rs 9.3142
cc 1
eloc 15
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
        $process = m::mock(Process::class)->makePartial();
113
        $process->shouldReceive('isSuccessful')->andReturn(false);
114
        $process->shouldReceive('getCommandLine')->andReturn('cmd');
115
        $process->shouldReceive('getExitCode')->andReturn(1);
116
        $process->shouldReceive('getExitCodeText')->andReturn('failed');
117
        $process->shouldReceive('getWorkingDirectory')->andReturn('bla');
118
        $process->shouldReceive('isOutputDisabled')->andReturn(true);
119
        $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...
120
121
        $this->processFactory->shouldReceive('createProcess')
122
                             ->andReturn($process);
123
124
        $file = new LocalFile(static::$dir . 'failed_find_encoding_process.test');
125
        $file->put('random stuff and things 2!');
126
127
        $this->expectException(ProcessFailedException::class);
128
129
        $this->findCompression->getCompression($file);
130
    }
131
132 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...
133
    {
134
        $file = new LocalFile(static::$dir . 'uncompressed_file.test');
135
        $file->put('some random text');
136
137
        static::assertTrue($this->findCompression->canModify($file));
138
    }
139
140 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...
141
    {
142
        $file = m::mock(FileNodeInterface::class);
143
        static::assertFalse($this->findCompression->canModify($file));
144
145
        $this->expectException(InvalidArgumentException::class);
146
        $this->findCompression->modify($file);
147
    }
148
149 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...
150
    {
151
        $file = new LocalFile(static::$dir . 'tobegzipped_file.test');
152
        $file->put('some random text');
153
        $gzip = new Gzip();
154
        $gzipFile = $gzip->compress($file);
155
        $gzipFile->setCompression(CompressionFactory::TYPE_NONE);
156
157
        $gzipFile = $this->findCompression->modify($gzipFile);
158
        static::assertEquals(Gzip::NAME, $gzipFile->getCompression());
159
    }
160
}
161