wiki.techinc.nl/tests/phpunit/mocks/FakeQqxMessageLocalizer.php
Bartosz Dziewoński 9d56257d8c Make Message and MessageValue compatible
Fix two problems that made it difficult to convert between Message
and MessageValue, or to write code that could accept both of them,
as exemplified by the StatusValue class:

* Implement a common interface
* Use the same internal format for message parameters

While these changes should be compatible with most of existing code,
where the authors were courteous enough to simply call methods such
as Message::numParam() and not look inside the values they return,
it is potentially a breaking change for anything that depended on
the formatted params being arrays or accessed their keys.
Example patches: https://gerrit.wikimedia.org/r/q/topic:message-param

Notable changes:

* Message and MessageValue now both implement MessageSpecifier
  (only Message implemented it before).

* Message::numParam() and other static methods for encoding params
  now return MessageParam objects, instead of special arrays.
  Use these MessageParam objects internally in Message.

* Narrow down the return type of MessageSpecifier::getParams() (it
  was just `array`, allowing any type in the array). Narrow down the
  types for Message::params() and MessageValue::params() to match.

* Deprecate MediaWiki\Message\Converter. As a replacement add
  MessageValue::newFromSpecifier(), which is analogous to
  Message::newFromSpecifier(), but without weird legacy edge cases.

* Make StatusValue::getMessages() return MessageValues. Remove code
  that converted between Message and MessageValue, no longer needed.

* Update many type declarations and comments to use MessageSpecifier
  instead of MessageValue, as well as a couple of tests that depended
  on implementation details.

Bug: T358779
Change-Id: I625a48a6ecd3fad5c2ed76b23343a0fef91e1b83
2024-10-19 15:00:07 +02:00

78 lines
1.8 KiB
PHP

<?php
declare( strict_types = 1 );
namespace MediaWiki\Tests\Unit;
use LanguageQqx;
use MediaWiki\Language\Language;
use MediaWiki\Message\Message;
use MessageLocalizer;
use Wikimedia\Message\MessageSpecifier;
/**
* A MessageLocalizer that does not make database/service calls, for use in unit tests
*
* To be used in a phpunit unit test like so:
*
* ```php
* $output = $this->createMock( OutputPage::class );
* $output->method( 'msg' )
* ->willReturnCallback( [ new FakeQqxMessageLocalizer(), 'msg' ] );
* ```
*
* @since 1.40 (backported in 1.39.4)
* @license GPL-2.0-or-later
*/
class FakeQqxMessageLocalizer implements MessageLocalizer {
public function msg( $key, ...$params ): Message {
$message = new class( $key ) extends Message {
protected function fetchMessage(): string {
return "($this->key$*)";
}
public static function newFromSpecifier( $value ) {
if ( $value instanceof MessageSpecifier ) {
return new self( $value );
}
return parent::newFromSpecifier( $value );
}
public function getLanguage(): Language {
return new class() extends LanguageQqx {
public function __construct() {
}
public function getCode(): string {
return 'qqx';
}
// Support using Message::numParam()
public function formatNum( $number ): string {
return (string)$number;
}
};
}
public function inContentLanguage(): Message {
return $this;
}
protected function transformText( $string ): string {
return $string;
}
};
if ( $params ) {
// we use ->params() instead of the $params constructor parameter
// because ->params() supports some additional calling conventions,
// which our callers might also have used
$message->params( ...$params );
}
return $message;
}
}