phpunit: Simplify mock object syntax in includes/db/ tests
[lhc/web/wiklou.git] / tests / phpunit / includes / db / LBFactoryTest.php
index a647445..862ec42 100644 (file)
@@ -33,14 +33,14 @@ class LBFactoryTest extends MediaWikiTestCase {
                        ->disableOriginalConstructor()
                        ->getMock();
 
-               $config = array(
+               $config = [
                        'class'          => $deprecated,
                        'connection'     => $mockDB,
                        # Various other parameters required:
-                       'sectionsByDB'   => array(),
-                       'sectionLoads'   => array(),
-                       'serverTemplate' => array(),
-               );
+                       'sectionsByDB'   => [],
+                       'sectionLoads'   => [],
+                       'serverTemplate' => [],
+               ];
 
                $this->hideDeprecated( '$wgLBFactoryConf must be updated. See RELEASE-NOTES for details' );
                $result = LBFactory::getLBFactoryClass( $config );
@@ -49,19 +49,19 @@ class LBFactoryTest extends MediaWikiTestCase {
        }
 
        public function getLBFactoryClassProvider() {
-               return array(
+               return [
                        # Format: new class, old class
-                       array( 'LBFactorySimple', 'LBFactory_Simple' ),
-                       array( 'LBFactorySingle', 'LBFactory_Single' ),
-                       array( 'LBFactoryMulti', 'LBFactory_Multi' ),
-                       array( 'LBFactoryFake', 'LBFactory_Fake' ),
-               );
+                       [ 'LBFactorySimple', 'LBFactory_Simple' ],
+                       [ 'LBFactorySingle', 'LBFactory_Single' ],
+                       [ 'LBFactoryMulti', 'LBFactory_Multi' ],
+                       [ 'LBFactoryFake', 'LBFactory_Fake' ],
+               ];
        }
 
        public function testLBFactorySimpleServer() {
                $this->setMwGlobals( 'wgDBservers', false );
 
-               $factory = new LBFactorySimple( array() );
+               $factory = new LBFactorySimple( [] );
                $lb = $factory->getMainLB();
 
                $dbw = $lb->getConnection( DB_MASTER );
@@ -77,8 +77,8 @@ class LBFactoryTest extends MediaWikiTestCase {
        public function testLBFactorySimpleServers() {
                global $wgDBserver, $wgDBname, $wgDBuser, $wgDBpassword, $wgDBtype;
 
-               $this->setMwGlobals( 'wgDBservers', array(
-                       array( // master
+               $this->setMwGlobals( 'wgDBservers', [
+                       [ // master
                                'host'          => $wgDBserver,
                                'dbname'    => $wgDBname,
                                'user'          => $wgDBuser,
@@ -86,8 +86,8 @@ class LBFactoryTest extends MediaWikiTestCase {
                                'type'          => $wgDBtype,
                                'load'      => 0,
                                'flags'     => DBO_TRX // REPEATABLE-READ for consistency
-                       ),
-                       array( // emulated slave
+                       ],
+                       [ // emulated slave
                                'host'          => $wgDBserver,
                                'dbname'    => $wgDBname,
                                'user'          => $wgDBuser,
@@ -95,10 +95,10 @@ class LBFactoryTest extends MediaWikiTestCase {
                                'type'          => $wgDBtype,
                                'load'      => 100,
                                'flags'     => DBO_TRX // REPEATABLE-READ for consistency
-                       )
-               ) );
+                       ]
+               ] );
 
-               $factory = new LBFactorySimple( array( 'loadMonitorClass' => 'LoadMonitorNull' ) );
+               $factory = new LBFactorySimple( [ 'loadMonitorClass' => 'LoadMonitorNull' ] );
                $lb = $factory->getMainLB();
 
                $dbw = $lb->getConnection( DB_MASTER );
@@ -107,7 +107,7 @@ class LBFactoryTest extends MediaWikiTestCase {
                        $wgDBserver, $dbw->getLBInfo( 'clusterMasterHost' ), 'cluster master set' );
 
                $dbr = $lb->getConnection( DB_SLAVE );
-               $this->assertTrue( $dbr->getLBInfo( 'slave' ), 'slave shows as slave' );
+               $this->assertTrue( $dbr->getLBInfo( 'replica' ), 'slave shows as slave' );
                $this->assertEquals(
                        $wgDBserver, $dbr->getLBInfo( 'clusterMasterHost' ), 'cluster master set' );
 
@@ -118,34 +118,34 @@ class LBFactoryTest extends MediaWikiTestCase {
        public function testLBFactoryMulti() {
                global $wgDBserver, $wgDBname, $wgDBuser, $wgDBpassword, $wgDBtype;
 
-               $factory = new LBFactoryMulti( array(
-                       'sectionsByDB' => array(),
-                       'sectionLoads' => array(
-                               'DEFAULT' => array(
+               $factory = new LBFactoryMulti( [
+                       'sectionsByDB' => [],
+                       'sectionLoads' => [
+                               'DEFAULT' => [
                                        'test-db1' => 0,
                                        'test-db2' => 100,
-                               ),
-                       ),
-                       'serverTemplate' => array(
+                               ],
+                       ],
+                       'serverTemplate' => [
                                'dbname'          => $wgDBname,
                                'user'            => $wgDBuser,
                                'password'        => $wgDBpassword,
                                'type'            => $wgDBtype,
                                'flags'           => DBO_DEFAULT
-                       ),
-                       'hostsByName' => array(
+                       ],
+                       'hostsByName' => [
                                'test-db1'  => $wgDBserver,
                                'test-db2'  => $wgDBserver
-                       ),
+                       ],
                        'loadMonitorClass' => 'LoadMonitorNull'
-               ) );
+               ] );
                $lb = $factory->getMainLB();
 
                $dbw = $lb->getConnection( DB_MASTER );
                $this->assertTrue( $dbw->getLBInfo( 'master' ), 'master shows as master' );
 
                $dbr = $lb->getConnection( DB_SLAVE );
-               $this->assertTrue( $dbr->getLBInfo( 'slave' ), 'slave shows as slave' );
+               $this->assertTrue( $dbr->getLBInfo( 'replica' ), 'slave shows as slave' );
 
                $factory->shutdown();
                $lb->closeAll();
@@ -155,36 +155,43 @@ class LBFactoryTest extends MediaWikiTestCase {
                // (a) First HTTP request
                $mPos = new MySQLMasterPos( 'db1034-bin.000976', '843431247' );
 
+               $now = microtime( true );
                $mockDB = $this->getMockBuilder( 'DatabaseMysql' )
                        ->disableOriginalConstructor()
                        ->getMock();
-               $mockDB->expects( $this->any() )
-                       ->method( 'doneWrites' )->will( $this->returnValue( true ) );
-               $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( array( 'id' => "main-DEFAULT" ) ) );
-               $lb->expects( $this->any() )
-                       ->method( 'getAnyOpenConnection' )->will( $this->returnValue( $mockDB ) );
+               $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' ] );
+                                       $p |= call_user_func( [ $mockDB, 'lastDoneWrites' ] );
+
+                                       return (bool)$p;
+                               }
+                       ) );
+               $lb->method( 'getMasterPos' )->willReturn( $mPos );
 
                $bag = new HashBagOStuff();
                $cp = new ChronologyProtector(
                        $bag,
-                       array(
+                       [
                                'ip' => '127.0.0.1',
                                'agent' => "Totally-Not-FireFox"
-                       )
+                       ]
                );
 
-               $mockDB->expects( $this->exactly( 2 ) )->method( 'doneWrites' );
+               $mockDB->expects( $this->exactly( 2 ) )->method( 'writesOrCallbacksPending' );
+               $mockDB->expects( $this->exactly( 2 ) )->method( 'lastDoneWrites' );
 
                // Nothing to wait for
                $cp->initLB( $lb );
@@ -195,10 +202,10 @@ class LBFactoryTest extends MediaWikiTestCase {
                // (b) Second HTTP request
                $cp = new ChronologyProtector(
                        $bag,
-                       array(
+                       [
                                'ip' => '127.0.0.1',
                                'agent' => "Totally-Not-FireFox"
-                       )
+                       ]
                );
 
                $lb->expects( $this->once() )