MediaWiki  REL1_23
ApiQueryTestBase.php
Go to the documentation of this file.
00001 <?php
00027 abstract class ApiQueryTestBase extends ApiTestCase {
00028 
00029     const PARAM_ASSERT = <<<STR
00030 Each parameter must be an array of two elements,
00031 first - an array of params to the API call,
00032 and the second array - expected results as returned by the API
00033 STR;
00034 
00040     protected function merge( /*...*/ ) {
00041         $request = array();
00042         $expected = array();
00043         foreach ( func_get_args() as $v ) {
00044             list( $req, $exp ) = $this->validateRequestExpectedPair( $v );
00045             $request = array_merge_recursive( $request, $req );
00046             $this->mergeExpected( $expected, $exp );
00047         }
00048 
00049         return array( $request, $expected );
00050     }
00051 
00056     private function validateRequestExpectedPair( $v ) {
00057         $this->assertType( 'array', $v, self::PARAM_ASSERT );
00058         $this->assertEquals( 2, count( $v ), self::PARAM_ASSERT );
00059         $this->assertArrayHasKey( 0, $v, self::PARAM_ASSERT );
00060         $this->assertArrayHasKey( 1, $v, self::PARAM_ASSERT );
00061         $this->assertType( 'array', $v[0], self::PARAM_ASSERT );
00062         $this->assertType( 'array', $v[1], self::PARAM_ASSERT );
00063 
00064         return $v;
00065     }
00066 
00070     private function mergeExpected( &$all, $item ) {
00071         foreach ( $item as $k => $v ) {
00072             if ( array_key_exists( $k, $all ) ) {
00073                 if ( is_array( $all[$k] ) ) {
00074                     $this->mergeExpected( $all[$k], $v );
00075                 } else {
00076                     $this->assertEquals( $all[$k], $v );
00077                 }
00078             } else {
00079                 $all[$k] = $v;
00080             }
00081         }
00082     }
00083 
00089     protected function check( $values ) {
00090         list( $req, $exp ) = $this->validateRequestExpectedPair( $values );
00091         if ( !array_key_exists( 'action', $req ) ) {
00092             $req['action'] = 'query';
00093         }
00094         foreach ( $req as &$val ) {
00095             if ( is_array( $val ) ) {
00096                 $val = implode( '|', array_unique( $val ) );
00097             }
00098         }
00099         $result = $this->doApiRequest( $req );
00100         $this->assertResult( array( 'query' => $exp ), $result[0], $req );
00101     }
00102 
00103     protected function assertResult( $exp, $result, $message = '' ) {
00104         try {
00105             $exp = self::sanitizeResultArray( $exp );
00106             $result = self::sanitizeResultArray( $result );
00107             $this->assertEquals( $exp, $result );
00108         } catch ( PHPUnit_Framework_ExpectationFailedException $e ) {
00109             if ( is_array( $message ) ) {
00110                 $message = http_build_query( $message );
00111             }
00112             throw new PHPUnit_Framework_ExpectationFailedException(
00113                 $e->getMessage() . "\nRequest: $message",
00114                 new PHPUnit_Framework_ComparisonFailure(
00115                     $exp,
00116                     $result,
00117                     print_r( $exp, true ),
00118                     print_r( $result, true ),
00119                     false,
00120                     $e->getComparisonFailure()->getMessage() . "\nRequest: $message"
00121                 )
00122             );
00123         }
00124     }
00125 
00131     private static function sanitizeResultArray( $result ) {
00132         unset( $result['pageid'] );
00133         foreach ( $result as $key => $value ) {
00134             if ( is_array( $value ) ) {
00135                 $result[$key] = self::sanitizeResultArray( $value );
00136             }
00137         }
00138 
00139         // Sort the result by keys, then take advantage of how array_merge will
00140         // renumber numeric keys while leaving others alone.
00141         ksort( $result );
00142         return array_merge( $result );
00143     }
00144 }