testCanReFormatWithANonLocalInputFileAndOutputFormat()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 38
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 38
c 0
b 0
f 0
rs 8.8571
cc 1
eloc 27
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')
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in Graze\DataFile\Format\Pa...\ParserFactoryInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
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')
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in Graze\DataFile\Format\Pa...\ParserFactoryInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
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')
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in Graze\DataFile\Helper\Builder\BuilderInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
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('insertAll')
129
               ->with($iterator)
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
        $file->shouldReceive('exists')
143
             ->andReturn(true);
144
145
        $file->shouldReceive('getClone')
146
             ->andReturn($target);
147
        $target->shouldReceive('setPath')
148
               ->with('/tmp/file-format.txt')
149
               ->andReturn($target);
150
151
        $format = m::mock(FormatInterface::class);
152
153
        $target->shouldReceive('setFormat')
154
               ->with($format)
155
               ->andReturn($target);
156
157
        $reader = m::mock(FileReader::class);
158
        $this->builder->shouldReceive('build')
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in Graze\DataFile\Helper\Builder\BuilderInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
159
                      ->with(FileReader::class, $file, null, $this->parserFactory)
160
                      ->andReturn($reader);
161
        $writer = m::mock(FileWriter::class);
162
        $this->builder->shouldReceive('build')
163
                      ->with(FileWriter::Class, $target, $format, $this->formatterFactory)
164
                      ->andReturn($writer);
165
166
        $iterator = new ArrayIterator(['first', 'second']);
167
168
        $reader->shouldReceive('fetch')
169
               ->andReturn($iterator);
170
171
        $writer->shouldReceive('insertAll')
172
               ->with($iterator)
173
               ->once();
174
175
        static::assertSame($target, $this->reFormatter->reFormat($file, $format));
176
    }
177
178 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...
179
    {
180
        $file = m::mock(FileNodeInterface::class, FormatAwareInterface::class);
181
        $target = m::mock(LocalFileNodeInterface::class, FormatAwareInterface::class);
182
183
        $file->shouldReceive('exists')
184
             ->andReturn(true);
185
186
        $reader = m::mock(FileReader::class);
187
        $this->builder->shouldReceive('build')
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in Graze\DataFile\Helper\Builder\BuilderInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
188
                      ->with(FileReader::class, $file, null, $this->parserFactory)
189
                      ->andReturn($reader);
190
        $writer = m::mock(FileWriter::class);
191
        $this->builder->shouldReceive('build')
192
                      ->with(FileWriter::Class, $target, null, $this->formatterFactory)
193
                      ->andReturn($writer);
194
195
        $iterator = new ArrayIterator(['first', 'second']);
196
197
        $reader->shouldReceive('fetch')
198
               ->andReturn($iterator);
199
200
        $writer->shouldReceive('insertAll')
201
               ->with($iterator)
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
        $input->shouldReceive('exists')
215
              ->andReturn(true);
216
217
        $reader = m::mock(FileReader::class);
218
        $this->builder->shouldReceive('build')
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in Graze\DataFile\Helper\Builder\BuilderInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
219
                      ->with(FileReader::class, $input, $inputFormat, $this->parserFactory)
220
                      ->andReturn($reader);
221
        $writer = m::mock(FileWriter::class);
222
        $this->builder->shouldReceive('build')
223
                      ->with(FileWriter::Class, $output, $outputFormat, $this->formatterFactory)
224
                      ->andReturn($writer);
225
226
        $iterator = new ArrayIterator(['first', 'second']);
227
228
        $reader->shouldReceive('fetch')
229
               ->andReturn($iterator);
230
231
        $writer->shouldReceive('insertAll')
232
               ->with($iterator)
233
               ->once();
234
235
        static::assertSame($output, $this->reFormatter->reFormat($input, $outputFormat, $output, $inputFormat));
236
    }
237
238
    public function testModifyWithOutOutputOrFormatSpecified()
239
    {
240
        $file = m::mock(FileNodeInterface::class);
241
242
        static::expectException(InvalidArgumentException::class);
243
244
        $this->reFormatter->modify($file);
245
    }
