|
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\Parser\ParserInterface; |
|
18
|
|
|
use Graze\DataFile\IO\StreamReader; |
|
19
|
|
|
use Graze\DataFile\Test\Helper\CreateStreamTrait; |
|
20
|
|
|
use Graze\DataFile\Test\TestCase; |
|
21
|
|
|
use Iterator; |
|
22
|
|
|
use Mockery as m; |
|
23
|
|
|
use Mockery\MockInterface; |
|
24
|
|
|
|
|
25
|
|
|
class StreamReaderTest extends TestCase |
|
26
|
|
|
{ |
|
27
|
|
|
use CreateStreamTrait; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param string $string |
|
31
|
|
|
* @param Iterator $iterator |
|
32
|
|
|
* |
|
33
|
|
|
* @return StreamReader |
|
34
|
|
|
*/ |
|
35
|
|
|
private function buildReader($string, Iterator $iterator) |
|
36
|
|
|
{ |
|
37
|
|
|
$stream = $this->createStream($string); |
|
38
|
|
|
/** @var ParserInterface|MockInterface $parser */ |
|
39
|
|
|
$parser = m::mock(ParserInterface::class); |
|
40
|
|
|
|
|
41
|
|
|
$parser->shouldReceive('parse') |
|
|
|
|
|
|
42
|
|
|
->with($stream) |
|
43
|
|
|
->andReturn($iterator); |
|
44
|
|
|
|
|
45
|
|
|
return new StreamReader($stream, $parser); |
|
|
|
|
|
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function testFetchWithNoCallable() |
|
49
|
|
|
{ |
|
50
|
|
|
$iterator = new ArrayIterator(['some', 'text']); |
|
51
|
|
|
$reader = $this->buildReader("some text in a stream", $iterator); |
|
52
|
|
|
|
|
53
|
|
|
$actual = $reader->fetch(); |
|
54
|
|
|
static::assertSame($iterator, $actual); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
View Code Duplication |
public function testFetchAllWithNoCallable() |
|
|
|
|
|
|
58
|
|
|
{ |
|
59
|
|
|
$iterator = new ArrayIterator(['some', 'text']); |
|
60
|
|
|
$reader = $this->buildReader("some text in a stream", $iterator); |
|
61
|
|
|
|
|
62
|
|
|
$actual = $reader->fetchAll(); |
|
63
|
|
|
static::assertEquals(['some', 'text'], $actual); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function testFetchWithCallable() |
|
67
|
|
|
{ |
|
68
|
|
|
$iterator = new ArrayIterator(['some', 'text']); |
|
69
|
|
|
$reader = $this->buildReader("some text in a stream", $iterator); |
|
70
|
|
|
|
|
71
|
|
|
$actual = $reader->fetch(function ($item) { |
|
72
|
|
|
return $item == 'some'; |
|
73
|
|
|
}); |
|
74
|
|
|
static::assertNotSame($iterator, $actual); |
|
75
|
|
|
|
|
76
|
|
|
$result = iterator_to_array($actual); |
|
77
|
|
|
static::assertEquals(['some'], $result); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
View Code Duplication |
public function testFetchAllWithCallable() |
|
|
|
|
|
|
81
|
|
|
{ |
|
82
|
|
|
$iterator = new ArrayIterator(['some', 'text']); |
|
83
|
|
|
$reader = $this->buildReader("some text in a stream", $iterator); |
|
84
|
|
|
|
|
85
|
|
|
$actual = $reader->fetchAll(function ($item) { |
|
86
|
|
|
return $item == 'text'; |
|
87
|
|
|
}); |
|
88
|
|
|
static::assertEquals(['text'], array_values($actual)); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
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:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: