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); |
|
|
|
|
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() |
|
|
|
|
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() |
|
|
|
|
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); |
|
|
|
|
109
|
|
|
|
110
|
|
|
static::assertInstanceOf(WriterInterface::class, $writer); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return Stream |
|
|
|
|
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() |
|
|
|
|
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); |
|
|
|
|
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() |
|
|
|
|
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); |
|
|
|
|
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
|
|
|
|
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: