TermStoreTest   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 246
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 7
dl 0
loc 246
rs 10
c 0
b 0
f 0

17 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 3 1
A testGivenNotMatchingArgs_getTermByIdAndLanguageReturnsNull() 0 3 1
A testStoreIdAndFingerprint() 0 26 1
A testGetIdByLabelAndLanguage() 0 14 1
A testStoreFingerprintRemovesOldData() 0 23 1
A testGivenNonMatchingArgs_getAliasesReturnsEmptyArray() 0 3 1
A testGetIdByTextReturnsMatchBasedOnLabel() 0 22 1
A testGetIdByTextReturnsAliasBasedMatchIfNoLabelsMatch() 0 22 1
A testByLabelLookupIsCaseInsensitive() 0 13 1
A testGetItemIdByLabelReturnsNoPropertyIds() 0 10 1
A testGetPropertyIdByLabelReturnsNoItemIds() 0 10 1
A testGetItemIdByLabelReturnsItemIds() 0 13 1
A testGetPropertyIdByLabelReturnsPropertyIds() 0 13 1
A testGetItemIdByTextReturnsNoPropertyIds() 0 10 1
A testGetPropertyIdByTextReturnsNoItemIds() 0 10 1
A testGetItemIdByTextReturnsItemIds() 0 13 1
A testGetPropertyIdByTextReturnsPropertyIds() 0 13 1
1
<?php
2
3
namespace Tests\Queryr\TermStore;
4
5
use PHPUnit\Framework\TestCase;
6
use Queryr\TermStore\TermStore;
7
use Wikibase\DataModel\Entity\ItemId;
8
use Wikibase\DataModel\Entity\PropertyId;
9
use Wikibase\DataModel\Term\Fingerprint;
10
11
/**
12
 * @covers \Queryr\TermStore\TermStore
13
 *
14
 * @covers \Queryr\TermStore\IdLookup
15
 * @covers \Queryr\TermStore\TermStoreWriter
16
 * @covers \Queryr\TermStore\TableQueryExecutor
17
 *
18
 * @licence GNU GPL v2+
19
 * @author Jeroen De Dauw < [email protected] >
20
 */
21
class TermStoreTest extends TestCase {
22
23
	/**
24
	 * @var TermStore
25
	 */
26
	private $store;
27
28
	public function setUp() {
29
		$this->store = TestEnvironment::newInstance()->getFactory()->newTermStore();
30
	}
31
32
	public function testGivenNotMatchingArgs_getTermByIdAndLanguageReturnsNull() {
33
		$this->assertNull( $this->store->getLabelByIdAndLanguage( new ItemId( 'Q1337' ), 'en' ) );
34
	}
35
36
	public function testStoreIdAndFingerprint() {
37
		$id = new ItemId( 'Q1337' );
38
39
		$fingerprint = new Fingerprint();
40
		$fingerprint->setLabel( 'en', 'en label' );
41
		$fingerprint->setLabel( 'de', 'de label' );
42
		$fingerprint->setDescription( 'en', 'en description' );
43
		$fingerprint->setAliasGroup( 'en', [ 'first en alias', 'second en alias' ] );
44
45
		$this->store->storeEntityFingerprint( $id, $fingerprint );
46
47
		$this->assertSame(
48
			'en label',
49
			$this->store->getLabelByIdAndLanguage( $id, 'en' )
50
		);
51
52
		$this->assertSame(
53
			'de label',
54
			$this->store->getLabelByIdAndLanguage( $id, 'de' )
55
		);
56
57
		$this->assertSame(
58
			[ 'first en alias', 'second en alias' ],
59
			$this->store->getAliasesByIdAndLanguage( $id, 'en' )
60
		);
61
	}
62
63
	public function testGetIdByLabelAndLanguage() {
64
		$id = new ItemId( 'Q1337' );
65
66
		$fingerprint = new Fingerprint();
67
		$fingerprint->setLabel( 'en', 'en label' );
68
		$fingerprint->setLabel( 'de', 'de label' );
69
70
		$this->store->storeEntityFingerprint( $id, $fingerprint );
71
72
		$this->assertSame(
73
			'Q1337',
74
			$this->store->getIdByLabel( 'en', 'en label' )
75
		);
76
	}
77
78
	public function testStoreFingerprintRemovesOldData() {
79
		$id = new ItemId( 'Q1337' );
80
81
		$fingerprint = new Fingerprint();
82
		$fingerprint->setLabel( 'en', 'en label' );
83
		$fingerprint->setLabel( 'de', 'de label' );
84
		$fingerprint->setAliasGroup( 'en', [ 'first en alias', 'second en alias' ] );
85
86
		$this->store->storeEntityFingerprint( $id, $fingerprint );
87
88
		$fingerprint = new Fingerprint();
89
		$fingerprint->setLabel( 'de', 'new de label' );
90
91
		$this->store->storeEntityFingerprint( $id, $fingerprint );
92
93
		$this->assertEquals(
94
			'new de label',
95
			$this->store->getLabelByIdAndLanguage( $id, 'de' )
96
		);
97
98
		$this->assertNull( $this->store->getLabelByIdAndLanguage( $id, 'en' ) );
99
		$this->assertEmpty( $this->store->getAliasesByIdAndLanguage( $id, 'en' ) );
100
	}
101
102
	public function testGivenNonMatchingArgs_getAliasesReturnsEmptyArray() {
103
		$this->assertSame( [], $this->store->getAliasesByIdAndLanguage( new ItemId( 'Q1337' ), 'en' ) );
104
	}
105
106
	public function testGetIdByTextReturnsMatchBasedOnLabel() {
107
		$id = new ItemId( 'Q1337' );
108
109
		$fingerprint = new Fingerprint();
110
		$fingerprint->setLabel( 'en', 'kittens' );
111
		$fingerprint->setAliasGroup( 'en', [ 'first en alias', 'second en alias' ] );
112
113
		$this->store->storeEntityFingerprint( $id, $fingerprint );
114
115
		$id = new ItemId( 'Q42' );
116
117
		$fingerprint = new Fingerprint();
118
		$fingerprint->setLabel( 'en', 'foobar' );
119
		$fingerprint->setAliasGroup( 'en', [ 'kittens', 'first en alias' ] );
120
121
		$this->store->storeEntityFingerprint( $id, $fingerprint );
122
123
		$this->assertSame(
124
			'Q1337',
125
			$this->store->getIdByText( 'en', 'kittens' )
126
		);
127
	}
128
129
	public function testGetIdByTextReturnsAliasBasedMatchIfNoLabelsMatch() {
130
		$id = new ItemId( 'Q1337' );
131
132
		$fingerprint = new Fingerprint();
133
		$fingerprint->setLabel( 'en', 'foobar' );
134
		$fingerprint->setAliasGroup( 'en', [ 'first en alias', 'second en alias' ] );
135
136
		$this->store->storeEntityFingerprint( $id, $fingerprint );
137
138
		$id = new ItemId( 'Q42' );
139
140
		$fingerprint = new Fingerprint();
141
		$fingerprint->setLabel( 'en', 'foobar' );
142
		$fingerprint->setAliasGroup( 'en', [ 'kittens', 'first en alias' ] );
143
144
		$this->store->storeEntityFingerprint( $id, $fingerprint );
145
146
		$this->assertSame(
147
			'Q42',
148
			$this->store->getIdByText( 'en', 'kittens' )
149
		);
150
	}
151
152
	public function testByLabelLookupIsCaseInsensitive() {
153
		$id = new ItemId( 'Q1337' );
154
155
		$fingerprint = new Fingerprint();
156
		$fingerprint->setLabel( 'en', 'EN label' );
157
158
		$this->store->storeEntityFingerprint( $id, $fingerprint );
159
160
		$this->assertSame(
161
			'Q1337',
162
			$this->store->getIdByLabel( 'en', 'en LABEL' )
163
		);
164
	}
165
166
	public function testGetItemIdByLabelReturnsNoPropertyIds() {
167
		$id = new PropertyId( 'P1337' );
168
169
		$fingerprint = new Fingerprint();
170
		$fingerprint->setLabel( 'en', 'some label' );
171
172
		$this->store->storeEntityFingerprint( $id, $fingerprint );
173
174
		$this->assertNull( $this->store->getItemIdByLabel( 'en', 'some label' ) );
175
	}
176
177
	public function testGetPropertyIdByLabelReturnsNoItemIds() {
178
		$id = new ItemId( 'Q1337' );
179
180
		$fingerprint = new Fingerprint();
181
		$fingerprint->setLabel( 'en', 'some label' );
182
183
		$this->store->storeEntityFingerprint( $id, $fingerprint );
184
185
		$this->assertNull( $this->store->getPropertyIdByLabel( 'en', 'some label' ) );
186
	}
187
188
	public function testGetItemIdByLabelReturnsItemIds() {
189
		$id = new ItemId( 'Q1337' );
190
191
		$fingerprint = new Fingerprint();
192
		$fingerprint->setLabel( 'en', 'some label' );
193
194
		$this->store->storeEntityFingerprint( $id, $fingerprint );
195
196
		$this->assertSame(
197
			'Q1337',
198
			$this->store->getItemIdByLabel( 'en', 'some label' )
199
		);
200
	}
201
202
	public function testGetPropertyIdByLabelReturnsPropertyIds() {
203
		$id = new PropertyId( 'P1337' );
204
205
		$fingerprint = new Fingerprint();
206
		$fingerprint->setLabel( 'en', 'some label' );
207
208
		$this->store->storeEntityFingerprint( $id, $fingerprint );
209
210
		$this->assertSame(
211
			'P1337',
212
			$this->store->getPropertyIdByLabel( 'en', 'some label' )
213
		);
214
	}
215
216
	public function testGetItemIdByTextReturnsNoPropertyIds() {
217
		$id = new PropertyId( 'P1337' );
218
219
		$fingerprint = new Fingerprint();
220
		$fingerprint->setLabel( 'en', 'some label' );
221
222
		$this->store->storeEntityFingerprint( $id, $fingerprint );
223
224
		$this->assertNull( $this->store->getItemIdByText( 'en', 'some label' ) );
225
	}
226
227
	public function testGetPropertyIdByTextReturnsNoItemIds() {
228
		$id = new ItemId( 'Q1337' );
229
230
		$fingerprint = new Fingerprint();
231
		$fingerprint->setLabel( 'en', 'some label' );
232
233
		$this->store->storeEntityFingerprint( $id, $fingerprint );
234
235
		$this->assertNull( $this->store->getPropertyIdByText( 'en', 'some label' ) );
236
	}
237
238
	public function testGetItemIdByTextReturnsItemIds() {
239
		$id = new ItemId( 'Q1337' );
240
241
		$fingerprint = new Fingerprint();
242
		$fingerprint->setLabel( 'en', 'some label' );
243
244
		$this->store->storeEntityFingerprint( $id, $fingerprint );
245
246
		$this->assertSame(
247
			'Q1337',
248
			$this->store->getItemIdByText( 'en', 'some label' )
249
		);
250
	}
251
252
	public function testGetPropertyIdByTextReturnsPropertyIds() {
253
		$id = new PropertyId( 'P1337' );
254
255
		$fingerprint = new Fingerprint();
256
		$fingerprint->setLabel( 'en', 'some label' );
257
258
		$this->store->storeEntityFingerprint( $id, $fingerprint );
259
260
		$this->assertSame(
261
			'P1337',
262
			$this->store->getPropertyIdByText( 'en', 'some label' )
263
		);
264
	}
265
266
}
267