r96713 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r96712‎ | r96713 | r96714 >
Date:05:24, 10 September 2011
Author:demon
Status:ok (Comments)
Tags:
Comment:
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 :)
Modified paths:
  • /trunk/phase3/tests/phpunit/includes/json (added) (history)
  • /trunk/phase3/tests/phpunit/includes/json/ServicesJsonTest.php (added) (history)

Diff [purge]

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
173 + native

Follow-up revisions

RevisionCommit summaryAuthorDate
r96717Add some other ServicesJson tests.dantman07:15, 10 September 2011

Comments

#Comment by Hashar (talk | contribs)   16:26, 17 October 2011

The if / else if skipping test in testJsonEncode() could probably be rewritten using test dependencies. That might make the test a bit faster.

#Comment by Hashar (talk | contribs)   16:27, 17 October 2011

Your provider may also provide the expected comparison status (true / false ). This way you can add expecting failures in your code :D Anyway, it is a good base to build upon. Congrats :-)

Status & tagging log