BatchingEntityFetcherTest   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A insertEntities() 0 9 2
A getFetcherThatContinuesFrom() 0 13 1
A testWhenFetchingLessEntitiesThenExist_fullBatchOfEntitiesIsReturned() 0 6 1
A testStartingPositionIsHeldIntoAccountAndIsIncremented() 0 19 1
A assertAreItemsWithIds() 0 11 2
A testRewindSetsThePositionBackToTheInitialValue() 0 14 1
1
<?php
2
3
namespace Wikibase\EntityStore\Tests\Integration;
4
5
use Wikibase\DataModel\Entity\EntityId;
6
use Wikibase\DataModel\Entity\Item;
7
use Wikibase\DataModel\Entity\ItemId;
8
use Wikibase\EntityStore\BatchingEntityFetcher;
9
use Wikibase\EntityStore\BatchingEntityIdFetcherBuilder;
10
use Wikibase\Repo\WikibaseRepo;
11
12
/**
13
 * @covers Wikibase\EntityStore\BatchingEntityFetcher
14
 *
15
 * @group Database
16
 *
17
 * @licence GNU GPL v2+
18
 * @author Jeroen De Dauw < [email protected] >
19
 */
20
class BatchingEntityFetcherTest extends \MediaWikiTestCase {
21
22
	public function setUp() {
23
		parent::setUp();
24
25
		$this->insertEntities();
26
	}
27
28
	private function insertEntities() {
29
		$entityStore = WikibaseRepo::getDefaultInstance()->getStore()->getEntityStore();
30
31
		foreach ( range( 1000070, 1000075 ) as $itemId ) {
32
			$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...
33
			$item->setId( $itemId );
34
			$entityStore->saveEntity( $item, __CLASS__, $GLOBALS['wgUser'] );
35
		}
36
	}
37
38
	private function getFetcherThatContinuesFrom( EntityId $previousId = null ) {
39
		$repo = WikibaseRepo::getDefaultInstance();
40
41
		$fetcherBuilder = new BatchingEntityIdFetcherBuilder(
42
			$repo->getStore()->newEntityPerPage(),
43
			$previousId
44
		);
45
46
		return new BatchingEntityFetcher(
47
			$fetcherBuilder->getFetcher(),
48
			$repo->getEntityLookup()
49
		);
50
	}
51
52
	public function testWhenFetchingLessEntitiesThenExist_fullBatchOfEntitiesIsReturned() {
53
		$entities = $this->getFetcherThatContinuesFrom( null )->fetchNext( 5 );
54
55
		$this->assertCount( 5, $entities );
56
		$this->assertContainsOnlyInstancesOf( 'Wikibase\DataModel\Entity\Item', $entities );
57
	}
58
59
	public function testStartingPositionIsHeldIntoAccountAndIsIncremented() {
60
		$fetcher = $this->getFetcherThatContinuesFrom( new ItemId( 'Q1000070' ) );
61
62
		$this->assertAreItemsWithIds(
63
			array(
64
				new ItemId( 'Q1000071' ),
65
				new ItemId( 'Q1000072' ),
66
			),
67
			$fetcher->fetchNext( 2 )
68
		);
69
70
		$this->assertAreItemsWithIds(
71
			array(
72
				new ItemId( 'Q1000073' ),
73
				new ItemId( 'Q1000074' ),
74
			),
75
			$fetcher->fetchNext( 2 )
76
		);
77
	}
78
79
	/**
80
	 * @param ItemId[] $expectedIds
81
	 * @param Item[] $items
82
	 */
83
	private function assertAreItemsWithIds( array $expectedIds, array $items ) {
84
		$this->assertContainsOnlyInstancesOf( 'Wikibase\DataModel\Entity\Item', $items );
85
86
		$actualIds = array();
87
88
		foreach ( $items as $item ) {
89
			$actualIds[] = $item->getId();
90
		}
91
92
		$this->assertEquals( $expectedIds, $actualIds );
93
	}
94
95
	public function testRewindSetsThePositionBackToTheInitialValue() {
96
		$fetcher = $this->getFetcherThatContinuesFrom( new ItemId( 'Q1000070' ) );
97
98
		$fetcher->fetchNext( 5 );
99
		$fetcher->rewind();
100
101
		$this->assertAreItemsWithIds(
102
			array(
103
				new ItemId( 'Q1000071' ),
104
				new ItemId( 'Q1000072' ),
105
			),
106
			$fetcher->fetchNext( 2 )
107
		);
108
	}
109
110
}
111