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

FindEncodingTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 123
Duplicated Lines 41.46 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 51
loc 123
wmc 9
lcom 1
cbo 11
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 6 1
A testGetFileEncodingForASCIIFile() 11 11 1
A testGetFileEncodingForUtf8File() 11 11 1
A testGetFileEncodingForCompressedFile() 0 16 1
A testWhenTheProcessFailsAnExceptionIsThrownOnFindEncoding() 0 21 1
A testWhenTheProcessReturnsAnUnknownFileNullIsReturned() 14 14 1
A testCanModifyCanModifyLocalFiles() 7 7 1
A testUnableToModifyNonLocalFiles() 8 8 1
A testModifyWillSetTheEncoding() 0 8 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
namespace Graze\DataFile\Test\Integration\Modify\Encoding;
4
5
use Graze\DataFile\Helper\Process\ProcessFactory;
6
use Graze\DataFile\Modify\Compress\Gzip;
7
use Graze\DataFile\Modify\Encoding\FindEncoding;
8
use Graze\DataFile\Node\FileNodeInterface;
9
use Graze\DataFile\Node\LocalFile;
10
use Graze\DataFile\Test\FileTestCase;
11
use InvalidArgumentException;
12
use Mockery as m;
13
use Symfony\Component\Process\Exception\ProcessFailedException;
14
use Symfony\Component\Process\Process;
15
16
class FindEncodingTest extends FileTestCase
17
{
18
    /**
19
     * @var FindEncoding
20
     */
21
    protected $findEncoding;
22
23
    /**
24
     * @var ProcessFactory|m\MockInterface
25
     */
26
    protected $processFactory;
27
28
    public function setUp()
29
    {
30
        $this->processFactory = m::mock(ProcessFactory::class)->makePartial();
31
        $this->findEncoding = new FindEncoding();
32
        $this->findEncoding->setProcessFactory($this->processFactory);
1 ignored issue
show
Documentation introduced by
$this->processFactory is of type object<Mockery\Mock>, but the function expects a object<Graze\DataFile\He...Process\ProcessFactory>.

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...
33
    }
34
35 View Code Duplication
    public function testGetFileEncodingForASCIIFile()
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...
36
    {
37
        $asciiFile = (new LocalFile(static::$dir . 'ascii_file.test'))
38
            ->setEncoding('us-ascii');
39
        $asciiFile->put(mb_convert_encoding('Some random Text', 'ASCII'));
40
41
        static::assertEquals(
42
            strtolower($asciiFile->getEncoding()),
43
            strtolower($this->findEncoding->getEncoding($asciiFile))
44
        );
45
    }
46
47 View Code Duplication
    public function testGetFileEncodingForUtf8File()
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...
48
    {
49
        $utf8file = (new LocalFile(static::$dir . 'utf8_file.test'))
50
            ->setEncoding('UTF-8');
51
        $utf8file->put(mb_convert_encoding('Some random Text €±§', 'UTF-8'));
52
53
        static::assertEquals(
54
            strtolower($utf8file->getEncoding()),
55
            strtolower($this->findEncoding->getEncoding($utf8file))
56
        );
57
    }
58
59
    public function testGetFileEncodingForCompressedFile()
60
    {
61
        $utf8file = (new LocalFile(static::$dir . 'utf8_tobegzipped_file.test'))
62
            ->setEncoding('UTF-8');
63
        $utf8file->put(mb_convert_encoding('Some random Text €±§', 'UTF-8'));
64
        $gzip = new Gzip();
65
        $gzipFile = $gzip->compress($utf8file);
66
67
        static::assertEquals(
68
            strtolower($gzipFile->getEncoding()),
69
            strtolower($this->findEncoding->getEncoding($gzipFile))
70
        );
71
72
        static::assertEquals('utf-8', $this->findEncoding->getEncoding($gzipFile));
73
        static::assertEquals($utf8file->getEncoding(), $gzipFile->getEncoding());
74
    }
75
76
    public function testWhenTheProcessFailsAnExceptionIsThrownOnFindEncoding()
77
    {
78
        $process = m::mock(Process::class)->makePartial();
79
        $process->shouldReceive('isSuccessful')->andReturn(false);
80
        $process->shouldReceive('getCommandLine')->andReturn('cmd');
81
        $process->shouldReceive('getExitCode')->andReturn(1);
82
        $process->shouldReceive('getExitCodeText')->andReturn('failed');
83
        $process->shouldReceive('getWorkingDirectory')->andReturn('bla');
84
        $process->shouldReceive('isOutputDisabled')->andReturn(true);
85
        $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...
86
87
        $this->processFactory->shouldReceive('createProcess')
88
                             ->andReturn($process);
89
90
        $file = new LocalFile(static::$dir . 'failed_find_encoding_process.test');
91
        $file->put('random stuff and things 2!');
92
93
        $this->expectException(ProcessFailedException::class);
94
95
        $this->findEncoding->getEncoding($file);
96
    }
97
98 View Code Duplication
    public function testWhenTheProcessReturnsAnUnknownFileNullIsReturned()
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...
99
    {
100
        $process = m::mock(Process::class)->makePartial();
101
        $process->shouldReceive('mustRun');
102
        $process->shouldReceive('getOutput')->andReturn('some random stuff with no charset');
103
104
        $this->processFactory->shouldReceive('createProcess')
105
                             ->andReturn($process);
106
107
        $file = new LocalFile(static::$dir . 'unknown_compression.test');
108
        $file->put('random stuff and things 2!');
109
110
        static::assertNull($this->findEncoding->getEncoding($file));
111
    }
112
113 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...
114
    {
115
        $file = new LocalFile(static::$dir . 'uncompressed_file.test');
116
        $file->put('some random text');
117
118
        static::assertTrue($this->findEncoding->canModify($file));
119
    }
120
121 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...
122
    {
123
        $file = m::mock(FileNodeInterface::class);
124
        static::assertFalse($this->findEncoding->canModify($file));
125
126
        $this->expectException(InvalidArgumentException::class);
127
        $this->findEncoding->modify($file);
128
    }
129
130
    public function testModifyWillSetTheEncoding()
131
    {
132
        $utf8file = (new LocalFile(static::$dir . 'utf8_file.test'));
133
        $utf8file->put(mb_convert_encoding('Some random Text €±§', 'UTF-8'));
134
135
        $utf8file = $this->findEncoding->modify($utf8file);
136
        static::assertEquals('utf-8', $utf8file->getEncoding());
137
    }
138
}
139