phpunit: Simplify mock object syntax in includes/db/ tests
authorTimo Tijhof <krinklemail@gmail.com>
Wed, 14 Sep 2016 22:51:05 +0000 (15:51 -0700)
committerKrinkle <krinklemail@gmail.com>
Thu, 15 Sep 2016 01:47:18 +0000 (01:47 +0000)
* Omit redundant `expects( $this->any() )`.
* Use willReturn() instead of `will( $this->returnValue() )`.

Change-Id: If90ac651748af8a78a720992240e40ba53cec79c

tests/phpunit/includes/db/DatabaseMysqlBaseTest.php
tests/phpunit/includes/db/LBFactoryTest.php

index 607f25c..f13ead4 100644 (file)
@@ -169,15 +169,11 @@ class DatabaseMysqlBaseTest extends MediaWikiTestCase {
                        ->setMethods( [ 'fetchRow', 'query' ] )
                        ->getMock();
 
-               $db->expects( $this->any() )
-                       ->method( 'query' )
+               $db->method( 'query' )
                        ->with( $this->anything() )
-                       ->will(
-                               $this->returnValue( null )
-                       );
+                       ->willReturn( null );
 
-               $db->expects( $this->any() )
-                       ->method( 'fetchRow' )
+               $db->method( 'fetchRow' )
                        ->with( $this->anything() )
                        ->will( $this->onConsecutiveCalls(
                                [ 'Tables_in_' => 'view1' ],
@@ -361,13 +357,11 @@ class DatabaseMysqlBaseTest extends MediaWikiTestCase {
                                'getLagDetectionMethod', 'getHeartbeatData', 'getMasterServerInfo' ] )
                        ->getMock();
 
-               $db->expects( $this->any() )
-                       ->method( 'getLagDetectionMethod' )
-                       ->will( $this->returnValue( 'pt-heartbeat' ) );
+               $db->method( 'getLagDetectionMethod' )
+                       ->willReturn( 'pt-heartbeat' );
 
-               $db->expects( $this->any() )
-                       ->method( 'getMasterServerInfo' )
-                       ->will( $this->returnValue( [ 'serverId' => 172, 'asOf' => time() ] ) );
+               $db->method( 'getMasterServerInfo' )
+                       ->willReturn( [ 'serverId' => 172, 'asOf' => time() ] );
 
                // Fake the current time.
                list( $nowSecFrac, $nowSec ) = explode( ' ', microtime() );
@@ -381,10 +375,9 @@ class DatabaseMysqlBaseTest extends MediaWikiTestCase {
                $ptTimeISO = $ptDateTime->format( 'Y-m-d\TH:i:s' );
                $ptTimeISO .= ltrim( number_format( $ptSecFrac, 6 ), '0' );
 
-               $db->expects( $this->any() )
-                       ->method( 'getHeartbeatData' )
+               $db->method( 'getHeartbeatData' )
                        ->with( [ 'server_id' => 172 ] )
-                       ->will( $this->returnValue( [ $ptTimeISO, $now ] ) );
+                       ->willReturn( [ $ptTimeISO, $now ] );
 
                $db->setLBInfo( 'clusterMasterHost', 'db1052' );
                $lagEst = $db->getLag();
index bf78d13..862ec42 100644 (file)
@@ -159,26 +159,18 @@ class LBFactoryTest extends MediaWikiTestCase {
                $mockDB = $this->getMockBuilder( 'DatabaseMysql' )
                        ->disableOriginalConstructor()
                        ->getMock();
-               $mockDB->expects( $this->any() )
-                       ->method( 'writesOrCallbacksPending' )->will( $this->returnValue( true ) );
-               $mockDB->expects( $this->any() )
-                       ->method( 'lastDoneWrites' )->will( $this->returnValue( $now ) );
-               $mockDB->expects( $this->any() )
-                       ->method( 'getMasterPos' )->will( $this->returnValue( $mPos ) );
+               $mockDB->method( 'writesOrCallbacksPending' )->willReturn( true );
+               $mockDB->method( 'lastDoneWrites' )->willReturn( $now );
+               $mockDB->method( 'getMasterPos' )->willReturn( $mPos );
 
                $lb = $this->getMockBuilder( 'LoadBalancer' )
                        ->disableOriginalConstructor()
                        ->getMock();
-               $lb->expects( $this->any() )
-                       ->method( 'getConnection' )->will( $this->returnValue( $mockDB ) );
-               $lb->expects( $this->any() )
-                       ->method( 'getServerCount' )->will( $this->returnValue( 2 ) );
-               $lb->expects( $this->any() )
-                       ->method( 'parentInfo' )->will( $this->returnValue( [ 'id' => "main-DEFAULT" ] ) );
-               $lb->expects( $this->any() )
-                       ->method( 'getAnyOpenConnection' )->will( $this->returnValue( $mockDB ) );
-               $lb->expects( $this->any() )
-                       ->method( 'hasOrMadeRecentMasterChanges' )->will( $this->returnCallback(
+               $lb->method( 'getConnection' )->willReturn( $mockDB );
+               $lb->method( 'getServerCount' )->willReturn( 2 );
+               $lb->method( 'parentInfo' )->willReturn( [ 'id' => "main-DEFAULT" ] );
+               $lb->method( 'getAnyOpenConnection' )->willReturn( $mockDB );
+               $lb->method( 'hasOrMadeRecentMasterChanges' )->will( $this->returnCallback(
                                function () use ( $mockDB ) {
                                        $p = 0;
                                        $p |= call_user_func( [ $mockDB, 'writesOrCallbacksPending' ] );
@@ -187,8 +179,7 @@ class LBFactoryTest extends MediaWikiTestCase {
                                        return (bool)$p;
                                }
                        ) );
-               $lb->expects( $this->any() )
-                       ->method( 'getMasterPos' )->will( $this->returnValue( $mPos ) );
+               $lb->method( 'getMasterPos' )->willReturn( $mPos );
 
                $bag = new HashBagOStuff();
                $cp = new ChronologyProtector(