MediaWiki  REL1_24
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 
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 
00074     private function mergeExpected( &$all, $item ) {
00075         foreach ( $item as $k => $v ) {
00076             if ( array_key_exists( $k, $all ) ) {
00077                 if ( is_array( $all[$k] ) ) {
00078                     $this->mergeExpected( $all[$k], $v );
00079                 } else {
00080                     $this->assertEquals( $all[$k], $v );
00081                 }
00082             } else {
00083                 $all[$k] = $v;
00084             }
00085         }
00086     }
00087 
00093     protected function check( $values ) {
00094         list( $req, $exp ) = $this->validateRequestExpectedPair( $values );
00095         if ( !array_key_exists( 'action', $req ) ) {
00096             $req['action'] = 'query';
00097         }
00098         foreach ( $req as &$val ) {
00099             if ( is_array( $val ) ) {
00100                 $val = implode( '|', array_unique( $val ) );
00101             }
00102         }
00103         $result = $this->doApiRequest( $req );
00104         $this->assertResult( array( 'query' => $exp ), $result[0], $req );
00105     }
00106 
00107     protected function assertResult( $exp, $result, $message = '' ) {
00108         try {
00109             $exp = self::sanitizeResultArray( $exp );
00110             $result = self::sanitizeResultArray( $result );
00111             $this->assertEquals( $exp, $result );
00112         } catch ( PHPUnit_Framework_ExpectationFailedException $e ) {
00113             if ( is_array( $message ) ) {
00114                 $message = http_build_query( $message );
00115             }
00116             throw new PHPUnit_Framework_ExpectationFailedException(
00117                 $e->getMessage() . "\nRequest: $message",
00118                 new PHPUnit_Framework_ComparisonFailure(
00119                     $exp,
00120                     $result,
00121                     print_r( $exp, true ),
00122                     print_r( $result, true ),
00123                     false,
00124                     $e->getComparisonFailure()->getMessage() . "\nRequest: $message"
00125                 )
00126             );
00127         }
00128     }
00129 
00135     private static function sanitizeResultArray( $result ) {
00136         unset( $result['pageid'] );
00137         foreach ( $result as $key => $value ) {
00138             if ( is_array( $value ) ) {
00139                 $result[$key] = self::sanitizeResultArray( $value );
00140             }
00141         }
00142 
00143         // Sort the result by keys, then take advantage of how array_merge will
00144         // renumber numeric keys while leaving others alone.
00145         ksort( $result );
00146         return array_merge( $result );
00147     }
00148 }