|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare( strict_types = 1 ); |
|
4
|
|
|
|
|
5
|
|
|
namespace Tests\BatchingIterator; |
|
6
|
|
|
|
|
7
|
|
|
use BatchingIterator\Fetchers\InMemoryBatchingFetcher; |
|
8
|
|
|
use BatchingIterator\Fetchers\MultipleBatchingFetcher; |
|
9
|
|
|
use PHPUnit\Framework\TestCase; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @covers BatchingIterator\Fetchers\MultipleBatchingFetcher |
|
13
|
|
|
* |
|
14
|
|
|
* @licence GNU GPL v2+ |
|
15
|
|
|
* @author Jeroen De Dauw < [email protected] > |
|
16
|
|
|
*/ |
|
17
|
|
|
class MultipleBatchingFetcherTest extends TestCase { |
|
18
|
|
|
|
|
19
|
|
|
public function testGivenNonFetcher_constructorThrowsException() { |
|
20
|
|
|
$this->expectException( 'InvalidArgumentException' ); |
|
21
|
|
|
|
|
22
|
|
|
new MultipleBatchingFetcher( |
|
23
|
|
|
new InMemoryBatchingFetcher( [] ), |
|
24
|
|
|
new InMemoryBatchingFetcher( [] ), |
|
25
|
|
|
null, |
|
26
|
|
|
new InMemoryBatchingFetcher( [] ) |
|
27
|
|
|
); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function testGivenOneFetcher_behaviourIsNotAltered() { |
|
31
|
|
|
$values = [ 'foo', 'bar', 'baz' ]; |
|
32
|
|
|
|
|
33
|
|
|
$fetcher = new MultipleBatchingFetcher( new InMemoryBatchingFetcher( $values ) ); |
|
34
|
|
|
|
|
35
|
|
|
$this->assertEquals( $values, $fetcher->fetchNext( 4 ) ); |
|
36
|
|
|
$this->assertEquals( [], $fetcher->fetchNext( 4 ) ); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function testGivenMultipleFetchers_allValuesCanBeFetched() { |
|
40
|
|
|
$fetcher = new MultipleBatchingFetcher( |
|
41
|
|
|
new InMemoryBatchingFetcher( [ 'foo', 'bar', 'baz' ] ), |
|
42
|
|
|
new InMemoryBatchingFetcher( [ '0', '1' ] ), |
|
43
|
|
|
new InMemoryBatchingFetcher( [] ), |
|
44
|
|
|
new InMemoryBatchingFetcher( [ '2' ] ) |
|
45
|
|
|
); |
|
46
|
|
|
|
|
47
|
|
|
$this->assertEquals( [ 'foo', 'bar' ], $fetcher->fetchNext( 2 ) ); |
|
48
|
|
|
$this->assertEquals( [ 'baz' ], $fetcher->fetchNext( 2 ) ); |
|
49
|
|
|
$this->assertEquals( [ '0', '1' ], $fetcher->fetchNext( 3 ) ); |
|
50
|
|
|
$this->assertEquals( [ '2' ], $fetcher->fetchNext( 3 ) ); |
|
51
|
|
|
$this->assertEquals( [], $fetcher->fetchNext( 3 ) ); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function testGivenMultipleFetchers_rewindWorksCorrectly() { |
|
55
|
|
|
$fetcher = new MultipleBatchingFetcher( |
|
56
|
|
|
new InMemoryBatchingFetcher( [ 'foo', 'bar', 'baz' ] ), |
|
57
|
|
|
new InMemoryBatchingFetcher( [] ), |
|
58
|
|
|
new InMemoryBatchingFetcher( [ '0', '1' ] ), |
|
59
|
|
|
new InMemoryBatchingFetcher( [ '2' ] ) |
|
60
|
|
|
); |
|
61
|
|
|
|
|
62
|
|
|
$fetcher->fetchNext( 2 ); |
|
63
|
|
|
$fetcher->fetchNext( 2 ); |
|
64
|
|
|
$fetcher->fetchNext( 2 ); |
|
65
|
|
|
|
|
66
|
|
|
$fetcher->rewind(); |
|
67
|
|
|
|
|
68
|
|
|
$this->assertEquals( [ 'foo' ], $fetcher->fetchNext( 1 ) ); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function testCanConstructWithArray() { |
|
72
|
|
|
new MultipleBatchingFetcher( [ |
|
73
|
|
|
new InMemoryBatchingFetcher( [] ), |
|
74
|
|
|
new InMemoryBatchingFetcher( [] ) |
|
75
|
|
|
] ); |
|
76
|
|
|
|
|
77
|
|
|
$this->assertTrue( true ); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function testGivenArrayAndFetchers_constructorThrowsException() { |
|
81
|
|
|
$this->expectException( 'InvalidArgumentException' ); |
|
82
|
|
|
|
|
83
|
|
|
new MultipleBatchingFetcher( |
|
84
|
|
|
[ |
|
85
|
|
|
new InMemoryBatchingFetcher( [] ), |
|
86
|
|
|
new InMemoryBatchingFetcher( [] ) |
|
87
|
|
|
], |
|
88
|
|
|
new InMemoryBatchingFetcher( [] ) |
|
89
|
|
|
); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function testGivenArrayWithNonFetcher_constructorThrowsException() { |
|
93
|
|
|
$this->expectException( 'InvalidArgumentException' ); |
|
94
|
|
|
|
|
95
|
|
|
new MultipleBatchingFetcher( |
|
96
|
|
|
[ |
|
97
|
|
|
new InMemoryBatchingFetcher( [] ), |
|
98
|
|
|
new InMemoryBatchingFetcher( [] ), |
|
99
|
|
|
null, |
|
100
|
|
|
new InMemoryBatchingFetcher( [] ) |
|
101
|
|
|
] |
|
102
|
|
|
); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
|