Make BatchRowUpdateTest compatible with PHPUnit 6
[lhc/web/wiklou.git] / tests / phpunit / includes / utils / BatchRowUpdateTest.php
index 6506d58..52b1433 100644 (file)
@@ -4,11 +4,15 @@
  * Tests for BatchRowUpdate and its components
  *
  * @group db
+ *
+ * @covers BatchRowUpdate
+ * @covers BatchRowIterator
+ * @covers BatchRowWriter
  */
 class BatchRowUpdateTest extends MediaWikiTestCase {
 
        public function testWriterBasicFunctionality() {
-               $db = $this->mockDb();
+               $db = $this->mockDb( [ 'update' ] );
                $writer = new BatchRowWriter( $db, 'echo_event' );
 
                $updates = [
@@ -32,17 +36,13 @@ class BatchRowUpdateTest extends MediaWikiTestCase {
        }
 
        public function testReaderBasicIterate() {
-               $db = $this->mockDb();
                $batchSize = 2;
-               $reader = new BatchRowIterator( $db, 'some_table', 'id_field', $batchSize );
-
-               $response = $this->genSelectResult( $batchSize, /*numRows*/ 5, function() {
+               $response = $this->genSelectResult( $batchSize, /*numRows*/ 5, function () {
                        static $i = 0;
                        return [ 'id_field' => ++$i ];
                } );
-               $db->expects( $this->exactly( count( $response ) ) )
-                       ->method( 'select' )
-                       ->will( $this->consecutivelyReturnFromSelect( $response ) );
+               $db = $this->mockDbConsecutiveSelect( $response );
+               $reader = new BatchRowIterator( $db, 'some_table', 'id_field', $batchSize );
 
                $pos = 0;
                foreach ( $reader as $rows ) {
@@ -126,7 +126,7 @@ class BatchRowUpdateTest extends MediaWikiTestCase {
        public function testReaderSetFetchColumns(
                $message, array $columns, array $primaryKeys, array $fetchColumns
        ) {
-               $db = $this->mockDb();
+               $db = $this->mockDb( [ 'select' ] );
                $db->expects( $this->once() )
                        ->method( 'select' )
                        // only testing second parameter of Database::select
@@ -171,7 +171,7 @@ class BatchRowUpdateTest extends MediaWikiTestCase {
        public function testReaderSelectConditionsMultiplePrimaryKeys(
                $message, $expectedSecondIteration, $primaryKeys, $batchSize = 3
        ) {
-               $results = $this->genSelectResult( $batchSize, $batchSize * 3, function() {
+               $results = $this->genSelectResult( $batchSize, $batchSize * 3, function () {
                        static $i = 0, $j = 100, $k = 1000;
                        return [ 'id_field' => ++$i, 'foo' => ++$j, 'bar' => ++$k ];
                } );
@@ -198,13 +198,13 @@ class BatchRowUpdateTest extends MediaWikiTestCase {
        }
 
        protected function mockDbConsecutiveSelect( array $retvals ) {
-               $db = $this->mockDb();
+               $db = $this->mockDb( [ 'select', 'addQuotes' ] );
                $db->expects( $this->any() )
                        ->method( 'select' )
                        ->will( $this->consecutivelyReturnFromSelect( $retvals ) );
                $db->expects( $this->any() )
                        ->method( 'addQuotes' )
-                       ->will( $this->returnCallback( function( $value ) {
+                       ->will( $this->returnCallback( function ( $value ) {
                                return "'$value'"; // not real quoting: doesn't matter in test
                        } ) );
 
@@ -234,11 +234,12 @@ class BatchRowUpdateTest extends MediaWikiTestCase {
                return $res;
        }
 
-       protected function mockDb() {
+       protected function mockDb( $methods = [] ) {
                // @TODO: mock from Database
                // FIXME: the constructor normally sets mAtomicLevels and mSrvCache
-               $databaseMysql = $this->getMockBuilder( 'DatabaseMysqli' )
+               $databaseMysql = $this->getMockBuilder( Wikimedia\Rdbms\DatabaseMysqli::class )
                        ->disableOriginalConstructor()
+                       ->setMethods( array_merge( [ 'isOpen', 'getApproximateLagStatus' ], $methods ) )
                        ->getMock();
                $databaseMysql->expects( $this->any() )
                        ->method( 'isOpen' )