http://www.mediawiki.org/wiki/Special:Code/MediaWiki/96713
Revision: 96713
Author: demon
Date: 2011-09-10 05:24:46 +0000 (Sat, 10 Sep 2011)
Log Message:
-----------
Add some basic tests to compare output of native json support and the
Services_Json implementation, as I promised on wikitech-l. Could use plenty
more test cases, but that should be trivial since I did it properly with data
providers :)
Added Paths:
-----------
trunk/phase3/tests/phpunit/includes/json/
trunk/phase3/tests/phpunit/includes/json/ServicesJsonTest.php
Added: trunk/phase3/tests/phpunit/includes/json/ServicesJsonTest.php
===================================================================
--- trunk/phase3/tests/phpunit/includes/json/ServicesJsonTest.php
(rev 0)
+++ trunk/phase3/tests/phpunit/includes/json/ServicesJsonTest.php
2011-09-10 05:24:46 UTC (rev 96713)
@@ -0,0 +1,71 @@
+<?php
+/*
+ * Test cases for our Services_Json library. Requires PHP json support as well,
+ * so we can compare output
+ */
+class ServicesJsonTest extends MediaWikiTestCase {
+ /**
+ * Test to make sure core json_encode() and our
Services_Json()->encode()
+ * produce the same output
+ *
+ * @dataProvider provideValuesToEncode
+ */
+ public function testJsonEncode( $input, $desc ) {
+ if ( !function_exists( 'json_encode' ) ) {
+ $this->markTestIncomplete( 'No PHP json support, unable
to test' );
+ return;
+ } elseif( strtolower( json_encode( "\xf0\xa0\x80\x80" ) ) !=
'"\ud840\udc00"' ) {
+ $this->markTestIncomplete( 'Have buggy PHP json
support, unable to test' );
+ return;
+ } else {
+ $jsonObj = new Services_JSON();
+ $this->assertEquals(
+ $jsonObj->encode( $input ),
+ json_encode( $input ),
+ $desc
+ );
+ }
+ }
+
+ /**
+ * Test to make sure core json_decode() and our
Services_Json()->decode()
+ * produce the same output
+ *
+ * @dataProvider provideValuesToDecode
+ */
+ public function testJsonDecode( $input, $desc ) {
+ if ( !function_exists( 'json_decode' ) ) {
+ $this->markTestIncomplete( 'No PHP json support, unable
to test' );
+ return;
+ } else {
+ $jsonObj = new Services_JSON();
+ $this->assertEquals(
+ $jsonObj->decode( $input ),
+ json_decode( $input ),
+ $desc
+ );
+ }
+ }
+
+ function provideValuesToEncode() {
+ $obj = new stdClass();
+ $obj->property = 'value';
+ $obj->property2 = null;
+ $obj->property3 = 1.234;
+ return array(
+ array( 1, 'basic integer' ),
+ array( true, 'basic bool true' ),
+ array( false, 'basic bool false' ),
+ array( 'some string', 'basic string test' ),
+ array( array( 'some', 'string', 'values' ), 'basic
array of strings' ),
+ array( array( 'key1' => 'val1', 'key2' => 'val2' ),
'array with string keys' ),
+ array( $obj, 'basic object test' ),
+ );
+ }
+
+ function provideValuesToDecode() {
+ return array(
+ array( '{"key":"value"}', 'Basic key => value test' ),
+ );
+ }
+}
Property changes on:
trunk/phase3/tests/phpunit/includes/json/ServicesJsonTest.php
___________________________________________________________________
Added: svn:eol-style
+ native
_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs