From f6ce15ead374722409f031ab1466d60c2fedff6d Mon Sep 17 00:00:00 2001 From: Timo Tijhof Date: Wed, 14 Sep 2016 15:51:05 -0700 Subject: [PATCH] phpunit: Simplify mock object syntax in includes/db/ tests * Omit redundant `expects( $this->any() )`. * Use willReturn() instead of `will( $this->returnValue() )`. Change-Id: If90ac651748af8a78a720992240e40ba53cec79c --- .../includes/db/DatabaseMysqlBaseTest.php | 25 +++++++---------- tests/phpunit/includes/db/LBFactoryTest.php | 27 +++++++------------ 2 files changed, 18 insertions(+), 34 deletions(-) diff --git a/tests/phpunit/includes/db/DatabaseMysqlBaseTest.php b/tests/phpunit/includes/db/DatabaseMysqlBaseTest.php index 607f25c36a..f13ead4606 100644 --- a/tests/phpunit/includes/db/DatabaseMysqlBaseTest.php +++ b/tests/phpunit/includes/db/DatabaseMysqlBaseTest.php @@ -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(); diff --git a/tests/phpunit/includes/db/LBFactoryTest.php b/tests/phpunit/includes/db/LBFactoryTest.php index bf78d13f26..862ec42c8b 100644 --- a/tests/phpunit/includes/db/LBFactoryTest.php +++ b/tests/phpunit/includes/db/LBFactoryTest.php @@ -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( -- 2.20.1