From a4e8bea57d9dbf2b64fa662ec408cf48f61bfe18 Mon Sep 17 00:00:00 2001 From: Kunal Mehta Date: Wed, 10 Oct 2018 22:31:37 -0700 Subject: [PATCH] tests: Add helper function for ini_set with automatic cleanup 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 | 23 +++++++++++++++++++ .../languages/LanguageConverterTest.php | 3 +-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/tests/phpunit/MediaWikiTestCase.php b/tests/phpunit/MediaWikiTestCase.php index feab0df451..287d28c3b9 100644 --- a/tests/phpunit/MediaWikiTestCase.php +++ b/tests/phpunit/MediaWikiTestCase.php @@ -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. diff --git a/tests/phpunit/languages/LanguageConverterTest.php b/tests/phpunit/languages/LanguageConverterTest.php index 8ccacfc23a..0ce0e90b14 100644 --- a/tests/phpunit/languages/LanguageConverterTest.php +++ b/tests/phpunit/languages/LanguageConverterTest.php @@ -169,9 +169,8 @@ class LanguageConverterTest extends MediaWikiLangTestCase { $testString .= 'xxx xxx xxx'; } $testString .= "\n"; - $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' ), -- 2.20.1