aboutsummaryrefslogtreecommitdiffstats
path: root/tests/phpunit/includes/json/FormatJsonTest.php
diff options
context:
space:
mode:
authorBryan Davis <bd808@wikimedia.org>2014-10-30 17:22:08 -0600
committerBryan Davis <bd808@wikimedia.org>2014-10-31 11:25:25 -0600
commit5e23faa0f4637e9a4cdcba6d00dfe3ef85f641c1 (patch)
tree4a8110e13c2db7a0ac06c145fb6d03828135877c /tests/phpunit/includes/json/FormatJsonTest.php
parentb6be73dd87c6450885c40b87cb6eac159220fba3 (diff)
downloadmediawikicore-5e23faa0f4637e9a4cdcba6d00dfe3ef85f641c1.tar.gz
mediawikicore-5e23faa0f4637e9a4cdcba6d00dfe3ef85f641c1.zip
hhvm: fix FormatJsonTest::testParseTryFixing for lenient json parser
Bug: 72778 Change-Id: I147e0c2107c4d74d2d50ede91d329d524b1f0798
Diffstat (limited to 'tests/phpunit/includes/json/FormatJsonTest.php')
-rw-r--r--tests/phpunit/includes/json/FormatJsonTest.php55
1 files changed, 45 insertions, 10 deletions
diff --git a/tests/phpunit/includes/json/FormatJsonTest.php b/tests/phpunit/includes/json/FormatJsonTest.php
index 456266f7be6a..76576338f4f1 100644
--- a/tests/phpunit/includes/json/FormatJsonTest.php
+++ b/tests/phpunit/includes/json/FormatJsonTest.php
@@ -169,12 +169,28 @@ class FormatJsonTest extends MediaWikiTestCase {
$this->assertEquals( $value, $st->getValue() );
}
+ /**
+ * Test data for testParseTryFixing.
+ *
+ * HHVM has a lenient json parser (yeah great idea right?) which allows
+ * trailing commas for array and object delarations among other things, so
+ * our JSON_ERROR_SYNTAX rescue block is not always triggered. It however
+ * isn't lenient in exactly the same ways as our TRY_FIXING mode, so the
+ * assertions in this test are a bit more complicated than they ideally
+ * would be:
+ *
+ * Optional third argument: true if hhvm parses the value without
+ * intervention, false otherwise. Defaults to true.
+ *
+ * Optional fourth argument: expected cannonical JSON serialization of HHVM
+ * parsed result. Defaults to the second argument's value.
+ */
public static function provideParseTryFixing() {
return array(
- array( "[,]", '[]' ),
- array( "[ , ]", '[]' ),
+ array( "[,]", '[]', false ),
+ array( "[ , ]", '[]', false ),
array( "[ , }", false ),
- array( '[1],', false ),
+ array( '[1],', false, true, '[1]' ),
array( "[1,]", '[1]' ),
array( "[1\n,]", '[1]' ),
array( "[1,\n]", '[1]' ),
@@ -182,24 +198,43 @@ class FormatJsonTest extends MediaWikiTestCase {
array( "[1\n,\n]\n", '[1]' ),
array( '["a,",]', '["a,"]' ),
array( "[[1,]\n,[2,\n],[3\n,]]", '[[1],[2],[3]]' ),
- array( '[[1,],[2,],[3,]]', false ), // I wish we could parse this, but would need quote parsing
- array( '[1,,]', false ),
+ // I wish we could parse this, but would need quote parsing
+ array( '[[1,],[2,],[3,]]', false, true, '[[1],[2],[3]]' ),
+ array( '[1,,]', false, false, '[1]' ),
);
}
/**
* @dataProvider provideParseTryFixing
* @param string $value
- * @param string|bool $expected
+ * @param string|bool $expected Expected result with Zend PHP
+ * @param bool $hhvmParses Will HHVM parse this value without TRY_FIXING?
+ * @param string|bool $expectedHHVM Expected result with HHVM if different
+ * from the PHP5 expectation
*/
- public function testParseTryFixing( $value, $expected ) {
+ public function testParseTryFixing(
+ $value, $expected,
+ $hhvmParses = true, $expectedHHVM = null
+ ) {
+ // PHP5 results are always expected to have isGood() === false
+ $expectedGoodStatus = false;
+
+ if ( wfIsHHVM() ) {
+ // Use HHVM specific expected result if provided
+ $expected = ( $expectedHHVM === null ) ? $expected : $expectedHHVM;
+ // If HHVM parses the value natively, expect isGood() === true
+ $expectedGoodStatus = $hhvmParses;
+ }
+
$st = FormatJson::parse( $value, FormatJson::TRY_FIXING );
$this->assertType( 'Status', $st );
if ( $expected === false ) {
- $this->assertFalse( $st->isOK() );
+ $this->assertFalse( $st->isOK(), 'Expected isOK() == false' );
} else {
- $this->assertFalse( $st->isGood() );
- $this->assertTrue( $st->isOK() );
+ $this->assertSame( $expectedGoodStatus, $st->isGood(),
+ 'Expected isGood() == ' . ( $expectedGoodStatus ? 'true' : 'false' )
+ );
+ $this->assertTrue( $st->isOK(), 'Expected isOK == true' );
$val = FormatJson::encode( $st->getValue(), false, FormatJson::ALL_OK );
$this->assertEquals( $expected, $val );
}