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
|
|
|
|
24
|
|
|
class StreamReaderTest extends TestCase |
25
|
|
|
{ |
26
|
|
|
use CreateStreamTrait; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param string $string |
30
|
|
|
* @param Iterator $iterator |
31
|
|
|
* |
32
|
|
|
* @return StreamReader |
33
|
|
|
*/ |
34
|
|
|
private function buildReader($string, Iterator $iterator) |
35
|
|
|
{ |
36
|
|
|
$stream = $this->createStream($string); |
37
|
|
|
$parser = m::mock(ParserInterface::class); |
38
|
|
|
|
39
|
|
|
$parser->shouldReceive('parse') |
40
|
|
|
->with($stream) |
41
|
|
|
->andReturn($iterator); |
42
|
|
|
|
43
|
|
|
return new StreamReader($stream, $parser); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testFetchWithNoCallable() |
47
|
|
|
{ |
48
|
|
|
$iterator = new ArrayIterator(['some', 'text']); |
49
|
|
|
$reader = $this->buildReader("some text in a stream", $iterator); |
50
|
|
|
|
51
|
|
|
$actual = $reader->fetch(); |
52
|
|
|
static::assertInstanceOf(Iterator::class, $actual); |
53
|
|
|
static::assertEquals(['some', 'text'], iterator_to_array($actual)); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
View Code Duplication |
public function testFetchAllWithNoCallable() |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
$iterator = new ArrayIterator(['some', 'text']); |
59
|
|
|
$reader = $this->buildReader("some text in a stream", $iterator); |
60
|
|
|
|
61
|
|
|
$actual = $reader->fetchAll(); |
62
|
|
|
static::assertEquals(['some', 'text'], $actual); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testFetchWithCallable() |
66
|
|
|
{ |
67
|
|
|
$iterator = new ArrayIterator(['some', 'text']); |
68
|
|
|
$reader = $this->buildReader("some text in a stream", $iterator); |
69
|
|
|
|
70
|
|
|
$actual = $reader->fetch(function ($item) { |
71
|
|
|
return $item == 'some'; |
72
|
|
|
}); |
73
|
|
|
static::assertNotSame($iterator, $actual); |
74
|
|
|
|
75
|
|
|
$result = iterator_to_array($actual); |
76
|
|
|
static::assertEquals(['some'], $result); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
View Code Duplication |
public function testFetchAllWithCallable() |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
$iterator = new ArrayIterator(['some', 'text']); |
82
|
|
|
$reader = $this->buildReader("some text in a stream", $iterator); |
83
|
|
|
|
84
|
|
|
$actual = $reader->fetchAll(function ($item) { |
85
|
|
|
return $item == 'text'; |
86
|
|
|
}); |
87
|
|
|
static::assertEquals(['text'], array_values($actual)); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
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.