| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Revision\RevisionLookup; |
| 4 |
use MediaWiki\Title\Title; |
| 5 |
|
| 6 |
class SearchResultSetTest extends MediaWikiIntegrationTestCase { |
| 7 |
protected function setUp(): void { |
| 8 |
parent::setUp(); |
| 9 |
$this->setService( 'RevisionLookup', $this->createMock( RevisionLookup::class ) ); |
| 10 |
} |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 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() ); |
| 22 |
$count = 0; |
| 23 |
foreach ( $resultSet as $iterResult ) { |
| 24 |
$this->assertEquals( $result, $iterResult ); |
| 25 |
$count++; |
| 26 |
} |
| 27 |
$this->assertSame( 1, $count ); |
| 28 |
|
| 29 |
$this->hideDeprecated( 'BaseSearchResultSet::rewind' ); |
| 30 |
$this->hideDeprecated( 'BaseSearchResultSet::next' ); |
| 31 |
$resultSet->rewind(); |
| 32 |
$count = 0; |
| 33 |
foreach ( $resultSet as $iterResult ) { |
| 34 |
$this->assertEquals( $result, $iterResult ); |
| 35 |
$count++; |
| 36 |
} |
| 37 |
$this->assertSame( 1, $count ); |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 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' |
| 53 |
] ); |
| 54 |
$this->assertEquals( [ 'foo' => 'bar' ], $result->getExtensionData() ); |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 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() ); |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 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() ); |
| 86 |
} |
| 87 |
} |