1616use Wikibase \DataModel \Term \TermTypes ;
1717use Wikibase \DataModel \Tests \NewItem ;
1818use Wikibase \Lib \LanguageFallbackChainFactory ;
19- use Wikibase \Lib \StaticContentLanguages ;
2019use Wikibase \Lib \Store \MatchingTermsLookup ;
2120use Wikibase \Lib \TermIndexEntry ;
2221use Wikibase \Lib \TermLanguageFallbackChain ;
3938 */
4039class SqlTermStoreSearchEngineTest extends TestCase {
4140
42- private MatchingTermsLookup $ matchingTermsLookup ;
43- private TermRetriever $ termRetriever ;
44- private EntityLookup $ entityLookup ;
45- private TermLanguageFallbackChain $ termLanguageFallbackChain ;
46-
47- protected function setUp (): void {
48- parent ::setUp ();
49- $ this ->matchingTermsLookup = $ this ->createStub ( MatchingTermsLookup::class );
50- $ this ->termRetriever = $ this ->createStub ( TermRetriever::class );
51- $ this ->entityLookup = $ this ->createStub ( EntityLookup::class );
52- $ this ->termLanguageFallbackChain = new TermLanguageFallbackChain ( [], new StaticContentLanguages ( [] ) );
53- }
54-
5541 public function testGivenSearchResultForItemLabel (): void {
5642 $ searchTerm = 'potato ' ;
5743 $ languageCode = 'en ' ;
@@ -63,25 +49,26 @@ public function testGivenSearchResultForItemLabel(): void {
6349 new MatchedData ( TermTypes::TYPE_LABEL , 'en ' , 'potato ' )
6450 );
6551
66- $ this -> matchingTermsLookup = $ this ->createMock ( MatchingTermsLookup::class );
67- $ this -> matchingTermsLookup ->expects ( $ this ->once () )
52+ $ matchingTermsLookup = $ this ->createMock ( MatchingTermsLookup::class );
53+ $ matchingTermsLookup ->expects ( $ this ->once () )
6854 ->method ( 'getMatchingTerms ' )
6955 ->willReturn (
7056 [ $ this ->newTermIndexEntry ( TermTypes::TYPE_LABEL , new ItemId ( 'Q123 ' ), 'en ' , 'potato ' ) ]
7157 );
7258
73- $ this -> termRetriever = $ this ->createMock ( TermRetriever::class );
59+ $ termRetriever = $ this ->createMock ( TermRetriever::class );
7460 // we will not call TermRetriever::getLabel() because the index entry is of type label already
75- $ this -> termRetriever ->expects ( $ this ->never () )
61+ $ termRetriever ->expects ( $ this ->never () )
7662 ->method ( 'getLabel ' );
77- $ this -> termRetriever ->expects ( $ this ->once () )
63+ $ termRetriever ->expects ( $ this ->once () )
7864 ->method ( 'getDescription ' )
7965 ->with ( 'Q123 ' , $ languageCode )
8066 ->willReturn ( new Description ( 'en ' , 'staple food ' ) );
8167
8268 $ this ->assertEquals (
8369 new ItemSearchResults ( $ expectedSearchResult ),
84- $ this ->newEngine ()->searchItemByLabel ( $ searchTerm , $ languageCode , 10 , 0 )
70+ $ this ->newEngine ( matchingTermsLookup: $ matchingTermsLookup , termRetriever: $ termRetriever )
71+ ->searchItemByLabel ( $ searchTerm , $ languageCode , 10 , 0 )
8572 );
8673 }
8774
@@ -96,25 +83,26 @@ public function testGivenSearchResultForPropertyLabel(): void {
9683 new MatchedData ( TermTypes::TYPE_LABEL , 'en ' , 'instance of ' )
9784 );
9885
99- $ this -> matchingTermsLookup = $ this ->createMock ( MatchingTermsLookup::class );
100- $ this -> matchingTermsLookup ->expects ( $ this ->once () )
86+ $ matchingTermsLookup = $ this ->createMock ( MatchingTermsLookup::class );
87+ $ matchingTermsLookup ->expects ( $ this ->once () )
10188 ->method ( 'getMatchingTerms ' )
10289 ->willReturn (
10390 [ $ this ->newTermIndexEntry ( TermTypes::TYPE_LABEL , new NumericPropertyId ( 'P123 ' ), 'en ' , 'instance of ' ) ]
10491 );
10592
106- $ this -> termRetriever = $ this ->createMock ( TermRetriever::class );
93+ $ termRetriever = $ this ->createMock ( TermRetriever::class );
10794 // we will not call TermRetriever::getLabel() because the index entry is of type label already
108- $ this -> termRetriever ->expects ( $ this ->never () )
95+ $ termRetriever ->expects ( $ this ->never () )
10996 ->method ( 'getLabel ' );
110- $ this -> termRetriever ->expects ( $ this ->once () )
97+ $ termRetriever ->expects ( $ this ->once () )
11198 ->method ( 'getDescription ' )
11299 ->with ( 'P123 ' , $ languageCode )
113100 ->willReturn ( new Description ( 'en ' , 'the class of which this subject is a particular example and member ' ) );
114101
115102 $ this ->assertEquals (
116103 new PropertySearchResults ( $ expectedSearchResult ),
117- $ this ->newEngine ()->searchPropertyByLabel ( $ searchTerm , $ languageCode , 10 , 0 )
104+ $ this ->newEngine ( matchingTermsLookup: $ matchingTermsLookup , termRetriever: $ termRetriever )
105+ ->searchPropertyByLabel ( $ searchTerm , $ languageCode , 10 , 0 )
118106 );
119107 }
120108
@@ -129,26 +117,26 @@ public function testGivenSearchResultForAlias(): void {
129117 new MatchedData ( TermTypes::TYPE_ALIAS , 'en ' , 'spud ' )
130118 );
131119
132- $ this -> matchingTermsLookup = $ this ->createMock ( MatchingTermsLookup::class );
133- $ this -> matchingTermsLookup ->expects ( $ this ->once () )
120+ $ matchingTermsLookup = $ this ->createMock ( MatchingTermsLookup::class );
121+ $ matchingTermsLookup ->expects ( $ this ->once () )
134122 ->method ( 'getMatchingTerms ' )
135123 ->willReturn (
136124 [ $ this ->newTermIndexEntry ( TermTypes::TYPE_ALIAS , new ItemId ( 'Q123 ' ), 'en ' , 'spud ' ) ]
137125 );
138126
139- $ this -> termRetriever = $ this ->createMock ( TermRetriever::class );
140- $ this -> termRetriever ->expects ( $ this ->once () )
127+ $ termRetriever = $ this ->createMock ( TermRetriever::class );
128+ $ termRetriever ->expects ( $ this ->once () )
141129 ->method ( 'getLabel ' )
142130 ->with ( 'Q123 ' , $ languageCode )
143131 ->willReturn ( new Label ( 'en ' , 'potato ' ) );
144- $ this -> termRetriever ->expects ( $ this ->once () )
132+ $ termRetriever ->expects ( $ this ->once () )
145133 ->method ( 'getDescription ' )
146134 ->with ( 'Q123 ' , $ languageCode )
147135 ->willReturn ( new Description ( 'en ' , 'staple food ' ) );
148136
149137 $ this ->assertEquals (
150138 new ItemSearchResults ( $ expectedSearchResult ),
151- $ this ->newEngine ()->searchItemByLabel (
139+ $ this ->newEngine ( matchingTermsLookup: $ matchingTermsLookup , termRetriever: $ termRetriever )->searchItemByLabel (
152140 $ searchTerm ,
153141 $ languageCode ,
154142 10 ,
@@ -170,16 +158,14 @@ public function testGivenSearchResultForItemId(): void {
170158 new MatchedData ( 'entityId ' , null , "$ itemId " )
171159 );
172160
173- $ this ->matchingTermsLookup = $ this ->createStub ( MatchingTermsLookup::class );
174- $ this ->termRetriever = $ this ->createStub ( TermRetriever::class );
175- $ this ->termLanguageFallbackChain = $ this ->createMock ( TermLanguageFallbackChain::class );
176- $ this ->termLanguageFallbackChain ->method ( 'extractPreferredValue ' )
161+ $ languageFallbackChain = $ this ->createStub ( TermLanguageFallbackChain::class );
162+ $ languageFallbackChain ->method ( 'extractPreferredValue ' )
177163 ->willReturnOnConsecutiveCalls (
178164 [ 'language ' => $ languageCode , 'value ' => $ label ],
179165 [ 'language ' => $ languageCode , 'value ' => $ description ]
180166 );
181167
182- $ this -> entityLookup = $ this ->newMockEntityLookup (
168+ $ entityLookup = $ this ->newMockEntityLookup (
183169 $ itemId ,
184170 NewItem::withId ( "$ itemId " )
185171 ->andLabel ( $ languageCode , $ label )
@@ -189,17 +175,20 @@ public function testGivenSearchResultForItemId(): void {
189175
190176 $ this ->assertEquals (
191177 new ItemSearchResults ( $ expectedSearchResult ),
192- $ this ->newEngine ()->searchItemByLabel ( "$ itemId " , $ languageCode , 10 , 0 )
178+ $ this ->newEngine (
179+ languageFallbackChain: $ languageFallbackChain ,
180+ entityLookup: $ entityLookup
181+ )->searchItemByLabel ( "$ itemId " , $ languageCode , 10 , 0 )
193182 );
194183 }
195184
196185 public function testGivenItemIdResult_limitIsAdjusted (): void {
197186 $ searchTerm = 'Q42 ' ;
198187 $ limit = 5 ;
199188
200- $ this -> entityLookup = $ this ->newMockEntityLookup ( new ItemId ( 'Q42 ' ), NewItem::withId ( 'Q42 ' )->build () );
201- $ this -> matchingTermsLookup = $ this ->createMock ( MatchingTermsLookup::class );
202- $ this -> matchingTermsLookup ->expects ( $ this ->once () )
189+ $ entityLookup = $ this ->newMockEntityLookup ( new ItemId ( 'Q42 ' ), NewItem::withId ( 'Q42 ' )->build () );
190+ $ matchingTermsLookup = $ this ->createMock ( MatchingTermsLookup::class );
191+ $ matchingTermsLookup ->expects ( $ this ->once () )
203192 ->method ( 'getMatchingTerms ' )
204193 ->with ( $ searchTerm ,
205194 Item::ENTITY_TYPE ,
@@ -212,16 +201,20 @@ public function testGivenItemIdResult_limitIsAdjusted(): void {
212201 [ 'Q1 ' , 'Q2 ' , 'Q3 ' , 'Q4 ' ]
213202 ) );
214203
215- $ this ->assertCount ( $ limit , $ this ->newEngine ()->searchItemByLabel ( $ searchTerm , 'en ' , $ limit , 0 ) );
204+ $ this ->assertCount (
205+ $ limit ,
206+ $ this ->newEngine ( entityLookup: $ entityLookup , matchingTermsLookup: $ matchingTermsLookup )
207+ ->searchItemByLabel ( $ searchTerm , 'en ' , $ limit , 0 )
208+ );
216209 }
217210
218211 public function testGivenItemIdResult_offsetIsAdjusted (): void {
219212 $ searchTerm = 'Q42 ' ;
220213 $ offset = 5 ;
221214
222- $ this -> entityLookup = $ this ->newMockEntityLookup ( new ItemId ( 'Q42 ' ), NewItem::withId ( 'Q42 ' )->build () );
223- $ this -> matchingTermsLookup = $ this ->createMock ( MatchingTermsLookup::class );
224- $ this -> matchingTermsLookup ->expects ( $ this ->once () )
215+ $ entityLookup = $ this ->newMockEntityLookup ( new ItemId ( 'Q42 ' ), NewItem::withId ( 'Q42 ' )->build () );
216+ $ matchingTermsLookup = $ this ->createMock ( MatchingTermsLookup::class );
217+ $ matchingTermsLookup ->expects ( $ this ->once () )
225218 ->method ( 'getMatchingTerms ' )
226219 ->with ( $ searchTerm ,
227220 Item::ENTITY_TYPE ,
@@ -234,7 +227,11 @@ public function testGivenItemIdResult_offsetIsAdjusted(): void {
234227 [ 'Q5 ' , 'Q6 ' , 'Q7 ' , 'Q8 ' , 'Q9 ' ]
235228 ) );
236229
237- $ this ->assertCount ( 5 , $ this ->newEngine ()->searchItemByLabel ( $ searchTerm , 'en ' , 5 , $ offset ) );
230+ $ this ->assertCount (
231+ 5 ,
232+ $ this ->newEngine ( entityLookup: $ entityLookup , matchingTermsLookup: $ matchingTermsLookup )
233+ ->searchItemByLabel ( $ searchTerm , 'en ' , 5 , $ offset )
234+ );
238235 }
239236
240237 public function testGivenSearchResultForPropertyId (): void {
@@ -250,16 +247,14 @@ public function testGivenSearchResultForPropertyId(): void {
250247 new MatchedData ( 'entityId ' , null , "$ propertyId " )
251248 );
252249
253- $ this ->matchingTermsLookup = $ this ->createStub ( MatchingTermsLookup::class );
254- $ this ->termRetriever = $ this ->createStub ( TermRetriever::class );
255- $ this ->termLanguageFallbackChain = $ this ->createMock ( TermLanguageFallbackChain::class );
256- $ this ->termLanguageFallbackChain ->method ( 'extractPreferredValue ' )
250+ $ languageFallbackChain = $ this ->createMock ( TermLanguageFallbackChain::class );
251+ $ languageFallbackChain ->method ( 'extractPreferredValue ' )
257252 ->willReturnOnConsecutiveCalls (
258253 [ 'language ' => $ languageCode , 'value ' => $ label ],
259254 [ 'language ' => $ languageCode , 'value ' => $ description ]
260255 );
261256
262- $ this -> entityLookup = $ this ->newMockEntityLookup (
257+ $ entityLookup = $ this ->newMockEntityLookup (
263258 $ propertyId ,
264259 new Property (
265260 $ propertyId ,
@@ -273,20 +268,21 @@ public function testGivenSearchResultForPropertyId(): void {
273268
274269 $ this ->assertEquals (
275270 new PropertySearchResults ( $ expectedSearchResult ),
276- $ this ->newEngine ()->searchPropertyByLabel ( "$ propertyId " , $ languageCode , 10 , 0 )
271+ $ this ->newEngine ( languageFallbackChain: $ languageFallbackChain , entityLookup: $ entityLookup )
272+ ->searchPropertyByLabel ( "$ propertyId " , $ languageCode , 10 , 0 )
277273 );
278274 }
279275
280276 public function testGivenPropertyIdResult_limitIsAdjusted (): void {
281277 $ searchTerm = 'P1 ' ;
282278 $ limit = 5 ;
283279
284- $ this -> entityLookup = $ this ->newMockEntityLookup (
280+ $ entityLookup = $ this ->newMockEntityLookup (
285281 new NumericPropertyId ( 'P1 ' ),
286282 new Property ( new NumericPropertyId ( 'P1 ' ), null , 'dataTypeId ' )
287283 );
288- $ this -> matchingTermsLookup = $ this ->createMock ( MatchingTermsLookup::class );
289- $ this -> matchingTermsLookup ->expects ( $ this ->once () )
284+ $ matchingTermsLookup = $ this ->createMock ( MatchingTermsLookup::class );
285+ $ matchingTermsLookup ->expects ( $ this ->once () )
290286 ->method ( 'getMatchingTerms ' )
291287 ->with ( $ searchTerm ,
292288 Property::ENTITY_TYPE ,
@@ -299,19 +295,23 @@ public function testGivenPropertyIdResult_limitIsAdjusted(): void {
299295 [ 'P1 ' , 'P2 ' , 'P3 ' , 'P4 ' ]
300296 ) );
301297
302- $ this ->assertCount ( $ limit , $ this ->newEngine ()->searchPropertyByLabel ( $ searchTerm , 'en ' , $ limit , 0 ) );
298+ $ this ->assertCount (
299+ $ limit ,
300+ $ this ->newEngine ( entityLookup: $ entityLookup , matchingTermsLookup: $ matchingTermsLookup )
301+ ->searchPropertyByLabel ( $ searchTerm , 'en ' , $ limit , 0 )
302+ );
303303 }
304304
305305 public function testGivenPropertyIdResult_offsetIsAdjusted (): void {
306306 $ searchTerm = 'P1 ' ;
307307 $ offset = 5 ;
308308
309- $ this -> entityLookup = $ this ->newMockEntityLookup (
309+ $ entityLookup = $ this ->newMockEntityLookup (
310310 new NumericPropertyId ( 'P1 ' ),
311311 new Property ( new NumericPropertyId ( 'P1 ' ), null , 'dataTypeId ' )
312312 );
313- $ this -> matchingTermsLookup = $ this ->createMock ( MatchingTermsLookup::class );
314- $ this -> matchingTermsLookup ->expects ( $ this ->once () )
313+ $ matchingTermsLookup = $ this ->createMock ( MatchingTermsLookup::class );
314+ $ matchingTermsLookup ->expects ( $ this ->once () )
315315 ->method ( 'getMatchingTerms ' )
316316 ->with ( $ searchTerm ,
317317 Property::ENTITY_TYPE ,
@@ -324,7 +324,11 @@ public function testGivenPropertyIdResult_offsetIsAdjusted(): void {
324324 [ 'P5 ' , 'P6 ' , 'P7 ' , 'P8 ' , 'P9 ' ]
325325 ) );
326326
327- $ this ->assertCount ( 5 , $ this ->newEngine ()->searchPropertyByLabel ( $ searchTerm , 'en ' , 5 , $ offset ) );
327+ $ this ->assertCount (
328+ 5 ,
329+ $ this ->newEngine ( entityLookup: $ entityLookup , matchingTermsLookup: $ matchingTermsLookup )
330+ ->searchPropertyByLabel ( $ searchTerm , 'en ' , 5 , $ offset )
331+ );
328332 }
329333
330334 public function testResultPagination (): void {
@@ -335,8 +339,8 @@ public function testResultPagination(): void {
335339 }
336340 }
337341
338- $ this -> matchingTermsLookup = $ this ->createMock ( MatchingTermsLookup::class );
339- $ this -> matchingTermsLookup ->expects ( $ this ->once () )
342+ $ matchingTermsLookup = $ this ->createMock ( MatchingTermsLookup::class );
343+ $ matchingTermsLookup ->expects ( $ this ->once () )
340344 ->method ( 'getMatchingTerms ' )
341345 ->with (
342346 'some query ' ,
@@ -347,7 +351,8 @@ public function testResultPagination(): void {
347351 )
348352 ->willReturn ( array_slice ( $ results , 2 , 5 ) );
349353
350- $ searchResult = $ this ->newEngine ()->searchItemByLabel ( 'some query ' , 'en ' , 5 , 2 );
354+ $ searchResult = $ this ->newEngine ( matchingTermsLookup: $ matchingTermsLookup )
355+ ->searchItemByLabel ( 'some query ' , 'en ' , 5 , 2 );
351356 $ this ->assertCount ( 5 , $ searchResult );
352357 $ this ->assertEquals ( new ItemId ( 'Q3 ' ), $ searchResult [0 ]->getItemId () );
353358 }
@@ -356,20 +361,20 @@ public function testGivenSearchFails_returnsNoResults(): void {
356361 $ searchTerm = 'potato ' ;
357362 $ languageCode = 'en ' ;
358363
359- $ this -> matchingTermsLookup = $ this ->createMock ( MatchingTermsLookup::class );
360- $ this -> matchingTermsLookup ->expects ( $ this ->once () )
364+ $ matchingTermsLookup = $ this ->createMock ( MatchingTermsLookup::class );
365+ $ matchingTermsLookup ->expects ( $ this ->once () )
361366 ->method ( 'getMatchingTerms ' )
362367 ->willReturn ( [] );
363368
364- $ this -> termRetriever = $ this ->createMock ( TermRetriever::class );
365- $ this -> termRetriever ->expects ( $ this ->never () )
369+ $ termRetriever = $ this ->createMock ( TermRetriever::class );
370+ $ termRetriever ->expects ( $ this ->never () )
366371 ->method ( 'getLabel ' );
367- $ this -> termRetriever ->expects ( $ this ->never () )
372+ $ termRetriever ->expects ( $ this ->never () )
368373 ->method ( 'getDescription ' );
369374
370375 $ this ->assertEquals (
371376 new ItemSearchResults (),
372- $ this ->newEngine ()->searchItemByLabel (
377+ $ this ->newEngine ( matchingTermsLookup: $ matchingTermsLookup , termRetriever: $ termRetriever )->searchItemByLabel (
373378 $ searchTerm ,
374379 $ languageCode ,
375380 5 ,
@@ -387,16 +392,21 @@ private function newTermIndexEntry( string $type, EntityId $id, string $language
387392 ] );
388393 }
389394
390- private function newEngine (): SqlTermStoreSearchEngine {
395+ private function newEngine (
396+ ?MatchingTermsLookup $ matchingTermsLookup = null ,
397+ ?EntityLookup $ entityLookup = null ,
398+ ?TermRetriever $ termRetriever = null ,
399+ ?TermLanguageFallbackChain $ languageFallbackChain = null
400+ ): SqlTermStoreSearchEngine {
391401 $ languageFallbackChainFactory = $ this ->createStub ( LanguageFallbackChainFactory::class );
392402 $ languageFallbackChainFactory
393403 ->method ( 'newFromLanguageCode ' )
394- ->willReturn ( $ this ->termLanguageFallbackChain );
404+ ->willReturn ( $ languageFallbackChain ?? $ this ->createStub ( TermLanguageFallbackChain::class ) );
395405
396406 return new SqlTermStoreSearchEngine (
397- $ this ->matchingTermsLookup ,
398- $ this ->entityLookup ,
399- $ this ->termRetriever ,
407+ $ matchingTermsLookup ?? $ this ->createStub ( MatchingTermsLookup::class ) ,
408+ $ entityLookup ?? $ this ->createStub ( EntityLookup::class ) ,
409+ $ termRetriever ?? $ this ->createStub ( TermRetriever::class ) ,
400410 $ languageFallbackChainFactory
401411 );
402412 }
0 commit comments