1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wikibase\DataModel\Services\Tests\Lookup; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
use Wikibase\DataModel\Entity\EntityId; |
8
|
|
|
use Wikibase\DataModel\Entity\ItemId; |
9
|
|
|
use Wikibase\DataModel\Services\Lookup\EntityAccessLimitException; |
10
|
|
|
use Wikibase\DataModel\Services\Lookup\EntityLookup; |
11
|
|
|
use Wikibase\DataModel\Services\Lookup\RestrictedEntityLookup; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @covers \Wikibase\DataModel\Services\Lookup\RestrictedEntityLookup |
15
|
|
|
* |
16
|
|
|
* @license GPL-2.0-or-later |
17
|
|
|
* @author Marius Hoch |
18
|
|
|
*/ |
19
|
|
|
class RestrictedEntityLookupTest extends TestCase { |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @return EntityLookup |
23
|
|
|
*/ |
24
|
|
|
private function getEntityLookup() { |
25
|
|
|
$entityLookup = $this->createMock( EntityLookup::class ); |
26
|
|
|
|
27
|
|
|
$entityLookup->expects( $this->any() ) |
28
|
|
|
->method( 'hasEntity' ) |
29
|
|
|
->will( $this->returnValue( true ) ); |
30
|
|
|
|
31
|
|
|
$entityLookup->expects( $this->any() ) |
32
|
|
|
->method( 'getEntity' ) |
33
|
|
|
->will( $this->returnCallback( function( EntityId $id ) { |
34
|
|
|
return $id->getSerialization(); |
35
|
|
|
} ) ); |
36
|
|
|
|
37
|
|
|
return $entityLookup; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testConstructor() { |
41
|
|
|
$lookup = new RestrictedEntityLookup( $this->getEntityLookup(), 1 ); |
42
|
|
|
$this->assertInstanceOf( RestrictedEntityLookup::class, $lookup ); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function testConstructor_exception() { |
46
|
|
|
$this->expectException( InvalidArgumentException::class ); |
47
|
|
|
new RestrictedEntityLookup( $this->getEntityLookup(), 0 ); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function testHasEntity() { |
51
|
|
|
$lookup = new RestrictedEntityLookup( $this->getEntityLookup(), 200 ); |
|
|
|
|
52
|
|
|
|
53
|
|
|
$this->assertTrue( $lookup->hasEntity( new ItemId( 'Q22' ) ) ); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function testGetEntityAccessCount() { |
57
|
|
|
$lookup = new RestrictedEntityLookup( $this->getEntityLookup(), 200 ); |
|
|
|
|
58
|
|
|
|
59
|
|
|
for ( $i = 1; $i < 6; $i++ ) { |
60
|
|
|
$lookup->getEntity( new ItemId( 'Q' . $i ) ); |
61
|
|
|
} |
62
|
|
|
// Q3 has already been loaded, thus doesn't count |
63
|
|
|
$lookup->getEntity( new ItemId( 'Q3' ) ); |
64
|
|
|
|
65
|
|
|
$this->assertSame( 5, $lookup->getEntityAccessCount() ); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function testReset() { |
69
|
|
|
$lookup = new RestrictedEntityLookup( $this->getEntityLookup(), 200 ); |
70
|
|
|
$lookup->getEntity( new ItemId( 'Q1' ) ); |
71
|
|
|
|
72
|
|
|
$lookup->reset(); |
73
|
|
|
|
74
|
|
|
$lookup->getEntity( new ItemId( 'Q2' ) ); |
75
|
|
|
$this->assertSame( 1, $lookup->getEntityAccessCount() ); |
76
|
|
|
|
77
|
|
|
// An entity accessed before, but after reset counts again |
78
|
|
|
$lookup->getEntity( new ItemId( 'Q1' ) ); |
79
|
|
|
$this->assertSame( 2, $lookup->getEntityAccessCount() ); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function testGetEntity() { |
83
|
|
|
$lookup = new RestrictedEntityLookup( $this->getEntityLookup(), 200 ); |
|
|
|
|
84
|
|
|
|
85
|
|
|
for ( $i = 1; $i < 6; $i++ ) { |
86
|
|
|
$this->assertSame( |
87
|
|
|
'Q' . $i, |
88
|
|
|
$lookup->getEntity( new ItemId( 'Q' . $i ) ) |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function testGetEntity_exception() { |
94
|
|
|
$lookup = new RestrictedEntityLookup( $this->getEntityLookup(), 3 ); |
|
|
|
|
95
|
|
|
|
96
|
|
|
$lookup->getEntity( new ItemId( 'Q1' ) ); |
97
|
|
|
$lookup->getEntity( new ItemId( 'Q2' ) ); |
98
|
|
|
$lookup->getEntity( new ItemId( 'Q3' ) ); |
99
|
|
|
|
100
|
|
|
$this->expectException( EntityAccessLimitException::class ); |
101
|
|
|
$lookup->getEntity( new ItemId( 'Q4' ) ); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function testHasEntityBeenAccessed() { |
105
|
|
|
$lookup = new RestrictedEntityLookup( $this->getEntityLookup(), 200 ); |
|
|
|
|
106
|
|
|
$lookup->getEntity( new ItemId( 'Q2' ) ); |
107
|
|
|
|
108
|
|
|
$this->assertTrue( $lookup->entityHasBeenAccessed( new ItemId( 'Q2' ) ) ); |
109
|
|
|
$this->assertFalse( $lookup->entityHasBeenAccessed( new ItemId( 'Q42' ) ) ); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: