wiki.techinc.nl/tests/phpunit/includes/api/ApiOpenSearchTest.php
Thiemo Kreuz 40764d277c Replace PHPUnit ->returnValue() with ->willReturn() shortcut
It's the same and makes the test code much more readable, I
would like to argue.

Because of the was I split all the changes I made into smaller
patches this patch contains some other changes in the same
lines where I could not split them off. E.g. removal of
->any(), which is the default anyway and doesn't do anything.

Change-Id: Ib297b989d4aec33b31a4e33fe9d5032865b39be0
2021-04-22 10:37:45 +02:00

65 lines
1.7 KiB
PHP

<?php
/**
* @covers ApiOpenSearch
*/
class ApiOpenSearchTest extends MediaWikiIntegrationTestCase {
public function testGetAllowedParams() {
$config = $this->replaceSearchEngineConfig();
$config->method( 'getSearchTypes' )
->willReturn( [ 'the one ring' ] );
$api = $this->createApi();
$engine = $this->replaceSearchEngine();
$engine->expects( $this->any() )
->method( 'getProfiles' )
->will( $this->returnValueMap( [
[ SearchEngine::COMPLETION_PROFILE_TYPE, $api->getUser(), [
[
'name' => 'normal',
'desc-message' => 'normal-message',
'default' => true,
],
[
'name' => 'strict',
'desc-message' => 'strict-message',
],
] ],
] ) );
$params = $api->getAllowedParams();
$this->assertArrayNotHasKey( 'offset', $params );
$this->assertArrayHasKey( 'profile', $params, print_r( $params, true ) );
$this->assertEquals( 'normal', $params['profile'][ApiBase::PARAM_DFLT] );
}
private function replaceSearchEngineConfig() {
$config = $this->getMockBuilder( SearchEngineConfig::class )
->disableOriginalConstructor()
->getMock();
$this->setService( 'SearchEngineConfig', $config );
return $config;
}
private function replaceSearchEngine() {
$engine = $this->getMockBuilder( SearchEngine::class )
->disableOriginalConstructor()
->getMock();
$engineFactory = $this->getMockBuilder( SearchEngineFactory::class )
->disableOriginalConstructor()
->getMock();
$engineFactory->method( 'create' )
->willReturn( $engine );
$this->setService( 'SearchEngineFactory', $engineFactory );
return $engine;
}
private function createApi() {
$ctx = new RequestContext();
$apiMain = new ApiMain( $ctx );
return new ApiOpenSearch( $apiMain, 'opensearch', '' );
}
}