testWhenFetchedBeyondTheLastPage_newlyAddedPagesAreStillFetchedOnNextCall()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Tests\Queryr\Replicator\EntitySource;
4
5
use PHPUnit\Framework\TestCase;
6
use Queryr\Replicator\EntitySource\BatchingEntityPageFetcher;
7
use Tests\Queryr\Replicator\Fixtures\FakeEntityPageBatchFetcher;
8
9
/**
10
 * @covers \Queryr\Replicator\EntitySource\BatchingEntityPageFetcher
11
 *
12
 * @licence GNU GPL v2+
13
 * @author Jeroen De Dauw < [email protected] >
14
 */
15
class BatchingEntityPageFetcherTest extends TestCase {
16
17
	public function testGivenNoPagesToFetch_noneAreReturned() {
18
		$fetcher = $this->newFetcherForPages( [] );
19
20
		$this->assertSame( [], $fetcher->fetchNext( 10 ) );
21
		$this->assertSame( [], $fetcher->fetchNext( 10 ) );
22
	}
23
24
	private function newFetcherForPages( array $entityIds, array $idsToIgnore = [] ) {
25
		return new BatchingEntityPageFetcher( new FakeEntityPageBatchFetcher( $idsToIgnore ), $entityIds );
26
	}
27
28
	public function testRewind() {
29
		$fetcher = $this->newFetcherForPages( [ 'Q1', 'Q2', 'Q3' ] );
30
31
		$fetcher->fetchNext( 2 );
32
		$fetcher->rewind();
33
		$this->assertSame( [ 'Q1', 'Q2' ], $fetcher->fetchNext( 2 ) );
34
	}
35
36
	public function testProgressionThroughArray() {
37
		$fetcher = $this->newFetcherForPages( [ 'Q1', 'Q2', 'Q3' ] );
38
39
		$this->assertSame( [ 'Q1', 'Q2' ], $fetcher->fetchNext( 2 ) );
40
		$this->assertSame( [ 'Q3' ], $fetcher->fetchNext( 2 ) );
41
		$this->assertSame( [], $fetcher->fetchNext( 2 ) );
42
	}
43
44
	public function testWhenAtInitialPosition_addingNewPagesWorks() {
45
		$fetcher = $this->newFetcherForPages( [ 'Q1', 'Q2', 'Q3' ] );
46
47
		$fetcher->addPagesToFetch( [ 'Q4' ] );
48
49
		$this->assertSame( [ 'Q1', 'Q2', 'Q3', 'Q4' ], $fetcher->fetchNext( 5 ) );
50
	}
51
52
	public function testWhenPartiallyIterated_addingNewPagesWorks() {
53
		$fetcher = $this->newFetcherForPages( [ 'Q1', 'Q2', 'Q3' ] );
54
55
		$fetcher->fetchNext( 2 );
56
		$fetcher->addPagesToFetch( [ 'Q4' ] );
57
58
		$this->assertSame( [ 'Q3', 'Q4' ], $fetcher->fetchNext( 2 ) );
59
	}
60
61
	public function testWhenFetchedBeyondTheLastPage_newlyAddedPagesAreStillFetchedOnNextCall() {
62
		$fetcher = $this->newFetcherForPages( [ 'Q1', 'Q2' ] );
63
64
		$fetcher->fetchNext( 3 );
65
		$fetcher->addPagesToFetch( [ 'Q3', 'Q4' ] );
66
67
		$this->assertSame( [ 'Q3', 'Q4' ], $fetcher->fetchNext( 3 ) );
68
	}
69
70
	public function testWhenPartiallyIterated_additionOfDuplicatesGetsIgnored() {
71
		$fetcher = $this->newFetcherForPages( [ 'Q1', 'Q2', 'Q3' ] );
72
73
		$fetcher->fetchNext( 2 );
74
		$fetcher->addPagesToFetch( [ 'Q1', 'Q4', 'Q3' ] );
75
76
		$this->assertSame( [ 'Q3', 'Q4' ], $fetcher->fetchNext( 3 ) );
77
	}
78
79
	public function testWhenFullBatchIsEmpty_theNextBatchIsRequested() {
80
		$fetcher = $this->newFetcherForPages(
81
			[ 'Q1', 'Q2', 'Q3', 'Q4', 'Q5' ], // These IDs are requested
82
			[ 'Q1', 'Q2', 'Q3' ] // These IDs are not found
83
		);
84
85
		$this->assertSame( [ 'Q4', 'Q5' ], $fetcher->fetchNext( 3 ) );
86
	}
87
88
}
89