Add MockMessageLocalizer
This MessageLocalizer may be used in tests. By default, it sets the language for all messages to 'qqx', to make the tests independent of the $wgLanguageCode of the wiki the tests are running in. Change-Id: I7412ec49b6c0fab8146e09bb8aa599b35d88fc97
This commit is contained in:
parent
6f9c30c67b
commit
b216f4e071
1 changed files with 53 additions and 0 deletions
53
tests/phpunit/mocks/MockMessageLocalizer.php
Normal file
53
tests/phpunit/mocks/MockMessageLocalizer.php
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
<?php
|
||||
|
||||
use Message;
|
||||
use MessageLocalizer;
|
||||
use MessageSpecifier;
|
||||
|
||||
/**
|
||||
* A simple {@link MessageLocalizer} implementation for use in tests.
|
||||
* By default, it sets the message language to 'qqx',
|
||||
* to make the tests independent of the wiki configuration.
|
||||
*
|
||||
* @author Lucas Werkmeister
|
||||
* @license GPL-2.0-or-later
|
||||
*/
|
||||
class MockMessageLocalizer implements MessageLocalizer {
|
||||
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
private $languageCode;
|
||||
|
||||
/**
|
||||
* @param string|null $languageCode The language code to use for messages by default.
|
||||
* You can specify null to use the user language,
|
||||
* but this is not recommended as it may make your tests depend on the wiki configuration.
|
||||
*/
|
||||
public function __construct( $languageCode = 'qqx' ) {
|
||||
$this->languageCode = $languageCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a Message object.
|
||||
* Parameters are the same as {@link wfMessage()}.
|
||||
*
|
||||
* @param string|string[]|MessageSpecifier $key Message key, or array of keys,
|
||||
* or a MessageSpecifier.
|
||||
* @param mixed $args,...
|
||||
* @return Message
|
||||
*/
|
||||
public function msg( $key ) {
|
||||
$args = func_get_args();
|
||||
|
||||
/** @var Message $message */
|
||||
$message = call_user_func_array( 'wfMessage', $args );
|
||||
|
||||
if ( $this->languageCode !== null ) {
|
||||
$message->inLanguage( $this->languageCode );
|
||||
}
|
||||
|
||||
return $message;
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in a new issue