FileWriterTest   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 182
Duplicated Lines 75.27 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 5
dl 137
loc 182
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A testNodeStreamFileWillGetAStream() 16 16 1
A testFileNodeWillThrowAnException() 0 7 1
A testNodeWithNoFormatAndNoFormatSpecifiedWillThrowAnException() 12 12 1
A testNodeWithFormatWillUseThatFormat() 19 19 1
A testProvidingAParserFactoryWillUseTheFactory() 0 21 1
A getStream() 0 4 1
A testInsertAll() 47 47 1
B testInsertOne() 43 43 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\IO;
15
16
use Graze\DataFile\Format\CsvFormat;
17
use Graze\DataFile\Format\FormatAwareInterface;
18
use Graze\DataFile\Format\Formatter\FormatterFactoryInterface;
19
use Graze\DataFile\Format\Formatter\FormatterInterface;
20
use Graze\DataFile\Format\JsonFormat;
21
use Graze\DataFile\IO\FileWriter;
22
use Graze\DataFile\IO\WriterInterface;
23
use Graze\DataFile\Node\FileNodeInterface;
24
use Graze\DataFile\Node\NodeStreamInterface;
25
use Graze\DataFile\Test\TestCase;
26
use InvalidArgumentException;
27
use Mockery as m;
28
29
class FileWriterTest extends TestCase
30
{
31 View Code Duplication
    public function testNodeStreamFileWillGetAStream()
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...
32
    {
33
        $stream = $this->getStream();
34
35
        $file = m::mock(FileNodeInterface::class, NodeStreamInterface::class);
36
        $file->shouldReceive('getStream')
37
             ->with('c+b')
38
             ->andReturn($stream);
39
40
        $format = m::mock(CsvFormat::class)
41
                   ->makePartial();
42
43
        $writer = new FileWriter($file, $format);
0 ignored issues
show
Documentation introduced by
$format is of type object<Mockery\Mock>, but the function expects a null|object<Graze\DataFi...Format\FormatInterface>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
44
45
        static::assertInstanceOf(WriterInterface::class, $writer);
46
    }
47
48
    public function testFileNodeWillThrowAnException()
49
    {
50
        $file = m::mock(FileNodeInterface::class);
51
52
        static::expectException(InvalidArgumentException::class);
53
        new FileWriter($file);
54
    }
55
56 View Code Duplication
    public function testNodeWithNoFormatAndNoFormatSpecifiedWillThrowAnException()
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...
57
    {
58
        $stream = $this->getStream();
59
60
        $file = m::mock(FileNodeInterface::class, NodeStreamInterface::class);
61
        $file->shouldReceive('getStream')
62
             ->with('c+b')
63
             ->andReturn($stream);
64
65
        static::expectException(InvalidArgumentException::class);
66
        new FileWriter($file);
67
    }
68
69 View Code Duplication
    public function testNodeWithFormatWillUseThatFormat()
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...
70
    {
71
        $stream = $this->getStream();
72
73
        $file = m::mock(FileNodeInterface::class, NodeStreamInterface::class, FormatAwareInterface::class);
74
        $file->shouldReceive('getStream')
75
             ->with('c+b')
76
             ->andReturn($stream);
77
78
        $format = m::mock(CsvFormat::class)
79
                   ->makePartial();
80
81
        $file->shouldReceive('getFormat')
82
             ->andReturn($format);
83
84
        $writer = new FileWriter($file);
85
86
        static::assertInstanceOf(WriterInterface::class, $writer);
87
    }
88
89
    public function testProvidingAParserFactoryWillUseTheFactory()
90
    {
91
        $stream = $this->getStream();
92
93
        $file = m::mock(FileNodeInterface::class, NodeStreamInterface::class);
94
        $file->shouldReceive('getStream')
95
             ->with('c+b')
96
             ->andReturn($stream);
97
98
        $format = m::mock(JsonFormat::class)
99
                   ->makePartial();
100
        $factory = m::mock(FormatterFactoryInterface::class);
101
        $formatter = m::mock(FormatterInterface::class);
102
        $factory->shouldReceive('getFormatter')
103
                ->with($format)
104
                ->andReturn($formatter);
105
106
        $writer = new FileWriter($file, $format, $factory);
0 ignored issues
show
Documentation introduced by
$format is of type object<Mockery\Mock>, but the function expects a null|object<Graze\DataFi...Format\FormatInterface>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
107
108
        static::assertInstanceOf(WriterInterface::class, $writer);
109
    }
110
111
    /**
112
     * @return resource
113
     */
114
    private function getStream()
115
    {
116
        return fopen('php://temp', 'c+b');
117
    }
118
119 View Code Duplication
    public function testInsertAll()
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...
120
    {
121
        $data = [
122
            ['a', 'b', 'c', 'd'],
123
            ['e', 'f', 'g', 'h'],
124
        ];
125
126
        $stream = $this->getStream();
127
128
        $file = m::mock(FileNodeInterface::class, NodeStreamInterface::class);
129
        $file->shouldReceive('getStream')
130
             ->with('c+b')
131
             ->andReturn($stream);
132
133
        $format = m::mock(CsvFormat::class)->makePartial();
134
        $formatter = m::mock(FormatterInterface::class);
135
        $factory = m::mock(FormatterFactoryInterface::class);
136
        $factory->shouldReceive('getFormatter')
137
                ->with($format)
138
                ->andReturn($formatter);
139
        $writer = new FileWriter($file, $format, $factory);
0 ignored issues
show
Documentation introduced by
$format is of type object<Mockery\Mock>, but the function expects a null|object<Graze\DataFi...Format\FormatInterface>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
140
141
        $formatter->shouldReceive('format')
142
                  ->with(['a', 'b', 'c', 'd'])
143
                  ->andReturn('first line');
144
        $formatter->shouldReceive('format')
145
                  ->with(['e', 'f', 'g', 'h'])
146
                  ->andReturn('second line');
147
        $formatter->shouldReceive('getInitialBlock')
148
                  ->andReturn('initial' . "\n");
149
        $formatter->shouldReceive('getClosingBlock')
150
                  ->andReturn("\n" . 'close');
151
        $formatter->shouldReceive('getRowSeparator')
152
                  ->andReturn("\n");
153
154
        $expected = <<<CSV
155
initial
156
first line
157
second line
158
close
159
CSV;
160
161
        $writer->insertAll($data);
162
163
        fseek($stream, 0, SEEK_SET);
164
        static::assertEquals($expected, stream_get_contents($stream));
165
    }
166
167 View Code Duplication
    public function testInsertOne()
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...
168
    {
169
        $stream = $this->getStream();
170
171
        $file = m::mock(FileNodeInterface::class, NodeStreamInterface::class);
172
        $file->shouldReceive('getStream')
173
             ->with('c+b')
174
             ->andReturn($stream);
175
176
        $format = m::mock(CsvFormat::class)->makePartial();
177
        $formatter = m::mock(FormatterInterface::class);
178
        $factory = m::mock(FormatterFactoryInterface::class);
179
        $factory->shouldReceive('getFormatter')
180
                ->with($format)
181
                ->andReturn($formatter);
182
        $writer = new FileWriter($file, $format, $factory);
0 ignored issues
show
Documentation introduced by
$format is of type object<Mockery\Mock>, but the function expects a null|object<Graze\DataFi...Format\FormatInterface>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
183
184
        $formatter->shouldReceive('format')
185
                  ->with(['a', 'b', 'c', 'd'])
186
                  ->andReturn('first line');
187
        $formatter->shouldReceive('format')
188
                  ->with(['e', 'f', 'g', 'h'])
189
                  ->andReturn('second line');
190
        $formatter->shouldReceive('getInitialBlock')
191
                  ->andReturn('initial' . "\n");
192
        $formatter->shouldReceive('getClosingBlock')
193
                  ->andReturn("\n" . 'close');
194
        $formatter->shouldReceive('getRowSeparator')
195
                  ->andReturn("\n");
196
197
        $expected = <<<CSV
198
initial
199
first line
200
second line
201
close
202
CSV;
203
204
        $writer->insert(['a', 'b', 'c', 'd']);
205
        $writer->insert(['e', 'f', 'g', 'h']);
206
207
        fseek($stream, 0, SEEK_SET);
208
        static::assertEquals($expected, stream_get_contents($stream));
209
    }
210
}
211