Completed
Pull Request — master (#1)
by Harry
04:52
created

testGetFileEncodingForASCIIFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 11
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 11
loc 11
rs 9.4285
cc 1
eloc 7
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);
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
        $process = m::mock(Process::class)->makePartial();
80
        $process->shouldReceive('isSuccessful')->andReturn(false);
81
        $process->shouldReceive('getCommandLine')->andReturn('cmd');
82
        $process->shouldReceive('getExitCode')->andReturn(1);
83
        $process->shouldReceive('getExitCodeText')->andReturn('failed');
84
        $process->shouldReceive('getWorkingDirectory')->andReturn('bla');
85
        $process->shouldReceive('isOutputDisabled')->andReturn(true);
86
        $process->shouldReceive('mustRun')->andThrow(new ProcessFailedException($process));
87
88
        $this->processFactory->shouldReceive('createProcess')
89
                             ->andReturn($process);
90
91
        $file = new LocalFile(static::$dir . 'failed_find_encoding_process.test');
92
        $file->put('random stuff and things 2!');
93
94
        $this->expectException(ProcessFailedException::class);
95
96
        $this->findEncoding->getEncoding($file);
97
    }
98
99 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...
100
    {
101
        $process = m::mock(Process::class)->makePartial();
102
        $process->shouldReceive('mustRun');
103
        $process->shouldReceive('getOutput')->andReturn('some random stuff with no charset');
104
105
        $this->processFactory->shouldReceive('createProcess')
106
                             ->andReturn($process);
107
108
        $file = new LocalFile(static::$dir . 'unknown_compression.test');
109
        $file->put('random stuff and things 2!');
110
111
        static::assertNull($this->findEncoding->getEncoding($file));
112
    }
113
114 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...
115
    {
116
        $file = new LocalFile(static::$dir . 'uncompressed_file.test');
117
        $file->put('some random text');
118
119
        static::assertTrue($this->findEncoding->canModify($file));
120
    }
121
122 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...
123
    {
124
        $file = m::mock(FileNodeInterface::class);
125
        static::assertFalse($this->findEncoding->canModify($file));
126
127
        $this->expectException(InvalidArgumentException::class);
128
        $this->findEncoding->modify($file);
129
    }
130
131
    public function testModifyWillSetTheEncoding()
132
    {
133
        $utf8file = (new LocalFile(static::$dir . 'utf8_file.test'));
134
        $utf8file->put(mb_convert_encoding('Some random Text €±§', 'UTF-8'));
135
136
        $utf8file = $this->findEncoding->modify($utf8file);
137
        static::assertEquals('utf-8', $utf8file->getEncoding());
138
    }
139
}
140