From 6bcb32ef300c5227fcd6a920c16c4f7e43cbf4ca Mon Sep 17 00:00:00 2001 From: Iman Ghatami Date: Thu, 1 Oct 2015 09:57:03 +0200 Subject: [PATCH 1/2] This is a fix for issue #1013 which causes phpunit to loose any php value configured in configuration xml to be lost when test is ran in isolation and preserve globals is disabled. --- src/Framework/TestCase.php | 3 ++- src/TextUI/TestRunner.php | 1 + src/Util/PHP/Template/TestCaseMethod.tpl.dist | 7 +++++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Framework/TestCase.php b/src/Framework/TestCase.php index d9932648201..a24da672a9a 100644 --- a/src/Framework/TestCase.php +++ b/src/Framework/TestCase.php @@ -690,7 +690,8 @@ public function run(PHPUnit_Framework_TestResult $result = null) 'isStrictAboutOutputDuringTests' => $isStrictAboutOutputDuringTests, 'isStrictAboutTestSize' => $isStrictAboutTestSize, 'isStrictAboutTodoAnnotatedTests' => $isStrictAboutTodoAnnotatedTests, - 'codeCoverageFilter' => $codeCoverageFilter + 'codeCoverageFilter' => $codeCoverageFilter, + 'configurationFile' => (isset($GLOBALS['__PHPUNIT_CONFIGURATION_FILE']) ? $GLOBALS['__PHPUNIT_CONFIGURATION_FILE'] : '') ) ); diff --git a/src/TextUI/TestRunner.php b/src/TextUI/TestRunner.php index 66df075847e..c7a8a4cd233 100644 --- a/src/TextUI/TestRunner.php +++ b/src/TextUI/TestRunner.php @@ -592,6 +592,7 @@ protected function handleConfiguration(array &$arguments) { if (isset($arguments['configuration']) && !$arguments['configuration'] instanceof PHPUnit_Util_Configuration) { + $GLOBALS['__PHPUNIT_CONFIGURATION_FILE'] = $arguments['configuration']; $arguments['configuration'] = PHPUnit_Util_Configuration::getInstance( $arguments['configuration'] ); diff --git a/src/Util/PHP/Template/TestCaseMethod.tpl.dist b/src/Util/PHP/Template/TestCaseMethod.tpl.dist index ed90c9c24f8..696f46c1ba8 100644 --- a/src/Util/PHP/Template/TestCaseMethod.tpl.dist +++ b/src/Util/PHP/Template/TestCaseMethod.tpl.dist @@ -70,6 +70,13 @@ function __phpunit_run_isolated_test() ); } +$configurationFile = '{configurationFile}'; + +if('' !== $configurationFile) { + $configuration = PHPUnit_Util_Configuration::getInstance($configurationFile); + $configuration->handlePHPConfiguration(); +} + {constants} {included_files} {globals} From cf3390933c01e5f2f600820d5da004833aff16c6 Mon Sep 17 00:00:00 2001 From: Iman Ghatami Date: Thu, 1 Oct 2015 11:19:53 +0200 Subject: [PATCH 2/2] #1013 some minor refactoring and coding style fixes --- src/Framework/TestCase.php | 4 +++- src/TextUI/TestRunner.php | 5 ++++- src/Util/PHP/Template/TestCaseMethod.tpl.dist | 6 +++--- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/Framework/TestCase.php b/src/Framework/TestCase.php index a24da672a9a..10e3dca73a2 100644 --- a/src/Framework/TestCase.php +++ b/src/Framework/TestCase.php @@ -670,6 +670,8 @@ public function run(PHPUnit_Framework_TestResult $result = null) $includePath = "'." . $includePath . ".'"; $codeCoverageFilter = "'." . $codeCoverageFilter . ".'"; + $configurationFilePath = (isset($GLOBALS['__PHPUNIT_CONFIGURATION_FILE']) ? $GLOBALS['__PHPUNIT_CONFIGURATION_FILE'] : ''); + $template->setVar( array( 'composerAutoload' => $composerAutoload, @@ -691,7 +693,7 @@ public function run(PHPUnit_Framework_TestResult $result = null) 'isStrictAboutTestSize' => $isStrictAboutTestSize, 'isStrictAboutTodoAnnotatedTests' => $isStrictAboutTodoAnnotatedTests, 'codeCoverageFilter' => $codeCoverageFilter, - 'configurationFile' => (isset($GLOBALS['__PHPUNIT_CONFIGURATION_FILE']) ? $GLOBALS['__PHPUNIT_CONFIGURATION_FILE'] : '') + 'configurationFilePath' => $configurationFilePath ) ); diff --git a/src/TextUI/TestRunner.php b/src/TextUI/TestRunner.php index c7a8a4cd233..94909d98366 100644 --- a/src/TextUI/TestRunner.php +++ b/src/TextUI/TestRunner.php @@ -142,6 +142,10 @@ private function processSuiteFilters(PHPUnit_Framework_TestSuite $suite, array $ */ public function doRun(PHPUnit_Framework_Test $suite, array $arguments = array()) { + if (isset($arguments['configuration'])) { + $GLOBALS['__PHPUNIT_CONFIGURATION_FILE'] = $arguments['configuration']; + } + $this->handleConfiguration($arguments); $this->processSuiteFilters($suite, $arguments); @@ -592,7 +596,6 @@ protected function handleConfiguration(array &$arguments) { if (isset($arguments['configuration']) && !$arguments['configuration'] instanceof PHPUnit_Util_Configuration) { - $GLOBALS['__PHPUNIT_CONFIGURATION_FILE'] = $arguments['configuration']; $arguments['configuration'] = PHPUnit_Util_Configuration::getInstance( $arguments['configuration'] ); diff --git a/src/Util/PHP/Template/TestCaseMethod.tpl.dist b/src/Util/PHP/Template/TestCaseMethod.tpl.dist index 696f46c1ba8..e595099439a 100644 --- a/src/Util/PHP/Template/TestCaseMethod.tpl.dist +++ b/src/Util/PHP/Template/TestCaseMethod.tpl.dist @@ -70,10 +70,10 @@ function __phpunit_run_isolated_test() ); } -$configurationFile = '{configurationFile}'; +$configurationFilePath = '{configurationFilePath}'; -if('' !== $configurationFile) { - $configuration = PHPUnit_Util_Configuration::getInstance($configurationFile); +if ('' !== $configurationFilePath) { + $configuration = PHPUnit_Util_Configuration::getInstance($configurationFilePath); $configuration->handlePHPConfiguration(); }