(bug 17602) fix Monobook action tabs not quite touching the page body
[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
28 /** This class has some common functionality for testing query module
29 */
30 abstract class ApiQueryTestBase extends ApiTestCase {
31
32 const PARAM_ASSERT = <<<STR
33 Each parameter must be an array of two elements,
34 first - an array of params to the API call,
35 and the second array - expected results as returned by the API
36 STR;
37
38 /**
39 * Merges all requests parameter + expected values into one
40 * @param ... list of arrays, each of which contains exactly two
41 * @return array
42 */
43 protected function merge( /*...*/ ) {
44 $request = array();
45 $expected = array();
46 foreach ( func_get_args() as $v ) {
47 list( $req, $exp ) = $this->validateRequestExpectedPair( $v );
48 $request = array_merge_recursive( $request, $req );
49 $this->mergeExpected( $expected, $exp );
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 return $v;
66 }
67
68 /**
69 * Recursively merges the expected values in the $item into the $all
70 */
71 private function mergeExpected( &$all, $item ) {
72 foreach ( $item as $k => $v ) {
73 if ( array_key_exists( $k, $all ) ) {
74 if ( is_array( $all[$k] ) ) {
75 $this->mergeExpected( $all[$k], $v );
76 } else {
77 $this->assertEquals( $all[$k], $v );
78 }
79 } else {
80 $all[$k] = $v;
81 }
82 }
83 }
84
85 /**
86 * Checks that the request's result matches the expected results.
87 * @param $values array is a two element array( request, expected_results )
88 * @throws Exception
89 */
90 protected function check( $values ) {
91 list( $req, $exp ) = $this->validateRequestExpectedPair( $values );
92 if ( !array_key_exists( 'action', $req ) ) {
93 $req['action'] = 'query';
94 }
95 foreach ( $req as &$val ) {
96 if ( is_array( $val ) ) {
97 $val = implode( '|', array_unique( $val ) );
98 }
99 }
100 $result = $this->doApiRequest( $req );
101 $this->assertResult( array( 'query' => $exp ), $result[0], $req );
102 }
103
104 protected function assertResult( $exp, $result, $message = '' ) {
105 try {
106 $this->assertResultRecursive( $exp, $result );
107 } catch ( Exception $e ) {
108 if ( is_array( $message ) ) {
109 $message = http_build_query( $message );
110 }
111 print( "\nRequest: $message\n" );
112 print( "\nExpected:\n" );
113 print_r( $exp );
114 print( "\nResult:\n" );
115 print_r( $result );
116 throw $e; // rethrow it
117 }
118 }
119
120 /**
121 * Recursively compare arrays, ignoring mismatches in numeric key and pageids.
122 * @param $expected array expected values
123 * @param $result array returned values
124 */
125 private function assertResultRecursive( $expected, $result ) {
126 reset( $expected );
127 reset( $result );
128 while ( true ) {
129 $e = each( $expected );
130 $r = each( $result );
131 // If either of the arrays is shorter, abort. If both are done, success.
132 $this->assertEquals( (bool)$e, (bool)$r );
133 if ( !$e ) {
134 break; // done
135 }
136 // continue only if keys are identical or both keys are numeric
137 $this->assertTrue( $e['key'] === $r['key'] || ( is_numeric( $e['key'] ) && is_numeric( $r['key'] ) ) );
138 // don't compare pageids
139 if ( $e['key'] !== 'pageid' ) {
140 // If values are arrays, compare recursively, otherwise compare with ===
141 if ( is_array( $e['value'] ) && is_array( $r['value'] ) ) {
142 $this->assertResultRecursive( $e['value'], $r['value'] );
143 } else {
144 $this->assertEquals( $e['value'], $r['value'] );
145 }
146 }
147 }
148 }
149 }