aboutsummaryrefslogtreecommitdiffstats
path: root/tests/phpunit/includes/api/query/ApiQueryTestBase.php
diff options
context:
space:
mode:
authorBrad Jorsch <bjorsch@wikimedia.org>2013-12-04 11:52:32 -0500
committerBrad Jorsch <bjorsch@wikimedia.org>2013-12-04 12:00:12 -0500
commit946c575fe6993196eb88e96c0bb7d159fc96c99b (patch)
tree1e39f5281673ad3c7d37d260500a8c187d09e8c9 /tests/phpunit/includes/api/query/ApiQueryTestBase.php
parent2f36dff7d41b1864f4f3c9be21e3b63676da9697 (diff)
downloadmediawikicore-946c575fe6993196eb88e96c0bb7d159fc96c99b.tar.gz
mediawikicore-946c575fe6993196eb88e96c0bb7d159fc96c99b.zip
Improve ApiQueryTestBase::assertResult
It really shouldn't be printing output in the middle of the phpunit progress indicator; this output should be included in the final output instead. This rewrites the function to make use of PHPUnit's data structure diffing support. Bug: 57974 Change-Id: Ia4b66182922005da8e2bc173794d712492ad13fa
Diffstat (limited to 'tests/phpunit/includes/api/query/ApiQueryTestBase.php')
-rw-r--r--tests/phpunit/includes/api/query/ApiQueryTestBase.php60
1 files changed, 28 insertions, 32 deletions
diff --git a/tests/phpunit/includes/api/query/ApiQueryTestBase.php b/tests/phpunit/includes/api/query/ApiQueryTestBase.php
index 9cfab41c0208..a657459d31dd 100644
--- a/tests/phpunit/includes/api/query/ApiQueryTestBase.php
+++ b/tests/phpunit/includes/api/query/ApiQueryTestBase.php
@@ -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 );
}
}