Completed
Pull Request — master (#10)
by Harry
06:44
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 Mockery as m;
32
use Mockery\MockInterface;
33
34
class ReFormatTest extends TestCase
35
{
36
    /** @var ParserFactoryInterface|MockInterface */
37
    private $parserFactory;
38
    /** @var FormatterFactoryInterface|MockInterface */
39
    private $formatterFactory;
40
    /** @var ReFormat */
41
    private $reFormatter;
42
    /** @var BuilderInterface|MockInterface */
43
    private $builder;
44
45
    public function setUp()
46
    {
47
        parent::setUp();
48
49
        $this->builder = m::mock(BuilderInterface::class);
50
        $this->parserFactory = m::mock(ParserFactoryInterface::class);
51
        $this->formatterFactory = m::mock(FormatterFactoryInterface::class);
52
        $this->reFormatter = new ReFormat($this->formatterFactory, $this->parserFactory, $this->builder);
53
    }
54
55
    public function testInstanceOf()
56
    {
57
        static::assertInstanceOf(FileModifierInterface::class, $this->reFormatter);
58
    }
59
60 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...
61
    {
62
        $file = m::mock(FileNodeInterface::class, FormatAwareInterface::class);
63
        $file->shouldReceive('exists')
64
             ->andReturn(true);
65
66
        $format = m::mock(FormatInterface::class);
67
        $parser = m::mock(ParserInterface::class);
68
        $this->parserFactory->shouldReceive('getParser')
69
                            ->with($format)
70
                            ->andReturn($parser);
71
72
        $file->shouldReceive('getFormat')
73
             ->andReturn($format);
74
75
        static::assertTrue($this->reFormatter->canModify($file));
76
    }
77
78 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...
79
    {
80
        $file = m::mock(FileNodeInterface::class, FormatAwareInterface::class);
81
        $file->shouldReceive('exists')
82
             ->andReturn(true);
83
84
        $format = m::mock(FormatInterface::class);
85
        $this->parserFactory->shouldReceive('getParser')
86
                            ->with($format)
87
                            ->andReturn(null);
88
89
        $file->shouldReceive('getFormat')
90
             ->andReturn($format);
91
92
        static::assertFalse($this->reFormatter->canModify($file));
93
    }
94
95
    public function testCanReFormatWithANonLocalInputFileAndOutputFormat()
96
    {
97
        $file = m::mock(FileNodeInterface::class, FormatAwareInterface::class);
98
        $target = m::mock(LocalFile::class);
99
100
        $this->builder->shouldReceive('build')
101
                      ->with(LocalFile::class, m::type('string'))
102
                      ->andReturn($target);
103
104
        $format = m::mock(FormatInterface::class);
105
106
        $target->shouldReceive('setFormat')
107
               ->with($format)
108
               ->andReturn($target);
109
110
        $reader = m::mock(FileReader::class);
111
        $this->builder->shouldReceive('build')
112
                      ->with(FileReader::class, $file, null, $this->parserFactory)
113
                      ->andReturn($reader);
114
        $writer = m::mock(FileWriter::class);
115
        $this->builder->shouldReceive('build')
116
                      ->with(FileWriter::Class, $target, $format, $this->formatterFactory)
117
                      ->andReturn($writer);
118
119
        $iterator = new ArrayIterator(['first', 'second']);
120
121
        $reader->shouldReceive('fetch')
122
               ->andReturn($iterator);
123
124
        $writer->shouldReceive('insertOne')
125
               ->with('first')
126
               ->once();
127
        $writer->shouldReceive('insertOne')
128
               ->with('second')
129
               ->once();
130
131
        static::assertSame($target, $this->reFormatter->reFormat($file, $format));
132
    }
133
134
    public function testCanReFormatALocalInputFile()
135
    {
136
        $file = m::mock(LocalFileNodeInterface::class, FormatAwareInterface::class);
137
        $target = m::mock(LocalFileNodeInterface::class, FormatAwareInterface::class);
138
139
        $file->shouldReceive('getPath')
140
             ->andReturn('/tmp/file.txt');
141
142
        $file->shouldReceive('getClone')
143
             ->andReturn($target);
144
        $target->shouldReceive('setPath')
145
               ->with('/tmp/file-format.txt')
146
               ->andReturn($target);
147
148
        $format = m::mock(FormatInterface::class);
149
150
        $target->shouldReceive('setFormat')
151
               ->with($format)
152
               ->andReturn($target);
153
154
        $reader = m::mock(FileReader::class);
155
        $this->builder->shouldReceive('build')
156
                      ->with(FileReader::class, $file, null, $this->parserFactory)
157
                      ->andReturn($reader);
158
        $writer = m::mock(FileWriter::class);
159
        $this->builder->shouldReceive('build')
160
                      ->with(FileWriter::Class, $target, $format, $this->formatterFactory)
161
                      ->andReturn($writer);
162
163
        $iterator = new ArrayIterator(['first', 'second']);
164
165
        $reader->shouldReceive('fetch')
166
               ->andReturn($iterator);
167
168
        $writer->shouldReceive('insertOne')
169
               ->with('first')
170
               ->once();
171
        $writer->shouldReceive('insertOne')
172
               ->with('second')
173
               ->once();
174
175
        static::assertSame($target, $this->reFormatter->reFormat($file, $format));
176
    }
177
178
    public function testCanReformatWithIntputAndOutputFiles()
179
    {
180
        $file = m::mock(FileNodeInterface::class, FormatAwareInterface::class);
181
        $target = m::mock(LocalFileNodeInterface::class, FormatAwareInterface::class);
182
183
        $reader = m::mock(FileReader::class);
184
        $this->builder->shouldReceive('build')
185
                      ->with(FileReader::class, $file, null, $this->parserFactory)
186
                      ->andReturn($reader);
187
        $writer = m::mock(FileWriter::class);
188
        $this->builder->shouldReceive('build')
189
                      ->with(FileWriter::Class, $target, null, $this->formatterFactory)
190
                      ->andReturn($writer);
191
192
        $iterator = new ArrayIterator(['first', 'second']);
193
194
        $reader->shouldReceive('fetch')
195
               ->andReturn($iterator);
196
197
        $writer->shouldReceive('insertOne')
198
               ->with('first')
199
               ->once();
200
        $writer->shouldReceive('insertOne')
201
               ->with('second')
202
               ->once();
203
204
        static::assertSame($target, $this->reFormatter->reFormat($file, null, $target));
205
    }
206
207
    public function testInputAndOutputFormatsArePassedToReaderAndWriter()
208
    {
209
        $input = m::mock(FileNodeInterface::class);
210
        $output = m::mock(FileNodeInterface::class);
211
        $inputFormat = m::mock(FormatInterface::class);
212
        $outputFormat = m::mock(FormatInterface::class);
213
214
        $reader = m::mock(FileReader::class);
215
        $this->builder->shouldReceive('build')
216
                      ->with(FileReader::class, $input, $inputFormat, $this->parserFactory)
217
                      ->andReturn($reader);
218
        $writer = m::mock(FileWriter::class);
219
        $this->builder->shouldReceive('build')
220
                      ->with(FileWriter::Class, $output, $outputFormat, $this->formatterFactory)
221
                      ->andReturn($writer);
222
223
        $iterator = new ArrayIterator(['first', 'second']);
224
225
        $reader->shouldReceive('fetch')
226
               ->andReturn($iterator);
227
228
        $writer->shouldReceive('insertOne')
229
               ->with('first')
230
               ->once();
231
        $writer->shouldReceive('insertOne')
232
               ->with('second')
233
               ->once();
234
235
        static::assertSame($output, $this->reFormatter->reFormat($input, $outputFormat, $output, $inputFormat));
236
    }
237
}
238