246
247 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...
248
    {
249
        $file = m::mock(FileNodeInterface::class, FormatAwareInterface::class);
250
        $target = m::mock(LocalFileNodeInterface::class, FormatAwareInterface::class);
251
252
        $file->shouldReceive('exists')
253
             ->andReturn(true);
254
255
        $reader = m::mock(FileReader::class);
256
        $this->builder->shouldReceive('build')
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in Graze\DataFile\Helper\Builder\BuilderInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
257
                      ->with(FileReader::class, $file, null, $this->parserFactory)
258
                      ->andReturn($reader);
259
        $writer = m::mock(FileWriter::class);
260
        $this->builder->shouldReceive('build')
261
                      ->with(FileWriter::Class, $target, null, $this->formatterFactory)
262
                      ->andReturn($writer);
263
264
        $iterator = new ArrayIterator(['first', 'second']);
265
266
        $reader->shouldReceive('fetch')
267
               ->andReturn($iterator);
268
269
        $writer->shouldReceive('insertAll')
270
               ->with($iterator)
271
               ->once();
272
273
        static::assertSame($target, $this->reFormatter->modify($file, ['output' => $target]));
274
    }
275
276 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...
277
    {
278
        $file = m::mock(LocalFileNodeInterface::class, FormatAwareInterface::class);
279
        $target = m::mock(LocalFileNodeInterface::class, FormatAwareInterface::class);
280
281
        $file->shouldReceive('getPath')
282
             ->andReturn('/tmp/file.txt');
283
        $file->shouldReceive('exists')
284
             ->andReturn(true);
285
286
        $file->shouldReceive('getClone')
287
             ->andReturn($target);
288
        $target->shouldReceive('setPath')
289
               ->with('/tmp/file-format.txt')
290
               ->andReturn($target);
291
292
        $format = m::mock(FormatInterface::class);
293
294
        $target->shouldReceive('setFormat')
295
               ->with($format)
296
               ->andReturn($target);
297
298
        $reader = m::mock(FileReader::class);
299
        $this->builder->shouldReceive('build')
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in Graze\DataFile\Helper\Builder\BuilderInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
300
                      ->with(FileReader::class, $file, null, $this->parserFactory)
301
                      ->andReturn($reader);
302
        $writer = m::mock(FileWriter::class);
303
        $this->builder->shouldReceive('build')
304
                      ->with(FileWriter::Class, $target, $format, $this->formatterFactory)
305
                      ->andReturn($writer);
306
307
        $iterator = new ArrayIterator(['first', 'second']);
308
309
        $reader->shouldReceive('fetch')
310
               ->andReturn($iterator);
311
312
        $writer->shouldReceive('insertAll')
313
               ->with($iterator)
314
               ->once();
315
316
        static::assertSame($target, $this->reFormatter->modify($file, ['format' => $format]));
317
    }
318
319
    public function testReFormatWithDeleteOldFile()
320
    {
321
        $file = m::mock(FileNodeInterface::class, FormatAwareInterface::class);
322
        $target = m::mock(LocalFileNodeInterface::class, FormatAwareInterface::class);
323
324
        $reader = m::mock(FileReader::class);
325
        $this->builder->shouldReceive('build')
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in Graze\DataFile\Helper\Builder\BuilderInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
326
                      ->with(FileReader::class, $file, null, $this->parserFactory)
327
                      ->andReturn($reader);
328
        $writer = m::mock(FileWriter::class);
329
        $this->builder->shouldReceive('build')
330
                      ->with(FileWriter::Class, $target, null, $this->formatterFactory)
331
                      ->andReturn($writer);
332
333
        $iterator = new ArrayIterator(['first', 'second']);
334
335
        $reader->shouldReceive('fetch')
336
               ->andReturn($iterator);
337
338
        $writer->shouldReceive('insertAll')
339
               ->with($iterator)
340
               ->once();
341
342
        $file->shouldReceive('exists')
343
             ->andReturn(true);
344
        $file->shouldReceive('delete')
345
             ->once();
346
347
        static::assertSame($target, $this->reFormatter->reFormat($file, null, $target, null, ['keepOldFile' => false]));
348
    }
349
}
350