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\Unit\Modify; |
15
|
|
|
|
16
|
|
|
use ArrayIterator; |
17
|
|
|
use Graze\DataFile\Format\FormatAwareInterface; |
18
|
|
|
use Graze\DataFile\Format\FormatInterface; |
19
|
|
|
use Graze\DataFile\Format\Formatter\FormatterFactoryInterface; |
20
|
|
|
use Graze\DataFile\Format\Parser\ParserFactoryInterface; |
21
|
|
|
use Graze\DataFile\Format\Parser\ParserInterface; |
22
|
|
|
use Graze\DataFile\Helper\Builder\BuilderInterface; |
23
|
|
|
use Graze\DataFile\IO\FileReader; |
24
|
|
|
use Graze\DataFile\IO\FileWriter; |
25
|
|
|
use Graze\DataFile\Modify\FileModifierInterface; |
26
|
|
|
use Graze\DataFile\Modify\ReFormat; |
27
|
|
|
use Graze\DataFile\Node\FileNodeInterface; |
28
|
|
|
use Graze\DataFile\Node\LocalFile; |
29
|
|
|
use Graze\DataFile\Node\LocalFileNodeInterface; |
30
|
|
|
use Graze\DataFile\Test\TestCase; |
31
|
|
|
use InvalidArgumentException; |
32
|
|
|
use Mockery as m; |
33
|
|
|
use Mockery\MockInterface; |
34
|
|
|
|
35
|
|
|
class ReFormatTest extends TestCase |
36
|
|
|
{ |
37
|
|
|
/** @var ParserFactoryInterface|MockInterface */ |
38
|
|
|
private $parserFactory; |
39
|
|
|
/** @var FormatterFactoryInterface|MockInterface */ |
40
|
|
|
private $formatterFactory; |
41
|
|
|
/** @var ReFormat */ |
42
|
|
|
private $reFormatter; |
43
|
|
|
/** @var BuilderInterface|MockInterface */ |
44
|
|
|
private $builder; |
45
|
|
|
|
46
|
|
|
public function setUp() |
47
|
|
|
{ |
48
|
|
|
parent::setUp(); |
49
|
|
|
|
50
|
|
|
$this->builder = m::mock(BuilderInterface::class); |
51
|
|
|
$this->parserFactory = m::mock(ParserFactoryInterface::class); |
52
|
|
|
$this->formatterFactory = m::mock(FormatterFactoryInterface::class); |
53
|
|
|
$this->reFormatter = new ReFormat($this->formatterFactory, $this->parserFactory, $this->builder); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function testInstanceOf() |
57
|
|
|
{ |
58
|
|
|
static::assertInstanceOf(FileModifierInterface::class, $this->reFormatter); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
View Code Duplication |
public function testCanModifyAcceptsFileNodeInterfaceWithAFormat() |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
$file = m::mock(FileNodeInterface::class, FormatAwareInterface::class); |
64
|
|
|
$file->shouldReceive('exists') |
65
|
|
|
->andReturn(true); |
66
|
|
|
|
67
|
|
|
$format = m::mock(FormatInterface::class); |
68
|
|
|
$parser = m::mock(ParserInterface::class); |
69
|
|
|
$this->parserFactory->shouldReceive('getParser') |
70
|
|
|
->with($format) |
71
|
|
|
->andReturn($parser); |
72
|
|
|
|
73
|
|
|
$file->shouldReceive('getFormat') |
74
|
|
|
->andReturn($format); |
75
|
|
|
|
76
|
|
|
static::assertTrue($this->reFormatter->canModify($file)); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
View Code Duplication |
public function testCanModifyDoesNotAcceptAFormatThatCanNotBeParsed() |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
$file = m::mock(FileNodeInterface::class, FormatAwareInterface::class); |
82
|
|
|
$file->shouldReceive('exists') |
83
|
|
|
->andReturn(true); |
84
|
|
|
|
85
|
|
|
$format = m::mock(FormatInterface::class); |
86
|
|
|
$this->parserFactory->shouldReceive('getParser') |
87
|
|
|
->with($format) |
88
|
|
|
->andReturn(null); |
89
|
|
|
|
90
|
|
|
$file->shouldReceive('getFormat') |
91
|
|
|
->andReturn($format); |
92
|
|
|
|
93
|
|
|
static::assertFalse($this->reFormatter->canModify($file)); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function testCanReFormatWithANonLocalInputFileAndOutputFormat() |
97
|
|
|
{ |
98
|
|
|
$file = m::mock(FileNodeInterface::class, FormatAwareInterface::class); |
99
|
|
|
$target = m::mock(LocalFile::class); |
100
|
|
|
|
101
|
|
|
$file->shouldReceive('exists') |
102
|
|
|
->andReturn(true); |
103
|
|
|
|
104
|
|
|
$this->builder->shouldReceive('build') |
105
|
|
|
->with(LocalFile::class, m::type('string')) |
106
|
|
|
->andReturn($target); |
107
|
|
|
|
108
|
|
|
$format = m::mock(FormatInterface::class); |
109
|
|
|
|
110
|
|
|
$target->shouldReceive('setFormat') |
111
|
|
|
->with($format) |
112
|
|
|
->andReturn($target); |
113
|
|
|
|
114
|
|
|
$reader = m::mock(FileReader::class); |
115
|
|
|
$this->builder->shouldReceive('build') |
116
|
|
|
->with(FileReader::class, $file, null, $this->parserFactory) |
117
|
|
|
->andReturn($reader); |
118
|
|
|
$writer = m::mock(FileWriter::class); |
119
|
|
|
$this->builder->shouldReceive('build') |
120
|
|
|
->with(FileWriter::Class, $target, $format, $this->formatterFactory) |
121
|
|
|
->andReturn($writer); |
122
|
|
|
|
123
|
|
|
$iterator = new ArrayIterator(['first', 'second']); |
124
|
|
|
|
125
|
|
|
$reader->shouldReceive('fetch') |
126
|
|
|
->andReturn($iterator); |
127
|
|
|
|
128
|
|
|
$writer->shouldReceive('insertOne') |
129
|
|
|
->with('first') |
130
|
|
|
->once(); |
131
|
|
|
$writer->shouldReceive('insertOne') |
132
|
|
|
->with('second') |
133
|
|
|
->once(); |
134
|
|
|
|
135
|
|
|
static::assertSame($target, $this->reFormatter->reFormat($file, $format)); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
View Code Duplication |
public function testCanReFormatALocalInputFile() |
|
|
|
|
139
|
|
|
{ |
140
|
|
|
$file = m::mock(LocalFileNodeInterface::class, FormatAwareInterface::class); |
141
|
|
|
$target = m::mock(LocalFileNodeInterface::class, FormatAwareInterface::class); |
142
|
|
|
|
143
|
|
|
$file->shouldReceive('getPath') |
144
|
|
|
->andReturn('/tmp/file.txt'); |
145
|
|
|
$file->shouldReceive('exists') |
146
|
|
|
->andReturn(true); |
147
|
|
|
|
148
|
|
|
$file->shouldReceive('getClone') |
149
|
|
|
->andReturn($target); |
150
|
|
|
$target->shouldReceive('setPath') |
151
|
|
|
->with('/tmp/file-format.txt') |
152
|
|
|
->andReturn($target); |
153
|
|
|
|
154
|
|
|
$format = m::mock(FormatInterface::class); |
155
|
|
|
|
156
|
|
|
$target->shouldReceive('setFormat') |
157
|
|
|
->with($format) |
158
|
|
|
->andReturn($target); |
159
|
|
|
|
160
|
|
|
$reader = m::mock(FileReader::class); |
161
|
|
|
$this->builder->shouldReceive('build') |
162
|
|
|
->with(FileReader::class, $file, null, $this->parserFactory) |
163
|
|
|
->andReturn($reader); |
164
|
|
|
$writer = m::mock(FileWriter::class); |
165
|
|
|
$this->builder->shouldReceive('build') |
166
|
|
|
->with(FileWriter::Class, $target, $format, $this->formatterFactory) |
167
|
|
|
->andReturn($writer); |
168
|
|
|
|
169
|
|
|
$iterator = new ArrayIterator(['first', 'second']); |
170
|
|
|
|
171
|
|
|
$reader->shouldReceive('fetch') |
172
|
|
|
->andReturn($iterator); |
173
|
|
|
|
174
|
|
|
$writer->shouldReceive('insertOne') |
175
|
|
|
->with('first') |
176
|
|
|
->once(); |
177
|
|
|
$writer->shouldReceive('insertOne') |
178
|
|
|
->with('second') |
179
|
|
|
->once(); |
180
|
|
|
|
181
|
|
|
static::assertSame($target, $this->reFormatter->reFormat($file, $format)); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
View Code Duplication |
public function testCanReformatWithIntputAndOutputFiles() |
|
|
|
|
185
|
|
|
{ |
186
|
|
|
$file = m::mock(FileNodeInterface::class, FormatAwareInterface::class); |
187
|
|
|
$target = m::mock(LocalFileNodeInterface::class, FormatAwareInterface::class); |
188
|
|
|
|
189
|
|
|
$file->shouldReceive('exists') |
190
|
|
|
->andReturn(true); |
191
|
|
|
|
192
|
|
|
$reader = m::mock(FileReader::class); |
193
|
|
|
$this->builder->shouldReceive('build') |
194
|
|
|
->with(FileReader::class, $file, null, $this->parserFactory) |
195
|
|
|
->andReturn($reader); |
196
|
|
|
$writer = m::mock(FileWriter::class); |
197
|
|
|
$this->builder->shouldReceive('build') |
198
|
|
|
->with(FileWriter::Class, $target, null, $this->formatterFactory) |
199
|
|
|
->andReturn($writer); |
200
|
|
|
|
201
|
|
|
$iterator = new ArrayIterator(['first', 'second']); |
202
|
|
|
|
203
|
|
|
$reader->shouldReceive('fetch') |
204
|
|
|
->andReturn($iterator); |
205
|
|
|
|
206
|
|
|
$writer->shouldReceive('insertOne') |
207
|
|
|
->with('first') |
208
|
|
|
->once(); |
209
|
|
|
$writer->shouldReceive('insertOne') |
210
|
|
|
->with('second') |
211
|
|
|
->once(); |
212
|
|
|
|
213
|
|
|
static::assertSame($target, $this->reFormatter->reFormat($file, null, $target)); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
public function testInputAndOutputFormatsArePassedToReaderAndWriter() |
217
|
|
|
{ |
218
|
|
|
$input = m::mock(FileNodeInterface::class); |
219
|
|
|
$output = m::mock(FileNodeInterface::class); |
220
|
|
|
$inputFormat = m::mock(FormatInterface::class); |
221
|
|
|
$outputFormat = m::mock(FormatInterface::class); |
222
|
|
|
|
223
|
|
|
$input->shouldReceive('exists') |
224
|
|
|
->andReturn(true); |
225
|
|
|
|
226
|
|
|
$reader = m::mock(FileReader::class); |
227
|
|
|
$this->builder->shouldReceive('build') |
228
|
|
|
->with(FileReader::class, $input, $inputFormat, $this->parserFactory) |
229
|
|
|
->andReturn($reader); |
230
|
|
|
$writer = m::mock(FileWriter::class); |
231
|
|
|
$this->builder->shouldReceive('build') |
232
|
|
|
->with(FileWriter::Class, $output, $outputFormat, $this->formatterFactory) |
233
|
|
|
->andReturn($writer); |
234
|
|
|
|
235
|
|
|
$iterator = new ArrayIterator(['first', 'second']); |
236
|
|
|
|
237
|
|
|
$reader->shouldReceive('fetch') |
238
|
|
|
->andReturn($iterator); |
239
|
|
|
|
240
|
|
|
$writer->shouldReceive('insertOne') |
241
|
|
|
->with('first') |
242
|
|
|
->once(); |
243
|
|
|
$writer->shouldReceive('insertOne') |
244
|
|
|
->with('second') |
245
|
|
|
->once(); |
246
|
|
|
|
247
|
|
|
static::assertSame($output, $this->reFormatter->reFormat($input, $outputFormat, $output, $inputFormat)); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
public function testModifyWithOutOutputOrFormatSpecified() |
251
|
|
|
{ |
252
|
|
|
$file = m::mock(FileNodeInterface::class); |
253
|
|
|
|
254
|
|
|
static::expectException(InvalidArgumentException::class); |
255
|
|
|
|
256
|
|
|
$this->reFormatter->modify($file); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
View Code Duplication |
public function testModifyWithOutputFileSet() |
|
|
|
|
260
|
|
|
{ |
261
|
|
|
$file = m::mock(FileNodeInterface::class, FormatAwareInterface::class); |
262
|
|
|
$target = m::mock(LocalFileNodeInterface::class, FormatAwareInterface::class); |
263
|
|
|
|
264
|
|
|
$file->shouldReceive('exists') |
265
|
|
|
->andReturn(true); |
266
|
|
|
|
267
|
|
|
$reader = m::mock(FileReader::class); |
268
|
|
|
$this->builder->shouldReceive('build') |
269
|
|
|
->with(FileReader::class, $file, null, $this->parserFactory) |
270
|
|
|
->andReturn($reader); |
271
|
|
|
$writer = m::mock(FileWriter::class); |
272
|
|
|
$this->builder->shouldReceive('build') |
273
|
|
|
->with(FileWriter::Class, $target, null, $this->formatterFactory) |
274
|
|
|
->andReturn($writer); |
275
|
|
|
|
276
|
|
|
$iterator = new ArrayIterator(['first', 'second']); |
277
|
|
|
|
278
|
|
|
$reader->shouldReceive('fetch') |
279
|
|
|
->andReturn($iterator); |
280
|
|
|
|
281
|
|
|
$writer->shouldReceive('insertOne') |
282
|
|
|
->with('first') |
283
|
|
|
->once(); |
284
|
|
|
$writer->shouldReceive('insertOne') |
285
|
|
|
->with('second') |
286
|
|
|
->once(); |
287
|
|
|
|
288
|
|
|
static::assertSame($target, $this->reFormatter->modify($file, ['output' => $target])); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
View Code Duplication |
public function testModifyWithFormatOption() |
|
|
|
|
292
|
|
|
{ |
293
|
|
|
$file = m::mock(LocalFileNodeInterface::class, FormatAwareInterface::class); |
294
|
|
|
$target = m::mock(LocalFileNodeInterface::class, FormatAwareInterface::class); |
295
|
|
|
|
296
|
|
|
$file->shouldReceive('getPath') |
297
|
|
|
->andReturn('/tmp/file.txt'); |
298
|
|
|
$file->shouldReceive('exists') |
299
|
|
|
->andReturn(true); |
300
|
|
|
|
301
|
|
|
$file->shouldReceive('getClone') |
302
|
|
|
->andReturn($target); |
303
|
|
|
$target->shouldReceive('setPath') |
304
|
|
|
->with('/tmp/file-format.txt') |
305
|
|
|
->andReturn($target); |
306
|
|
|
|
307
|
|
|
$format = m::mock(FormatInterface::class); |
308
|
|
|
|
309
|
|
|
$target->shouldReceive('setFormat') |
310
|
|
|
->with($format) |
311
|
|
|
->andReturn($target); |
312
|
|
|
|
313
|
|
|
$reader = m::mock(FileReader::class); |
314
|
|
|
$this->builder->shouldReceive('build') |
315
|
|
|
->with(FileReader::class, $file, null, $this->parserFactory) |
316
|
|
|
->andReturn($reader); |
317
|
|
|
$writer = m::mock(FileWriter::class); |
318
|
|
|
$this->builder->shouldReceive('build') |
319
|
|
|
->with(FileWriter::Class, $target, $format, $this->formatterFactory) |
320
|
|
|
->andReturn($writer); |
321
|
|
|
|
322
|
|
|
$iterator = new ArrayIterator(['first', 'second']); |
323
|
|
|
|
324
|
|
|
$reader->shouldReceive('fetch') |
325
|
|
|
->andReturn($iterator); |
326
|
|
|
|
327
|
|
|
$writer->shouldReceive('insertOne') |
328
|
|
|
->with('first') |
329
|
|
|
->once(); |
330
|
|
|
$writer->shouldReceive('insertOne') |
331
|
|
|
->with('second') |
332
|
|
|
->once(); |
333
|
|
|
|
334
|
|
|
static::assertSame($target, $this->reFormatter->modify($file, ['format' => $format])); |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
public function testReFormatWithDeleteOldFile() |
338
|
|
|
{ |
339
|
|
|
$file = m::mock(FileNodeInterface::class, FormatAwareInterface::class); |
340
|
|
|
$target = m::mock(LocalFileNodeInterface::class, FormatAwareInterface::class); |
341
|
|
|
|
342
|
|
|
$reader = m::mock(FileReader::class); |
343
|
|
|
$this->builder->shouldReceive('build') |
344
|
|
|
->with(FileReader::class, $file, null, $this->parserFactory) |
345
|
|
|
->andReturn($reader); |
346
|
|
|
$writer = m::mock(FileWriter::class); |
347
|
|
|
$this->builder->shouldReceive('build') |
348
|
|
|
->with(FileWriter::Class, $target, null, $this->formatterFactory) |
349
|
|
|
->andReturn($writer); |
350
|
|
|
|
351
|
|
|
$iterator = new ArrayIterator(['first', 'second']); |
352
|
|
|
|
353
|
|
|
$reader->shouldReceive('fetch') |
354
|
|
|
->andReturn($iterator); |
355
|
|
|
|
356
|
|
|
$writer->shouldReceive('insertOne') |
357
|
|
|
->with('first') |
358
|
|
|
->once(); |
359
|
|
|
$writer->shouldReceive('insertOne') |
360
|
|
|
->with('second') |
361
|
|
|
->once(); |
362
|
|
|
|
363
|
|
|
$file->shouldReceive('exists') |
364
|
|
|
->andReturn(true); |
365
|
|
|
$file->shouldReceive('delete') |
366
|
|
|
->once(); |
367
|
|
|
|
368
|
|
|
static::assertSame($target, $this->reFormatter->reFormat($file, null, $target, null, ['keepOldFile' => false])); |
369
|
|
|
} |
370
|
|
|
} |
371
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.