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

testWhenTheProcessFailsAnExceptionIsThrownOnFindEncoding()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 22
rs 9.2
cc 1
eloc 15
nc 1
nop 0
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 Mockery\MockInterface;
14
use Symfony\Component\Process\Exception\ProcessFailedException;
15
use Symfony\Component\Process\Process;
16
17
class FindEncodingTest extends FileTestCase
18
{
19
    /**
20
     * @var FindEncoding
21
     */
22
    protected $findEncoding;
23
24
    /**
25
     * @var ProcessFactory|MockInterface
26
     */
27
    protected $processFactory;
28
29
    public function setUp()
30
    {
31
        $this->processFactory = m::mock(ProcessFactory::class)->makePartial();
32
        $this->findEncoding = new FindEncoding();
33
        $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...
34
    }
35
36 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...
37
    {
38
        $asciiFile = (new LocalFile(static::$dir . 'ascii_file.test'))
39
            ->setEncoding('us-ascii');
40
        $asciiFile->put(mb_convert_encoding('Some random Text', 'ASCII'));
41
42
        static::assertEquals(
43
            strtolower($asciiFile->getEncoding()),
44
            strtolower($this->findEncoding->getEncoding($asciiFile))
45
        );
46
    }
47
48 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...
49
    {
50
        $utf8file = (new LocalFile(static::$dir . 'utf8_file.test'))
51
            ->setEncoding('UTF-8');
52
        $utf8file->put(mb_convert_encoding('Some random Text €±§', 'UTF-8'));
53
54
        static::assertEquals(
55
            strtolower($utf8file->getEncoding()),
56
            strtolower($this->findEncoding->getEncoding($utf8file))
57
        );
58
    }
59
60
    public function testGetFileEncodingForCompressedFile()
61
    {
62
        $utf8file = (new LocalFile(static::$dir . 'utf8_tobegzipped_file.test'))
63
            ->setEncoding('UTF-8');
64
        $utf8file->put(mb_convert_encoding('Some random Text €±§', 'UTF-8'));
65
        $gzip = new Gzip();
66
        $gzipFile = $gzip->compress($utf8file);
67
68
        static::assertEquals(
69
            strtolower($gzipFile->getEncoding()),
70
            strtolower($this->findEncoding->getEncoding($gzipFile))
71
        );
72
73
        static::assertEquals('utf-8', $this->findEncoding->getEncoding($gzipFile));
74
        static::assertEquals($utf8file->getEncoding(), $gzipFile->getEncoding());
75
    }
76
77
    public function testWhenTheProcessFailsAnExceptionIsThrownOnFindEncoding()
78
    {
79
        /** @var Process|MockInterface $process */
80
        $process = m::mock(Process::class)->makePartial();
81
        $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...
82
        $process->shouldReceive('getCommandLine')->andReturn('cmd');
83
        $process->shouldReceive('getExitCode')->andReturn(1);
84
        $process->shouldReceive('getExitCodeText')->andReturn('failed');
85
        $process->shouldReceive('getWorkingDirectory')->andReturn('bla');
86
        $process->shouldReceive('isOutputDisabled')->andReturn(true);
87
        $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 80 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...
88
89
        $this->processFactory->shouldReceive('createProcess')
90
                             ->andReturn($process);
91
92
        $file = new LocalFile(static::$dir . 'failed_find_encoding_process.test');
93
        $file->put('random stuff and things 2!');
94
95
        $this->expectException(ProcessFailedException::class);
96
97
        $this->findEncoding->getEncoding($file);
98
    }
99
100 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...
101
    {
102
        $process = m::mock(Process::class)->makePartial();
103
        $process->shouldReceive('mustRun');
104
        $process->shouldReceive('getOutput')->andReturn('some random stuff with no charset');
105
106
        $this->processFactory->shouldReceive('createProcess')
107
                             ->andReturn($process);
108
109
        $file = new LocalFile(static::$dir . 'unknown_compression.test');
110
        $file->put('random stuff and things 2!');
111
112
        static::assertNull($this->findEncoding->getEncoding($file));
113
    }
114
115 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...
116
    {
117
        $file = new LocalFile(static::$dir . 'uncompressed_file.test');
118
        $file->put('some random text');
119
120
        static::assertTrue($this->findEncoding->canModify($file));
121
    }
122
123 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...
124
    {
125
        $file = m::mock(FileNodeInterface::class);
126
        static::assertFalse($this->findEncoding->canModify($file));
127
128
        $this->expectException(InvalidArgumentException::class);
129
        $this->findEncoding->modify($file);
130
    }
131
132
    public function testModifyWillSetTheEncoding()
133
    {
134
        $utf8file = (new LocalFile(static::$dir . 'utf8_file.test'));
135
        $utf8file->put(mb_convert_encoding('Some random Text €±§', 'UTF-8'));
136
137
        $utf8file = $this->findEncoding->modify($utf8file);
138
        static::assertEquals('utf-8', $utf8file->getEncoding());
139
    }
140
}
141