1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare( strict_types = 1 ); |
4
|
|
|
|
5
|
|
|
namespace Tests\BatchingIterator; |
6
|
|
|
|
7
|
|
|
use BatchingIterator\BatchingIterator; |
8
|
|
|
use BatchingIterator\Fetchers\InMemoryBatchingFetcher; |
9
|
|
|
use PHPUnit\Framework\TestCase; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @covers BatchingIterator\BatchingIterator |
13
|
|
|
* |
14
|
|
|
* @licence GNU GPL v2+ |
15
|
|
|
* @author Jeroen De Dauw < [email protected] > |
16
|
|
|
*/ |
17
|
|
|
class BatchingIteratorTest extends TestCase { |
18
|
|
|
|
19
|
|
|
public function testWhenFetcherReturnsEmptyRightAway_iteratorIsEmpty() { |
20
|
|
|
$fetcher = $this->createMock( 'BatchingIterator\BatchingFetcher' ); |
21
|
|
|
|
22
|
|
|
$fetcher->expects( $this->once() ) |
23
|
|
|
->method( 'fetchNext' ) |
24
|
|
|
->will( $this->returnValue( [] ) ); |
25
|
|
|
|
26
|
|
|
$iterator = new BatchingIterator( $fetcher ); |
|
|
|
|
27
|
|
|
|
28
|
|
|
$this->assertSame( [], iterator_to_array( $iterator ) ); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @dataProvider batchSizeProvider |
33
|
|
|
*/ |
34
|
|
|
public function testAllValuesEndUpInTheIterator( $batchSize ) { |
35
|
|
|
$values = [ 'foo', 'bar', 'baz', 'bah' ]; |
36
|
|
|
|
37
|
|
|
$fetcher = new InMemoryBatchingFetcher( $values ); |
38
|
|
|
$iterator = new BatchingIterator( $fetcher ); |
39
|
|
|
$iterator->setMaxBatchSize( $batchSize ); |
40
|
|
|
|
41
|
|
|
$this->assertSame( $values, iterator_to_array( $iterator ) ); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function batchSizeProvider() { |
45
|
|
|
return [ |
46
|
|
|
[ 1 ], |
47
|
|
|
[ 2 ], |
48
|
|
|
[ 3 ], |
49
|
|
|
[ 10 ], |
50
|
|
|
[ 9001 ], |
51
|
|
|
]; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testCorrectCallsAreMadeToTheBatchingFetcher() { |
55
|
|
|
$fetcher = $this->createMock( 'BatchingIterator\BatchingFetcher' ); |
56
|
|
|
|
57
|
|
|
$fetcher->expects( $this->at( 0 ) ) |
58
|
|
|
->method( 'rewind' ); |
59
|
|
|
|
60
|
|
|
$fetcher->expects( $this->at( 1 ) ) |
61
|
|
|
->method( 'fetchNext' ) |
62
|
|
|
->with( $this->equalTo( 2 ) ) |
63
|
|
|
->will( $this->returnValue( [ 'foo', 'bar' ] ) ); |
64
|
|
|
|
65
|
|
|
$fetcher->expects( $this->at( 2 ) ) |
66
|
|
|
->method( 'fetchNext' ) |
67
|
|
|
->with( $this->equalTo( 2 ) ) |
68
|
|
|
->will( $this->returnValue( [ 'baz' ] ) ); |
69
|
|
|
|
70
|
|
|
$fetcher->expects( $this->at( 3 ) ) |
71
|
|
|
->method( 'fetchNext' ) |
72
|
|
|
->with( $this->equalTo( 2 ) ) |
73
|
|
|
->will( $this->returnValue( [] ) ); |
74
|
|
|
|
75
|
|
|
$iterator = new BatchingIterator( $fetcher ); |
|
|
|
|
76
|
|
|
$iterator->setMaxBatchSize( 2 ); |
77
|
|
|
|
78
|
|
|
$this->assertSame( [ 'foo', 'bar', 'baz' ], iterator_to_array( $iterator ) ); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @dataProvider invalidBatchSizeProvider |
83
|
|
|
*/ |
84
|
|
|
public function testSettingInvalidMaxBatchSizeCausesException( $invalidBatchSize ) { |
85
|
|
|
$iterator = new BatchingIterator( $this->createMock( 'BatchingIterator\BatchingFetcher' ) ); |
|
|
|
|
86
|
|
|
|
87
|
|
|
$this->expectException( 'InvalidArgumentException' ); |
88
|
|
|
$iterator->setMaxBatchSize( $invalidBatchSize ); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function invalidBatchSizeProvider() { |
92
|
|
|
return [ |
93
|
|
|
[ 0 ], |
94
|
|
|
[ -1 ], |
95
|
|
|
[ -5 ], |
96
|
|
|
]; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function testWhenRewindingTheIterator_theFetcherIsAlsoRewinded() { |
100
|
|
|
$fetcher = $this->createMock( 'BatchingIterator\BatchingFetcher' ); |
101
|
|
|
|
102
|
|
|
$fetcher->expects( $this->once() ) |
103
|
|
|
->method( 'rewind' ); |
104
|
|
|
|
105
|
|
|
$iterator = new BatchingIterator( $fetcher ); |
|
|
|
|
106
|
|
|
|
107
|
|
|
$iterator->rewind(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function testMultipleIteration() { |
111
|
|
|
$values = [ 'foo', 'bar', 'baz' ]; |
112
|
|
|
|
113
|
|
|
$iterator = new BatchingIterator( new InMemoryBatchingFetcher( $values ) ); |
114
|
|
|
|
115
|
|
|
$this->assertEquals( $values, iterator_to_array( $iterator ) ); |
116
|
|
|
$this->assertEquals( $values, iterator_to_array( $iterator ) ); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
} |
120
|
|
|
|
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: