1<?php 2 3/** 4 * @group Search 5 * @covers FauxSearchResultSet 6 */ 7class FauxSearchResultSetTest extends MediaWikiUnitTestCase { 8 9 public function testConstruct() { 10 $titles = array_map( [ $this, 'getTitleMock' ], [ 'Foo', 'Bar', 'Baz' ] ); 11 $rs = new FauxSearchResultSet( $titles ); 12 $titleTexts = []; 13 foreach ( $rs as $result ) { 14 /** @var $result SearchResult */ 15 $titleTexts[] = $result->getTitle()->getPrefixedText(); 16 } 17 $this->assertSame( [ 'Foo', 'Bar', 'Baz' ], $titleTexts ); 18 $this->assertSame( $titles, $rs->extractTitles() ); 19 } 20 21 public function testGetTotalHits() { 22 $titles = array_map( [ $this, 'getTitleMock' ], [ 'Foo', 'Bar', 'Baz' ] ); 23 $rs = new FauxSearchResultSet( $titles ); 24 $this->assertSame( 3, $rs->getTotalHits() ); 25 $this->assertFalse( $rs->hasMoreResults() ); 26 27 $rs = new FauxSearchResultSet( $titles, 5 ); 28 $this->assertCount( 3, $rs ); 29 $this->assertSame( 5, $rs->getTotalHits() ); 30 $this->assertTrue( $rs->hasMoreResults() ); 31 32 $rs = new FauxSearchResultSet( $titles, 3 ); 33 $this->assertFalse( $rs->hasMoreResults() ); 34 $rs = new FauxSearchResultSet( $titles, 1 ); 35 $this->assertSame( 3, $rs->getTotalHits() ); 36 $this->assertFalse( $rs->hasMoreResults() ); 37 } 38 39 /** 40 * @param string $titleText 41 * @return Title 42 */ 43 private function getTitleMock( $titleText ) { 44 $title = $this->getMockBuilder( Title::class ) 45 ->disableOriginalConstructor() 46 ->onlyMethods( [ 'getPrefixedText' ] ) 47 ->getMock(); 48 $title->method( 'getPrefixedText' )->willReturn( $titleText ); 49 return $title; 50 } 51 52} 53