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(); |
|
|
|
|
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
|
|
|
|
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.