2019-09-11 17:05:47 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Wikimedia\Tests\Message;
|
|
|
|
|
|
2024-04-02 21:56:24 +00:00
|
|
|
use MediaWiki\Json\JsonCodec;
|
|
|
|
|
use MediaWikiUnitTestCase;
|
2019-09-11 17:05:47 +00:00
|
|
|
use Wikimedia\Message\DataMessageValue;
|
|
|
|
|
use Wikimedia\Message\ParamType;
|
|
|
|
|
use Wikimedia\Message\ScalarParam;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers \Wikimedia\Message\DataMessageValue
|
|
|
|
|
*/
|
2024-04-02 21:56:24 +00:00
|
|
|
class DataMessageValueTest extends MediaWikiUnitTestCase {
|
|
|
|
|
use MessageSerializationTestTrait;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Overrides SerializationTestTrait::getClassToTest
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public static function getClassToTest(): string {
|
|
|
|
|
return DataMessageValue::class;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-11 17:05:47 +00:00
|
|
|
public static function provideConstruct() {
|
|
|
|
|
return [
|
2024-04-02 21:56:24 +00:00
|
|
|
'empty' => [
|
2019-09-11 17:05:47 +00:00
|
|
|
[ 'key' ],
|
|
|
|
|
'<datamessage key="key" code="key"></datamessage>',
|
|
|
|
|
],
|
2024-04-02 21:56:24 +00:00
|
|
|
'withParam' => [
|
2019-09-11 17:05:47 +00:00
|
|
|
[ 'key', [ 'a' ] ],
|
|
|
|
|
'<datamessage key="key" code="key"><params><text>a</text></params></datamessage>'
|
|
|
|
|
],
|
2024-04-02 21:56:24 +00:00
|
|
|
'withCode' => [
|
2019-09-11 17:05:47 +00:00
|
|
|
[ 'key', [], 'code' ],
|
|
|
|
|
'<datamessage key="key" code="code"></datamessage>'
|
|
|
|
|
],
|
2024-04-02 21:56:24 +00:00
|
|
|
'withData' => [
|
2019-09-11 17:05:47 +00:00
|
|
|
[ 'key', [ new ScalarParam( ParamType::NUM, 1 ) ], 'code', [ 'value' => 1 ] ],
|
|
|
|
|
'<datamessage key="key" code="code">'
|
|
|
|
|
. '<params><num>1</num></params>'
|
|
|
|
|
. '<data>{"value":1}</data>'
|
|
|
|
|
. '</datamessage>'
|
|
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-02 21:56:24 +00:00
|
|
|
/** @dataProvider provideConstruct */
|
|
|
|
|
public function testSerialize( $args, $_ ) {
|
|
|
|
|
$codec = new JsonCodec;
|
|
|
|
|
$obj = new DataMessageValue( ...$args );
|
|
|
|
|
|
|
|
|
|
$serialized = $codec->serialize( $obj );
|
|
|
|
|
$newObj = $codec->deserialize( $serialized );
|
|
|
|
|
|
|
|
|
|
// XXX: would be nice to have a proper ::equals() method.
|
|
|
|
|
$this->assertEquals( $obj->dump(), $newObj->dump() );
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-11 17:05:47 +00:00
|
|
|
/** @dataProvider provideConstruct */
|
|
|
|
|
public function testConstruct( $args, $expected ) {
|
|
|
|
|
$mv = new DataMessageValue( ...$args );
|
|
|
|
|
$this->assertSame( $expected, $mv->dump() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @dataProvider provideConstruct */
|
|
|
|
|
public function testNew( $args, $expected ) {
|
|
|
|
|
$mv = DataMessageValue::new( ...$args );
|
|
|
|
|
$this->assertSame( $expected, $mv->dump() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testGetCode() {
|
|
|
|
|
$mv = new DataMessageValue( 'key', [], 'code' );
|
|
|
|
|
$this->assertSame( 'code', $mv->getCode() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testGetData() {
|
|
|
|
|
$mv = new DataMessageValue( 'key', [], 'code', [ 'data' => 'foobar' ] );
|
|
|
|
|
$this->assertSame( [ 'data' => 'foobar' ], $mv->getData() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|