Localisation updates from https://translatewiki.net.
[mediawiki.git] / tests / phpunit / mocks / search / MockSearchEngine.php
blob5d91bd9b7277bfb2e00a840b3eab31d087185c1b
1 <?php
3 class MockSearchEngine extends SearchEngine {
4 use LinkCacheTestTrait;
6 /** @var SearchResult[][] */
7 private static $results = [];
8 /** @var ISearchResultSet[][] */
9 private static $interwikiResults = [];
11 public static function clearMockResults() {
12 self::$results = [];
13 self::$interwikiResults = [];
16 /**
17 * @param string $query The query searched for *after* initial
18 * transformations have been applied.
19 * @param SearchResult[] $results The results to return for $query
21 public static function addMockResults( $query, array $results ) {
22 $mockSearchEngine = new self();
23 foreach ( $results as &$result ) {
24 // Resolve deferred results; needed to work around T203279
25 if ( is_callable( $result ) ) {
26 $result = $result();
29 // TODO: better page ids? Does it matter?
30 $mockSearchEngine->addGoodLinkObject( mt_rand(), $result->getTitle() );
32 self::$results[$query] = $results;
35 /**
36 * @param ISearchResultSet[][] $interwikiResults
38 public static function setMockInterwikiResults( array $interwikiResults ) {
39 self::$interwikiResults = $interwikiResults;
42 protected function doSearchText( $term ) {
43 if ( isset( self::$results[ $term ] ) ) {
44 $results = array_slice( self::$results[ $term ], $this->offset, $this->limit );
45 } else {
46 $results = [];
48 return new MockSearchResultSet( $results, self::$interwikiResults );