X-Git-Url: http://git.heureux-cyclage.org/?a=blobdiff_plain;f=tests%2Fphpunit%2Fincludes%2Fapi%2Fquery%2FApiQueryTestBase.php;h=1b9c1ce86a1ff8dda6b29c9190bac0eb94639299;hb=ba8a835dcb3de335a04c81ebe8980a858d478cae;hp=9cfab41c020843790dc2078ca37ecded8956d187;hpb=6c539fc9e62c04172ba44aae54cb11acf3995255;p=lhc%2Fweb%2Fwiklou.git diff --git a/tests/phpunit/includes/api/query/ApiQueryTestBase.php b/tests/phpunit/includes/api/query/ApiQueryTestBase.php index 9cfab41c02..1b9c1ce86a 100644 --- a/tests/phpunit/includes/api/query/ApiQueryTestBase.php +++ b/tests/phpunit/includes/api/query/ApiQueryTestBase.php @@ -83,7 +83,7 @@ STR; /** * Checks that the request's result matches the expected results. - * @param $values array is a two element array( request, expected_results ) + * @param array $values Array is a two element array( request, expected_results ) * @throws Exception */ protected function check( $values ) { @@ -102,47 +102,43 @@ STR; protected function assertResult( $exp, $result, $message = '' ) { try { - $this->assertResultRecursive( $exp, $result ); - } catch ( Exception $e ) { + $exp = self::sanitizeResultArray( $exp ); + $result = self::sanitizeResultArray( $result ); + $this->assertEquals( $exp, $result ); + } catch ( PHPUnit_Framework_ExpectationFailedException $e ) { if ( is_array( $message ) ) { $message = http_build_query( $message ); } - print "\nRequest: $message\n"; - print "\nExpected:\n"; - print_r( $exp ); - print "\nResult:\n"; - print_r( $result ); - throw $e; // rethrow it + throw new PHPUnit_Framework_ExpectationFailedException( + $e->getMessage() . "\nRequest: $message", + new PHPUnit_Framework_ComparisonFailure( + $exp, + $result, + print_r( $exp, true ), + print_r( $result, true ), + false, + $e->getComparisonFailure()->getMessage() . "\nRequest: $message" + ) + ); } } /** - * Recursively compare arrays, ignoring mismatches in numeric key and pageids. - * @param $expected array expected values - * @param $result array returned values + * Recursively ksorts a result array and removes any 'pageid' keys. + * @param array $result + * @return array */ - private function assertResultRecursive( $expected, $result ) { - reset( $expected ); - reset( $result ); - while ( true ) { - $e = each( $expected ); - $r = each( $result ); - // If either of the arrays is shorter, abort. If both are done, success. - $this->assertEquals( (bool)$e, (bool)$r ); - if ( !$e ) { - break; // done - } - // continue only if keys are identical or both keys are numeric - $this->assertTrue( $e['key'] === $r['key'] || ( is_numeric( $e['key'] ) && is_numeric( $r['key'] ) ) ); - // don't compare pageids - if ( $e['key'] !== 'pageid' ) { - // If values are arrays, compare recursively, otherwise compare with === - if ( is_array( $e['value'] ) && is_array( $r['value'] ) ) { - $this->assertResultRecursive( $e['value'], $r['value'] ); - } else { - $this->assertEquals( $e['value'], $r['value'] ); - } + private static function sanitizeResultArray( $result ) { + unset( $result['pageid'] ); + foreach ( $result as $key => $value ) { + if ( is_array( $value ) ) { + $result[$key] = self::sanitizeResultArray( $value ); } } + + // Sort the result by keys, then take advantage of how array_merge will + // renumber numeric keys while leaving others alone. + ksort( $result ); + return array_merge( $result ); } }