2019-07-15 10:24:38 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace MediaWiki\Tests\Message;
|
|
|
|
|
|
|
|
|
|
use MediaWiki\Message\TextFormatter;
|
|
|
|
|
use MediaWikiTestCase;
|
|
|
|
|
use Message;
|
|
|
|
|
use Wikimedia\Message\MessageValue;
|
|
|
|
|
use Wikimedia\Message\ParamType;
|
2019-08-29 20:52:27 +00:00
|
|
|
use Wikimedia\Message\ScalarParam;
|
2019-07-15 10:24:38 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers \MediaWiki\Message\TextFormatter
|
|
|
|
|
* @covers \Wikimedia\Message\MessageValue
|
|
|
|
|
* @covers \Wikimedia\Message\ListParam
|
2019-08-29 20:52:27 +00:00
|
|
|
* @covers \Wikimedia\Message\ScalarParam
|
2019-07-15 10:24:38 +00:00
|
|
|
* @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',
|
2019-08-29 20:52:27 +00:00
|
|
|
new ScalarParam( ParamType::BITRATE, 100 ),
|
2019-07-15 10:24:38 +00:00
|
|
|
] );
|
|
|
|
|
$result = $formatter->format( $mv );
|
|
|
|
|
$this->assertSame( 'test a, 100 bps $2', $result );
|
|
|
|
|
}
|
2019-08-30 19:14:37 +00:00
|
|
|
|
|
|
|
|
public function testFormatMessage() {
|
|
|
|
|
$formatter = $this->createTextFormatter( 'en' );
|
|
|
|
|
$mv = ( new MessageValue( 'test' ) )
|
|
|
|
|
->params( new MessageValue( 'test2', [ 'a', 'b' ] ) )
|
|
|
|
|
->commaListParams( [
|
|
|
|
|
'x',
|
|
|
|
|
new ScalarParam( ParamType::BITRATE, 100 ),
|
|
|
|
|
new MessageValue( 'test3', [ 'c', new MessageValue( 'test4', [ 'd', 'e' ] ) ] )
|
|
|
|
|
] );
|
|
|
|
|
$result = $formatter->format( $mv );
|
|
|
|
|
$this->assertSame( 'test test2 a b x, 100 bps, test3 c test4 d e', $result );
|
|
|
|
|
}
|
2019-07-15 10:24:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class FakeMessage extends Message {
|
|
|
|
|
public function fetchMessage() {
|
|
|
|
|
return "{$this->getKey()} $1 $2";
|
|
|
|
|
}
|
|
|
|
|
}
|