Add some basic tests to compare output of native json support and the Services_Json...
[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( true, 'basic bool true' ),
58 array( false, 'basic bool false' ),
59 array( 'some string', 'basic string test' ),
60 array( array( 'some', 'string', 'values' ), 'basic array of strings' ),
61 array( array( 'key1' => 'val1', 'key2' => 'val2' ), 'array with string keys' ),
62 array( $obj, 'basic object test' ),
63 );
64 }
65
66 function provideValuesToDecode() {
67 return array(
68 array( '{"key":"value"}', 'Basic key => value test' ),
69 );
70 }
71 }