Add some other ServicesJson tests.
[lhc/web/wiklou.git] / tests / phpunit / includes / json / ServicesJsonTest.php
1 <?php
2 /*
3 * Test cases for our Services_Json library. Requires PHP json support as well,
4 * so we can compare output
5 */
6 class ServicesJsonTest extends MediaWikiTestCase {
7 /**
8 * Test to make sure core json_encode() and our Services_Json()->encode()
9 * produce the same output
10 *
11 * @dataProvider provideValuesToEncode
12 */
13 public function testJsonEncode( $input, $desc ) {
14 if ( !function_exists( 'json_encode' ) ) {
15 $this->markTestIncomplete( 'No PHP json support, unable to test' );
16 return;
17 } elseif( strtolower( json_encode( "\xf0\xa0\x80\x80" ) ) != '"\ud840\udc00"' ) {
18 $this->markTestIncomplete( 'Have buggy PHP json support, unable to test' );
19 return;
20 } else {
21 $jsonObj = new Services_JSON();
22 $this->assertEquals(
23 $jsonObj->encode( $input ),
24 json_encode( $input ),
25 $desc
26 );
27 }
28 }
29
30 /**
31 * Test to make sure core json_decode() and our Services_Json()->decode()
32 * produce the same output
33 *
34 * @dataProvider provideValuesToDecode
35 */
36 public function testJsonDecode( $input, $desc ) {
37 if ( !function_exists( 'json_decode' ) ) {
38 $this->markTestIncomplete( 'No PHP json support, unable to test' );
39 return;
40 } else {
41 $jsonObj = new Services_JSON();
42 $this->assertEquals(
43 $jsonObj->decode( $input ),
44 json_decode( $input ),
45 $desc
46 );
47 }
48 }
49
50 function provideValuesToEncode() {
51 $obj = new stdClass();
52 $obj->property = 'value';
53 $obj->property2 = null;
54 $obj->property3 = 1.234;
55 return array(
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' ),
71 );
72 }
73
74 function provideValuesToDecode() {
75 return array(
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' ),
91 );
92 }
93 }