mediawiki-extensions-Templa.../tests/phpunit/SerializationTest.php
Cindy Cicalese b10862a333 Make extension data JSON-serializable #1 - forward-compat.
Transitioning of ParserCache to JSON serialization will add
a requirement that all the extension data is JSON-serializable.

This is the first step in transition - making it forward-compatible.

Bug: T266252
Change-Id: If1c9d9bb5b0039df80a9d9b30c247206d8844c0a
2020-10-30 12:33:20 -04:00

74 lines
2.6 KiB
PHP

<?php
/**
* @group TemplateData
* @covers \TemplateDataHooks::setStatusToParserOutput
* @covers \TemplateDataHooks::getStatusFromParserOutput
*/
class SerializationTest extends MediaWikiTestCase {
public function testParserOutputPersistenceForwardCompatibility() {
$output = new ParserOutput();
$status = Status::newFatal( 'a', 'b', 'c' );
$status->fatal( 'f' );
$status->warning( 'd', 'e' );
// Set JSONified state. Should work before we set JSON-serializable data,
// to be robust against old code reading new data after a rollback.
$output->setExtensionData( 'TemplateDataStatus',
TemplateDataHooks::jsonSerializeStatus( $status ) );
$result = TemplateDataHooks::getStatusFromParserOutput( $output );
$this->assertEquals( $status->getStatusValue(), $result->getStatusValue() );
$this->assertEquals( $status->__toString(), $result->__toString() );
}
public function testParserOutputPersistenceBackwardCompatibility() {
$output = new ParserOutput();
$status = Status::newFatal( 'a', 'b', 'c' );
$status->fatal( 'f' );
$status->warning( 'd', 'e' );
// Set the object directly. Should still work once we normally set JSON-serializable data.
$output->setExtensionData( 'TemplateDataStatus', $status );
$result = TemplateDataHooks::getStatusFromParserOutput( $output );
$this->assertEquals( $status->getStatusValue(), $result->getStatusValue() );
$this->assertEquals( $status->__toString(), $result->__toString() );
}
public function provideStatus() {
yield [ Status::newGood() ];
$status = Status::newFatal( 'a', 'b', 'c' );
$status->fatal( 'f' );
$status->warning( 'd', 'e' );
yield [ $status ];
}
/**
* @dataProvider provideStatus
* @covers \TemplateDataHooks::setStatusToParserOutput
* @covers \TemplateDataHooks::getStatusFromParserOutput
*/
public function testParserOutputPersistenceRoundTrip( Status $status ) {
$parserOutput = new ParserOutput();
TemplateDataHooks::setStatusToParserOutput( $parserOutput, $status );
$result = TemplateDataHooks::getStatusFromParserOutput( $parserOutput );
$this->assertEquals( $status->getStatusValue(), $result->getStatusValue() );
$this->assertEquals( $status->__toString(), $result->__toString() );
}
/**
* @dataProvider provideStatus
* @covers \TemplateDataHooks::jsonSerializeStatus
* @covers \TemplateDataHooks::newStatusFromJson
*/
public function testJsonRoundTrip( Status $status ) {
$json = TemplateDataHooks::jsonSerializeStatus( $status );
$result = TemplateDataHooks::newStatusFromJson( $json );
$this->assertEquals( $status->getStatusValue(), $result->getStatusValue() );
$this->assertEquals( $status->__toString(), $result->__toString() );
}
}