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.
This commit is contained in:
Mikael Pajunen 2015-01-02 18:12:15 +02:00
parent 99ff8a6de4
commit 75d0d593e3
5 changed files with 12 additions and 23 deletions

View File

@ -26,21 +26,14 @@ class ErrorHandlerTest extends \PHPUnit_Framework_TestCase
*/ */
protected $errorReporting; protected $errorReporting;
/**
* @var string Display errors setting before running tests.
*/
protected $displayErrors;
public function setUp() public function setUp()
{ {
$this->errorReporting = error_reporting(E_ALL | E_STRICT); $this->errorReporting = error_reporting(E_ALL | E_STRICT);
$this->displayErrors = ini_get('display_errors'); $this->iniSet('display_errors', '1');
ini_set('display_errors', '1');
} }
public function tearDown() public function tearDown()
{ {
ini_set('display_errors', $this->displayErrors);
error_reporting($this->errorReporting); error_reporting($this->errorReporting);
} }

View File

@ -35,8 +35,8 @@ class NativeSessionStorageTest extends \PHPUnit_Framework_TestCase
protected function setUp() protected function setUp()
{ {
ini_set('session.save_handler', 'files'); $this->iniSet('session.save_handler', 'files');
ini_set('session.save_path', $this->savePath = sys_get_temp_dir().'/sf2test'); $this->iniSet('session.save_path', $this->savePath = sys_get_temp_dir().'/sf2test');
if (!is_dir($this->savePath)) { if (!is_dir($this->savePath)) {
mkdir($this->savePath); mkdir($this->savePath);
} }
@ -121,7 +121,7 @@ class NativeSessionStorageTest extends \PHPUnit_Framework_TestCase
public function testDefaultSessionCacheLimiter() public function testDefaultSessionCacheLimiter()
{ {
ini_set('session.cache_limiter', 'nocache'); $this->iniSet('session.cache_limiter', 'nocache');
$storage = new NativeSessionStorage(); $storage = new NativeSessionStorage();
$this->assertEquals('', ini_get('session.cache_limiter')); $this->assertEquals('', ini_get('session.cache_limiter'));
@ -129,7 +129,7 @@ class NativeSessionStorageTest extends \PHPUnit_Framework_TestCase
public function testExplicitSessionCacheLimiter() public function testExplicitSessionCacheLimiter()
{ {
ini_set('session.cache_limiter', 'nocache'); $this->iniSet('session.cache_limiter', 'nocache');
$storage = new NativeSessionStorage(array('cache_limiter' => 'public')); $storage = new NativeSessionStorage(array('cache_limiter' => 'public'));
$this->assertEquals('public', ini_get('session.cache_limiter')); $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.'); $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 = $this->getStorage();
$storage->setSaveHandler(); $storage->setSaveHandler();
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\NativeProxy', $storage->getSaveHandler()); $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.'); $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 = $this->getStorage();
$storage->setSaveHandler(); $storage->setSaveHandler();
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy', $storage->getSaveHandler()); $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy', $storage->getSaveHandler());

View File

@ -30,8 +30,8 @@ class PhpBridgeSessionStorageTest extends \PHPUnit_Framework_TestCase
protected function setUp() protected function setUp()
{ {
ini_set('session.save_handler', 'files'); $this->iniSet('session.save_handler', 'files');
ini_set('session.save_path', $this->savePath = sys_get_temp_dir().'/sf2test'); $this->iniSet('session.save_path', $this->savePath = sys_get_temp_dir().'/sf2test');
if (!is_dir($this->savePath)) { if (!is_dir($this->savePath)) {
mkdir($this->savePath); mkdir($this->savePath);
} }

View File

@ -56,8 +56,7 @@ class ExceptionListenerTest extends \PHPUnit_Framework_TestCase
*/ */
public function testHandleWithoutLogger($event, $event2) public function testHandleWithoutLogger($event, $event2)
{ {
// store the current error_log, and disable it temporarily $this->iniSet('error_log', file_exists('/dev/null') ? '/dev/null' : 'nul');
$errorLog = ini_set('error_log', file_exists('/dev/null') ? '/dev/null' : 'nul');
$l = new ExceptionListener('foo'); $l = new ExceptionListener('foo');
$l->onKernelException($event); $l->onKernelException($event);
@ -69,9 +68,6 @@ class ExceptionListenerTest extends \PHPUnit_Framework_TestCase
} catch (\Exception $e) { } catch (\Exception $e) {
$this->assertSame('foo', $e->getMessage()); $this->assertSame('foo', $e->getMessage());
} }
// restore the old error_log
ini_set('error_log', $errorLog);
} }
/** /**

View File

@ -102,7 +102,7 @@ class ExecutableFinderTest extends \PHPUnit_Framework_TestCase
$this->markTestSkipped('Cannot test when open_basedir is set'); $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(); $finder = new ExecutableFinder();
$result = $finder->find($this->getPhpBinaryName()); $result = $finder->find($this->getPhpBinaryName());
@ -125,7 +125,7 @@ class ExecutableFinderTest extends \PHPUnit_Framework_TestCase
} }
$this->setPath(''); $this->setPath('');
ini_set('open_basedir', PHP_BINARY.PATH_SEPARATOR.'/'); $this->iniSet('open_basedir', PHP_BINARY.PATH_SEPARATOR.'/');
$finder = new ExecutableFinder(); $finder = new ExecutableFinder();
$result = $finder->find($this->getPhpBinaryName(), false); $result = $finder->find($this->getPhpBinaryName(), false);