1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wikibase\DataModel\Services\Tests\Lookup; |
4
|
|
|
|
5
|
|
|
use Wikibase\DataModel\Entity\EntityId; |
6
|
|
|
use Wikibase\DataModel\Entity\ItemId; |
7
|
|
|
use Wikibase\DataModel\Services\Lookup\EntityLookup; |
8
|
|
|
use Wikibase\DataModel\Services\Lookup\RestrictedEntityLookup; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @covers Wikibase\DataModel\Services\Lookup\RestrictedEntityLookup |
12
|
|
|
* |
13
|
|
|
* @licence GNU GPL v2+ |
14
|
|
|
* @author Marius Hoch |
15
|
|
|
*/ |
16
|
|
|
class RestrictedEntityLookupTest extends \PHPUnit_Framework_TestCase { |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @return EntityLookup |
20
|
|
|
*/ |
21
|
|
|
private function getEntityLookup() { |
22
|
|
|
$entityLookup = $this->getMock( 'Wikibase\DataModel\Services\Lookup\EntityLookup' ); |
23
|
|
|
|
24
|
|
|
$entityLookup->expects( $this->any() ) |
25
|
|
|
->method( 'hasEntity' ) |
26
|
|
|
->will( $this->returnValue( true ) ); |
27
|
|
|
|
28
|
|
|
$entityLookup->expects( $this->any() ) |
29
|
|
|
->method( 'getEntity' ) |
30
|
|
|
->will( $this->returnCallback( function( EntityId $id ) { |
31
|
|
|
return $id->getSerialization(); |
32
|
|
|
} ) ); |
33
|
|
|
|
34
|
|
|
return $entityLookup; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function testConstructor() { |
38
|
|
|
$lookup = new RestrictedEntityLookup( $this->getEntityLookup(), 1 ); |
39
|
|
|
$this->assertInstanceOf( |
40
|
|
|
'Wikibase\DataModel\Services\Lookup\RestrictedEntityLookup', |
41
|
|
|
$lookup |
42
|
|
|
); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @expectedException \InvalidArgumentException |
47
|
|
|
*/ |
48
|
|
|
public function testConstructor_exception() { |
49
|
|
|
new RestrictedEntityLookup( $this->getEntityLookup(), 0 ); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function testHasEntity() { |
53
|
|
|
$lookup = new RestrictedEntityLookup( $this->getEntityLookup(), 200 ); |
54
|
|
|
|
55
|
|
|
$this->assertTrue( $lookup->hasEntity( new ItemId( 'Q22' ) ) ); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function testGetEntityAccessCount() { |
59
|
|
|
$lookup = new RestrictedEntityLookup( $this->getEntityLookup(), 200 ); |
60
|
|
|
|
61
|
|
|
for ( $i = 1; $i < 6; $i++ ) { |
62
|
|
|
$lookup->getEntity( new ItemId( 'Q' . $i ) ); |
63
|
|
|
} |
64
|
|
|
// Q3 has already been loaded, thus doesn't count |
65
|
|
|
$lookup->getEntity( new ItemId( 'Q3' ) ); |
66
|
|
|
|
67
|
|
|
$this->assertSame( 5, $lookup->getEntityAccessCount() ); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testReset() { |
71
|
|
|
$lookup = new RestrictedEntityLookup( $this->getEntityLookup(), 200 ); |
72
|
|
|
$lookup->getEntity( new ItemId( 'Q1' ) ); |
73
|
|
|
|
74
|
|
|
$lookup->reset(); |
75
|
|
|
|
76
|
|
|
$lookup->getEntity( new ItemId( 'Q2' ) ); |
77
|
|
|
$this->assertSame( 1, $lookup->getEntityAccessCount() ); |
78
|
|
|
|
79
|
|
|
// An entity accessed before, but after reset counts again |
80
|
|
|
$lookup->getEntity( new ItemId( 'Q1' ) ); |
81
|
|
|
$this->assertSame( 2, $lookup->getEntityAccessCount() ); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function testGetEntity() { |
85
|
|
|
$lookup = new RestrictedEntityLookup( $this->getEntityLookup(), 200 ); |
86
|
|
|
|
87
|
|
|
for ( $i = 1; $i < 6; $i++ ) { |
88
|
|
|
$this->assertSame( |
89
|
|
|
'Q' . $i, |
90
|
|
|
$lookup->getEntity( new ItemId( 'Q' . $i ) ) |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function testGetEntity_exception() { |
96
|
|
|
$lookup = new RestrictedEntityLookup( $this->getEntityLookup(), 3 ); |
97
|
|
|
|
98
|
|
|
$lookup->getEntity( new ItemId( 'Q1' ) ); |
99
|
|
|
$lookup->getEntity( new ItemId( 'Q2' ) ); |
100
|
|
|
$lookup->getEntity( new ItemId( 'Q3' ) ); |
101
|
|
|
|
102
|
|
|
$this->setExpectedException( '\Wikibase\DataModel\Services\Lookup\EntityAccessLimitException' ); |
103
|
|
|
$lookup->getEntity( new ItemId( 'Q4' ) ); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function testHasEntityBeenAccessed() { |
107
|
|
|
$lookup = new RestrictedEntityLookup( $this->getEntityLookup(), 200 ); |
108
|
|
|
$lookup->getEntity( new ItemId( 'Q2' ) ); |
109
|
|
|
|
110
|
|
|
$this->assertTrue( $lookup->entityHasBeenAccessed( new ItemId( 'Q2' ) ) ); |
111
|
|
|
$this->assertFalse( $lookup->entityHasBeenAccessed( new ItemId( 'Q42' ) ) ); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
} |
115
|
|
|
|