1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare( strict_types = 1 ); |
4
|
|
|
|
5
|
|
|
namespace Tests\BatchingIterator; |
6
|
|
|
|
7
|
|
|
use BatchingIterator\Fetchers\IteratorBasedBatchingFetcher; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @covers BatchingIterator\Fetchers\IteratorBasedBatchingFetcher |
12
|
|
|
* |
13
|
|
|
* @licence GNU GPL v2+ |
14
|
|
|
* @author Jeroen De Dauw < [email protected] > |
15
|
|
|
*/ |
16
|
|
|
class IteratorBasedBatchingFetcherTest extends TestCase { |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @dataProvider fetchCountProvider |
20
|
|
|
*/ |
21
|
|
|
public function testGivenNoValues_fetchNextReturnEmptyArray( $fetchCount ) { |
22
|
|
|
$fetcher = new IteratorBasedBatchingFetcher( new \ArrayIterator() ); |
23
|
|
|
|
24
|
|
|
$this->assertSame( |
25
|
|
|
[], |
26
|
|
|
$fetcher->fetchNext( $fetchCount ) |
27
|
|
|
); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function fetchCountProvider() { |
31
|
|
|
return [ |
32
|
|
|
[ 1 ], |
33
|
|
|
[ 2 ], |
34
|
|
|
[ 3 ], |
35
|
|
|
[ 10 ], |
36
|
|
|
[ 9001 ], |
37
|
|
|
]; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testGivenOneValue_onlyFirstFetchReturnsIt() { |
41
|
|
|
$fetcher = new IteratorBasedBatchingFetcher( new \ArrayIterator( [ 'foo' ] ) ); |
42
|
|
|
|
43
|
|
|
$this->assertSame( [ 'foo' ], $fetcher->fetchNext( 1 ) ); |
44
|
|
|
$this->assertSame( [], $fetcher->fetchNext( 1 ) ); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function testGivenOneValue_fetchingTwoReturnsOnlyOne() { |
48
|
|
|
$fetcher = new IteratorBasedBatchingFetcher( new \ArrayIterator( [ 'foo' ] ) ); |
49
|
|
|
|
50
|
|
|
$this->assertSame( [ 'foo' ], $fetcher->fetchNext( 2 ) ); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function testSuccessiveFetchesGivenMultipleValues() { |
54
|
|
|
$fetcher = new IteratorBasedBatchingFetcher( new \ArrayIterator( [ 'foo', 'bar', 'baz', 'bah' ] ) ); |
55
|
|
|
|
56
|
|
|
$this->assertSame( [ 'foo' ], $fetcher->fetchNext( 1 ) ); |
57
|
|
|
$this->assertSame( [ 'bar', 'baz' ], $fetcher->fetchNext( 2 ) ); |
58
|
|
|
$this->assertSame( [ 'bah' ], $fetcher->fetchNext( 3 ) ); |
59
|
|
|
$this->assertSame( [], $fetcher->fetchNext( 2 ) ); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function testWhenResultsHaveRunOut_rewindGoesToFirstElement() { |
63
|
|
|
$fetcher = new IteratorBasedBatchingFetcher( new \ArrayIterator( [ 'foo', 'bar', 'baz', 'bah' ] ) ); |
64
|
|
|
|
65
|
|
|
$fetcher->fetchNext( 10 ); |
66
|
|
|
$fetcher->rewind(); |
67
|
|
|
|
68
|
|
|
$this->assertSame( [ 'foo' ], $fetcher->fetchNext( 1 ) ); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function testWhenSomeFetchingHasHappened_rewindGoesToFirstElement() { |
72
|
|
|
$fetcher = new IteratorBasedBatchingFetcher( new \ArrayIterator( [ 'foo', 'bar', 'baz', 'bah' ] ) ); |
73
|
|
|
|
74
|
|
|
$fetcher->fetchNext( 2 ); |
75
|
|
|
$fetcher->rewind(); |
76
|
|
|
|
77
|
|
|
$this->assertSame( [ 'foo' ], $fetcher->fetchNext( 1 ) ); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function testWhenNoFetchingHasHappened_rewindStaysAtTheFirstElement() { |
81
|
|
|
$fetcher = new IteratorBasedBatchingFetcher( new \ArrayIterator( [ 'foo', 'bar', 'baz', 'bah' ] ) ); |
82
|
|
|
|
83
|
|
|
$fetcher->rewind(); |
84
|
|
|
|
85
|
|
|
$this->assertSame( [ 'foo' ], $fetcher->fetchNext( 1 ) ); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
} |
89
|
|
|
|