MediaWiki
REL1_22
|
00001 <?php 00029 abstract class ApiQueryTestBase extends ApiTestCase { 00030 00031 const PARAM_ASSERT = <<<STR 00032 Each parameter must be an array of two elements, 00033 first - an array of params to the API call, 00034 and the second array - expected results as returned by the API 00035 STR; 00036 00042 protected function merge( /*...*/ ) { 00043 $request = array(); 00044 $expected = array(); 00045 foreach ( func_get_args() as $v ) { 00046 list( $req, $exp ) = $this->validateRequestExpectedPair( $v ); 00047 $request = array_merge_recursive( $request, $req ); 00048 $this->mergeExpected( $expected, $exp ); 00049 } 00050 00051 return array( $request, $expected ); 00052 } 00053 00058 private function validateRequestExpectedPair( $v ) { 00059 $this->assertType( 'array', $v, self::PARAM_ASSERT ); 00060 $this->assertEquals( 2, count( $v ), self::PARAM_ASSERT ); 00061 $this->assertArrayHasKey( 0, $v, self::PARAM_ASSERT ); 00062 $this->assertArrayHasKey( 1, $v, self::PARAM_ASSERT ); 00063 $this->assertType( 'array', $v[0], self::PARAM_ASSERT ); 00064 $this->assertType( 'array', $v[1], self::PARAM_ASSERT ); 00065 00066 return $v; 00067 } 00068 00072 private function mergeExpected( &$all, $item ) { 00073 foreach ( $item as $k => $v ) { 00074 if ( array_key_exists( $k, $all ) ) { 00075 if ( is_array( $all[$k] ) ) { 00076 $this->mergeExpected( $all[$k], $v ); 00077 } else { 00078 $this->assertEquals( $all[$k], $v ); 00079 } 00080 } else { 00081 $all[$k] = $v; 00082 } 00083 } 00084 } 00085 00091 protected function check( $values ) { 00092 list( $req, $exp ) = $this->validateRequestExpectedPair( $values ); 00093 if ( !array_key_exists( 'action', $req ) ) { 00094 $req['action'] = 'query'; 00095 } 00096 foreach ( $req as &$val ) { 00097 if ( is_array( $val ) ) { 00098 $val = implode( '|', array_unique( $val ) ); 00099 } 00100 } 00101 $result = $this->doApiRequest( $req ); 00102 $this->assertResult( array( 'query' => $exp ), $result[0], $req ); 00103 } 00104 00105 protected function assertResult( $exp, $result, $message = '' ) { 00106 try { 00107 $this->assertResultRecursive( $exp, $result ); 00108 } catch ( Exception $e ) { 00109 if ( is_array( $message ) ) { 00110 $message = http_build_query( $message ); 00111 } 00112 print "\nRequest: $message\n"; 00113 print "\nExpected:\n"; 00114 print_r( $exp ); 00115 print "\nResult:\n"; 00116 print_r( $result ); 00117 throw $e; // rethrow it 00118 } 00119 } 00120 00126 private function assertResultRecursive( $expected, $result ) { 00127 reset( $expected ); 00128 reset( $result ); 00129 while ( true ) { 00130 $e = each( $expected ); 00131 $r = each( $result ); 00132 // If either of the arrays is shorter, abort. If both are done, success. 00133 $this->assertEquals( (bool)$e, (bool)$r ); 00134 if ( !$e ) { 00135 break; // done 00136 } 00137 // continue only if keys are identical or both keys are numeric 00138 $this->assertTrue( $e['key'] === $r['key'] || ( is_numeric( $e['key'] ) && is_numeric( $r['key'] ) ) ); 00139 // don't compare pageids 00140 if ( $e['key'] !== 'pageid' ) { 00141 // If values are arrays, compare recursively, otherwise compare with === 00142 if ( is_array( $e['value'] ) && is_array( $r['value'] ) ) { 00143 $this->assertResultRecursive( $e['value'], $r['value'] ); 00144 } else { 00145 $this->assertEquals( $e['value'], $r['value'] ); 00146 } 00147 } 00148 } 00149 } 00150 }