wiki.techinc.nl/tests/phpunit/unit/includes/content/JsonContentTest.php
Taavi Väänänen 5d82c7b061
Indent JsonContent using tabs
This reduces JSON page sizes by a significant amount as one tab is one
characters and four spaces is four characters.

Bug: T326065
Depends-On: I67c87484ea4ec23f703480c8d423b800c74f6518
Change-Id: I94e9580d48066b011d33d18751969c43799aa76a
2024-01-02 23:14:34 +02:00

77 lines
1.6 KiB
PHP

<?php
namespace MediaWiki\Tests\Unit;
use JsonContent;
use MediaWikiUnitTestCase;
/**
* Split from \JsonContentTest integration tests
*
* @author Addshore
* @covers \JsonContent
*/
class JsonContentTest extends MediaWikiUnitTestCase {
public static function provideValidConstruction() {
return [
[ 'foo', false, null ],
[ '[]', true, [] ],
[ '{}', true, (object)[] ],
[ '""', true, '' ],
[ '"0"', true, '0' ],
[ '"bar"', true, 'bar' ],
[ '0', true, '0' ],
[ '{ "0": "bar" }', true, (object)[ 'bar' ] ],
];
}
/**
* @dataProvider provideValidConstruction
*/
public function testIsValid( $text, $isValid, $expected ) {
$obj = new JsonContent( $text, CONTENT_MODEL_JSON );
$this->assertEquals( $isValid, $obj->isValid() );
$this->assertEquals( $expected, $obj->getData()->getValue() );
}
public static function provideDataToEncode() {
return [
[
// Round-trip empty array
'[]',
'[]',
],
[
// Round-trip empty object
'{}',
'{}',
],
[
// Round-trip empty array/object (nested)
'{ "foo": {}, "bar": [] }',
"{\n\t\"foo\": {},\n\t\"bar\": []\n}",
],
[
'{ "foo": "bar" }',
"{\n\t\"foo\": \"bar\"\n}",
],
[
'{ "foo": 1000 }',
"{\n\t\"foo\": 1000\n}",
],
[
'{ "foo": 1000, "0": "bar" }',
"{\n\t\"foo\": 1000,\n\t\"0\": \"bar\"\n}",
],
];
}
/**
* @dataProvider provideDataToEncode
*/
public function testBeautifyJson( $input, $beautified ) {
$obj = new JsonContent( $input );
$this->assertEquals( $beautified, $obj->beautifyJSON() );
}
}