MediaWiki  REL1_31
BatchRowUpdateTest.php
Go to the documentation of this file.
1 <?php
2 
13 
14  public function testWriterBasicFunctionality() {
15  $db = $this->mockDb( [ 'update' ] );
16  $writer = new BatchRowWriter( $db, 'echo_event' );
17 
18  $updates = [
19  self::mockUpdate( [ 'something' => 'changed' ] ),
20  self::mockUpdate( [ 'otherthing' => 'changed' ] ),
21  self::mockUpdate( [ 'and' => 'something', 'else' => 'changed' ] ),
22  ];
23 
24  $db->expects( $this->exactly( count( $updates ) ) )
25  ->method( 'update' );
26 
27  $writer->write( $updates );
28  }
29 
30  protected static function mockUpdate( array $changes ) {
31  static $i = 0;
32  return [
33  'primaryKey' => [ 'event_id' => $i++ ],
34  'changes' => $changes,
35  ];
36  }
37 
38  public function testReaderBasicIterate() {
39  $batchSize = 2;
40  $response = $this->genSelectResult( $batchSize, /*numRows*/ 5, function () {
41  static $i = 0;
42  return [ 'id_field' => ++$i ];
43  } );
45  $reader = new BatchRowIterator( $db, 'some_table', 'id_field', $batchSize );
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 
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 
126  public function testReaderSetFetchColumns(
127  $message, array $columns, array $primaryKeys, array $fetchColumns
128  ) {
129  $db = $this->mockDb( [ 'select' ] );
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 
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( [ 'select', 'addQuotes' ] );
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( $methods = [] ) {
238  // @TODO: mock from Database
239  // FIXME: the constructor normally sets mAtomicLevels and mSrvCache
240  $databaseMysql = $this->getMockBuilder( Wikimedia\Rdbms\DatabaseMysqli::class )
241  ->disableOriginalConstructor()
242  ->setMethods( array_merge( [ 'isOpen', 'getApproximateLagStatus' ], $methods ) )
243  ->getMock();
244  $databaseMysql->expects( $this->any() )
245  ->method( 'isOpen' )
246  ->will( $this->returnValue( true ) );
247  $databaseMysql->expects( $this->any() )
248  ->method( 'getApproximateLagStatus' )
249  ->will( $this->returnValue( [ 'lag' => 0, 'since' => 0 ] ) );
250  return $databaseMysql;
251  }
252 }
BatchRowUpdateTest\provider_readerGetPrimaryKey
static provider_readerGetPrimaryKey()
Definition: BatchRowUpdateTest.php:56
BatchRowUpdateTest\testReaderBasicIterate
testReaderBasicIterate()
Definition: BatchRowUpdateTest.php:38
object
globals will be eliminated from MediaWiki replaced by an application object which would be passed to constructors Whether that would be an convenient solution remains to be but certainly PHP makes such object oriented programming models easier than they were in previous versions For the time being MediaWiki programmers will have to work in an environment with some global context At the time of globals were initialised on startup by MediaWiki of these were configuration which are documented in DefaultSettings php There is no comprehensive documentation for the remaining however some of the most important ones are listed below They are typically initialised either in index php or in Setup php For a description of the see design txt $wgTitle Title object created from the request URL $wgOut OutputPage object for HTTP response $wgUser User object for the user associated with the current request $wgLang Language object selected by user preferences $wgContLang Language object associated with the wiki being viewed $wgParser Parser object Parser extensions register their hooks here $wgRequest WebRequest object
Definition: globals.txt:64
BatchRowUpdateTest\mockUpdate
static mockUpdate(array $changes)
Definition: BatchRowUpdateTest.php:30
$rows
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction $rows
Definition: hooks.txt:2783
$response
this hook is for auditing only $response
Definition: hooks.txt:783
array
the array() calling protocol came about after MediaWiki 1.4rc1.
BatchRowUpdateTest\mockDb
mockDb( $methods=[])
Definition: BatchRowUpdateTest.php:237
BatchRowUpdateTest
Tests for BatchRowUpdate and its components.
Definition: BatchRowUpdateTest.php:12
BatchRowIterator
Definition: BatchRowIterator.php:29
BatchRowUpdateTest\testReaderGetPrimaryKey
testReaderGetPrimaryKey( $message, array $expected, array $row)
provider_readerGetPrimaryKey
Definition: BatchRowUpdateTest.php:82
BatchRowUpdateTest\consecutivelyReturnFromSelect
consecutivelyReturnFromSelect(array $results)
Definition: BatchRowUpdateTest.php:214
$res
$res
Definition: database.txt:21
BatchRowUpdateTest\testReaderSetFetchColumns
testReaderSetFetchColumns( $message, array $columns, array $primaryKeys, array $fetchColumns)
provider_readerSetFetchColumns
Definition: BatchRowUpdateTest.php:126
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:37
BatchRowUpdateTest\provider_readerSelectConditions
static provider_readerSelectConditions()
Definition: BatchRowUpdateTest.php:142
MediaWikiTestCase
Definition: MediaWikiTestCase.php:17
BatchRowUpdateTest\testWriterBasicFunctionality
testWriterBasicFunctionality()
Definition: BatchRowUpdateTest.php:14
BatchRowUpdateTest\testReaderSelectConditionsMultiplePrimaryKeys
testReaderSelectConditionsMultiplePrimaryKeys( $message, $expectedSecondIteration, $primaryKeys, $batchSize=3)
Slightly hackish to use reflection, but asserting different parameters to consecutive calls of Databa...
Definition: BatchRowUpdateTest.php:171
$value
$value
Definition: styleTest.css.php:45
BatchRowWriter
Definition: BatchRowWriter.php:27
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:22
Wikimedia
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:56
BatchRowUpdateTest\mockDbConsecutiveSelect
mockDbConsecutiveSelect(array $retvals)
Definition: BatchRowUpdateTest.php:200
BatchRowUpdateTest\provider_readerSetFetchColumns
static provider_readerSetFetchColumns()
Definition: BatchRowUpdateTest.php:87
BatchRowUpdateTest\genSelectResult
genSelectResult( $batchSize, $numRows, $rowGenerator)
Definition: BatchRowUpdateTest.php:224
MediaWikiTestCase\$db
Database $db
Primary database.
Definition: MediaWikiTestCase.php:57