wiki.techinc.nl/tests/phpunit/unit/includes/search/FauxSearchResultTest.php
Gergő Tisza c296cf69b4
Add faux SearchResult and SearchResultSet
The use case is code that uses search internally, but wants to allow
a development setup where search is proxied via some public API and
the actual querying is performed on a production server, instead of
having to set up all the search services + staging content locally.

Also fix a small logic error in SearchResultSet::shrink.

Change-Id: I83931c1a19c1cb747380e17a4aff3b1fadd5fcf3
2019-10-07 16:35:58 +02:00

71 lines
2 KiB
PHP

<?php
use MediaWiki\Revision\MutableRevisionRecord;
use PHPUnit\Framework\MockObject\MockObject;
/**
* @group Search
* @covers FauxSearchResultSet
*/
class FauxSearchResultTest extends MediaWikiUnitTestCase {
public function testConstruct() {
$title = $this->getTitleMock( 'Foo' );
$r = new FauxSearchResult( $title );
$this->assertSame( $title, $r->getTitle() );
$this->assertTrue( $r->isMissingRevision() );
$this->assertNull( $r->getFile() );
$this->assertSame( 0, $r->getWordCount() );
$title = $this->getTitleMock( 'Foo', 1 );
$rev = new MutableRevisionRecord( $title );
$rev->setTimestamp( '20000101000000' );
$r = new FauxSearchResult( $title, $rev );
$this->assertFalse( $r->isMissingRevision() );
$this->assertSame( '20000101000000', $r->getTimestamp() );
$title = $this->getTitleMock( 'Foo', 1 );
$rev = new MutableRevisionRecord( $title );
$r = new FauxSearchResult( $title, $rev, null, '123' );
$this->assertSame( 3, $r->getByteSize() );
$title = $this->getTitleMock( 'Foo' );
$file = $this->getFileMock( 'Foo.png' );
$r = new FauxSearchResult( $title, null, $file );
$this->assertSame( $file, $r->getFile() );
}
/**
* @param $titleText
* @param int|null $articleId
* @return Title|MockObject
*/
private function getTitleMock( $titleText, $articleId = null ) {
$title = $this->getMockBuilder( Title::class )
->disableOriginalConstructor()
->setMethods( [ 'getPrefixedText', 'getArticleID' ] )
->getMock();
$title->method( 'getPrefixedText' )->willReturn( $titleText );
if ( $articleId ) {
$title->method( 'getArticleID' )->willReturn( $articleId );
} else {
$title->expects( $this->never() )->method( 'getArticleID' );
}
return $title;
}
/**
* @param string $filename
* @return File|MockObject
*/
private function getFileMock( $filename ) {
$title = $this->getMockBuilder( File::class )
->disableOriginalConstructor()
->setMethods( [ 'getName' ] )
->getMock();
$title->method( 'getName' )
->willReturn( $filename );
return $title;
}
}