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\Contract; |
15
|
|
|
|
16
|
|
|
use Graze\DataFile\Helper\Builder\Builder; |
17
|
|
|
use Graze\DataFile\Helper\Builder\BuilderInterface; |
18
|
|
|
use Graze\DataFile\Modify\Compress\CompressionFactory; |
19
|
|
|
use Graze\DataFile\Modify\Compress\Gzip; |
20
|
|
|
use Graze\DataFile\Modify\Contract\FileContractorInterface; |
21
|
|
|
use Graze\DataFile\Modify\Contract\MergeFiles; |
22
|
|
|
use Graze\DataFile\Modify\MakeDirectory; |
23
|
|
|
use Graze\DataFile\Node\FileNodeCollection; |
24
|
|
|
use Graze\DataFile\Node\FileNodeCollectionInterface; |
25
|
|
|
use Graze\DataFile\Node\FileNodeInterface; |
26
|
|
|
use Graze\DataFile\Node\LocalFile; |
27
|
|
|
use Graze\DataFile\Test\AbstractFileTestCase; |
28
|
|
|
use InvalidArgumentException; |
29
|
|
|
use Mockery as m; |
30
|
|
|
use Symfony\Component\Process\Exception\ProcessFailedException; |
31
|
|
|
|
32
|
|
|
class MergeFilesTest extends AbstractFileTestCase |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @var BuilderInterface|m\MockInterface |
36
|
|
|
*/ |
37
|
|
|
protected $builder; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var MergeFiles |
41
|
|
|
*/ |
42
|
|
|
private $merge; |
43
|
|
|
|
44
|
|
|
public function setUp() |
45
|
|
|
{ |
46
|
|
|
$this->builder = m::mock(Builder::class)->makePartial(); |
47
|
|
|
$this->merge = new MergeFiles(); |
48
|
|
|
$this->merge->setBuilder($this->builder); |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function testInstanceOf() |
52
|
|
|
{ |
53
|
|
|
static::assertInstanceOf(FileContractorInterface::class, $this->merge); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function testCanContractAcceptsFileNodeCollectionInterface() |
57
|
|
|
{ |
58
|
|
|
$collection = m::mock(FileNodeCollectionInterface::class); |
59
|
|
|
$collection->shouldReceive('getIterator') |
60
|
|
|
->andReturn([]); |
61
|
|
|
|
62
|
|
|
$node = m::mock(LocalFile::class); |
63
|
|
|
static::assertTrue($this->merge->canContract($collection, $node)); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
View Code Duplication |
public function testCanContractOnlyAcceptsLocalFiles() |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
$collection = new FileNodeCollection(); |
69
|
|
|
$file1 = m::mock(LocalFile::class); |
70
|
|
|
$file1->shouldReceive('exists') |
71
|
|
|
->andReturn(true); |
72
|
|
|
$file1->shouldReceive('getCompression') |
73
|
|
|
->andReturn(CompressionFactory::TYPE_NONE); |
74
|
|
|
$collection->add($file1); |
75
|
|
|
|
76
|
|
|
$out = m::mock(LocalFile::class); |
77
|
|
|
|
78
|
|
|
static::assertTrue($this->merge->canContract($collection, $out)); |
79
|
|
|
|
80
|
|
|
$file2 = m::mock(FileNodeInterface::class); |
81
|
|
|
$file2->shouldReceive('getCompression') |
82
|
|
|
->andReturn(CompressionFactory::TYPE_NONE); |
83
|
|
|
$collection->add($file2); |
84
|
|
|
|
85
|
|
|
static::assertFalse($this->merge->canContract($collection, $out)); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
View Code Duplication |
public function testCanContractOnlyWithFilesThatExist() |
|
|
|
|
89
|
|
|
{ |
90
|
|
|
$collection = new FileNodeCollection(); |
91
|
|
|
$file1 = m::mock(LocalFile::class); |
92
|
|
|
$file1->shouldReceive('exists') |
93
|
|
|
->andReturn(true); |
94
|
|
|
$file1->shouldReceive('getCompression') |
95
|
|
|
->andReturn(CompressionFactory::TYPE_NONE); |
96
|
|
|
$collection->add($file1); |
97
|
|
|
|
98
|
|
|
$out = m::mock(LocalFile::class); |
99
|
|
|
|
100
|
|
|
static::assertTrue($this->merge->canContract($collection, $out)); |
101
|
|
|
|
102
|
|
|
$file2 = m::mock(LocalFile::class); |
103
|
|
|
$file2->shouldReceive('exists') |
104
|
|
|
->andReturn(false); |
105
|
|
|
$file2->shouldReceive('getCompression') |
106
|
|
|
->andReturn(CompressionFactory::TYPE_NONE); |
107
|
|
|
$collection->add($file2); |
108
|
|
|
|
109
|
|
|
static::assertFalse($this->merge->canContract($collection, $out)); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
View Code Duplication |
public function testCanContractOnlyUncompressedFiles() |
|
|
|
|
113
|
|
|
{ |
114
|
|
|
$collection = new FileNodeCollection(); |
115
|
|
|
$file1 = m::mock(LocalFile::class); |
116
|
|
|
$file1->shouldReceive('exists') |
117
|
|
|
->andReturn(true); |
118
|
|
|
$file1->shouldReceive('getCompression') |
119
|
|
|
->andReturn(CompressionFactory::TYPE_NONE); |
120
|
|
|
$collection->add($file1); |
121
|
|
|
|
122
|
|
|
$out = m::mock(LocalFile::class); |
123
|
|
|
|
124
|
|
|
static::assertTrue($this->merge->canContract($collection, $out)); |
125
|
|
|
|
126
|
|
|
$file2 = m::mock(LocalFile::class); |
127
|
|
|
$file2->shouldReceive('exists') |
128
|
|
|
->andReturn(true); |
129
|
|
|
$file2->shouldReceive('getCompression') |
130
|
|
|
->andReturn(Gzip::NAME); |
131
|
|
|
$collection->add($file2); |
132
|
|
|
|
133
|
|
|
static::assertFalse($this->merge->canContract($collection, $out)); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
View Code Duplication |
public function testCallingContractWithAFileThatCannotBeContractedWillThrowAnException() |
|
|
|
|
137
|
|
|
{ |
138
|
|
|
$collection = new FileNodeCollection(); |
139
|
|
|
$file = m::mock(LocalFile::class); |
140
|
|
|
$file->shouldReceive('exists') |
141
|
|
|
->andReturn(false); |
142
|
|
|
$file->shouldReceive('getCompression') |
143
|
|
|
->andReturn(CompressionFactory::TYPE_NONE); |
144
|
|
|
$collection->add($file); |
145
|
|
|
|
146
|
|
|
$target = m::mock(LocalFile::class); |
147
|
|
|
|
148
|
|
|
$this->expectException(InvalidArgumentException::class); |
149
|
|
|
|
150
|
|
|
$this->merge->contract($collection, $target); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
View Code Duplication |
public function testCallingContractWithANonLocalTargetWillThrowAnException() |
|
|
|
|
154
|
|
|
{ |
155
|
|
|
$collection = new FileNodeCollection(); |
156
|
|
|
$file = m::mock(LocalFile::class); |
157
|
|
|
$file->shouldReceive('exists') |
158
|
|
|
->andReturn(true); |
159
|
|
|
$file->shouldReceive('getCompression') |
160
|
|
|
->andReturn(CompressionFactory::TYPE_NONE); |
161
|
|
|
$collection->add($file); |
162
|
|
|
|
163
|
|
|
$target = m::mock(FileNodeInterface::class); |
164
|
|
|
|
165
|
|
|
static::assertFalse($this->merge->canContract($collection, $target)); |
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
|
|
|
public function testProcessFailedThrowException() |
254
|
|
|
{ |
255
|
|
|
$process = m::mock('Symfony\Component\Process\Process')->makePartial(); |
256
|
|
|
$this->builder->shouldReceive('build') |
|
|
|
|
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: