1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Graze\DataFile\Test\Integration\Modify\Contract; |
4
|
|
|
|
5
|
|
|
use Graze\DataFile\Helper\Process\ProcessFactory; |
6
|
|
|
use Graze\DataFile\Modify\Compress\CompressionType; |
7
|
|
|
use Graze\DataFile\Modify\Contract\FileContractorInterface; |
8
|
|
|
use Graze\DataFile\Modify\Contract\MergeFiles; |
9
|
|
|
use Graze\DataFile\Modify\MakeDirectory; |
10
|
|
|
use Graze\DataFile\Node\FileNodeCollection; |
11
|
|
|
use Graze\DataFile\Node\FileNodeCollectionInterface; |
12
|
|
|
use Graze\DataFile\Node\FileNodeInterface; |
13
|
|
|
use Graze\DataFile\Node\LocalFile; |
14
|
|
|
use Graze\DataFile\Test\FileTestCase; |
15
|
|
|
use InvalidArgumentException; |
16
|
|
|
use Mockery as m; |
17
|
|
|
use Symfony\Component\Process\Exception\ProcessFailedException; |
18
|
|
|
|
19
|
|
|
class MergeFilesTest extends FileTestCase |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var ProcessFactory|m\MockInterface |
23
|
|
|
*/ |
24
|
|
|
protected $processFactory; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var MergeFiles |
28
|
|
|
*/ |
29
|
|
|
private $merge; |
30
|
|
|
|
31
|
|
|
public function setUp() |
32
|
|
|
{ |
33
|
|
|
$this->processFactory = m::mock(ProcessFactory::class)->makePartial(); |
34
|
|
|
$this->merge = new MergeFiles(); |
35
|
|
|
$this->merge->setProcessFactory($this->processFactory); |
|
|
|
|
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testInstanceOf() |
39
|
|
|
{ |
40
|
|
|
static::assertInstanceOf(FileContractorInterface::class, $this->merge); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testCanContractAcceptsFileNodeCollectionInterface() |
44
|
|
|
{ |
45
|
|
|
$collection = m::mock(FileNodeCollectionInterface::class); |
46
|
|
|
$collection->shouldReceive('getIterator') |
47
|
|
|
->andReturn([]); |
48
|
|
|
|
49
|
|
|
$node = m::mock(LocalFile::class); |
50
|
|
|
static::assertTrue($this->merge->canContract($collection, $node)); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
View Code Duplication |
public function testCanContractOnlyAcceptsLocalFiles() |
|
|
|
|
54
|
|
|
{ |
55
|
|
|
$collection = new FileNodeCollection(); |
56
|
|
|
$file1 = m::mock(LocalFile::class); |
57
|
|
|
$file1->shouldReceive('exists') |
58
|
|
|
->andReturn(true); |
59
|
|
|
$file1->shouldReceive('getCompression') |
60
|
|
|
->andReturn(CompressionType::NONE); |
61
|
|
|
$collection->add($file1); |
62
|
|
|
|
63
|
|
|
$out = m::mock(LocalFile::class); |
64
|
|
|
|
65
|
|
|
static::assertTrue($this->merge->canContract($collection, $out)); |
66
|
|
|
|
67
|
|
|
$file2 = m::mock(FileNodeInterface::class); |
68
|
|
|
$file2->shouldReceive('getCompression') |
69
|
|
|
->andReturn(CompressionType::NONE); |
70
|
|
|
$collection->add($file2); |
71
|
|
|
|
72
|
|
|
static::assertFalse($this->merge->canContract($collection, $out)); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
View Code Duplication |
public function testCanContractOnlyWithFilesThatExist() |
|
|
|
|
76
|
|
|
{ |
77
|
|
|
$collection = new FileNodeCollection(); |
78
|
|
|
$file1 = m::mock(LocalFile::class); |
79
|
|
|
$file1->shouldReceive('exists') |
80
|
|
|
->andReturn(true); |
81
|
|
|
$file1->shouldReceive('getCompression') |
82
|
|
|
->andReturn(CompressionType::NONE); |
83
|
|
|
$collection->add($file1); |
84
|
|
|
|
85
|
|
|
$out = m::mock(LocalFile::class); |
86
|
|
|
|
87
|
|
|
static::assertTrue($this->merge->canContract($collection, $out)); |
88
|
|
|
|
89
|
|
|
$file2 = m::mock(LocalFile::class); |
90
|
|
|
$file2->shouldReceive('exists') |
91
|
|
|
->andReturn(false); |
92
|
|
|
$file2->shouldReceive('getCompression') |
93
|
|
|
->andReturn(CompressionType::NONE); |
94
|
|
|
$collection->add($file2); |
95
|
|
|
|
96
|
|
|
static::assertFalse($this->merge->canContract($collection, $out)); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
View Code Duplication |
public function testCanContractOnlyUncompressedFiles() |
|
|
|
|
100
|
|
|
{ |
101
|
|
|
$collection = new FileNodeCollection(); |
102
|
|
|
$file1 = m::mock(LocalFile::class); |
103
|
|
|
$file1->shouldReceive('exists') |
104
|
|
|
->andReturn(true); |
105
|
|
|
$file1->shouldReceive('getCompression') |
106
|
|
|
->andReturn(CompressionType::NONE); |
107
|
|
|
$collection->add($file1); |
108
|
|
|
|
109
|
|
|
$out = m::mock(LocalFile::class); |
110
|
|
|
|
111
|
|
|
static::assertTrue($this->merge->canContract($collection, $out)); |
112
|
|
|
|
113
|
|
|
$file2 = m::mock(LocalFile::class); |
114
|
|
|
$file2->shouldReceive('exists') |
115
|
|
|
->andReturn(true); |
116
|
|
|
$file2->shouldReceive('getCompression') |
117
|
|
|
->andReturn(CompressionType::GZIP); |
118
|
|
|
$collection->add($file2); |
119
|
|
|
|
120
|
|
|
static::assertFalse($this->merge->canContract($collection, $out)); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
View Code Duplication |
public function testCallingCanContractWithANonLocalTargetWillThrowAnException() |
|
|
|
|
124
|
|
|
{ |
125
|
|
|
$collection = new FileNodeCollection(); |
126
|
|
|
$file = m::mock(LocalFile::class); |
127
|
|
|
$file->shouldReceive('exists') |
128
|
|
|
->andReturn(true); |
129
|
|
|
$file->shouldReceive('getCompression') |
130
|
|
|
->andReturn(CompressionType::NONE); |
131
|
|
|
$collection->add($file); |
132
|
|
|
|
133
|
|
|
$target = m::mock(FileNodeInterface::class); |
134
|
|
|
|
135
|
|
|
static::assertFalse($this->merge->canContract($collection, $target)); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
View Code Duplication |
public function testCallingContractWithAFileThatCannotBeContractedWillThrowAnException() |
|
|
|
|
139
|
|
|
{ |
140
|
|
|
$collection = new FileNodeCollection(); |
141
|
|
|
$file = m::mock(LocalFile::class); |
142
|
|
|
$file->shouldReceive('exists') |
143
|
|
|
->andReturn(false); |
144
|
|
|
$file->shouldReceive('getCompression') |
145
|
|
|
->andReturn(CompressionType::NONE); |
146
|
|
|
$collection->add($file); |
147
|
|
|
|
148
|
|
|
$target = m::mock(LocalFile::class); |
149
|
|
|
|
150
|
|
|
$this->expectException(InvalidArgumentException::class); |
151
|
|
|
|
152
|
|
|
$this->merge->contract($collection, $target); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
View Code Duplication |
public function testCallingContractWithANonLocalTargetWillThrowAnException() |
|
|
|
|
156
|
|
|
{ |
157
|
|
|
$collection = new FileNodeCollection(); |
158
|
|
|
$file = m::mock(LocalFile::class); |
159
|
|
|
$file->shouldReceive('exists') |
160
|
|
|
->andReturn(true); |
161
|
|
|
$file->shouldReceive('getCompression') |
162
|
|
|
->andReturn(CompressionType::NONE); |
163
|
|
|
$collection->add($file); |
164
|
|
|
|
165
|
|
|
$target = m::mock(FileNodeInterface::class); |
166
|
|
|
|
167
|
|
|
$this->expectException(InvalidArgumentException::class); |
168
|
|
|
|
169
|
|
|
$this->merge->contract($collection, $target); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
View Code Duplication |
public function testSimpleMergeFiles() |
|
|
|
|
173
|
|
|
{ |
174
|
|
|
$collection = $this->createCollection('simple.merge/', 3); |
175
|
|
|
|
176
|
|
|
$outputFile = new LocalFile(static::$dir . 'simple.merge.output'); |
177
|
|
|
|
178
|
|
|
$file = $this->merge->contract($collection, $outputFile); |
179
|
|
|
|
180
|
|
|
static::assertSame($file, $outputFile); |
181
|
|
|
static::assertEquals( |
182
|
|
|
[ |
183
|
|
|
"File 1 Line 1", |
184
|
|
|
"File 1 Line 2", |
185
|
|
|
"File 1 Line 3", |
186
|
|
|
"File 2 Line 1", |
187
|
|
|
"File 2 Line 2", |
188
|
|
|
"File 2 Line 3", |
189
|
|
|
"File 3 Line 1", |
190
|
|
|
"File 3 Line 2", |
191
|
|
|
"File 3 Line 3", |
192
|
|
|
], |
193
|
|
|
$file->getContents() |
194
|
|
|
); |
195
|
|
|
|
196
|
|
|
$exists = $collection->filter(function (FileNodeInterface $item) { |
197
|
|
|
return $item->exists(); |
198
|
|
|
}); |
199
|
|
|
|
200
|
|
|
static::assertCount(3, $exists); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @param string $rootDir |
205
|
|
|
* @param int $numFiles |
206
|
|
|
* |
207
|
|
|
* @return FileNodeCollectionInterface |
208
|
|
|
*/ |
209
|
|
|
private function createCollection($rootDir, $numFiles) |
210
|
|
|
{ |
211
|
|
|
$mkDir = new MakeDirectory(); |
212
|
|
|
$collection = new FileNodeCollection(); |
213
|
|
|
for ($i = 1; $i <= $numFiles; $i++) { |
214
|
|
|
$file = new LocalFile(static::$dir . $rootDir . 'part_' . $i); |
215
|
|
|
$mkDir->makeDirectory($file); |
216
|
|
|
$file->put("File $i Line 1\nFile $i Line 2\nFile $i Line 3\n"); |
217
|
|
|
$collection->add($file); |
218
|
|
|
} |
219
|
|
|
return $collection; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
View Code Duplication |
public function testCallingMergeWithKeepOldFilesAsFalseDeletesAllTheFilesInTheCollection() |
|
|
|
|
223
|
|
|
{ |
224
|
|
|
$collection = $this->createCollection('simple.merge.delete/', 3); |
225
|
|
|
|
226
|
|
|
$outputFile = new LocalFile(static::$dir . 'simple.merge.delete.output'); |
227
|
|
|
|
228
|
|
|
$file = $this->merge->contract($collection, $outputFile, ['keepOldFiles' => false]); |
229
|
|
|
|
230
|
|
|
static::assertSame($file, $outputFile); |
231
|
|
|
static::assertEquals( |
232
|
|
|
[ |
233
|
|
|
"File 1 Line 1", |
234
|
|
|
"File 1 Line 2", |
235
|
|
|
"File 1 Line 3", |
236
|
|
|
"File 2 Line 1", |
237
|
|
|
"File 2 Line 2", |
238
|
|
|
"File 2 Line 3", |
239
|
|
|
"File 3 Line 1", |
240
|
|
|
"File 3 Line 2", |
241
|
|
|
"File 3 Line 3", |
242
|
|
|
], |
243
|
|
|
$file->getContents() |
244
|
|
|
); |
245
|
|
|
|
246
|
|
|
$exists = $collection->filter(function (FileNodeInterface $item) { |
247
|
|
|
return $item->exists(); |
248
|
|
|
}); |
249
|
|
|
|
250
|
|
|
static::assertCount(0, $exists); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
View Code Duplication |
public function testProcessFailedThrowException() |
|
|
|
|
254
|
|
|
{ |
255
|
|
|
$process = m::mock('Symfony\Component\Process\Process')->makePartial(); |
256
|
|
|
$this->processFactory->shouldReceive('createProcess') |
257
|
|
|
->andReturn($process); |
258
|
|
|
|
259
|
|
|
$process->shouldReceive('isSuccessful')->andReturn(false); |
260
|
|
|
|
261
|
|
|
// set exception as no guarantee process will run on local system |
262
|
|
|
$this->expectException(ProcessFailedException::class); |
263
|
|
|
|
264
|
|
|
$collection = $this->createCollection('simple.merge/', 3); |
265
|
|
|
|
266
|
|
|
$outputFile = new LocalFile(static::$dir . 'simple.merge.output'); |
267
|
|
|
|
268
|
|
|
$this->merge->contract($collection, $outputFile); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
View Code Duplication |
public function testCallingContractWillPassThroughOptions() |
|
|
|
|
272
|
|
|
{ |
273
|
|
|
$collection = $this->createCollection('simple.contract.pass.through/', 3); |
274
|
|
|
$outputFile = new LocalFile(static::$dir . 'simple.contract.pass.through.output'); |
275
|
|
|
|
276
|
|
|
$file = $this->merge->contract( |
277
|
|
|
$collection, |
278
|
|
|
$outputFile, |
279
|
|
|
[ |
280
|
|
|
'keepOldFiles' => true, |
281
|
|
|
] |
282
|
|
|
); |
283
|
|
|
|
284
|
|
|
static::assertEquals( |
285
|
|
|
[ |
286
|
|
|
"File 1 Line 1", |
287
|
|
|
"File 1 Line 2", |
288
|
|
|
"File 1 Line 3", |
289
|
|
|
"File 2 Line 1", |
290
|
|
|
"File 2 Line 2", |
291
|
|
|
"File 2 Line 3", |
292
|
|
|
"File 3 Line 1", |
293
|
|
|
"File 3 Line 2", |
294
|
|
|
"File 3 Line 3", |
295
|
|
|
], |
296
|
|
|
$file->getContents() |
297
|
|
|
); |
298
|
|
|
|
299
|
|
|
$exists = $collection->filter(function (FileNodeInterface $item) { |
300
|
|
|
return $item->exists(); |
301
|
|
|
}); |
302
|
|
|
|
303
|
|
|
static::assertCount(3, $exists); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
public function testDeleteOldFilesWillDeleteAnyEmptyDirectories() |
307
|
|
|
{ |
308
|
|
|
$collection = $this->createCollection('simple.merge.delete.folder/', 3); |
309
|
|
|
|
310
|
|
|
$outputFile = new LocalFile(static::$dir . 'simple.merge.delete.output'); |
311
|
|
|
|
312
|
|
|
$file = $this->merge->contract($collection, $outputFile, ['keepOldFiles' => false]); |
313
|
|
|
|
314
|
|
|
static::assertSame($file, $outputFile); |
315
|
|
|
static::assertEquals( |
316
|
|
|
[ |
317
|
|
|
"File 1 Line 1", |
318
|
|
|
"File 1 Line 2", |
319
|
|
|
"File 1 Line 3", |
320
|
|
|
"File 2 Line 1", |
321
|
|
|
"File 2 Line 2", |
322
|
|
|
"File 2 Line 3", |
323
|
|
|
"File 3 Line 1", |
324
|
|
|
"File 3 Line 2", |
325
|
|
|
"File 3 Line 3", |
326
|
|
|
], |
327
|
|
|
$file->getContents() |
328
|
|
|
); |
329
|
|
|
|
330
|
|
|
static::assertFalse(file_exists($collection->getAll()[0]->getDirectory())); |
331
|
|
|
|
332
|
|
|
$exists = $collection->filter(function (FileNodeInterface $item) { |
333
|
|
|
return $item->exists(); |
334
|
|
|
}); |
335
|
|
|
|
336
|
|
|
static::assertCount(0, $exists); |
337
|
|
|
static::assertTrue($file->exists(), 'output file should exist'); |
338
|
|
|
} |
339
|
|
|
} |
340
|
|
|
|
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: