Completed
Push — master ( 461219...630231 )
by Harry
04:03
created

testCanModifyAcceptsFileNodeInterfaceWithAFormat()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 12

Duplication

Lines 17
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 17
loc 17
rs 9.4285
cc 1
eloc 12
nc 1
nop 0
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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
        $this->builder->shouldReceive('build')
102
                      ->with(LocalFile::class, m::type('string'))
103
                      ->andReturn($target);
104
105
        $format = m::mock(FormatInterface::class);
106
107
        $target->shouldReceive('setFormat')
108
               ->with($format)
109
               ->andReturn($target);
110
111
        $reader = m::mock(FileReader::class);
112
        $this->builder->shouldReceive('build')
113
                      ->with(FileReader::class, $file, null, $this->parserFactory)
114
                      ->andReturn($reader);
115
        $writer = m::mock(FileWriter::class);
116
        $this->builder->shouldReceive('build')
117
                      ->with(FileWriter::Class, $target, $format, $this->formatterFactory)
118
                      ->andReturn($writer);
119
120
        $iterator = new ArrayIterator(['first', 'second']);
121
122
        $reader->shouldReceive('fetch')
123
               ->andReturn($iterator);
124
125
        $writer->shouldReceive('insertOne')
126
               ->with('first')
127
               ->once();
128
        $writer->shouldReceive('insertOne')
129
               ->with('second')
130
               ->once();
131
132
        static::assertSame($target, $this->reFormatter->reFormat($file, $format));
133
    }
134
135 View Code Duplication
    public function testCanReFormatALocalInputFile()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
136
    {
137
        $file = m::mock(LocalFileNodeInterface::class, FormatAwareInterface::class);
138
        $target = m::mock(LocalFileNodeInterface::class, FormatAwareInterface::class);
139
140
        $file->shouldReceive('getPath')
141
             ->andReturn('/tmp/file.txt');
142
143
        $file->shouldReceive('getClone')
144
             ->andReturn($target);
145
        $target->shouldReceive('setPath')
146
               ->with('/tmp/file-format.txt')
147
               ->andReturn($target);
148
149
        $format = m::mock(FormatInterface::class);
150
151
        $target->shouldReceive('setFormat')
152
               ->with($format)
153
               ->andReturn($target);
154
155
        $reader = m::mock(FileReader::class);
156
        $this->builder->shouldReceive('build')
157
                      ->with(FileReader::class, $file, null, $this->parserFactory)
158
                      ->andReturn($reader);
159
        $writer = m::mock(FileWriter::class);
160
        $this->builder->shouldReceive('build')
161
                      ->with(FileWriter::Class, $target, $format, $this->formatterFactory)
162
                      ->andReturn($writer);
163
164
        $iterator = new ArrayIterator(['first', 'second']);
165
166
        $reader->shouldReceive('fetch')
167
               ->andReturn($iterator);
168
169
        $writer->shouldReceive('insertOne')
170
               ->with('first')
171
               ->once();
172
        $writer->shouldReceive('insertOne')
173
               ->with('second')
174
               ->once();
175
176
        static::assertSame($target, $this->reFormatter->reFormat($file, $format));
177
    }
178
179 View Code Duplication
    public function testCanReformatWithIntputAndOutputFiles()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
180
    {
181
        $file = m::mock(FileNodeInterface::class, FormatAwareInterface::class);
182
        $target = m::mock(LocalFileNodeInterface::class, FormatAwareInterface::class);
183
184
        $reader = m::mock(FileReader::class);
185
        $this->builder->shouldReceive('build')
186
                      ->with(FileReader::class, $file, null, $this->parserFactory)
187
                      ->andReturn($reader);
188
        $writer = m::mock(FileWriter::class);
189
        $this->builder->shouldReceive('build')
190
                      ->with(FileWriter::Class, $target, null, $this->formatterFactory)
191
                      ->andReturn($writer);
192
193
        $iterator = new ArrayIterator(['first', 'second']);
194
195
        $reader->shouldReceive('fetch')
196
               ->andReturn($iterator);
197
198
        $writer->shouldReceive('insertOne')
199
               ->with('first')
200
               ->once();
201
        $writer->shouldReceive('insertOne')
202
               ->with('second')
203
               ->once();
204
205
        static::assertSame($target, $this->reFormatter->reFormat($file, null, $target));
206
    }
207
208
    public function testInputAndOutputFormatsArePassedToReaderAndWriter()
209
    {
210
        $input = m::mock(FileNodeInterface::class);
211
        $output = m::mock(FileNodeInterface::class);
212
        $inputFormat = m::mock(FormatInterface::class);
213
        $outputFormat = m::mock(FormatInterface::class);
214
215
        $reader = m::mock(FileReader::class);
216
        $this->builder->shouldReceive('build')
217
                      ->with(FileReader::class, $input, $inputFormat, $this->parserFactory)
218
                      ->andReturn($reader);
219
        $writer = m::mock(FileWriter::class);
220
        $this->builder->shouldReceive('build')
221
                      ->with(FileWriter::Class, $output, $outputFormat, $this->formatterFactory)
222
                      ->andReturn($writer);
223
224
        $iterator = new ArrayIterator(['first', 'second']);
225
226
        $reader->shouldReceive('fetch')
227
               ->andReturn($iterator);
228
229
        $writer->shouldReceive('insertOne')
230
               ->with('first')
231
               ->once();
232
        $writer->shouldReceive('insertOne')
233
               ->with('second')
234
               ->once();
235
236
        static::assertSame($output, $this->reFormatter->reFormat($input, $outputFormat, $output, $inputFormat));
237
    }
238
239
    public function testModifyWithOutOutputOrFormatSpecified()
240
    {
241
        $file = m::mock(FileNodeInterface::class);
242
243
        static::expectException(InvalidArgumentException::class);
244
245
        $this->reFormatter->modify($file);
246
    }
247
248 View Code Duplication
    public function testModifyWithOutputFileSet()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
249
    {
250
        $file = m::mock(FileNodeInterface::class, FormatAwareInterface::class);
251
        $target = m::mock(LocalFileNodeInterface::class, FormatAwareInterface::class);
252
253
        $reader = m::mock(FileReader::class);
254
        $this->builder->shouldReceive('build')
255
                      ->with(FileReader::class, $file, null, $this->parserFactory)
256
                      ->andReturn($reader);
257
        $writer = m::mock(FileWriter::class);
258
        $this->builder->shouldReceive('build')
259
                      ->with(FileWriter::Class, $target, null, $this->formatterFactory)
260
                      ->andReturn($writer);
261
262
        $iterator = new ArrayIterator(['first', 'second']);
263
264
        $reader->shouldReceive('fetch')
265
               ->andReturn($iterator);
266
267
        $writer->shouldReceive('insertOne')
268
               ->with('first')
269
               ->once();
270
        $writer->shouldReceive('insertOne')
271
               ->with('second')
272
               ->once();
273
274
        static::assertSame($target, $this->reFormatter->modify($file, ['output' => $target]));
275
    }
276
277 View Code Duplication
    public function testModifyWithFormatOption()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
278
    {
279
        $file = m::mock(LocalFileNodeInterface::class, FormatAwareInterface::class);
280
        $target = m::mock(LocalFileNodeInterface::class, FormatAwareInterface::class);
281
282
        $file->shouldReceive('getPath')
283
             ->andReturn('/tmp/file.txt');
284
285
        $file->shouldReceive('getClone')
286
             ->andReturn($target);
287
        $target->shouldReceive('setPath')
288
               ->with('/tmp/file-format.txt')
289
               ->andReturn($target);
290
291
        $format = m::mock(FormatInterface::class);
292
293
        $target->shouldReceive('setFormat')
294
               ->with($format)
295
               ->andReturn($target);
296
297
        $reader = m::mock(FileReader::class);
298
        $this->builder->shouldReceive('build')
299
                      ->with(FileReader::class, $file, null, $this->parserFactory)
300
                      ->andReturn($reader);
301
        $writer = m::mock(FileWriter::class);
302
        $this->builder->shouldReceive('build')
303
                      ->with(FileWriter::Class, $target, $format, $this->formatterFactory)
304
                      ->andReturn($writer);
305
306
        $iterator = new ArrayIterator(['first', 'second']);
307
308
        $reader->shouldReceive('fetch')
309
               ->andReturn($iterator);
310
311
        $writer->shouldReceive('insertOne')
312
               ->with('first')
313
               ->once();
314
        $writer->shouldReceive('insertOne')
315
               ->with('second')
316
               ->once();
317
318
        static::assertSame($target, $this->reFormatter->modify($file, ['format' => $format]));
319
    }
320
}
321