MediaWiki  REL1_19
ServicesJsonTest.php
Go to the documentation of this file.
00001 <?php
00002 /* 
00003  * Test cases for our Services_Json library. Requires PHP json support as well,
00004  * so we can compare output
00005  */
00006 class ServicesJsonTest extends MediaWikiTestCase {
00013         public function testJsonEncode( $input, $desc ) {
00014                 if ( !function_exists( 'json_encode' ) ) {
00015                         $this->markTestIncomplete( 'No PHP json support, unable to test' );
00016                         return;
00017                 } elseif( strtolower( json_encode( "\xf0\xa0\x80\x80" ) ) != '"\ud840\udc00"' ) {
00018                         $this->markTestIncomplete( 'Have buggy PHP json support, unable to test' );
00019                         return;
00020                 } else {
00021                         $jsonObj = new Services_JSON();
00022                         $this->assertEquals(
00023                                 $jsonObj->encode( $input ),
00024                                 json_encode( $input ),
00025                                 $desc
00026                         );
00027                 }
00028         }
00029 
00036         public function testJsonDecode( $input, $desc ) {
00037                 if ( !function_exists( 'json_decode' ) ) {
00038                         $this->markTestIncomplete( 'No PHP json support, unable to test' );
00039                         return;
00040                 } else {
00041                         $jsonObj = new Services_JSON();
00042                         $this->assertEquals(
00043                                 $jsonObj->decode( $input ),
00044                                 json_decode( $input ),
00045                                 $desc
00046                         );
00047                 }
00048         }
00049 
00050         function provideValuesToEncode() {
00051                 $obj = new stdClass();
00052                 $obj->property = 'value';
00053                 $obj->property2 = null;
00054                 $obj->property3 = 1.234;
00055                 return array(
00056                         array( 1, 'basic integer' ),
00057                         array( -1, 'negative integer' ),
00058                         array( 1.1, 'basic float' ),
00059                         array( true, 'basic bool true' ),
00060                         array( false, 'basic bool false' ),
00061                         array( 'some string', 'basic string test' ),
00062                         array( "some string\nwith newline", 'newline string test' ),
00063                         array( '♥ü', 'unicode string test' ),
00064                         array( array( 'some', 'string', 'values' ), 'basic array of strings' ),
00065                         array( array( 'key1' => 'val1', 'key2' => 'val2' ), 'array with string keys' ),
00066                         array( array( 1 => 'val1', 3 => 'val2', '2' => 'val3' ), 'out of order numbered array test' ),
00067                         array( array(), 'empty array test' ),
00068                         array( $obj, 'basic object test' ),
00069                         array( new stdClass, 'empty object test' ),
00070                         array( null, 'null test' ),
00071                 );
00072         }
00073 
00074         function provideValuesToDecode() {
00075                 return array(
00076                         array( '1', 'basic integer' ),
00077                         array( '-1', 'negative integer' ),
00078                         array( '1.1', 'basic float' ),
00079                         array( '1.1e1', 'scientific float' ),
00080                         array( 'true', 'basic bool true' ),
00081                         array( 'false', 'basic bool false' ),
00082                         array( '"some string"', 'basic string test' ),
00083                         array( '"some string\nwith newline"', 'newline string test' ),
00084                         array( '"♥ü"', 'unicode character string test' ),
00085                         array( '"\u2665"', 'unicode \\u string test' ),
00086                         array( '["some","string","values"]', 'basic array of strings' ),
00087                         array( '[]', 'empty array test' ),
00088                         array( '{"key":"value"}', 'Basic key => value test' ),
00089                         array( '{}', 'empty object test' ),
00090                         array( 'null', 'null test' ),
00091                 );
00092         }
00093 }