1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of graze/data-file |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
* @license https://github.com/graze/data-file/blob/master/LICENSE.md |
11
|
|
|
* @link https://github.com/graze/data-file |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Graze\DataFile\Test\Integration\Modify\Encoding; |
15
|
|
|
|
16
|
|
|
use Graze\DataFile\Helper\Builder\Builder; |
17
|
|
|
use Graze\DataFile\Helper\Builder\BuilderInterface; |
18
|
|
|
use Graze\DataFile\Modify\Compress\Gzip; |
19
|
|
|
use Graze\DataFile\Modify\Encoding\FindEncoding; |
20
|
|
|
use Graze\DataFile\Node\FileNodeInterface; |
21
|
|
|
use Graze\DataFile\Node\LocalFile; |
22
|
|
|
use Graze\DataFile\Test\AbstractFileTestCase; |
23
|
|
|
use InvalidArgumentException; |
24
|
|
|
use Mockery as m; |
25
|
|
|
use Mockery\MockInterface; |
26
|
|
|
use Symfony\Component\Process\Exception\ProcessFailedException; |
27
|
|
|
use Symfony\Component\Process\Process; |
28
|
|
|
|
29
|
|
|
class FindEncodingTest extends AbstractFileTestCase |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var FindEncoding |
33
|
|
|
*/ |
34
|
|
|
protected $findEncoding; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var BuilderInterface|MockInterface |
38
|
|
|
*/ |
39
|
|
|
protected $builder; |
40
|
|
|
|
41
|
|
|
public function setUp() |
42
|
|
|
{ |
43
|
|
|
$this->builder = m::mock(Builder::class)->makePartial(); |
44
|
|
|
$this->findEncoding = new FindEncoding(); |
45
|
|
|
$this->findEncoding->setBuilder($this->builder); |
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
|
View Code Duplication |
public function testGetFileEncodingForASCIIFile() |
|
|
|
|
49
|
|
|
{ |
50
|
|
|
$asciiFile = (new LocalFile(static::$dir . 'ascii_file.test')) |
51
|
|
|
->setEncoding('us-ascii'); |
52
|
|
|
$asciiFile->put(mb_convert_encoding('Some random Text', 'ASCII')); |
53
|
|
|
|
54
|
|
|
static::assertEquals( |
55
|
|
|
strtolower($asciiFile->getEncoding()), |
56
|
|
|
strtolower($this->findEncoding->getEncoding($asciiFile)) |
57
|
|
|
); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
View Code Duplication |
public function testGetFileEncodingForUtf8File() |
|
|
|
|
61
|
|
|
{ |
62
|
|
|
$utf8file = (new LocalFile(static::$dir . 'utf8_file.test')) |
63
|
|
|
->setEncoding('UTF-8'); |
64
|
|
|
$utf8file->put(mb_convert_encoding('Some random Text €±§', 'UTF-8')); |
65
|
|
|
|
66
|
|
|
static::assertEquals( |
67
|
|
|
strtolower($utf8file->getEncoding()), |
68
|
|
|
strtolower($this->findEncoding->getEncoding($utf8file)) |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function testGetFileEncodingForCompressedFile() |
73
|
|
|
{ |
74
|
|
|
$utf8file = (new LocalFile(static::$dir . 'utf8_tobegzipped_file.test')) |
75
|
|
|
->setEncoding('UTF-8'); |
76
|
|
|
$utf8file->put(mb_convert_encoding('Some random Text €±§', 'UTF-8')); |
77
|
|
|
$gzip = new Gzip(); |
78
|
|
|
$gzipFile = $gzip->compress($utf8file); |
79
|
|
|
|
80
|
|
|
static::assertEquals( |
81
|
|
|
strtolower($gzipFile->getEncoding()), |
82
|
|
|
strtolower($this->findEncoding->getEncoding($gzipFile)) |
83
|
|
|
); |
84
|
|
|
|
85
|
|
|
static::assertEquals('utf-8', $this->findEncoding->getEncoding($gzipFile)); |
86
|
|
|
static::assertEquals($utf8file->getEncoding(), $gzipFile->getEncoding()); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
View Code Duplication |
public function testWhenTheProcessFailsAnExceptionIsThrownOnFindEncoding() |
|
|
|
|
90
|
|
|
{ |
91
|
|
|
$process = m::mock(Process::class)->makePartial(); |
92
|
|
|
$process->shouldReceive('isSuccessful')->andReturn(false); |
93
|
|
|
$process->shouldReceive('getCommandLine')->andReturn('cmd'); |
94
|
|
|
$process->shouldReceive('getExitCode')->andReturn(1); |
95
|
|
|
$process->shouldReceive('getExitCodeText')->andReturn('failed'); |
96
|
|
|
$process->shouldReceive('getWorkingDirectory')->andReturn('bla'); |
97
|
|
|
$process->shouldReceive('isOutputDisabled')->andReturn(true); |
98
|
|
|
$process->shouldReceive('mustRun')->andThrow(new ProcessFailedException($process)); |
99
|
|
|
|
100
|
|
|
$this->builder->shouldReceive('build') |
|
|
|
|
101
|
|
|
->andReturn($process); |
102
|
|
|
|
103
|
|
|
$file = new LocalFile(static::$dir . 'failed_find_encoding_process.test'); |
104
|
|
|
$file->put('random stuff and things 2!'); |
105
|
|
|
|
106
|
|
|
$this->expectException(ProcessFailedException::class); |
107
|
|
|
|
108
|
|
|
$this->findEncoding->getEncoding($file); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function testWhenTheProcessReturnsAnUnknownFileNullIsReturned() |
112
|
|
|
{ |
113
|
|
|
$process = m::mock(Process::class)->makePartial(); |
114
|
|
|
$process->shouldReceive('mustRun'); |
115
|
|
|
$process->shouldReceive('getOutput')->andReturn('some random stuff with no charset'); |
116
|
|
|
|
117
|
|
|
$this->builder->shouldReceive('build') |
|
|
|
|
118
|
|
|
->andReturn($process); |
119
|
|
|
|
120
|
|
|
$file = new LocalFile(static::$dir . 'unknown_compression.test'); |
121
|
|
|
$file->put('random stuff and things 2!'); |
122
|
|
|
|
123
|
|
|
static::assertNull($this->findEncoding->getEncoding($file)); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
View Code Duplication |
public function testCanModifyCanModifyLocalFiles() |
|
|
|
|
127
|
|
|
{ |
128
|
|
|
$file = new LocalFile(static::$dir . 'uncompressed_file.test'); |
129
|
|
|
$file->put('some random text'); |
130
|
|
|
|
131
|
|
|
static::assertTrue($this->findEncoding->canModify($file)); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
View Code Duplication |
public function testUnableToModifyNonLocalFiles() |
|
|
|
|
135
|
|
|
{ |
136
|
|
|
$file = m::mock(FileNodeInterface::class); |
137
|
|
|
static::assertFalse($this->findEncoding->canModify($file)); |
138
|
|
|
|
139
|
|
|
$this->expectException(InvalidArgumentException::class); |
140
|
|
|
$this->findEncoding->modify($file); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function testModifyWillSetTheEncoding() |
144
|
|
|
{ |
145
|
|
|
$utf8file = (new LocalFile(static::$dir . 'utf8_file.test')); |
146
|
|
|
$utf8file->put(mb_convert_encoding('Some random Text €±§', 'UTF-8')); |
147
|
|
|
|
148
|
|
|
$utf8file = $this->findEncoding->modify($utf8file); |
149
|
|
|
static::assertEquals('utf-8', $utf8file->getEncoding()); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
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: