Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
76.92% covered (warning)
76.92%
10 / 13
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
FauxSearchResultSet
83.33% covered (warning)
83.33%
10 / 12
50.00% covered (danger)
50.00%
1 / 2
4.07
0.00% covered (danger)
0.00%
0 / 1
 __construct
81.82% covered (warning)
81.82%
9 / 11
0.00% covered (danger)
0.00%
0 / 1
3.05
 getTotalHits
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace MediaWiki\Search;
4
5use InvalidArgumentException;
6use MediaWiki\Title\Title;
7
8/**
9 * A manually constructed search result set.
10 * Mainly meant for supporting developer setups where the search operation might be
11 * mocked or proxied.
12 */
13class FauxSearchResultSet extends SearchResultSet {
14
15    /**
16     * @var int|null
17     * @see getTotalHits
18     */
19    private $totalHits;
20
21    /**
22     * @param array<Title|SearchResult> $results Search results
23     * @param int|null $totalHits See getTotalHits()
24     */
25    public function __construct( array $results, $totalHits = null ) {
26        $totalHits = max( count( $results ), $totalHits );
27        $hasMoreResults = count( $results ) < $totalHits;
28        parent::__construct( false, $hasMoreResults );
29        $this->results = array_map( static function ( $result ) {
30            if ( $result instanceof SearchResult ) {
31                return $result;
32            } elseif ( $result instanceof Title ) {
33                return new FauxSearchResult( $result );
34            } else {
35                throw new InvalidArgumentException( '$results must contain Title or SearchResult' );
36            }
37        }, $results );
38        $this->totalHits = $totalHits;
39    }
40
41    /** @inheritDoc */
42    public function getTotalHits() {
43        return $this->totalHits;
44    }
45
46}
47
48/** @deprecated class alias since 1.46 */
49class_alias( FauxSearchResultSet::class, 'FauxSearchResultSet' );