Added "maxPartitionsTry" option to JobQueueFederated
[lhc/web/wiklou.git] / tests / phpunit / includes / api / query / ApiQueryTestBase.php
1 <?php
2 /**
3 *
4 *
5 * Created on Feb 10, 2013
6 *
7 * Copyright © 2013 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
8 *
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.
13 *
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.
18 *
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
23 *
24 * @file
25 */
26
27 /** This class has some common functionality for testing query module
28 */
29 abstract class ApiQueryTestBase extends ApiTestCase {
30
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
35 STR;
36
37 /**
38 * Merges all requests parameter + expected values into one
39 * @param ... list of arrays, each of which contains exactly two
40 * @return array
41 */
42 protected function merge( /*...*/ ) {
43 $request = array();
44 $expected = array();
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 );
49 }
50
51 return array( $request, $expected );
52 }
53
54 /**
55 * Check that the parameter is a valid two element array,
56 * with the first element being API request and the second - expected result
57 */
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 );
65
66 return $v;
67 }
68
69 /**
70 * Recursively merges the expected values in the $item into the $all
71 */
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 );
77 } else {
78 $this->assertEquals( $all[$k], $v );
79 }
80 } else {
81 $all[$k] = $v;
82 }
83 }
84 }
85
86 /**
87 * Checks that the request's result matches the expected results.
88 * @param $values array is a two element array( request, expected_results )
89 * @throws Exception
90 */
91 protected function check( $values ) {
92 list( $req, $exp ) = $this->validateRequestExpectedPair( $values );
93 if ( !array_key_exists( 'action', $req ) ) {
94 $req['action'] = 'query';
95 }
96 foreach ( $req as &$val ) {
97 if ( is_array( $val ) ) {
98 $val = implode( '|', array_unique( $val ) );
99 }
100 }
101 $result = $this->doApiRequest( $req );
102 $this->assertResult( array( 'query' => $exp ), $result[0], $req );
103 }
104
105 protected function assertResult( $exp, $result, $message = '' ) {
106 try {
107 $this->assertResultRecursive( $exp, $result );
108 } catch ( Exception $e ) {
109 if ( is_array( $message ) ) {
110 $message = http_build_query( $message );
111 }
112 print "\nRequest: $message\n";
113 print "\nExpected:\n";
114 print_r( $exp );
115 print "\nResult:\n";
116 print_r( $result );
117 throw $e; // rethrow it
118 }
119 }
120
121 /**
122 * Recursively compare arrays, ignoring mismatches in numeric key and pageids.
123 * @param $expected array expected values
124 * @param $result array returned values
125 */
126 private function assertResultRecursive( $expected, $result ) {
127 reset( $expected );
128 reset( $result );
129 while ( true ) {
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 );
134 if ( !$e ) {
135 break; // done
136 }
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'] );
144 } else {
145 $this->assertEquals( $e['value'], $r['value'] );
146 }
147 }
148 }
149 }
150 }