Completed
Push — master ( 85851c...4b020b )
by Harry
03:30
created

FileWriterTest::getStream()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
rs 9.4285
cc 1
eloc 5
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\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 GuzzleHttp\Psr7\Stream;
27
use InvalidArgumentException;
28
use Mockery as m;
29
use Psr\Http\Message\StreamInterface;
30
31
class FileWriterTest extends TestCase
32
{
33
    public function testNodeStreamFileWillGetAStream()
34
    {
35
        $stream = m::mock(StreamInterface::class);
36
37
        $file = m::mock(FileNodeInterface::class, NodeStreamInterface::class);
38
        $file->shouldReceive('getStream')
39
             ->with('a+')
40
             ->andReturn($stream);
41
42
        $format = m::mock(CsvFormat::class)
43
                   ->makePartial();
44
45
        $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...
46
47
        static::assertInstanceOf(WriterInterface::class, $writer);
48
    }
49
50
    public function testFileNodeWillThrowAnException()
51
    {
52
        $file = m::mock(FileNodeInterface::class);
53
54
        static::expectException(InvalidArgumentException::class);
55
        new FileWriter($file);
56
    }
57
58 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...
59
    {
60
        $stream = m::mock(StreamInterface::class);
61
62
        $file = m::mock(FileNodeInterface::class, NodeStreamInterface::class);
63
        $file->shouldReceive('getStream')
64
             ->with('a+')
65
             ->andReturn($stream);
66
67
        static::expectException(InvalidArgumentException::class);
68
        new FileWriter($file);
69
    }
70
71 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...
72
    {
73
        $stream = m::mock(StreamInterface::class);
74
75
        $file = m::mock(FileNodeInterface::class, NodeStreamInterface::class, FormatAwareInterface::class);
76
        $file->shouldReceive('getStream')
77
             ->with('a+')
78
             ->andReturn($stream);
79
80
        $format = m::mock(CsvFormat::class)
81
                   ->makePartial();
82
83
        $file->shouldReceive('getFormat')
84
             ->andReturn($format);
85
86
        $writer = new FileWriter($file);
87
88
        static::assertInstanceOf(WriterInterface::class, $writer);
89
    }
90
91
    public function testProvidingAParserFactoryWillUseTheFactory()
92
    {
93
        $stream = m::mock(StreamInterface::class);
94
95
        $file = m::mock(FileNodeInterface::class, NodeStreamInterface::class);
96
        $file->shouldReceive('getStream')
97
             ->with('a+')
98
             ->andReturn($stream);
99
100
        $format = m::mock(JsonFormat::class)
101
                   ->makePartial();
102
        $factory = m::mock(FormatterFactoryInterface::class);
103
        $formatter = m::mock(FormatterInterface::class);
104
        $factory->shouldReceive('getFormatter')
105
                ->with($format)
106
                ->andReturn($formatter);
107
108
        $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...
109
110
        static::assertInstanceOf(WriterInterface::class, $writer);
111
    }
112
113
    /**
114
     * @return Stream
0 ignored issues
show
Documentation introduced by
Should the return type not be m\Mock?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
115
     */
116
    private function getStream()
117
    {
118
        $stream = m::mock(new Stream(fopen('php://temp', 'r+')))->makePartial();
119
        $stream->shouldReceive('close')
120
               ->andReturnNull();
121
        return $stream;
122
    }
123
124 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...
125
    {
126
        $data = [
127
            ['a', 'b', 'c', 'd'],
128
            ['e', 'f', 'g', 'h'],
129
        ];
130
131
        $stream = $this->getStream();
132
133
        $file = m::mock(FileNodeInterface::class, NodeStreamInterface::class);
134
        $file->shouldReceive('getStream')
135
             ->with('a+')
136
             ->andReturn($stream);
137
138
        $format = m::mock(CsvFormat::class)->makePartial();
139
        $formatter = m::mock(FormatterInterface::class);
140
        $factory = m::mock(FormatterFactoryInterface::class);
141
        $factory->shouldReceive('getFormatter')
142
                ->with($format)
143
                ->andReturn($formatter);
144
        $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...
145
146
        $formatter->shouldReceive('format')
147
                  ->with(['a', 'b', 'c', 'd'])
148
                  ->andReturn('first line');
149
        $formatter->shouldReceive('format')
150
                  ->with(['e', 'f', 'g', 'h'])
151
                  ->andReturn('second line');
152
        $formatter->shouldReceive('getInitialBlock')
153
                  ->andReturn('initial' . "\n");
154
        $formatter->shouldReceive('getClosingBlock')
155
                  ->andReturn("\n" . 'close');
156
        $formatter->shouldReceive('getRowSeparator')
157
                  ->andReturn("\n");
158
159
        $expected = <<<CSV
160
initial
161
first line
162
second line
163
close
164
CSV;
165
166
        $writer->insertAll($data);
167
168
        $stream->rewind();
169
        static::assertEquals($expected, $stream->getContents());
170
    }
171
172 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...
173
    {
174
        $stream = $this->getStream();
175
176
        $file = m::mock(FileNodeInterface::class, NodeStreamInterface::class);
177
        $file->shouldReceive('getStream')
178
             ->with('a+')
179
             ->andReturn($stream);
180
181
        $format = m::mock(CsvFormat::class)->makePartial();
182
        $formatter = m::mock(FormatterInterface::class);
183
        $factory = m::mock(FormatterFactoryInterface::class);
184
        $factory->shouldReceive('getFormatter')
185
                ->with($format)
186
                ->andReturn($formatter);
187
        $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...
188
189
        $formatter->shouldReceive('format')
190
                  ->with(['a', 'b', 'c', 'd'])
191
                  ->andReturn('first line');
192
        $formatter->shouldReceive('format')
193
                  ->with(['e', 'f', 'g', 'h'])
194
                  ->andReturn('second line');
195
        $formatter->shouldReceive('getInitialBlock')
196
                  ->andReturn('initial' . "\n");
197
        $formatter->shouldReceive('getClosingBlock')
198
                  ->andReturn("\n" . 'close');
199
        $formatter->shouldReceive('getRowSeparator')
200
                  ->andReturn("\n");
201
202
        $expected = <<<CSV
203
initial
204
first line
205
second line
206
close
207
CSV;
208
209
        $writer->insertOne(['a', 'b', 'c', 'd']);
210
        $writer->insertOne(['e', 'f', 'g', 'h']);
211
212
        $stream->rewind();
213
        static::assertEquals($expected, $stream->getContents());
214
    }
215
}
216