test: coverage recording now needs to be explicit
[mediawiki.git] / tests / phpunit / includes / json / FormatJsonTest.php
blobef263c4dd4071d880ccbe47778517620dbfb0230
1 <?php
3 class FormatJsonTest extends MediaWikiTestCase {
5 public function testEncoderPrettyPrinting() {
6 $obj = array(
7 'emptyObject' => new stdClass,
8 'emptyArray' => array(),
9 'string' => 'foobar\\',
10 'filledArray' => array(
11 array(
12 123,
13 456,
15 '"7":["8",{"9":"10"}]',
19 // 4 space indent, no trailing whitespace, no trailing linefeed
20 $json = '{
21 "emptyObject": {
24 "emptyArray": [
27 "string": "foobar\\\\",
28 "filledArray": [
30 123,
31 456
33 "\"7\":[\"8\",{\"9\":\"10\"}]"
35 }';
37 $json = str_replace( "\r", '', $json ); // Windows compat
38 $this->assertSame( $json, FormatJson::encode( $obj, true ) );
41 public static function provideEncodeDefault() {
42 return self::getEncodeTestCases( array() );
45 /**
46 * @dataProvider provideEncodeDefault
48 public function testEncodeDefault( $from, $to ) {
49 $this->assertSame( $to, FormatJson::encode( $from ) );
52 public static function provideEncodeUtf8() {
53 return self::getEncodeTestCases( array( 'unicode' ) );
56 /**
57 * @dataProvider provideEncodeUtf8
59 public function testEncodeUtf8( $from, $to ) {
60 $this->assertSame( $to, FormatJson::encode( $from, false, FormatJson::UTF8_OK ) );
63 public static function provideEncodeXmlMeta() {
64 return self::getEncodeTestCases( array( 'xmlmeta' ) );
67 /**
68 * @dataProvider provideEncodeXmlMeta
70 public function testEncodeXmlMeta( $from, $to ) {
71 $this->assertSame( $to, FormatJson::encode( $from, false, FormatJson::XMLMETA_OK ) );
74 public static function provideEncodeAllOk() {
75 return self::getEncodeTestCases( array( 'unicode', 'xmlmeta' ) );
78 /**
79 * @dataProvider provideEncodeAllOk
81 public function testEncodeAllOk( $from, $to ) {
82 $this->assertSame( $to, FormatJson::encode( $from, false, FormatJson::ALL_OK ) );
85 public function testEncodePhpBug46944() {
86 $this->assertNotEquals(
87 '\ud840\udc00',
88 strtolower( FormatJson::encode( "\xf0\xa0\x80\x80" ) ),
89 'Test encoding an broken json_encode character (U+20000)'
93 public function testDecodeReturnType() {
94 $this->assertInternalType(
95 'object',
96 FormatJson::decode( '{"Name": "Cheeso", "Rank": 7}' ),
97 'Default to object'
100 $this->assertInternalType(
101 'array',
102 FormatJson::decode( '{"Name": "Cheeso", "Rank": 7}', true ),
103 'Optional array'
108 * Generate a set of test cases for a particular combination of encoder options.
110 * @param array $unescapedGroups List of character groups to leave unescaped
111 * @return array: Arrays of unencoded strings and corresponding encoded strings
113 private static function getEncodeTestCases( array $unescapedGroups ) {
114 $groups = array(
115 'always' => array(
116 // Forward slash (always unescaped)
117 '/' => '/',
119 // Control characters
120 "\0" => '\u0000',
121 "\x08" => '\b',
122 "\t" => '\t',
123 "\n" => '\n',
124 "\r" => '\r',
125 "\f" => '\f',
126 "\x1f" => '\u001f', // representative example
128 // Double quotes
129 '"' => '\"',
131 // Backslashes
132 '\\' => '\\\\',
133 '\\\\' => '\\\\\\\\',
134 '\\u00e9' => '\\\u00e9', // security check for Unicode unescaping
136 // Line terminators
137 "\xe2\x80\xa8" => '\u2028',
138 "\xe2\x80\xa9" => '\u2029',
140 'unicode' => array(
141 "\xc3\xa9" => '\u00e9',
142 "\xf0\x9d\x92\x9e" => '\ud835\udc9e', // U+1D49E, outside the BMP
144 'xmlmeta' => array(
145 '<' => '\u003C', // JSON_HEX_TAG uses uppercase hex digits
146 '>' => '\u003E',
147 '&' => '\u0026',
151 $cases = array();
152 foreach ( $groups as $name => $rules ) {
153 $leaveUnescaped = in_array( $name, $unescapedGroups );
154 foreach ( $rules as $from => $to ) {
155 $cases[] = array( $from, '"' . ( $leaveUnescaped ? $from : $to ) . '"' );
159 return $cases;