From 75d0d593e3290acc68e0a6c8402b842f7144c0f1 Mon Sep 17 00:00:00 2001 From: Mikael Pajunen Date: Fri, 2 Jan 2015 18:12:15 +0200 Subject: [PATCH] Use PHPUnit ini_set wrapper in tests PHPUnit ini_set wrapper is now used in tests to automatically reset ini settings after the test is run. This avoids possible side effects and test skipping. Native ini_set is still used in DefaultCsrfProviderTest, but its tests are run in isolation. --- .../Component/Debug/Tests/ErrorHandlerTest.php | 9 +-------- .../Session/Storage/NativeSessionStorageTest.php | 12 ++++++------ .../Session/Storage/PhpBridgeSessionStorageTest.php | 4 ++-- .../Tests/EventListener/ExceptionListenerTest.php | 6 +----- .../Component/Process/Tests/ExecutableFinderTest.php | 4 ++-- 5 files changed, 12 insertions(+), 23 deletions(-) diff --git a/src/Symfony/Component/Debug/Tests/ErrorHandlerTest.php b/src/Symfony/Component/Debug/Tests/ErrorHandlerTest.php index 3ba7ad21f8..409780cffc 100644 --- a/src/Symfony/Component/Debug/Tests/ErrorHandlerTest.php +++ b/src/Symfony/Component/Debug/Tests/ErrorHandlerTest.php @@ -26,21 +26,14 @@ class ErrorHandlerTest extends \PHPUnit_Framework_TestCase */ protected $errorReporting; - /** - * @var string Display errors setting before running tests. - */ - protected $displayErrors; - public function setUp() { $this->errorReporting = error_reporting(E_ALL | E_STRICT); - $this->displayErrors = ini_get('display_errors'); - ini_set('display_errors', '1'); + $this->iniSet('display_errors', '1'); } public function tearDown() { - ini_set('display_errors', $this->displayErrors); error_reporting($this->errorReporting); } diff --git a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/NativeSessionStorageTest.php b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/NativeSessionStorageTest.php index 41329e7248..e2146d8434 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/NativeSessionStorageTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/NativeSessionStorageTest.php @@ -35,8 +35,8 @@ class NativeSessionStorageTest extends \PHPUnit_Framework_TestCase protected function setUp() { - ini_set('session.save_handler', 'files'); - ini_set('session.save_path', $this->savePath = sys_get_temp_dir().'/sf2test'); + $this->iniSet('session.save_handler', 'files'); + $this->iniSet('session.save_path', $this->savePath = sys_get_temp_dir().'/sf2test'); if (!is_dir($this->savePath)) { mkdir($this->savePath); } @@ -121,7 +121,7 @@ class NativeSessionStorageTest extends \PHPUnit_Framework_TestCase public function testDefaultSessionCacheLimiter() { - ini_set('session.cache_limiter', 'nocache'); + $this->iniSet('session.cache_limiter', 'nocache'); $storage = new NativeSessionStorage(); $this->assertEquals('', ini_get('session.cache_limiter')); @@ -129,7 +129,7 @@ class NativeSessionStorageTest extends \PHPUnit_Framework_TestCase public function testExplicitSessionCacheLimiter() { - ini_set('session.cache_limiter', 'nocache'); + $this->iniSet('session.cache_limiter', 'nocache'); $storage = new NativeSessionStorage(array('cache_limiter' => 'public')); $this->assertEquals('public', ini_get('session.cache_limiter')); @@ -171,7 +171,7 @@ class NativeSessionStorageTest extends \PHPUnit_Framework_TestCase $this->markTestSkipped('Test skipped, for PHP 5.3 only.'); } - ini_set('session.save_handler', 'files'); + $this->iniSet('session.save_handler', 'files'); $storage = $this->getStorage(); $storage->setSaveHandler(); $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\NativeProxy', $storage->getSaveHandler()); @@ -193,7 +193,7 @@ class NativeSessionStorageTest extends \PHPUnit_Framework_TestCase $this->markTestSkipped('Test skipped, for PHP 5.4 only.'); } - ini_set('session.save_handler', 'files'); + $this->iniSet('session.save_handler', 'files'); $storage = $this->getStorage(); $storage->setSaveHandler(); $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy', $storage->getSaveHandler()); diff --git a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/PhpBridgeSessionStorageTest.php b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/PhpBridgeSessionStorageTest.php index d9f4a30a89..0acc4458cc 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/PhpBridgeSessionStorageTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/PhpBridgeSessionStorageTest.php @@ -30,8 +30,8 @@ class PhpBridgeSessionStorageTest extends \PHPUnit_Framework_TestCase protected function setUp() { - ini_set('session.save_handler', 'files'); - ini_set('session.save_path', $this->savePath = sys_get_temp_dir().'/sf2test'); + $this->iniSet('session.save_handler', 'files'); + $this->iniSet('session.save_path', $this->savePath = sys_get_temp_dir().'/sf2test'); if (!is_dir($this->savePath)) { mkdir($this->savePath); } diff --git a/src/Symfony/Component/HttpKernel/Tests/EventListener/ExceptionListenerTest.php b/src/Symfony/Component/HttpKernel/Tests/EventListener/ExceptionListenerTest.php index d691e0aaaf..02664b7eed 100644 --- a/src/Symfony/Component/HttpKernel/Tests/EventListener/ExceptionListenerTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/EventListener/ExceptionListenerTest.php @@ -56,8 +56,7 @@ class ExceptionListenerTest extends \PHPUnit_Framework_TestCase */ public function testHandleWithoutLogger($event, $event2) { - // store the current error_log, and disable it temporarily - $errorLog = ini_set('error_log', file_exists('/dev/null') ? '/dev/null' : 'nul'); + $this->iniSet('error_log', file_exists('/dev/null') ? '/dev/null' : 'nul'); $l = new ExceptionListener('foo'); $l->onKernelException($event); @@ -69,9 +68,6 @@ class ExceptionListenerTest extends \PHPUnit_Framework_TestCase } catch (\Exception $e) { $this->assertSame('foo', $e->getMessage()); } - - // restore the old error_log - ini_set('error_log', $errorLog); } /** diff --git a/src/Symfony/Component/Process/Tests/ExecutableFinderTest.php b/src/Symfony/Component/Process/Tests/ExecutableFinderTest.php index 9d0a0d141f..5033cdab02 100644 --- a/src/Symfony/Component/Process/Tests/ExecutableFinderTest.php +++ b/src/Symfony/Component/Process/Tests/ExecutableFinderTest.php @@ -102,7 +102,7 @@ class ExecutableFinderTest extends \PHPUnit_Framework_TestCase $this->markTestSkipped('Cannot test when open_basedir is set'); } - ini_set('open_basedir', dirname(PHP_BINARY).PATH_SEPARATOR.'/'); + $this->iniSet('open_basedir', dirname(PHP_BINARY).PATH_SEPARATOR.'/'); $finder = new ExecutableFinder(); $result = $finder->find($this->getPhpBinaryName()); @@ -125,7 +125,7 @@ class ExecutableFinderTest extends \PHPUnit_Framework_TestCase } $this->setPath(''); - ini_set('open_basedir', PHP_BINARY.PATH_SEPARATOR.'/'); + $this->iniSet('open_basedir', PHP_BINARY.PATH_SEPARATOR.'/'); $finder = new ExecutableFinder(); $result = $finder->find($this->getPhpBinaryName(), false);