InMemoryBatchingFetcherTest   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

8 Methods

Rating   Name   Duplication   Size   Complexity  
A testGivenNoValues_fetchNextReturnEmptyArray() 0 8 1
A fetchCountProvider() 0 9 1
A testGivenOneValue_onlyFirstFetchReturnsIt() 0 6 1
A testGivenOneValue_fetchingTwoReturnsOnlyOne() 0 5 1
A testSuccessiveFetchesGivenMultipleValues() 0 8 1
A testWhenResultsHaveRunOut_rewindGoesToFirstElement() 0 8 1
A testWhenSomeFetchingHasHappened_rewindGoesToFirstElement() 0 8 1
A testWhenNoFetchingHasHappened_rewindStaysAtTheFirstElement() 0 7 1
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace Tests\BatchingIterator;
6
7
use BatchingIterator\Fetchers\InMemoryBatchingFetcher;
8
use PHPUnit\Framework\TestCase;
9
10
/**
11
 * @covers BatchingIterator\Fetchers\InMemoryBatchingFetcher
12
 *
13
 * @licence GNU GPL v2+
14
 * @author Jeroen De Dauw < [email protected] >
15
 */
16
class InMemoryBatchingFetcherTest extends TestCase {
17
18
	/**
19
	 * @dataProvider fetchCountProvider
20
	 */
21
	public function testGivenNoValues_fetchNextReturnEmptyArray( $fetchCount ) {
22
		$fetcher = new InMemoryBatchingFetcher( [] );
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 InMemoryBatchingFetcher( [ '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 InMemoryBatchingFetcher( [ 'foo' ] );
49
50
		$this->assertSame( [ 'foo' ], $fetcher->fetchNext( 2 ) );
51
	}
52
53
	public function testSuccessiveFetchesGivenMultipleValues() {
54
		$fetcher = new InMemoryBatchingFetcher( [ '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 InMemoryBatchingFetcher( [ '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 InMemoryBatchingFetcher( [ '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 InMemoryBatchingFetcher( [ 'foo', 'bar', 'baz', 'bah' ] );
82
83
		$fetcher->rewind();
84
85
		$this->assertSame( [ 'foo' ], $fetcher->fetchNext( 1 ) );
86
	}
87
88
}
89