Move DatabaseDomain to Rdbms namespace
[mediawiki.git] / tests / phpunit / includes / api / format / ApiFormatJsonTest.php
blob7eb2a35ecfcb8f88dd697da6e8adcd55cfb99cda
1 <?php
3 /**
4 * @group API
5 * @covers ApiFormatJson
6 */
7 class ApiFormatJsonTest extends ApiFormatTestBase {
9 protected $printerName = 'json';
11 private static function addFormatVersion( $format, $arr ) {
12 foreach ( $arr as &$p ) {
13 if ( !isset( $p[2] ) ) {
14 $p[2] = [ 'formatversion' => $format ];
15 } else {
16 $p[2]['formatversion'] = $format;
19 return $arr;
22 public static function provideGeneralEncoding() {
23 return array_merge(
24 self::addFormatVersion( 1, [
25 // Basic types
26 [ [ null ], '[null]' ],
27 [ [ true ], '[""]' ],
28 [ [ false ], '[]' ],
29 [ [ true, ApiResult::META_BC_BOOLS => [ 0 ] ], '[true]' ],
30 [ [ false, ApiResult::META_BC_BOOLS => [ 0 ] ], '[false]' ],
31 [ [ 42 ], '[42]' ],
32 [ [ 42.5 ], '[42.5]' ],
33 [ [ 1e42 ], '[1.0e+42]' ],
34 [ [ 'foo' ], '["foo"]' ],
35 [ [ 'fóo' ], '["f\u00f3o"]' ],
36 [ [ 'fóo' ], '["fóo"]', [ 'utf8' => 1 ] ],
38 // Arrays and objects
39 [ [ [] ], '[[]]' ],
40 [ [ [ 1 ] ], '[[1]]' ],
41 [ [ [ 'x' => 1 ] ], '[{"x":1}]' ],
42 [ [ [ 2 => 1 ] ], '[{"2":1}]' ],
43 [ [ (object)[] ], '[{}]' ],
44 [ [ [ 1, ApiResult::META_TYPE => 'assoc' ] ], '[{"0":1}]' ],
45 [ [ [ 'x' => 1, ApiResult::META_TYPE => 'array' ] ], '[[1]]' ],
46 [ [ [ 'x' => 1, ApiResult::META_TYPE => 'kvp' ] ], '[{"x":1}]' ],
48 [ [
49 'x' => 1,
50 ApiResult::META_TYPE => 'BCkvp',
51 ApiResult::META_KVP_KEY_NAME => 'key'
52 ] ],
53 '[[{"key":"x","*":1}]]'
55 [ [ [ 'x' => 1, ApiResult::META_TYPE => 'BCarray' ] ], '[{"x":1}]' ],
56 [ [ [ 'a', 'b', ApiResult::META_TYPE => 'BCassoc' ] ], '[["a","b"]]' ],
58 // Content
59 [ [ 'content' => 'foo', ApiResult::META_CONTENT => 'content' ],
60 '{"*":"foo"}' ],
62 // BC Subelements
63 [ [ 'foo' => 'foo', ApiResult::META_BC_SUBELEMENTS => [ 'foo' ] ],
64 '{"foo":{"*":"foo"}}' ],
66 // Callbacks
67 [ [ 1 ], '/**/myCallback([1])', [ 'callback' => 'myCallback' ] ],
69 // Cross-domain mangling
70 [ [ '< Cross-Domain-Policy >' ], '["\u003C Cross-Domain-Policy >"]' ],
71 ] ),
72 self::addFormatVersion( 2, [
73 // Basic types
74 [ [ null ], '[null]' ],
75 [ [ true ], '[true]' ],
76 [ [ false ], '[false]' ],
77 [ [ true, ApiResult::META_BC_BOOLS => [ 0 ] ], '[true]' ],
78 [ [ false, ApiResult::META_BC_BOOLS => [ 0 ] ], '[false]' ],
79 [ [ 42 ], '[42]' ],
80 [ [ 42.5 ], '[42.5]' ],
81 [ [ 1e42 ], '[1.0e+42]' ],
82 [ [ 'foo' ], '["foo"]' ],
83 [ [ 'fóo' ], '["fóo"]' ],
84 [ [ 'fóo' ], '["f\u00f3o"]', [ 'ascii' => 1 ] ],
86 // Arrays and objects
87 [ [ [] ], '[[]]' ],
88 [ [ [ 'x' => 1 ] ], '[{"x":1}]' ],
89 [ [ [ 2 => 1 ] ], '[{"2":1}]' ],
90 [ [ (object)[] ], '[{}]' ],
91 [ [ [ 1, ApiResult::META_TYPE => 'assoc' ] ], '[{"0":1}]' ],
92 [ [ [ 'x' => 1, ApiResult::META_TYPE => 'array' ] ], '[[1]]' ],
93 [ [ [ 'x' => 1, ApiResult::META_TYPE => 'kvp' ] ], '[{"x":1}]' ],
95 [ [
96 'x' => 1,
97 ApiResult::META_TYPE => 'BCkvp',
98 ApiResult::META_KVP_KEY_NAME => 'key'
99 ] ],
100 '[{"x":1}]'
102 [ [ [ 'x' => 1, ApiResult::META_TYPE => 'BCarray' ] ], '[[1]]' ],
105 'a',
106 'b',
107 ApiResult::META_TYPE => 'BCassoc'
108 ] ],
109 '[{"0":"a","1":"b"}]'
112 // Content
113 [ [ 'content' => 'foo', ApiResult::META_CONTENT => 'content' ],
114 '{"content":"foo"}' ],
116 // BC Subelements
117 [ [ 'foo' => 'foo', ApiResult::META_BC_SUBELEMENTS => [ 'foo' ] ],
118 '{"foo":"foo"}' ],
120 // Callbacks
121 [ [ 1 ], '/**/myCallback([1])', [ 'callback' => 'myCallback' ] ],
123 // Cross-domain mangling
124 [ [ '< Cross-Domain-Policy >' ], '["\u003C Cross-Domain-Policy >"]' ],