Merge "Provide command to adjust phpunit.xml for code coverage"
[lhc/web/wiklou.git] / tests / phpunit / includes / Message / TextFormatterTest.php
1 <?php
2
3 namespace MediaWiki\Tests\Message;
4
5 use MediaWiki\Message\TextFormatter;
6 use MediaWikiTestCase;
7 use Message;
8 use Wikimedia\Message\MessageValue;
9 use Wikimedia\Message\ParamType;
10 use Wikimedia\Message\ScalarParam;
11
12 /**
13 * @covers \MediaWiki\Message\TextFormatter
14 * @covers \Wikimedia\Message\MessageValue
15 * @covers \Wikimedia\Message\ListParam
16 * @covers \Wikimedia\Message\ScalarParam
17 * @covers \Wikimedia\Message\MessageParam
18 */
19 class TextFormatterTest extends MediaWikiTestCase {
20 private function createTextFormatter( $langCode ) {
21 return new class( $langCode ) extends TextFormatter {
22 public function __construct( $langCode ) {
23 parent::__construct( $langCode );
24 }
25
26 protected function createMessage( $key ) {
27 return new FakeMessage( $key );
28 }
29 };
30 }
31
32 public function testGetLangCode() {
33 $formatter = $this->createTextFormatter( 'fr' );
34 $this->assertSame( 'fr', $formatter->getLangCode() );
35 }
36
37 public function testFormatBitrate() {
38 $formatter = $this->createTextFormatter( 'en' );
39 $mv = ( new MessageValue( 'test' ) )->bitrateParams( 100, 200 );
40 $result = $formatter->format( $mv );
41 $this->assertSame( 'test 100 bps 200 bps', $result );
42 }
43
44 public function testFormatList() {
45 $formatter = $this->createTextFormatter( 'en' );
46 $mv = ( new MessageValue( 'test' ) )->commaListParams( [
47 'a',
48 new ScalarParam( ParamType::BITRATE, 100 ),
49 ] );
50 $result = $formatter->format( $mv );
51 $this->assertSame( 'test a, 100 bps $2', $result );
52 }
53
54 public function testFormatMessage() {
55 $formatter = $this->createTextFormatter( 'en' );
56 $mv = ( new MessageValue( 'test' ) )
57 ->params( new MessageValue( 'test2', [ 'a', 'b' ] ) )
58 ->commaListParams( [
59 'x',
60 new ScalarParam( ParamType::BITRATE, 100 ),
61 new MessageValue( 'test3', [ 'c', new MessageValue( 'test4', [ 'd', 'e' ] ) ] )
62 ] );
63 $result = $formatter->format( $mv );
64 $this->assertSame( 'test test2 a b x, 100 bps, test3 c test4 d e', $result );
65 }
66 }
67
68 class FakeMessage extends Message {
69 public function fetchMessage() {
70 return "{$this->getKey()} $1 $2";
71 }
72 }