diff --git a/tests/TemplateDataBlobTest.php b/tests/TemplateDataBlobTest.php index d19656bd..77d72670 100644 --- a/tests/TemplateDataBlobTest.php +++ b/tests/TemplateDataBlobTest.php @@ -11,37 +11,55 @@ class TemplateDataBlobTest extends MediaWikiTestCase { } public static function provideParse() { - return array( + $cases = array( array( - ' - {} + 'input' => '[] ', - ' - {} - ', - 'Empty object' + 'status' => 'Property "templatedata" is expected to be of type "object".' ), array( - ' - { + 'input' => '{ + "params": {} + } + ', + 'output' => '{ + "description": null, + "params": {} + } + ', + 'status' => true, + 'msg' => 'Minimal valid blob' + ), + array( + 'input' => '{ + "params": {}, "foo": "bar" } ', - ' - {} - ', - 'Unknown properties are stripped' + 'status' => 'Unexpected property "foo".', + 'msg' => 'Unknown properties' ), array( - ' - { + 'input' => '{}', + 'status' => 'Required property "params" not found.', + 'msg' => 'Empty object' + ), + array( + 'input' => '{ + "foo": "bar" + } + ', + 'status' => 'Unexpected property "foo".', + 'msg' => 'Unknown properties invalidate the blob' + ), + array( + 'input' => '{ "params": { "foo": {} } } ', - ' - { + 'output' => '{ "description": null, "params": { "foo": { @@ -56,11 +74,10 @@ class TemplateDataBlobTest extends MediaWikiTestCase { } } ', - 'Optional properties are added if missing' + 'msg' => 'Optional properties are added if missing' ), array( - ' - { + 'input' => '{ "description": "User badge MediaWiki developers.", "params": { "nickname": { @@ -75,8 +92,7 @@ class TemplateDataBlobTest extends MediaWikiTestCase { } } ', - ' - { + 'output' => '{ "description": { "en": "User badge MediaWiki developers." }, @@ -97,11 +113,10 @@ class TemplateDataBlobTest extends MediaWikiTestCase { } } ', - 'InterfaceText is expanded to langcode-keyed object, assuming content language' + 'msg' => 'InterfaceText is expanded to langcode-keyed object, assuming content language' ), array( - ' - { + 'input' => '{ "description": { "en": "User badge MediaWiki developers." }, @@ -122,11 +137,10 @@ class TemplateDataBlobTest extends MediaWikiTestCase { } } ', - 'Fully normalised json should be valid input and stay unchanged' + 'msg' => 'Fully normalised json should be valid input and stay unchanged' ), array( - ' - { + 'input' => '{ "description": "Document the documenter.", "params": { "1d": { @@ -141,8 +155,7 @@ class TemplateDataBlobTest extends MediaWikiTestCase { } } ', - ' - { + 'output' => '{ "description": { "en": "Document the documenter." }, @@ -172,75 +185,56 @@ class TemplateDataBlobTest extends MediaWikiTestCase { } } ', - 'The inherits property copies over properties from another parameter (preserving overides)' + 'msg' => 'The inherits property copies over properties from another parameter (preserving overides)' ), ); + $calls = array(); + foreach ( $cases as $case ) { + $calls[] = array( $case ); + } + return $calls; } /** * @dataProvider provideParse */ - public function testParse( $input, $expected, $msg = null ) { - if ( !$msg ) { - $msg = $expected; - $expected = $input; + public function testParse( Array $cases ) { + + // Expand defaults + if ( !isset( $cases['status'] ) ) { + $cases['status'] = true; } - $t = TemplateDataBlob::newFromJSON( $input ); + if ( !isset( $cases['msg'] ) ) { + $cases['msg'] = is_string( $cases['status'] ) ? $cases['status'] : 'TemplateData assertion'; + } + if ( !isset( $cases['output'] ) ) { + if ( is_string( $cases['status'] ) ) { + $cases['output'] = '{}'; + } else { + $cases['output'] = $cases['input']; + } + } + + $t = TemplateDataBlob::newFromJSON( $cases['input'] ); $actual = $t->getJSON(); - $this->assertJsonStringEqualsJsonString( - $expected, - $actual, - $msg - ); - } - - public static function provideStatus() { - return array( - array( - ' - [] - ', - false, - 'Not an object' - ), - array( - ' - { - "params": {} - } - ', - true, - 'Minimal valid blob' - ), - array( - ' - { - "params": {}, - "foo": "bar" - } - ', - false, - 'Unknown properties' - ), - ); - } - - /** - * @dataProvider provideStatus - */ - public function testStatus( $inputJSON, $isGood, $msg ) { - // Make sure we don't have two errors cancelling each other out - if ( json_decode( $inputJSON ) === null ) { - throw new Exception( 'Test case provided invalid JSON.' ); - } - - $t = TemplateDataBlob::newFromJSON( $inputJSON ); $status = $t->getStatus(); - - $this->assertEquals( - $status->isGood(), - $isGood, - $msg + if ( !$status->isGood() ) { + $this->assertEquals( + $cases['status'], + $status->getHtml(), + 'Status: ' . $cases['msg'] + ); + } else { + $this->assertEquals( + $cases['status'], + $status->isGood(), + 'Status: ' . $cases['msg'] + ); + } + $this->assertJsonStringEqualsJsonString( + $cases['output'], + $actual, + $cases['msg'] ); } }