|
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); |
|
|
|
|
|
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
View Code Duplication |
public function testGetFileEncodingForASCIIFile() |
|
|
|
|
|
|
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() |
|
|
|
|
|
|
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)); |
|
|
|
|
|
|
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() |
|
|
|
|
|
|
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() |
|
|
|
|
|
|
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() |
|
|
|
|
|
|
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
|
|
|
|
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: