MessageValue and friends are pure value objects and newable, so it makes sense for them to be (de)serializable too. There are some places where we want to serialize messages, such as in ParserOutput. The structure of the resulting JSON is inspired by the way we represent Message objects as plain values elsewhere in MediaWiki, e.g. StatusValue::getStatusArray(). Co-Authored-By: C. Scott Ananian <cscott@cscott.net> Depends-On: Ia32f95a6bdf342262b4ef044140527f0676402b9 Depends-On: I7bafe80cd36c2558517f474871148286350a4e76 Change-Id: Id47d58b5e26707fa0e0dbdd37418c0d54c8dd503
53 lines
1.4 KiB
PHP
53 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace Wikimedia\Tests\Message;
|
|
|
|
use MediaWiki\Json\JsonCodec;
|
|
use Wikimedia\Tests\SerializationTestTrait;
|
|
|
|
trait MessageSerializationTestTrait {
|
|
use SerializationTestTrait;
|
|
|
|
/**
|
|
* Overrides SerializationTestTrait::getSerializedDataPath
|
|
* @return string
|
|
*/
|
|
public static function getSerializedDataPath(): string {
|
|
return __DIR__ . '/../../../../data/MessageValue';
|
|
}
|
|
|
|
/**
|
|
* Overrides SerializationTestTrait::getTestInstancesAndAssertions
|
|
* @return array
|
|
*/
|
|
public static function getTestInstancesAndAssertions(): array {
|
|
$className = self::getClassToTest();
|
|
return array_map( static function ( $test ) use ( $className ) {
|
|
[ $args, $expected ] = $test;
|
|
$obj = new $className( ...$args );
|
|
return [
|
|
'instance' => $obj,
|
|
'assertions' => static function ( $testCase, $obj ) use ( $expected ) {
|
|
$testCase->assertSame( $expected, $obj->dump() );
|
|
},
|
|
];
|
|
}, self::provideConstruct() );
|
|
}
|
|
|
|
/**
|
|
* Overrides SerializationTestTrait::getSupportedSerializationFormats
|
|
* @return array
|
|
*/
|
|
public static function getSupportedSerializationFormats(): array {
|
|
$jsonCodec = new JsonCodec();
|
|
return [ [
|
|
'ext' => 'json',
|
|
'serializer' => static function ( $obj ) use ( $jsonCodec ) {
|
|
return $jsonCodec->serialize( $obj );
|
|
},
|
|
'deserializer' => static function ( $data ) use ( $jsonCodec ) {
|
|
return $jsonCodec->deserialize( $data );
|
|
},
|
|
] ];
|
|
}
|
|
}
|