[MediaWiki-commits] [Gerrit] mediawiki/core[master]: phpunit: Simplify mock object syntax in includes/db/ tests
Krinkle has uploaded a new change for review. https://gerrit.wikimedia.org/r/310686 Change subject: phpunit: Simplify mock object syntax in includes/db/ tests .. phpunit: Simplify mock object syntax in includes/db/ tests * Omit redundant `expects( $this->any() )`. * Use willReturn() instead of `will( $this->returnValue() )`. Change-Id: If90ac651748af8a78a720992240e40ba53cec79c --- M tests/phpunit/includes/db/DatabaseMysqlBaseTest.php M tests/phpunit/includes/db/LBFactoryTest.php 2 files changed, 18 insertions(+), 34 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core refs/changes/86/310686/1 diff --git a/tests/phpunit/includes/db/DatabaseMysqlBaseTest.php b/tests/phpunit/includes/db/DatabaseMysqlBaseTest.php index 607f25c..f13ead4 100644 --- a/tests/phpunit/includes/db/DatabaseMysqlBaseTest.php +++ b/tests/phpunit/includes/db/DatabaseMysqlBaseTest.php @@ -169,15 +169,11 @@ ->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 @@ '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 @@ $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 bf78d13..862ec42 100644 --- a/tests/phpunit/includes/db/LBFactoryTest.php +++ b/tests/phpunit/includes/db/LBFactoryTest.php @@ -159,26 +159,18 @@ $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() ) - ->m
[MediaWiki-commits] [Gerrit] mediawiki/core[master]: phpunit: Simplify mock object syntax in includes/db/ tests
jenkins-bot has submitted this change and it was merged. Change subject: phpunit: Simplify mock object syntax in includes/db/ tests .. phpunit: Simplify mock object syntax in includes/db/ tests * Omit redundant `expects( $this->any() )`. * Use willReturn() instead of `will( $this->returnValue() )`. Change-Id: If90ac651748af8a78a720992240e40ba53cec79c --- M tests/phpunit/includes/db/DatabaseMysqlBaseTest.php M tests/phpunit/includes/db/LBFactoryTest.php 2 files changed, 18 insertions(+), 34 deletions(-) Approvals: Aaron Schulz: Looks good to me, approved jenkins-bot: Verified diff --git a/tests/phpunit/includes/db/DatabaseMysqlBaseTest.php b/tests/phpunit/includes/db/DatabaseMysqlBaseTest.php index 607f25c..f13ead4 100644 --- a/tests/phpunit/includes/db/DatabaseMysqlBaseTest.php +++ b/tests/phpunit/includes/db/DatabaseMysqlBaseTest.php @@ -169,15 +169,11 @@ ->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 @@ '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 @@ $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 bf78d13..862ec42 100644 --- a/tests/phpunit/includes/db/LBFactoryTest.php +++ b/tests/phpunit/includes/db/LBFactoryTest.php @@ -159,26 +159,18 @@ $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' )->wi