1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wikibase\DataModel\Services\Tests\Lookup; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
6
|
|
|
use Wikibase\DataModel\Entity\Item; |
7
|
|
|
use Wikibase\DataModel\Entity\ItemId; |
8
|
|
|
use Wikibase\DataModel\Services\Lookup\EntityLookup; |
9
|
|
|
use Wikibase\DataModel\Services\Lookup\EntityLookupException; |
10
|
|
|
use Wikibase\DataModel\Services\Lookup\EntityRetrievingTermLookup; |
11
|
|
|
use Wikibase\DataModel\Services\Lookup\InMemoryEntityLookup; |
12
|
|
|
use Wikibase\DataModel\Services\Lookup\TermLookupException; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @covers \Wikibase\DataModel\Services\Lookup\EntityRetrievingTermLookup |
16
|
|
|
* |
17
|
|
|
* @license GPL-2.0-or-later |
18
|
|
|
* @author Katie Filbert < [email protected] > |
19
|
|
|
* @author Daniel Kinzler |
20
|
|
|
*/ |
21
|
|
|
class EntityRetrievingTermLookupTest extends TestCase { |
22
|
|
|
|
23
|
|
|
public function testGetLabel() { |
24
|
|
|
$termLookup = $this->getEntityRetrievingTermLookup(); |
25
|
|
|
|
26
|
|
|
$label = $termLookup->getLabel( new ItemId( 'Q116' ), 'en' ); |
27
|
|
|
$this->assertEquals( 'New York City', $label ); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function testGetLabel_nullOnNoLabel() { |
31
|
|
|
$termLookup = $this->getEntityRetrievingTermLookup(); |
32
|
|
|
|
33
|
|
|
$this->assertNull( $termLookup->getLabel( new ItemId( 'Q116' ), 'fa' ) ); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function testGetLabel_entityNotFoundThrowsException() { |
37
|
|
|
$termLookup = $this->getEntityRetrievingTermLookup(); |
38
|
|
|
|
39
|
|
|
$this->expectException( TermLookupException::class ); |
40
|
|
|
$termLookup->getLabel( new ItemId( 'Q120' ), 'en' ); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testGetLabel_entityLookupExceptionGetsHandled() { |
44
|
|
|
$termLookup = $this->getEntityRetrievingTermLookup(); |
45
|
|
|
|
46
|
|
|
$this->expectException( TermLookupException::class ); |
47
|
|
|
$termLookup->getLabel( new ItemId( 'Q503' ), 'en' ); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function getLabelsProvider() { |
51
|
|
|
return [ |
52
|
|
|
[ |
53
|
|
|
[ 'en' => 'New York City', 'es' => 'Nueva York' ], |
54
|
|
|
new ItemId( 'Q116' ), |
55
|
|
|
[ 'en', 'es' ] |
56
|
|
|
], |
57
|
|
|
[ |
58
|
|
|
[ 'es' => 'Nueva York' ], |
59
|
|
|
new ItemId( 'Q116' ), |
60
|
|
|
[ 'es' ] |
61
|
|
|
], |
62
|
|
|
[ |
63
|
|
|
[ 'de' => 'Berlin' ], |
64
|
|
|
new ItemId( 'Q117' ), |
65
|
|
|
[ 'de' ] |
66
|
|
|
] |
67
|
|
|
]; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @dataProvider getLabelsProvider |
72
|
|
|
*/ |
73
|
|
|
public function testGetLabels( array $expected, ItemId $itemId, array $languageCodes ) { |
74
|
|
|
$termLookup = $this->getEntityRetrievingTermLookup(); |
75
|
|
|
|
76
|
|
|
$labels = $termLookup->getLabels( $itemId, $languageCodes ); |
77
|
|
|
$this->assertEquals( $expected, $labels ); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function testGetDescription() { |
81
|
|
|
$termLookup = $this->getEntityRetrievingTermLookup(); |
82
|
|
|
|
83
|
|
|
$description = $termLookup->getDescription( new ItemId( 'Q116' ), 'de' ); |
84
|
|
|
$expected = 'Metropole an der Ostküste der Vereinigten Staaten'; |
85
|
|
|
|
86
|
|
|
$this->assertEquals( $expected, $description ); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function testGetDescription_entityNotFoundThrowsException() { |
90
|
|
|
$termLookup = $this->getEntityRetrievingTermLookup(); |
91
|
|
|
|
92
|
|
|
$this->expectException( TermLookupException::class ); |
93
|
|
|
$termLookup->getDescription( new ItemId( 'Q120' ), 'en' ); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function testGetDescription_entityLookupExceptionGetHandled() { |
97
|
|
|
$termLookup = $this->getEntityRetrievingTermLookup(); |
98
|
|
|
|
99
|
|
|
$this->expectException( TermLookupException::class ); |
100
|
|
|
$termLookup->getDescription( new ItemId( 'Q503' ), 'en' ); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function testGetDescription_nullOnNoDescription() { |
104
|
|
|
$termLookup = $this->getEntityRetrievingTermLookup(); |
105
|
|
|
|
106
|
|
|
$this->assertNull( $termLookup->getDescription( new ItemId( 'Q116' ), 'fr' ) ); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function getDescriptionsProvider() { |
110
|
|
|
return [ |
111
|
|
|
[ |
112
|
|
|
[ |
113
|
|
|
'de' => 'Metropole an der Ostküste der Vereinigten Staaten', |
114
|
|
|
'en' => 'largest city in New York and the United States of America', |
115
|
|
|
], |
116
|
|
|
new ItemId( 'Q116' ), |
117
|
|
|
[ 'de', 'en' ] |
118
|
|
|
], |
119
|
|
|
[ |
120
|
|
|
[ |
121
|
|
|
'de' => 'Metropole an der Ostküste der Vereinigten Staaten', |
122
|
|
|
], |
123
|
|
|
new ItemId( 'Q116' ), |
124
|
|
|
[ 'de', 'fr' ] |
125
|
|
|
], |
126
|
|
|
[ |
127
|
|
|
[], |
128
|
|
|
new ItemId( 'Q117' ), |
129
|
|
|
[] |
130
|
|
|
] |
131
|
|
|
]; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @dataProvider getDescriptionsProvider |
136
|
|
|
*/ |
137
|
|
|
public function testGetDescriptions( array $expected, ItemId $itemId, array $languageCodes ) { |
138
|
|
|
$termLookup = $this->getEntityRetrievingTermLookup(); |
139
|
|
|
|
140
|
|
|
$descriptions = $termLookup->getDescriptions( $itemId, $languageCodes ); |
141
|
|
|
$this->assertEquals( $expected, $descriptions ); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @return EntityRetrievingTermLookup |
146
|
|
|
*/ |
147
|
|
|
private function getEntityRetrievingTermLookup() { |
148
|
|
|
return new EntityRetrievingTermLookup( $this->getEntityLookup() ); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @return EntityLookup |
153
|
|
|
*/ |
154
|
|
|
private function getEntityLookup() { |
155
|
|
|
$entityLookup = new InMemoryEntityLookup(); |
156
|
|
|
$entityLookup->addException( new EntityLookupException( new ItemId( 'Q503' ) ) ); |
157
|
|
|
|
158
|
|
|
$item = new Item( new ItemId( 'Q116' ) ); |
159
|
|
|
|
160
|
|
|
$item->setLabel( 'en', 'New York City' ); |
161
|
|
|
$item->setLabel( 'es', 'Nueva York' ); |
162
|
|
|
|
163
|
|
|
$item->setDescription( 'de', 'Metropole an der Ostküste der Vereinigten Staaten' ); |
164
|
|
|
$item->setDescription( 'en', 'largest city in New York and the United States of America' ); |
165
|
|
|
|
166
|
|
|
$entityLookup->addEntity( $item ); |
167
|
|
|
|
168
|
|
|
$item = new Item( new ItemId( 'Q117' ) ); |
169
|
|
|
|
170
|
|
|
$item->setLabel( 'de', 'Berlin' ); |
171
|
|
|
|
172
|
|
|
$entityLookup->addEntity( $item ); |
173
|
|
|
|
174
|
|
|
return $entityLookup; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
} |
178
|
|
|
|