Hashar has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/349413 )

Change subject: phpunit: factor out logic to handle globals vars
......................................................................

phpunit: factor out logic to handle globals vars

loggingTest data providers loads $wgConf in global scope. With PHPUnit
backing up globals on each test, that slow down the test run.  The class
has logic to restore the globals in the data provider so tests get run
with a clean global scope.

cirrusTest has the same need. Hence factor out global handling code to a
new PHPUnit_FrameWork_TestCase: WgConfTestCase.

Switch loggingTest to it.
Port cirrusTest.

When setting globals with setGlobals(), we want to make sure they are
restored with restoreGlobals().  Thus throw an exception whenever the
test case object is descruted but globals have not been restored, that
will prevent unintentional leaks.

Test runs for cirrusTest goes from 7s to 3s.

Later on WgConfTestCase can be extended to load a $wgConf object. That
seems to be a common behavior of loggingTest and cirrusTest.

Change-Id: I7a86e856881da69431c29b81cf1005070e029a0b
---
A tests/WgConfTestCase.php
M tests/bootstrap.php
M tests/cirrusTest.php
M tests/loggingTest.php
4 files changed, 65 insertions(+), 40 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/operations/mediawiki-config 
refs/changes/13/349413/1

diff --git a/tests/WgConfTestCase.php b/tests/WgConfTestCase.php
new file mode 100644
index 0000000..216fc00
--- /dev/null
+++ b/tests/WgConfTestCase.php
@@ -0,0 +1,50 @@
+<?php
+/**
+ * PHPUnit testcase for $wgConf testing.
+ *
+ * @license GPLv2 or later
+ * @author Erik Bernhardson <[email protected]>
+ * @copyright Copyright © 2015, Erik Bernhardson <[email protected]>
+ * @file
+ */
+
+class WgConfTestCase extends PHPUnit_Framework_TestCase {
+
+       protected $globals = array();
+
+       protected function restoreGlobals() {
+               foreach ( $this->globals as $key => $value ) {
+                       $GLOBALS[$key] = $value;
+               }
+               $this->globals = array();
+       }
+
+       protected function setGlobals( $pairs, $value = null ) {
+               if ( is_string( $pairs ) ) {
+                       $pairs = array( $pairs => $value );
+               }
+               foreach ( $pairs as $key => $value ) {
+                       // only set value in $this->globals on first call
+                       if ( !array_key_exists( $key, $this->globals ) ) {
+                               if ( isset( $GLOBALS[$key] ) ) {
+                                       // break any object references
+                                       try {
+                                               $this->globals[$key] = 
unserialize( serialize( $GLOBALS[$key] ) );
+                                       } catch ( \Exception $e ) {
+                                               $this->globals[$key] = 
$GLOBALS[$key];
+                                       }
+                               } else {
+                                       $this->globals[$key] = null;
+                               }
+                       }
+                       $GLOBALS[$key] = $value;
+               }
+       }
+
+       function __destruct() {
+               if ( ! empty( $this->globals ) ) {
+                       throw new Exception( __CLASS__ . ': setGlobals() used 
without restoreGlobals()' );
+               }
+       }
+
+}
diff --git a/tests/bootstrap.php b/tests/bootstrap.php
index d111b5d..31c9e2e 100644
--- a/tests/bootstrap.php
+++ b/tests/bootstrap.php
@@ -3,3 +3,4 @@
 // Load the shared utilities classes from here!
 require_once __DIR__ . "/DBList.php";
 require_once __DIR__ . "/Defines.php";
+require_once __DIR__ . "/WgConfTestCase.php";
diff --git a/tests/cirrusTest.php b/tests/cirrusTest.php
index eec305a..686f3c7 100644
--- a/tests/cirrusTest.php
+++ b/tests/cirrusTest.php
@@ -3,7 +3,7 @@
 require_once __DIR__ . '/../multiversion/MWMultiVersion.php';
 require_once __DIR__ . '/SiteConfiguration.php';
 
-class cirrusTests extends PHPUnit_Framework_TestCase {
+class cirrusTests extends WgConfTestCase {
        public function testClusterConfigurationForProdTestwiki() {
                $wmfDatacenter = 'unittest';
                $config = $this->loadCirrusConfig( 'production', 'testwiki', 
'wiki' );
@@ -83,12 +83,14 @@
                require "{$wmfConfigDir}/wgConf.php";
 
                // InitialiseSettings.php explicitly declares these as global, 
so we must too
-               $GLOBALS['wmfUdp2logDest'] = 'localhost';
-               $GLOBALS['wmfDatacenter'] = 'unittest';
-               $GLOBALS['wmfMasterDatacenter'] = 'unittest';
-               $GLOBALS['wmfRealm'] = $wmfRealm;
-               $GLOBALS['wmfConfigDir'] = $wmfConfigDir;
-               $GLOBALS['wgConf'] = $wgConf;
+               $this->setGlobals( array(
+                       'wmfUdp2logDest' => 'localhost',
+                       'wmfDatacenter' => 'unittest',
+                       'wmfMasterDatacenter' => 'unittest',
+                       'wmfRealm' => $wmfRealm,
+                       'wmfConfigDir' => $wmfConfigDir,
+                       'wgConf' => $wgConf,
+               ) );
 
                require __DIR__ . '/TestServices.php';
                require "{$wmfConfigDir}/InitialiseSettings.php";
@@ -149,7 +151,9 @@
 
                require "{$wmfConfigDir}/CirrusSearch-common.php";
 
-               return compact( array_keys( get_defined_vars() ) );
+               $ret = compact( array_keys( get_defined_vars() ) );
+               $this->restoreGlobals();
+               return $ret;
        }
 
        private static function resolveConfig( $config, $key ) {
@@ -227,6 +231,7 @@
                        }
                }
 
+               $this->restoreGlobals();
                return $tests;
        }
 
diff --git a/tests/loggingTest.php b/tests/loggingTest.php
index 935101e..6e7e701 100644
--- a/tests/loggingTest.php
+++ b/tests/loggingTest.php
@@ -8,16 +8,7 @@
  * @file
  */
 
-class loggingTests extends PHPUnit_Framework_TestCase {
-
-       private $globals = array();
-
-       protected function restoreGlobals() {
-               foreach ( $this->globals as $key => $value ) {
-                       $GLOBALS[$key] = $value;
-               }
-               $this->globals = array();
-       }
+class loggingTests extends WgConfTestCase {
 
        public function provideHandlerSetup() {
                return array(
@@ -191,28 +182,6 @@
                        if ( isset( $config[$handler] ) ) {
                                $this->assertValidLogLevel( $config[$handler] );
                        }
-               }
-       }
-
-       protected function setGlobals( $pairs, $value = null ) {
-               if ( is_string( $pairs ) ) {
-                       $pairs = array( $pairs => $value );
-               }
-               foreach ( $pairs as $key => $value ) {
-                       // only set value in $this->globals on first call
-                       if ( !array_key_exists( $key, $this->globals ) ) {
-                               if ( isset( $GLOBALS[$key] ) ) {
-                                       // break any object references
-                                       try {
-                                               $this->globals[$key] = 
unserialize( serialize( $GLOBALS[$key] ) );
-                                       } catch ( \Exception $e ) {
-                                               $this->globals[$key] = 
$GLOBALS[$key];
-                                       }
-                               } else {
-                                       $this->globals[$key] = null;
-                               }
-                       }
-                       $GLOBALS[$key] = $value;
                }
        }
 }

-- 
To view, visit https://gerrit.wikimedia.org/r/349413
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I7a86e856881da69431c29b81cf1005070e029a0b
Gerrit-PatchSet: 1
Gerrit-Project: operations/mediawiki-config
Gerrit-Branch: master
Gerrit-Owner: Hashar <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to