tests: Add helper function for ini_set with automatic cleanup
authorKunal Mehta <legoktm@member.fsf.org>
Thu, 11 Oct 2018 05:31:37 +0000 (22:31 -0700)
committerKunal Mehta <legoktm@member.fsf.org>
Thu, 11 Oct 2018 05:31:37 +0000 (22:31 -0700)
Some tests need to change the value of an ini setting, and typically implement
cleanup handling themselves, usually imperfectly.

Provide a helper function, $this->setIniSetting(), which will take care of
teardown in the same way that $this->setMwGlobals() does.

Change-Id: I7be4198592f0aaf73a28d3c60acb307a918b1a1f

tests/phpunit/MediaWikiTestCase.php
tests/phpunit/languages/LanguageConverterTest.php

index feab0df..287d28c 100644 (file)
@@ -100,6 +100,14 @@ abstract class MediaWikiTestCase extends PHPUnit\Framework\TestCase {
         */
        private $mwGlobalsToUnset = [];
 
+       /**
+        * Holds original values of ini settings to be restored
+        * in tearDown().
+        * @see setIniSettings()
+        * @var array
+        */
+       private $iniSettings = [];
+
        /**
         * Holds original loggers which have been replaced by setLogger()
         * @var LoggerInterface[]
@@ -573,6 +581,9 @@ abstract class MediaWikiTestCase extends PHPUnit\Framework\TestCase {
                foreach ( $this->mwGlobalsToUnset as $value ) {
                        unset( $GLOBALS[$value] );
                }
+               foreach ( $this->iniSettings as $name => $value ) {
+                       ini_set( $name, $value );
+               }
                if (
                        array_key_exists( 'wgExtraNamespaces', $this->mwGlobals ) ||
                        in_array( 'wgExtraNamespaces', $this->mwGlobalsToUnset )
@@ -722,6 +733,18 @@ abstract class MediaWikiTestCase extends PHPUnit\Framework\TestCase {
                }
        }
 
+       /**
+        * Set an ini setting for the duration of the test
+        * @param string $name Name of the setting
+        * @param string $value Value to set
+        * @since 1.32
+        */
+       protected function setIniSetting( $name, $value ) {
+               $original = ini_get( $name );
+               $this->iniSettings[$name] = $original;
+               ini_set( $name, $value );
+       }
+
        /**
         * Must be called whenever namespaces are changed, e.g., $wgExtraNamespaces is altered.
         * Otherwise old namespace data will lurk and cause bugs.
index 8ccacfc..0ce0e90 100644 (file)
@@ -169,9 +169,8 @@ class LanguageConverterTest extends MediaWikiLangTestCase {
                        $testString .= 'xxx xxx xxx';
                }
                $testString .= "\n<big id='в'></big>";
-               $old = ini_set( 'pcre.backtrack_limit', 200 );
+               $this->setIniSetting( 'pcre.backtrack_limit', 200 );
                $result = $this->lc->autoConvert( $testString, 'tg-latn' );
-               ini_set( 'pcre.backtrack_limit', $old );
                // The в in the id attribute should not get converted to a v
                $this->assertFalse(
                        strpos( $result, 'v' ),