BatchingIteratorTest   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 5
dl 0
loc 103
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A testWhenFetcherReturnsEmptyRightAway_iteratorIsEmpty() 0 11 1
A testAllValuesEndUpInTheIterator() 0 9 1
A batchSizeProvider() 0 9 1
A testCorrectCallsAreMadeToTheBatchingFetcher() 0 26 1
A testSettingInvalidMaxBatchSizeCausesException() 0 6 1
A invalidBatchSizeProvider() 0 7 1
A testWhenRewindingTheIterator_theFetcherIsAlsoRewinded() 0 10 1
A testMultipleIteration() 0 8 1
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 );
0 ignored issues
show
Documentation introduced by
$fetcher is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<BatchingIterator\BatchingFetcher>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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 );
0 ignored issues
show
Documentation introduced by
$fetcher is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<BatchingIterator\BatchingFetcher>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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' ) );
0 ignored issues
show
Documentation introduced by
$this->createMock('Batch...ator\\BatchingFetcher') is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<BatchingIterator\BatchingFetcher>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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 );
0 ignored issues
show
Documentation introduced by
$fetcher is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<BatchingIterator\BatchingFetcher>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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