Merge "mw.language.convertPlural: Check if matching form exists"
[lhc/web/wiklou.git] / tests / phpunit / includes / api / query / ApiQueryTestBase.php
1 <?php
2 /**
3 * Created on Feb 10, 2013
4 *
5 * Copyright © 2013 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
21 *
22 * @file
23 */
24
25 /** This class has some common functionality for testing query module
26 */
27 abstract class ApiQueryTestBase extends ApiTestCase {
28
29 const PARAM_ASSERT = <<<STR
30 Each parameter must be an array of two elements,
31 first - an array of params to the API call,
32 and the second array - expected results as returned by the API
33 STR;
34
35 /**
36 * Merges all requests parameter + expected values into one
37 * @param ... list of arrays, each of which contains exactly two
38 * @return array
39 */
40 protected function merge( /*...*/ ) {
41 $request = array();
42 $expected = array();
43 foreach ( func_get_args() as $v ) {
44 list( $req, $exp ) = $this->validateRequestExpectedPair( $v );
45 $request = array_merge_recursive( $request, $req );
46 $this->mergeExpected( $expected, $exp );
47 }
48
49 return array( $request, $expected );
50 }
51
52 /**
53 * Check that the parameter is a valid two element array,
54 * with the first element being API request and the second - expected result
55 */
56 private function validateRequestExpectedPair( $v ) {
57 $this->assertType( 'array', $v, self::PARAM_ASSERT );
58 $this->assertEquals( 2, count( $v ), self::PARAM_ASSERT );
59 $this->assertArrayHasKey( 0, $v, self::PARAM_ASSERT );
60 $this->assertArrayHasKey( 1, $v, self::PARAM_ASSERT );
61 $this->assertType( 'array', $v[0], self::PARAM_ASSERT );
62 $this->assertType( 'array', $v[1], self::PARAM_ASSERT );
63
64 return $v;
65 }
66
67 /**
68 * Recursively merges the expected values in the $item into the $all
69 */
70 private function mergeExpected( &$all, $item ) {
71 foreach ( $item as $k => $v ) {
72 if ( array_key_exists( $k, $all ) ) {
73 if ( is_array( $all[$k] ) ) {
74 $this->mergeExpected( $all[$k], $v );
75 } else {
76 $this->assertEquals( $all[$k], $v );
77 }
78 } else {
79 $all[$k] = $v;
80 }
81 }
82 }
83
84 /**
85 * Checks that the request's result matches the expected results.
86 * @param $values array is a two element array( request, expected_results )
87 * @throws Exception
88 */
89 protected function check( $values ) {
90 list( $req, $exp ) = $this->validateRequestExpectedPair( $values );
91 if ( !array_key_exists( 'action', $req ) ) {
92 $req['action'] = 'query';
93 }
94 foreach ( $req as &$val ) {
95 if ( is_array( $val ) ) {
96 $val = implode( '|', array_unique( $val ) );
97 }
98 }
99 $result = $this->doApiRequest( $req );
100 $this->assertResult( array( 'query' => $exp ), $result[0], $req );
101 }
102
103 protected function assertResult( $exp, $result, $message = '' ) {
104 try {
105 $this->assertResultRecursive( $exp, $result );
106 } catch ( Exception $e ) {
107 if ( is_array( $message ) ) {
108 $message = http_build_query( $message );
109 }
110 print "\nRequest: $message\n";
111 print "\nExpected:\n";
112 print_r( $exp );
113 print "\nResult:\n";
114 print_r( $result );
115 throw $e; // rethrow it
116 }
117 }
118
119 /**
120 * Recursively compare arrays, ignoring mismatches in numeric key and pageids.
121 * @param $expected array expected values
122 * @param $result array returned values
123 */
124 private function assertResultRecursive( $expected, $result ) {
125 reset( $expected );
126 reset( $result );
127 while ( true ) {
128 $e = each( $expected );
129 $r = each( $result );
130 // If either of the arrays is shorter, abort. If both are done, success.
131 $this->assertEquals( (bool)$e, (bool)$r );
132 if ( !$e ) {
133 break; // done
134 }
135 // continue only if keys are identical or both keys are numeric
136 $this->assertTrue( $e['key'] === $r['key'] || ( is_numeric( $e['key'] ) && is_numeric( $r['key'] ) ) );
137 // don't compare pageids
138 if ( $e['key'] !== 'pageid' ) {
139 // If values are arrays, compare recursively, otherwise compare with ===
140 if ( is_array( $e['value'] ) && is_array( $r['value'] ) ) {
141 $this->assertResultRecursive( $e['value'], $r['value'] );
142 } else {
143 $this->assertEquals( $e['value'], $r['value'] );
144 }
145 }
146 }
147 }
148 }