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() |
|
|
|
|
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() |
|
|
|
|
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); |
|
|
|
|
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)); |
|
|
|
|
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() |
|
|
|
|
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() |
|
|
|
|
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() |
|
|
|
|
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
|
|
|
|
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: