Index: trunk/phase3/tests/phpunit/includes/json/ServicesJsonTest.php |
— | — | @@ -0,0 +1,71 @@ |
| 2 | +<?php |
| 3 | +/* |
| 4 | + * Test cases for our Services_Json library. Requires PHP json support as well, |
| 5 | + * so we can compare output |
| 6 | + */ |
| 7 | +class ServicesJsonTest extends MediaWikiTestCase { |
| 8 | + /** |
| 9 | + * Test to make sure core json_encode() and our Services_Json()->encode() |
| 10 | + * produce the same output |
| 11 | + * |
| 12 | + * @dataProvider provideValuesToEncode |
| 13 | + */ |
| 14 | + public function testJsonEncode( $input, $desc ) { |
| 15 | + if ( !function_exists( 'json_encode' ) ) { |
| 16 | + $this->markTestIncomplete( 'No PHP json support, unable to test' ); |
| 17 | + return; |
| 18 | + } elseif( strtolower( json_encode( "\xf0\xa0\x80\x80" ) ) != '"\ud840\udc00"' ) { |
| 19 | + $this->markTestIncomplete( 'Have buggy PHP json support, unable to test' ); |
| 20 | + return; |
| 21 | + } else { |
| 22 | + $jsonObj = new Services_JSON(); |
| 23 | + $this->assertEquals( |
| 24 | + $jsonObj->encode( $input ), |
| 25 | + json_encode( $input ), |
| 26 | + $desc |
| 27 | + ); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Test to make sure core json_decode() and our Services_Json()->decode() |
| 33 | + * produce the same output |
| 34 | + * |
| 35 | + * @dataProvider provideValuesToDecode |
| 36 | + */ |
| 37 | + public function testJsonDecode( $input, $desc ) { |
| 38 | + if ( !function_exists( 'json_decode' ) ) { |
| 39 | + $this->markTestIncomplete( 'No PHP json support, unable to test' ); |
| 40 | + return; |
| 41 | + } else { |
| 42 | + $jsonObj = new Services_JSON(); |
| 43 | + $this->assertEquals( |
| 44 | + $jsonObj->decode( $input ), |
| 45 | + json_decode( $input ), |
| 46 | + $desc |
| 47 | + ); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + function provideValuesToEncode() { |
| 52 | + $obj = new stdClass(); |
| 53 | + $obj->property = 'value'; |
| 54 | + $obj->property2 = null; |
| 55 | + $obj->property3 = 1.234; |
| 56 | + return array( |
| 57 | + array( 1, 'basic integer' ), |
| 58 | + array( true, 'basic bool true' ), |
| 59 | + array( false, 'basic bool false' ), |
| 60 | + array( 'some string', 'basic string test' ), |
| 61 | + array( array( 'some', 'string', 'values' ), 'basic array of strings' ), |
| 62 | + array( array( 'key1' => 'val1', 'key2' => 'val2' ), 'array with string keys' ), |
| 63 | + array( $obj, 'basic object test' ), |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + function provideValuesToDecode() { |
| 68 | + return array( |
| 69 | + array( '{"key":"value"}', 'Basic key => value test' ), |
| 70 | + ); |
| 71 | + } |
| 72 | +} |
Property changes on: trunk/phase3/tests/phpunit/includes/json/ServicesJsonTest.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 73 | + native |