wiki.techinc.nl/tests/phpunit/includes/Message/TextFormatterTest.php
Tim Starling 09cd8eb080 MessageFormatterFactory
An injectable service interface for message formatting, somewhat
narrowed compared to Message.

Only the text format is implemented in this framework so far, with
getTextFormatter() returning a formatter that converts to the text
format. Other formatters could be added to MessageFormatterFactory.

Bug: T226598
Change-Id: Id053074c1dbcb692e8309fdca602f94a385bca0c
2019-08-28 12:28:05 +10:00

59 lines
1.6 KiB
PHP

<?php
namespace MediaWiki\Tests\Message;
use MediaWiki\Message\TextFormatter;
use MediaWikiTestCase;
use Message;
use Wikimedia\Message\MessageValue;
use Wikimedia\Message\ParamType;
use Wikimedia\Message\TextParam;
/**
* @covers \MediaWiki\Message\TextFormatter
* @covers \Wikimedia\Message\MessageValue
* @covers \Wikimedia\Message\ListParam
* @covers \Wikimedia\Message\TextParam
* @covers \Wikimedia\Message\MessageParam
*/
class TextFormatterTest extends MediaWikiTestCase {
private function createTextFormatter( $langCode ) {
return new class( $langCode ) extends TextFormatter {
public function __construct( $langCode ) {
parent::__construct( $langCode );
}
protected function createMessage( $key ) {
return new FakeMessage( $key );
}
};
}
public function testGetLangCode() {
$formatter = $this->createTextFormatter( 'fr' );
$this->assertSame( 'fr', $formatter->getLangCode() );
}
public function testFormatBitrate() {
$formatter = $this->createTextFormatter( 'en' );
$mv = ( new MessageValue( 'test' ) )->bitrateParams( 100, 200 );
$result = $formatter->format( $mv );
$this->assertSame( 'test 100 bps 200 bps', $result );
}
public function testFormatList() {
$formatter = $this->createTextFormatter( 'en' );
$mv = ( new MessageValue( 'test' ) )->commaListParams( [
'a',
new TextParam( ParamType::BITRATE, 100 ),
] );
$result = $formatter->format( $mv );
$this->assertSame( 'test a, 100 bps $2', $result );
}
}
class FakeMessage extends Message {
public function fetchMessage() {
return "{$this->getKey()} $1 $2";
}
}