3 use MediaWiki\Revision\RevisionLookup
;
4 use MediaWiki\Title\Title
;
6 class SearchResultSetTest
extends MediaWikiIntegrationTestCase
{
7 protected function setUp(): void
{
9 $this->setService( 'RevisionLookup', $this->createMock( RevisionLookup
::class ) );
13 * @covers \SearchResultSet::getIterator
14 * @covers \BaseSearchResultSet::next
15 * @covers \BaseSearchResultSet::rewind
17 public function testIterate() {
18 $title = Title
::makeTitle( NS_MAIN
, __METHOD__
);
19 $result = SearchResult
::newFromTitle( $title );
20 $resultSet = new MockSearchResultSet( [ $result ] );
21 $this->assertSame( 1, $resultSet->numRows() );
23 foreach ( $resultSet as $iterResult ) {
24 $this->assertEquals( $result, $iterResult );
27 $this->assertSame( 1, $count );
29 $this->hideDeprecated( 'BaseSearchResultSet::rewind' );
30 $this->hideDeprecated( 'BaseSearchResultSet::next' );
33 foreach ( $resultSet as $iterResult ) {
34 $this->assertEquals( $result, $iterResult );
37 $this->assertSame( 1, $count );
41 * @covers \SearchResultSetTrait::augmentResult
42 * @covers \SearchResultSetTrait::setAugmentedData
44 public function testDelayedResultAugment() {
45 $title = Title
::makeTitle( NS_MAIN
, __METHOD__
);
46 $title->resetArticleID( 42 );
47 $result = SearchResult
::newFromTitle( $title );
48 $resultSet = new MockSearchResultSet( [ $result ] );
49 $resultSet->augmentResult( $result );
50 $this->assertEquals( [], $result->getExtensionData() );
51 $resultSet->setAugmentedData( 'foo', [
52 $result->getTitle()->getArticleID() => 'bar'
54 $this->assertEquals( [ 'foo' => 'bar' ], $result->getExtensionData() );
58 * @covers \SearchResultSet::shrink
59 * @covers \SearchResultSet::count
60 * @covers \SearchResultSet::hasMoreResults
62 public function testHasMoreResults() {
63 $title = Title
::makeTitle( NS_MAIN
, __METHOD__
);
64 $result = SearchResult
::newFromTitle( $title );
65 $resultSet = new MockSearchResultSet( array_fill( 0, 3, $result ) );
66 $this->assertCount( 3, $resultSet );
67 $this->assertFalse( $resultSet->hasMoreResults() );
68 $resultSet->shrink( 3 );
69 $this->assertFalse( $resultSet->hasMoreResults() );
70 $resultSet->shrink( 2 );
71 $this->assertTrue( $resultSet->hasMoreResults() );
75 * @covers \SearchResultSet::shrink
77 public function testShrink() {
78 $title = Title
::makeTitle( NS_MAIN
, __METHOD__
);
79 $results = array_fill( 0, 3, SearchResult
::newFromTitle( $title ) );
80 $resultSet = new MockSearchResultSet( $results );
81 $this->assertCount( 3, $resultSet->extractResults() );
82 $this->assertCount( 3, $resultSet->extractTitles() );
83 $resultSet->shrink( 1 );
84 $this->assertCount( 1, $resultSet->extractResults() );
85 $this->assertCount( 1, $resultSet->extractTitles() );