Merge "Fix SearchEngineTest test descriptions"
[lhc/web/wiklou.git] / tests / phpunit / includes / utils / BatchRowUpdateTest.php
1 <?php
2
3 /**
4 * Tests for BatchRowUpdate and its components
5 *
6 * @group db
7 */
8 class BatchRowUpdateTest extends MediaWikiTestCase {
9
10 public function testWriterBasicFunctionality() {
11 $db = $this->mockDb();
12 $writer = new BatchRowWriter( $db, 'echo_event' );
13
14 $updates = [
15 self::mockUpdate( [ 'something' => 'changed' ] ),
16 self::mockUpdate( [ 'otherthing' => 'changed' ] ),
17 self::mockUpdate( [ 'and' => 'something', 'else' => 'changed' ] ),
18 ];
19
20 $db->expects( $this->exactly( count( $updates ) ) )
21 ->method( 'update' );
22
23 $writer->write( $updates );
24 }
25
26 protected static function mockUpdate( array $changes ) {
27 static $i = 0;
28 return [
29 'primaryKey' => [ 'event_id' => $i++ ],
30 'changes' => $changes,
31 ];
32 }
33
34 public function testReaderBasicIterate() {
35 $db = $this->mockDb();
36 $batchSize = 2;
37 $reader = new BatchRowIterator( $db, 'some_table', 'id_field', $batchSize );
38
39 $response = $this->genSelectResult( $batchSize, /*numRows*/ 5, function () {
40 static $i = 0;
41 return [ 'id_field' => ++$i ];
42 } );
43 $db->expects( $this->exactly( count( $response ) ) )
44 ->method( 'select' )
45 ->will( $this->consecutivelyReturnFromSelect( $response ) );
46
47 $pos = 0;
48 foreach ( $reader as $rows ) {
49 $this->assertEquals( $response[$pos], $rows, "Testing row in position $pos" );
50 $pos++;
51 }
52 // -1 is because the final array() marks the end and isnt included
53 $this->assertEquals( count( $response ) - 1, $pos );
54 }
55
56 public static function provider_readerGetPrimaryKey() {
57 $row = [
58 'id_field' => 42,
59 'some_col' => 'dvorak',
60 'other_col' => 'samurai',
61 ];
62 return [
63
64 [
65 'Must return single column pk when requested',
66 [ 'id_field' => 42 ],
67 $row
68 ],
69
70 [
71 'Must return multiple column pks when requested',
72 [ 'id_field' => 42, 'other_col' => 'samurai' ],
73 $row
74 ],
75
76 ];
77 }
78
79 /**
80 * @dataProvider provider_readerGetPrimaryKey
81 */
82 public function testReaderGetPrimaryKey( $message, array $expected, array $row ) {
83 $reader = new BatchRowIterator( $this->mockDb(), 'some_table', array_keys( $expected ), 8675309 );
84 $this->assertEquals( $expected, $reader->extractPrimaryKeys( (object)$row ), $message );
85 }
86
87 public static function provider_readerSetFetchColumns() {
88 return [
89
90 [
91 'Must merge primary keys into select conditions',
92 // Expected column select
93 [ 'foo', 'bar' ],
94 // primary keys
95 [ 'foo' ],
96 // setFetchColumn
97 [ 'bar' ]
98 ],
99
100 [
101 'Must not merge primary keys into the all columns selector',
102 // Expected column select
103 [ '*' ],
104 // primary keys
105 [ 'foo' ],
106 // setFetchColumn
107 [ '*' ],
108 ],
109
110 [
111 'Must not duplicate primary keys into column selector',
112 // Expected column select.
113 // TODO: figure out how to only assert the array_values portion and not the keys
114 [ 0 => 'foo', 1 => 'bar', 3 => 'baz' ],
115 // primary keys
116 [ 'foo', 'bar', ],
117 // setFetchColumn
118 [ 'bar', 'baz' ],
119 ],
120 ];
121 }
122
123 /**
124 * @dataProvider provider_readerSetFetchColumns
125 */
126 public function testReaderSetFetchColumns(
127 $message, array $columns, array $primaryKeys, array $fetchColumns
128 ) {
129 $db = $this->mockDb();
130 $db->expects( $this->once() )
131 ->method( 'select' )
132 // only testing second parameter of Database::select
133 ->with( 'some_table', $columns )
134 ->will( $this->returnValue( new ArrayIterator( [] ) ) );
135
136 $reader = new BatchRowIterator( $db, 'some_table', $primaryKeys, 22 );
137 $reader->setFetchColumns( $fetchColumns );
138 // triggers first database select
139 $reader->rewind();
140 }
141
142 public static function provider_readerSelectConditions() {
143 return [
144
145 [
146 "With single primary key must generate id > 'value'",
147 // Expected second iteration
148 [ "( id_field > '3' )" ],
149 // Primary key(s)
150 'id_field',
151 ],
152
153 [
154 'With multiple primary keys the first conditions ' .
155 'must use >= and the final condition must use >',
156 // Expected second iteration
157 [ "( id_field = '3' AND foo > '103' ) OR ( id_field > '3' )" ],
158 // Primary key(s)
159 [ 'id_field', 'foo' ],
160 ],
161
162 ];
163 }
164
165 /**
166 * Slightly hackish to use reflection, but asserting different parameters
167 * to consecutive calls of Database::select in phpunit is error prone
168 *
169 * @dataProvider provider_readerSelectConditions
170 */
171 public function testReaderSelectConditionsMultiplePrimaryKeys(
172 $message, $expectedSecondIteration, $primaryKeys, $batchSize = 3
173 ) {
174 $results = $this->genSelectResult( $batchSize, $batchSize * 3, function () {
175 static $i = 0, $j = 100, $k = 1000;
176 return [ 'id_field' => ++$i, 'foo' => ++$j, 'bar' => ++$k ];
177 } );
178 $db = $this->mockDbConsecutiveSelect( $results );
179
180 $conditions = [ 'bar' => 42, 'baz' => 'hai' ];
181 $reader = new BatchRowIterator( $db, 'some_table', $primaryKeys, $batchSize );
182 $reader->addConditions( $conditions );
183
184 $buildConditions = new ReflectionMethod( $reader, 'buildConditions' );
185 $buildConditions->setAccessible( true );
186
187 // On first iteration only the passed conditions must be used
188 $this->assertEquals( $conditions, $buildConditions->invoke( $reader ),
189 'First iteration must return only the conditions passed in addConditions' );
190 $reader->rewind();
191
192 // Second iteration must use the maximum primary key of last set
193 $this->assertEquals(
194 $conditions + $expectedSecondIteration,
195 $buildConditions->invoke( $reader ),
196 $message
197 );
198 }
199
200 protected function mockDbConsecutiveSelect( array $retvals ) {
201 $db = $this->mockDb();
202 $db->expects( $this->any() )
203 ->method( 'select' )
204 ->will( $this->consecutivelyReturnFromSelect( $retvals ) );
205 $db->expects( $this->any() )
206 ->method( 'addQuotes' )
207 ->will( $this->returnCallback( function ( $value ) {
208 return "'$value'"; // not real quoting: doesn't matter in test
209 } ) );
210
211 return $db;
212 }
213
214 protected function consecutivelyReturnFromSelect( array $results ) {
215 $retvals = [];
216 foreach ( $results as $rows ) {
217 // The Database::select method returns iterators, so we do too.
218 $retvals[] = $this->returnValue( new ArrayIterator( $rows ) );
219 }
220
221 return call_user_func_array( [ $this, 'onConsecutiveCalls' ], $retvals );
222 }
223
224 protected function genSelectResult( $batchSize, $numRows, $rowGenerator ) {
225 $res = [];
226 for ( $i = 0; $i < $numRows; $i += $batchSize ) {
227 $rows = [];
228 for ( $j = 0; $j < $batchSize && $i + $j < $numRows; $j++ ) {
229 $rows [] = (object)call_user_func( $rowGenerator );
230 }
231 $res[] = $rows;
232 }
233 $res[] = []; // termination condition requires empty result for last row
234 return $res;
235 }
236
237 protected function mockDb() {
238 // @TODO: mock from Database
239 // FIXME: the constructor normally sets mAtomicLevels and mSrvCache
240 $databaseMysql = $this->getMockBuilder( 'DatabaseMysqli' )
241 ->disableOriginalConstructor()
242 ->getMock();
243 $databaseMysql->expects( $this->any() )
244 ->method( 'isOpen' )
245 ->will( $this->returnValue( true ) );
246 $databaseMysql->expects( $this->any() )
247 ->method( 'getApproximateLagStatus' )
248 ->will( $this->returnValue( [ 'lag' => 0, 'since' => 0 ] ) );
249 return $databaseMysql;
250 }
251 }