Added release notes for 'ContentHandler::runLegacyHooks' removal
[mediawiki.git] / tests / phpunit / includes / utils / BatchRowUpdateTest.php
blob6506d58e21df0896830bed3d7e62f93fb458358e
1 <?php
3 /**
4 * Tests for BatchRowUpdate and its components
6 * @group db
7 */
8 class BatchRowUpdateTest extends MediaWikiTestCase {
10 public function testWriterBasicFunctionality() {
11 $db = $this->mockDb();
12 $writer = new BatchRowWriter( $db, 'echo_event' );
14 $updates = [
15 self::mockUpdate( [ 'something' => 'changed' ] ),
16 self::mockUpdate( [ 'otherthing' => 'changed' ] ),
17 self::mockUpdate( [ 'and' => 'something', 'else' => 'changed' ] ),
20 $db->expects( $this->exactly( count( $updates ) ) )
21 ->method( 'update' );
23 $writer->write( $updates );
26 protected static function mockUpdate( array $changes ) {
27 static $i = 0;
28 return [
29 'primaryKey' => [ 'event_id' => $i++ ],
30 'changes' => $changes,
34 public function testReaderBasicIterate() {
35 $db = $this->mockDb();
36 $batchSize = 2;
37 $reader = new BatchRowIterator( $db, 'some_table', 'id_field', $batchSize );
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 ) );
47 $pos = 0;
48 foreach ( $reader as $rows ) {
49 $this->assertEquals( $response[$pos], $rows, "Testing row in position $pos" );
50 $pos++;
52 // -1 is because the final array() marks the end and isnt included
53 $this->assertEquals( count( $response ) - 1, $pos );
56 public static function provider_readerGetPrimaryKey() {
57 $row = [
58 'id_field' => 42,
59 'some_col' => 'dvorak',
60 'other_col' => 'samurai',
62 return [
65 'Must return single column pk when requested',
66 [ 'id_field' => 42 ],
67 $row
71 'Must return multiple column pks when requested',
72 [ 'id_field' => 42, 'other_col' => 'samurai' ],
73 $row
79 /**
80 * @dataProvider provider_readerGetPrimaryKey
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 );
87 public static function provider_readerSetFetchColumns() {
88 return [
91 'Must merge primary keys into select conditions',
92 // Expected column select
93 [ 'foo', 'bar' ],
94 // primary keys
95 [ 'foo' ],
96 // setFetchColumn
97 [ 'bar' ]
101 'Must not merge primary keys into the all columns selector',
102 // Expected column select
103 [ '*' ],
104 // primary keys
105 [ 'foo' ],
106 // setFetchColumn
107 [ '*' ],
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' ],
124 * @dataProvider provider_readerSetFetchColumns
126 public function testReaderSetFetchColumns(
127 $message, array $columns, array $primaryKeys, array $fetchColumns
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( [] ) ) );
136 $reader = new BatchRowIterator( $db, 'some_table', $primaryKeys, 22 );
137 $reader->setFetchColumns( $fetchColumns );
138 // triggers first database select
139 $reader->rewind();
142 public static function provider_readerSelectConditions() {
143 return [
146 "With single primary key must generate id > 'value'",
147 // Expected second iteration
148 [ "( id_field > '3' )" ],
149 // Primary key(s)
150 'id_field',
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' ],
166 * Slightly hackish to use reflection, but asserting different parameters
167 * to consecutive calls of Database::select in phpunit is error prone
169 * @dataProvider provider_readerSelectConditions
171 public function testReaderSelectConditionsMultiplePrimaryKeys(
172 $message, $expectedSecondIteration, $primaryKeys, $batchSize = 3
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 );
180 $conditions = [ 'bar' => 42, 'baz' => 'hai' ];
181 $reader = new BatchRowIterator( $db, 'some_table', $primaryKeys, $batchSize );
182 $reader->addConditions( $conditions );
184 $buildConditions = new ReflectionMethod( $reader, 'buildConditions' );
185 $buildConditions->setAccessible( true );
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();
192 // Second iteration must use the maximum primary key of last set
193 $this->assertEquals(
194 $conditions + $expectedSecondIteration,
195 $buildConditions->invoke( $reader ),
196 $message
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 } ) );
211 return $db;
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 ) );
221 return call_user_func_array( [ $this, 'onConsecutiveCalls' ], $retvals );
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 );
231 $res[] = $rows;
233 $res[] = []; // termination condition requires empty result for last row
234 return $res;
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;