3 * Test cases for our Services_Json library. Requires PHP json support as well,
4 * so we can compare output
6 class ServicesJsonTest
extends MediaWikiTestCase
{
8 * Test to make sure core json_encode() and our Services_Json()->encode()
9 * produce the same output
11 * @dataProvider provideValuesToEncode
13 public function testJsonEncode( $input, $desc ) {
14 if ( !function_exists( 'json_encode' ) ) {
15 $this->markTestIncomplete( 'No PHP json support, unable to test' );
17 } elseif ( strtolower( json_encode( "\xf0\xa0\x80\x80" ) ) != '"\ud840\udc00"' ) {
18 $this->markTestIncomplete( 'Have buggy PHP json support, unable to test' );
21 $jsonObj = new Services_JSON();
23 $jsonObj->encode( $input ),
24 json_encode( $input ),
31 * Test to make sure core json_decode() and our Services_Json()->decode()
32 * produce the same output
34 * @dataProvider provideValuesToDecode
36 public function testJsonDecode( $input, $desc ) {
37 if ( !function_exists( 'json_decode' ) ) {
38 $this->markTestIncomplete( 'No PHP json support, unable to test' );
41 $jsonObj = new Services_JSON();
43 $jsonObj->decode( $input ),
44 json_decode( $input ),
50 public static function provideValuesToEncode() {
51 $obj = new stdClass();
52 $obj->property
= 'value';
53 $obj->property2
= null;
54 $obj->property3
= 1.234;
56 array( 1, 'basic integer' ),
57 array( -1, 'negative integer' ),
58 array( 1.1, 'basic float' ),
59 array( true, 'basic bool true' ),
60 array( false, 'basic bool false' ),
61 array( 'some string', 'basic string test' ),
62 array( "some string\nwith newline", 'newline string test' ),
63 array( '♥ü', 'unicode string test' ),
64 array( array( 'some', 'string', 'values' ), 'basic array of strings' ),
65 array( array( 'key1' => 'val1', 'key2' => 'val2' ), 'array with string keys' ),
66 array( array( 1 => 'val1', 3 => 'val2', '2' => 'val3' ), 'out of order numbered array test' ),
67 array( array(), 'empty array test' ),
68 array( $obj, 'basic object test' ),
69 array( new stdClass
, 'empty object test' ),
70 array( null, 'null test' ),
74 public static function provideValuesToDecode() {
76 array( '1', 'basic integer' ),
77 array( '-1', 'negative integer' ),
78 array( '1.1', 'basic float' ),
79 array( '1.1e1', 'scientific float' ),
80 array( 'true', 'basic bool true' ),
81 array( 'false', 'basic bool false' ),
82 array( '"some string"', 'basic string test' ),
83 array( '"some string\nwith newline"', 'newline string test' ),
84 array( '"♥ü"', 'unicode character string test' ),
85 array( '"\u2665"', 'unicode \\u string test' ),
86 array( '["some","string","values"]', 'basic array of strings' ),
87 array( '[]', 'empty array test' ),
88 array( '{"key":"value"}', 'Basic key => value test' ),
89 array( '{}', 'empty object test' ),
90 array( 'null', 'null test' ),