BatchingEntityFetcherTest   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 158
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

12 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 10 1
A newIdFetcher() 0 12 1
A newEntityLookup() 0 17 1
A newItemWithId() 0 5 1
A testFetchingOfTwoExistingEntities() 0 9 1
A testNonExistingEntitiesGetSkipped() 0 11 1
A testContinuationWorksEvenWhenEntityDoesNotExist() 0 11 1
A testFetchErrorCausesEntityToBeSkipped() 0 11 1
A testMissingEntitiesAndFetchErrorsCauseOnSkippedFunctionToBeCalled() 0 22 1
A testWhenAllEntitiesInBatchAreSkipped_aNewFetchCallIsMade() 0 10 1
A testEmptyArrayIsReturnedOnCallsAfterLastData() 0 5 1
A assertAreItemsWithIds() 0 11 2
1
<?php
2
3
namespace Wikibase\EntityStore\Tests;
4
5
use BatchingIterator\Fetchers\InMemoryBatchingFetcher;
6
use Wikibase\DataModel\Entity\EntityId;
7
use Wikibase\DataModel\Entity\Item;
8
use Wikibase\DataModel\Entity\ItemId;
9
use Wikibase\EntityStore\BatchingEntityFetcher;
10
use Wikibase\EntityStore\Tests\Fixtures\InMemoryEntityLookup;
11
12
/**
13
 * @covers Wikibase\EntityStore\BatchingEntityFetcher
14
 *
15
 * @licence GNU GPL v2+
16
 * @author Jeroen De Dauw < [email protected] >
17
 */
18
class BatchingEntityFetcherTest extends \PHPUnit_Framework_TestCase {
19
20
	/**
21
	 * @var BatchingEntityFetcher
22
	 */
23
	private $fetcher;
24
25
	public function setUp() {
26
		$idFetcher = $this->newIdFetcher();
27
28
		$entityLookup = $this->newEntityLookup();
29
30
		$this->fetcher = new BatchingEntityFetcher(
31
			$idFetcher,
32
			$entityLookup
33
		);
34
	}
35
36
	private function newIdFetcher() {
37
		return new InMemoryBatchingFetcher( array(
38
			new ItemId( 'Q1' ),
39
			new ItemId( 'Q2' ),
40
			new ItemId( 'Q3' ),
41
			new ItemId( 'Q4' ),
42
			new ItemId( 'Q5' ),
43
			new ItemId( 'Q6' ),
44
			new ItemId( 'Q7' ),
45
			new ItemId( 'Q8' ),
46
		) );
47
	}
48
49
	private function newEntityLookup() {
50
		return new InMemoryEntityLookup(
51
			array(
52
				$this->newItemWithId( 'Q1' ),
53
				$this->newItemWithId( 'Q2' ),
54
				// Q3 is missing
55
				$this->newItemWithId( 'Q4' ),
56
				$this->newItemWithId( 'Q5' ),
57
				// Q6 results in an error
58
				$this->newItemWithId( 'Q7' ),
59
				// Q8 is missing
60
			),
61
			array(
62
				new ItemId( 'Q6' ),
63
			)
64
		);
65
	}
66
67
	private function newItemWithId( $id ) {
68
		$item = Item::newEmpty();
0 ignored issues
show
Deprecated Code introduced by
The method Wikibase\DataModel\Entity\Item::newEmpty() has been deprecated with message: since 2.5, use new Item() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
69
		$item->setId( new ItemId( $id ) );
70
		return $item;
71
	}
72
73
	public function testFetchingOfTwoExistingEntities() {
74
		$this->assertAreItemsWithIds(
75
			array(
76
				new ItemId( 'Q1' ),
77
				new ItemId( 'Q2' ),
78
			),
79
			$this->fetcher->fetchNext( 2 )
80
		);
81
	}
82
83
	public function testNonExistingEntitiesGetSkipped() {
84
		$this->assertAreItemsWithIds(
85
			array(
86
				new ItemId( 'Q1' ),
87
				new ItemId( 'Q2' ),
88
				new ItemId( 'Q4' ),
89
				new ItemId( 'Q5' ),
90
			),
91
			$this->fetcher->fetchNext( 5 )
92
		);
93
	}
94
95
	public function testContinuationWorksEvenWhenEntityDoesNotExist() {
96
		$this->fetcher->fetchNext( 3 );
97
98
		$this->assertAreItemsWithIds(
99
			array(
100
				new ItemId( 'Q4' ),
101
				new ItemId( 'Q5' ),
102
			),
103
			$this->fetcher->fetchNext( 2 )
104
		);
105
	}
106
107
	public function testFetchErrorCausesEntityToBeSkipped() {
108
		$this->fetcher->fetchNext( 4 );
109
110
		$this->assertAreItemsWithIds(
111
			array(
112
				new ItemId( 'Q5' ),
113
				new ItemId( 'Q7' ),
114
			),
115
			$this->fetcher->fetchNext( 3 )
116
		);
117
	}
118
119
	public function testMissingEntitiesAndFetchErrorsCauseOnSkippedFunctionToBeCalled() {
120
		$onEntitySkippedCalls = array();
121
122
		$fetcher = new BatchingEntityFetcher(
123
			$this->newIdFetcher(),
124
			$this->newEntityLookup(),
125
			function( EntityId $id, $reasonMessage ) use ( &$onEntitySkippedCalls ) {
126
				$onEntitySkippedCalls[] = array( $id, $reasonMessage );
127
			}
128
		);
129
130
		$fetcher->fetchNext( 10 );
131
132
		$this->assertEquals(
133
			array(
134
				array( new ItemId( 'Q3' ), 'No such entity in the store' ),
135
				array( new ItemId( 'Q6' ), 'The id is in idsForWhichToThrowException' ),
136
				array( new ItemId( 'Q8' ), 'No such entity in the store' ),
137
			),
138
			$onEntitySkippedCalls
139
		);
140
	}
141
142
	public function testWhenAllEntitiesInBatchAreSkipped_aNewFetchCallIsMade() {
143
		$this->fetcher->fetchNext( 2 );
144
145
		$this->assertAreItemsWithIds(
146
			array(
147
				new ItemId( 'Q4' ),
148
			),
149
			$this->fetcher->fetchNext( 1 )
150
		);
151
	}
152
153
	public function testEmptyArrayIsReturnedOnCallsAfterLastData() {
154
		$this->fetcher->fetchNext( 10 );
155
		$this->assertSame( array(), $this->fetcher->fetchNext( 10 ) );
156
		$this->assertSame( array(), $this->fetcher->fetchNext( 10 ) );
157
	}
158
159
	/**
160
	 * @param ItemId[] $expectedIds
161
	 * @param Item[] $items
162
	 */
163
	private function assertAreItemsWithIds( array $expectedIds, array $items ) {
164
		$this->assertContainsOnlyInstancesOf( 'Wikibase\DataModel\Entity\Item', $items );
165
166
		$actualIds = array();
167
168
		foreach ( $items as $item ) {
169
			$actualIds[] = $item->getId();
170
		}
171
172
		$this->assertEquals( $expectedIds, $actualIds );
173
	}
174
175
}
176