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 ArrayIterator; |
17
|
|
|
use Graze\DataFile\Format\CsvFormat; |
18
|
|
|
use Graze\DataFile\Format\CsvFormatInterface; |
19
|
|
|
use Graze\DataFile\Format\FormatAwareInterface; |
20
|
|
|
use Graze\DataFile\Format\FormatInterface; |
21
|
|
|
use Graze\DataFile\Format\JsonFormat; |
22
|
|
|
use Graze\DataFile\Format\JsonFormatInterface; |
23
|
|
|
use Graze\DataFile\Format\Parser\ParserFactoryInterface; |
24
|
|
|
use Graze\DataFile\Format\Parser\ParserInterface; |
25
|
|
|
use Graze\DataFile\IO\FileReader; |
26
|
|
|
use Graze\DataFile\IO\ReaderInterface; |
27
|
|
|
use Graze\DataFile\Node\FileNodeInterface; |
28
|
|
|
use Graze\DataFile\Node\NodeStreamInterface; |
29
|
|
|
use Graze\DataFile\Test\Helper\CreateStreamTrait; |
30
|
|
|
use Graze\DataFile\Test\TestCase; |
31
|
|
|
use InvalidArgumentException; |
32
|
|
|
use Mockery as m; |
33
|
|
|
use Psr\Http\Message\StreamInterface; |
34
|
|
|
|
35
|
|
|
class FileReaderTest extends TestCase |
36
|
|
|
{ |
37
|
|
|
use CreateStreamTrait; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param StreamInterface $stream |
41
|
|
|
* @param ParserInterface $parser |
42
|
|
|
* |
43
|
|
|
* @return FileReader |
44
|
|
|
*/ |
45
|
|
|
private function buildFactoryReader(StreamInterface $stream, ParserInterface $parser) |
46
|
|
|
{ |
47
|
|
|
$file = m::mock(FileNodeInterface::class, NodeStreamInterface::class); |
48
|
|
|
$file->shouldReceive('getStream') |
49
|
|
|
->with('r') |
50
|
|
|
->andReturn($stream); |
51
|
|
|
|
52
|
|
|
$format = m::mock(JsonFormat::class)->makePartial(); |
53
|
|
|
|
54
|
|
|
$factory = m::mock(ParserFactoryInterface::class); |
55
|
|
|
$factory->shouldReceive('getParser') |
56
|
|
|
->with($format) |
57
|
|
|
->andReturn($parser); |
58
|
|
|
|
59
|
|
|
return new FileReader($file, $format, $factory); |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function testNodeStreamFileWillGetAStream() |
63
|
|
|
{ |
64
|
|
|
$stream = m::mock(StreamInterface::class); |
65
|
|
|
|
66
|
|
|
$file = m::mock(FileNodeInterface::class, NodeStreamInterface::class); |
67
|
|
|
$file->shouldReceive('getStream') |
68
|
|
|
->with('r') |
69
|
|
|
->andReturn($stream); |
70
|
|
|
|
71
|
|
|
$format = m::mock(CsvFormat::class)->makePartial(); |
72
|
|
|
$reader = new FileReader($file, $format); |
|
|
|
|
73
|
|
|
|
74
|
|
|
static::assertInstanceOf(ReaderInterface::class, $reader); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function testFileNodeWillGetAReadStream() |
78
|
|
|
{ |
79
|
|
|
$stream = fopen('php://memory', 'r'); |
80
|
|
|
|
81
|
|
|
try { |
82
|
|
|
$file = m::mock(FileNodeInterface::class); |
83
|
|
|
$file->shouldReceive('readStream') |
84
|
|
|
->andReturn($stream); |
85
|
|
|
|
86
|
|
|
$format = m::mock(FormatInterface::class, CsvFormatInterface::class); |
87
|
|
|
$format->shouldReceive('getType') |
88
|
|
|
->andReturn('csv'); |
89
|
|
|
|
90
|
|
|
$reader = new FileReader($file, $format); |
91
|
|
|
|
92
|
|
|
static::assertInstanceOf(ReaderInterface::class, $reader); |
93
|
|
|
} finally { |
94
|
|
|
fclose($stream); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
View Code Duplication |
public function testNodeWithNoFormatAndNoFormatSpecifiedWillThrowAnException() |
|
|
|
|
99
|
|
|
{ |
100
|
|
|
$stream = m::mock(StreamInterface::class); |
101
|
|
|
|
102
|
|
|
$file = m::mock(FileNodeInterface::class, NodeStreamInterface::class); |
103
|
|
|
$file->shouldReceive('getStream') |
104
|
|
|
->with('r') |
105
|
|
|
->andReturn($stream); |
106
|
|
|
|
107
|
|
|
static::expectException(InvalidArgumentException::class); |
108
|
|
|
new FileReader($file); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
View Code Duplication |
public function testNodeWithFormatWillUseThatFormat() |
|
|
|
|
112
|
|
|
{ |
113
|
|
|
$stream = m::mock(StreamInterface::class); |
114
|
|
|
|
115
|
|
|
$file = m::mock(FileNodeInterface::class, NodeStreamInterface::class, FormatAwareInterface::class); |
116
|
|
|
$file->shouldReceive('getStream') |
117
|
|
|
->with('r') |
118
|
|
|
->andReturn($stream); |
119
|
|
|
|
120
|
|
|
$format = m::mock(FormatInterface::class, JsonFormatInterface::class); |
121
|
|
|
$format->shouldReceive('getType') |
122
|
|
|
->andReturn('json'); |
123
|
|
|
|
124
|
|
|
$file->shouldReceive('getFormat') |
125
|
|
|
->andReturn($format); |
126
|
|
|
|
127
|
|
|
$reader = new FileReader($file); |
128
|
|
|
|
129
|
|
|
static::assertInstanceOf(ReaderInterface::class, $reader); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function testProvidingAParserFactoryWillUseTheFactory() |
133
|
|
|
{ |
134
|
|
|
$stream = m::mock(StreamInterface::class); |
135
|
|
|
$parser = m::mock(ParserInterface::class); |
136
|
|
|
|
137
|
|
|
$reader = $this->buildFactoryReader($stream, $parser); |
138
|
|
|
|
139
|
|
|
static::assertInstanceOf(ReaderInterface::class, $reader); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function testFetch() |
143
|
|
|
{ |
144
|
|
|
$iterator = new ArrayIterator(['some', 'text']); |
145
|
|
|
$stream = $this->createStream('some text in a stream'); |
146
|
|
|
|
147
|
|
|
$parser = m::mock(ParserInterface::class); |
148
|
|
|
$parser->shouldReceive('parse') |
149
|
|
|
->with($stream) |
150
|
|
|
->andReturn($iterator); |
151
|
|
|
|
152
|
|
|
$reader = $this->buildFactoryReader($stream, $parser); |
153
|
|
|
|
154
|
|
|
$actual = $reader->fetch(); |
155
|
|
|
static::assertSame($iterator, $actual); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function testFetchAll() |
159
|
|
|
{ |
160
|
|
|
$iterator = new ArrayIterator(['some', 'text']); |
161
|
|
|
$stream = $this->createStream('some text in a stream'); |
162
|
|
|
|
163
|
|
|
$parser = m::mock(ParserInterface::class); |
164
|
|
|
$parser->shouldReceive('parse') |
165
|
|
|
->with($stream) |
166
|
|
|
->andReturn($iterator); |
167
|
|
|
|
168
|
|
|
$reader = $this->buildFactoryReader($stream, $parser); |
169
|
|
|
|
170
|
|
|
$actual = $reader->fetchAll(); |
171
|
|
|
static::assertEquals(['some', 'text'], $actual); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
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: