1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wikibase\DataModel\Services\Tests\Lookup; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
use Wikibase\DataModel\Entity\ItemId; |
8
|
|
|
use Wikibase\DataModel\Entity\PropertyId; |
9
|
|
|
use Wikibase\DataModel\Services\Fixtures\FakeEntityDocument; |
10
|
|
|
use Wikibase\DataModel\Services\Fixtures\ItemFixtures; |
11
|
|
|
use Wikibase\DataModel\Services\Fixtures\PropertyFixtures; |
12
|
|
|
use Wikibase\DataModel\Services\Lookup\EntityLookupException; |
13
|
|
|
use Wikibase\DataModel\Services\Lookup\InMemoryEntityLookup; |
14
|
|
|
use Wikibase\DataModel\Services\Lookup\ItemLookupException; |
15
|
|
|
use Wikibase\DataModel\Services\Lookup\PropertyLookupException; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @covers \Wikibase\DataModel\Services\Lookup\InMemoryEntityLookup |
19
|
|
|
* |
20
|
|
|
* @license GPL-2.0-or-later |
21
|
|
|
* @author Jeroen De Dauw < [email protected] > |
22
|
|
|
*/ |
23
|
|
|
class InMemoryEntityLookupTest extends TestCase { |
24
|
|
|
|
25
|
|
|
public function testGivenUnknownEntityId_getEntityReturnsNull() { |
26
|
|
|
$lookup = new InMemoryEntityLookup(); |
27
|
|
|
$this->assertNull( $lookup->getEntity( new ItemId( 'Q1' ) ) ); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function testGivenKnownEntityId_getEntityReturnsTheEntity() { |
31
|
|
|
$lookup = new InMemoryEntityLookup(); |
32
|
|
|
|
33
|
|
|
$lookup->addEntity( new FakeEntityDocument( new ItemId( 'Q1' ) ) ); |
34
|
|
|
$lookup->addEntity( new FakeEntityDocument( new ItemId( 'Q2' ) ) ); |
35
|
|
|
$lookup->addEntity( new FakeEntityDocument( new ItemId( 'Q3' ) ) ); |
36
|
|
|
|
37
|
|
|
$this->assertEquals( |
38
|
|
|
new FakeEntityDocument( new ItemId( 'Q2' ) ), |
39
|
|
|
$lookup->getEntity( new ItemId( 'Q2' ) ) |
40
|
|
|
); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testGivenUnknownEntityId_hasEntityReturnsFalse() { |
44
|
|
|
$lookup = new InMemoryEntityLookup(); |
45
|
|
|
$this->assertFalse( $lookup->hasEntity( new ItemId( 'Q1' ) ) ); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function testGivenKnownEntityId_hasEntityReturnsTrue() { |
49
|
|
|
$lookup = new InMemoryEntityLookup(); |
50
|
|
|
|
51
|
|
|
$lookup->addEntity( new FakeEntityDocument( new ItemId( 'Q1' ) ) ); |
52
|
|
|
$lookup->addEntity( new FakeEntityDocument( new ItemId( 'Q2' ) ) ); |
53
|
|
|
$lookup->addEntity( new FakeEntityDocument( new ItemId( 'Q3' ) ) ); |
54
|
|
|
|
55
|
|
|
$this->assertTrue( $lookup->hasEntity( new ItemId( 'Q2' ) ) ); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function testGivenIdInExceptionList_getEntityThrowsException() { |
59
|
|
|
$lookup = new InMemoryEntityLookup(); |
60
|
|
|
|
61
|
|
|
$lookup->addException( new EntityLookupException( new ItemId( 'Q1' ) ) ); |
62
|
|
|
|
63
|
|
|
$lookup->getEntity( new ItemId( 'Q2' ) ); |
64
|
|
|
$this->expectException( EntityLookupException::class ); |
65
|
|
|
$lookup->getEntity( new ItemId( 'Q1' ) ); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function testGivenIdInExceptionList_hasEntityThrowsException() { |
69
|
|
|
$lookup = new InMemoryEntityLookup(); |
70
|
|
|
|
71
|
|
|
$lookup->addException( new EntityLookupException( new ItemId( 'Q1' ) ) ); |
72
|
|
|
|
73
|
|
|
$lookup->hasEntity( new ItemId( 'Q2' ) ); |
74
|
|
|
$this->expectException( EntityLookupException::class ); |
75
|
|
|
$lookup->hasEntity( new ItemId( 'Q1' ) ); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function testGivenConstructorVarArgEntities_theyCanBeRetrieved() { |
79
|
|
|
$lookup = new InMemoryEntityLookup( |
80
|
|
|
new FakeEntityDocument( new ItemId( 'Q1' ) ), |
81
|
|
|
new FakeEntityDocument( new ItemId( 'Q2' ) ) |
82
|
|
|
); |
83
|
|
|
|
84
|
|
|
$this->assertTrue( $lookup->hasEntity( new ItemId( 'Q1' ) ) ); |
85
|
|
|
$this->assertTrue( $lookup->hasEntity( new ItemId( 'Q2' ) ) ); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function testGivenEntityWithoutIdInConstructor_exceptionIsThrown() { |
89
|
|
|
$this->expectException( InvalidArgumentException::class ); |
90
|
|
|
|
91
|
|
|
new InMemoryEntityLookup( |
92
|
|
|
new FakeEntityDocument() |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function testGivenKnownItem_getItemForIdReturnsIt() { |
97
|
|
|
$item = ItemFixtures::newItem(); |
98
|
|
|
|
99
|
|
|
$lookup = new InMemoryEntityLookup( $item ); |
100
|
|
|
|
101
|
|
|
$this->assertEquals( |
102
|
|
|
$item, |
103
|
|
|
$lookup->getItemForId( $item->getId() ) |
|
|
|
|
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function testGivenKnownProperty_getPropertyForIdReturnsIt() { |
108
|
|
|
$property = PropertyFixtures::newProperty(); |
109
|
|
|
|
110
|
|
|
$lookup = new InMemoryEntityLookup( $property ); |
111
|
|
|
|
112
|
|
|
$this->assertEquals( |
113
|
|
|
$property, |
114
|
|
|
$lookup->getPropertyForId( $property->getId() ) |
|
|
|
|
115
|
|
|
); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function testWhenItemIsNotKnown_getItemForIdReturnsNull() { |
119
|
|
|
$this->assertNull( |
120
|
|
|
( new InMemoryEntityLookup() )->getItemForId( new ItemId( 'Q1' ) ) |
121
|
|
|
); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function testWhenPropertyIsNotKnown_getPropertyForIdReturnsNull() { |
125
|
|
|
$this->assertNull( |
126
|
|
|
( new InMemoryEntityLookup() )->getPropertyForId( new PropertyId( 'P1' ) ) |
127
|
|
|
); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function testGetItemForIdThrowsCorrectExceptionType() { |
131
|
|
|
$id = new ItemId( 'Q1' ); |
132
|
|
|
|
133
|
|
|
$lookup = new InMemoryEntityLookup(); |
134
|
|
|
$lookup->addException( new EntityLookupException( $id ) ); |
135
|
|
|
|
136
|
|
|
$this->expectException( ItemLookupException::class ); |
137
|
|
|
$lookup->getItemForId( $id ); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function testGetPropertyForIdThrowsCorrectExceptionType() { |
141
|
|
|
$id = new PropertyId( 'P1' ); |
142
|
|
|
|
143
|
|
|
$lookup = new InMemoryEntityLookup(); |
144
|
|
|
$lookup->addException( new EntityLookupException( $id ) ); |
145
|
|
|
|
146
|
|
|
$this->expectException( PropertyLookupException::class ); |
147
|
|
|
$lookup->getPropertyForId( $id ); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
} |
151
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: