Merge "Fix 'Tags' padding to keep it farther from the edge and document the source...
[lhc/web/wiklou.git] / tests / phpunit / mocks / search / MockSearchEngine.php
1 <?php
2
3 use MediaWiki\MediaWikiServices;
4
5 class MockSearchEngine extends SearchEngine {
6 /** @var SearchResult[][] */
7 private static $results = [];
8 /** @var SearchResultSet[][] */
9 private static $interwikiResults = [];
10
11 public static function clearMockResults() {
12 self::$results = [];
13 self::$interwikiResults = [];
14 }
15
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
20 */
21 public static function addMockResults( $query, array $results ) {
22 self::$results[$query] = $results;
23 $lc = MediaWikiServices::getInstance()->getLinkCache();
24 foreach ( $results as $result ) {
25 // TODO: better page ids? Does it matter?
26 $lc->addGoodLinkObj( mt_rand(), $result->getTitle() );
27 }
28 }
29
30 /**
31 * @param SearchResultSet[][] $interwikiResults
32 */
33 public static function setMockInterwikiResults( array $interwikiResults ) {
34 self::$interwikiResults = $interwikiResults;
35 }
36
37 protected function doSearchText( $term ) {
38 if ( isset( self::$results[ $term ] ) ) {
39 $results = array_slice( self::$results[ $term ], $this->offset, $this->limit );
40 } else {
41 $results = [];
42 }
43 return new MockSearchResultSet( $results, self::$interwikiResults );
44 }
45 }