5 * Created on Feb 10, 2013
7 * Copyright © 2013 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
27 /** This class has some common functionality for testing query module
29 abstract class ApiQueryTestBase
extends ApiTestCase
{
31 const PARAM_ASSERT
= <<<STR
32 Each parameter must be an array of two elements,
33 first - an array of params to the API call,
34 and the second array - expected results as returned by the API
38 * Merges all requests parameter + expected values into one
39 * @param ... list of arrays, each of which contains exactly two
42 protected function merge( /*...*/ ) {
45 foreach ( func_get_args() as $v ) {
46 list( $req, $exp ) = $this->validateRequestExpectedPair( $v );
47 $request = array_merge_recursive( $request, $req );
48 $this->mergeExpected( $expected, $exp );
51 return array( $request, $expected );
55 * Check that the parameter is a valid two element array,
56 * with the first element being API request and the second - expected result
58 private function validateRequestExpectedPair( $v ) {
59 $this->assertType( 'array', $v, self
::PARAM_ASSERT
);
60 $this->assertEquals( 2, count( $v ), self
::PARAM_ASSERT
);
61 $this->assertArrayHasKey( 0, $v, self
::PARAM_ASSERT
);
62 $this->assertArrayHasKey( 1, $v, self
::PARAM_ASSERT
);
63 $this->assertType( 'array', $v[0], self
::PARAM_ASSERT
);
64 $this->assertType( 'array', $v[1], self
::PARAM_ASSERT
);
70 * Recursively merges the expected values in the $item into the $all
72 private function mergeExpected( &$all, $item ) {
73 foreach ( $item as $k => $v ) {
74 if ( array_key_exists( $k, $all ) ) {
75 if ( is_array( $all[$k] ) ) {
76 $this->mergeExpected( $all[$k], $v );
78 $this->assertEquals( $all[$k], $v );
87 * Checks that the request's result matches the expected results.
88 * @param $values array is a two element array( request, expected_results )
91 protected function check( $values ) {
92 list( $req, $exp ) = $this->validateRequestExpectedPair( $values );
93 if ( !array_key_exists( 'action', $req ) ) {
94 $req['action'] = 'query';
96 foreach ( $req as &$val ) {
97 if ( is_array( $val ) ) {
98 $val = implode( '|', array_unique( $val ) );
101 $result = $this->doApiRequest( $req );
102 $this->assertResult( array( 'query' => $exp ), $result[0], $req );
105 protected function assertResult( $exp, $result, $message = '' ) {
107 $this->assertResultRecursive( $exp, $result );
108 } catch ( Exception
$e ) {
109 if ( is_array( $message ) ) {
110 $message = http_build_query( $message );
112 print "\nRequest: $message\n";
113 print "\nExpected:\n";
117 throw $e; // rethrow it
122 * Recursively compare arrays, ignoring mismatches in numeric key and pageids.
123 * @param $expected array expected values
124 * @param $result array returned values
126 private function assertResultRecursive( $expected, $result ) {
130 $e = each( $expected );
131 $r = each( $result );
132 // If either of the arrays is shorter, abort. If both are done, success.
133 $this->assertEquals( (bool)$e, (bool)$r );
137 // continue only if keys are identical or both keys are numeric
138 $this->assertTrue( $e['key'] === $r['key'] ||
( is_numeric( $e['key'] ) && is_numeric( $r['key'] ) ) );
139 // don't compare pageids
140 if ( $e['key'] !== 'pageid' ) {
141 // If values are arrays, compare recursively, otherwise compare with ===
142 if ( is_array( $e['value'] ) && is_array( $r['value'] ) ) {
143 $this->assertResultRecursive( $e['value'], $r['value'] );
145 $this->assertEquals( $e['value'], $r['value'] );