3 declare( strict_types
= 1 );
5 namespace MediaWiki\Tests\Unit
;
8 use MediaWiki\Language\Language
;
9 use MediaWiki\Message\Message
;
11 use Wikimedia\Message\MessageSpecifier
;
14 * A MessageLocalizer that does not make database/service calls, for use in unit tests
16 * To be used in a phpunit unit test like so:
19 * $output = $this->createMock( OutputPage::class );
20 * $output->method( 'msg' )
21 * ->willReturnCallback( [ new FakeQqxMessageLocalizer(), 'msg' ] );
24 * @since 1.40 (backported in 1.39.4)
25 * @license GPL-2.0-or-later
27 class FakeQqxMessageLocalizer
implements MessageLocalizer
{
29 public function msg( $key, ...$params ): Message
{
30 $message = new class( $key ) extends Message
{
32 protected function fetchMessage(): string {
33 return "($this->key$*)";
36 public static function newFromSpecifier( $value ) {
37 if ( $value instanceof MessageSpecifier
) {
38 return new self( $value );
40 return parent
::newFromSpecifier( $value );
43 public function getLanguage(): Language
{
44 return new class() extends LanguageQqx
{
46 public function __construct() {
49 public function getCode(): string {
53 // Support using Message::numParam()
54 public function formatNum( $number ): string {
55 return (string)$number;
60 public function inContentLanguage(): Message
{
64 protected function transformText( $string ): string {
70 // we use ->params() instead of the $params constructor parameter
71 // because ->params() supports some additional calling conventions,
72 // which our callers might also have used
73 $message->params( ...$params );