From ff379efb59de25bcdb88225f7389013fecf54b17 Mon Sep 17 00:00:00 2001 From: Paul Mitchum Date: Fri, 2 Jun 2017 12:44:10 -0700 Subject: [PATCH 1/2] [Bridge\PhpUnit] Handle deprecations triggered in separate processes --- .../PhpUnit/DeprecationErrorHandler.php | 14 +++++ .../Legacy/SymfonyTestsListenerTrait.php | 59 ++++++++++++++----- .../PhpUnit/Tests/ProcessIsolationTest.php | 23 ++++++++ src/Symfony/Bridge/PhpUnit/bootstrap.php | 4 ++ 4 files changed, 86 insertions(+), 14 deletions(-) create mode 100644 src/Symfony/Bridge/PhpUnit/Tests/ProcessIsolationTest.php diff --git a/src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php b/src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php index 0f54a52bb8cab..a7dbc08e0c6c2 100644 --- a/src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php +++ b/src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php @@ -238,6 +238,20 @@ public static function register($mode = 0) } } + public static function collectDeprecations($outputFile) + { + $deprecations = array(); + $previousErrorHandler = set_error_handler(function ($type, $msg, $file, $line, $context = array()) use (&$deprecations, &$previousErrorHandler) { + if (E_USER_DEPRECATED !== $type && E_DEPRECATED !== $type) { + return $previousErrorHandler ? $previousErrorHandler($type, $msg, $file, $line, $context) : false; + } + $deprecations[] = array(error_reporting(), $msg); + }); + register_shutdown_function(function () use ($outputFile, &$deprecations) { + file_put_contents($outputFile, serialize($deprecations)); + }); + } + private static function hasColorSupport() { if ('\\' === DIRECTORY_SEPARATOR) { diff --git a/src/Symfony/Bridge/PhpUnit/Legacy/SymfonyTestsListenerTrait.php b/src/Symfony/Bridge/PhpUnit/Legacy/SymfonyTestsListenerTrait.php index b561c8f2f53a5..166c44a3f8f2c 100644 --- a/src/Symfony/Bridge/PhpUnit/Legacy/SymfonyTestsListenerTrait.php +++ b/src/Symfony/Bridge/PhpUnit/Legacy/SymfonyTestsListenerTrait.php @@ -39,6 +39,7 @@ class SymfonyTestsListenerTrait private $testsWithWarnings; private $reportUselessTests; private $error; + private $runsInSeparateProcess = false; /** * @param array $mockedNamespaces List of namespaces, indexed by mocked features (time-sensitive or dns-sensitive) @@ -183,6 +184,12 @@ public function startTest($test) $this->reportUselessTests = $test->getTestResultObject()->isStrictAboutTestsThatDoNotTestAnything(); } + // This event is triggered before the test is re-run in isolation + if ($this->willBeIsolated($test)) { + $this->runsInSeparateProcess = tempnam(sys_get_temp_dir(), 'deprec'); + putenv('SYMFONY_DEPRECATIONS_SERIALIZE='.$this->runsInSeparateProcess); + } + if (class_exists('PHPUnit_Util_Blacklist', false)) { $Test = 'PHPUnit_Util_Test'; $AssertionFailedError = 'PHPUnit_Framework_AssertionFailedError'; @@ -192,12 +199,14 @@ public function startTest($test) } $groups = $Test::getGroups(get_class($test), $test->getName(false)); - if (in_array('time-sensitive', $groups, true)) { - ClockMock::register(get_class($test)); - ClockMock::withClockMock(true); - } - if (in_array('dns-sensitive', $groups, true)) { - DnsMock::register(get_class($test)); + if (!$this->runsInSeparateProcess) { + if (in_array('time-sensitive', $groups, true)) { + ClockMock::register(get_class($test)); + ClockMock::withClockMock(true); + } + if (in_array('dns-sensitive', $groups, true)) { + DnsMock::register(get_class($test)); + } } $annotations = $Test::parseTestMethodAnnotations(get_class($test), $test->getName(false)); @@ -245,15 +254,20 @@ public function endTest($test, $time) $this->reportUselessTests = null; } - $errored = false; + if ($errored = null !== $this->error) { + $test->getTestResultObject()->addError($test, $this->error, 0); + $this->error = null; + } - if (null !== $this->error) { - if ($BaseTestRunner::STATUS_PASSED === $test->getStatus()) { - $test->getTestResultObject()->addError($test, $this->error, 0); - $errored = true; + if ($this->runsInSeparateProcess) { + foreach (unserialize(file_get_contents($this->runsInSeparateProcess)) as $deprecation) { + if ($deprecation[0]) { + trigger_error($deprecation[1], E_USER_DEPRECATED); + } else { + @trigger_error($deprecation[1], E_USER_DEPRECATED); + } } - - $this->error = null; + $this->runsInSeparateProcess = false; } if ($this->expectedDeprecations) { @@ -277,7 +291,7 @@ public function endTest($test, $time) $this->expectedDeprecations = $this->gatheredDeprecations = array(); $this->previousErrorHandler = null; } - if (-2 < $this->state && ($test instanceof \PHPUnit_Framework_TestCase || $test instanceof TestCase)) { + if (!$this->runsInSeparateProcess && -2 < $this->state && ($test instanceof \PHPUnit_Framework_TestCase || $test instanceof TestCase)) { if (in_array('time-sensitive', $groups, true)) { ClockMock::withClockMock(false); } @@ -315,4 +329,21 @@ public function handleError($type, $msg, $file, $line, $context = array()) } $this->gatheredDeprecations[] = $msg; } + + /** + * @param Test $test + * + * @return bool + */ + private function willBeIsolated($test) + { + if ($test->isInIsolation()) { + return false; + } + + $r = new \ReflectionProperty($test, 'runTestInSeparateProcess'); + $r->setAccessible(true); + + return $r->getValue($test); + } } diff --git a/src/Symfony/Bridge/PhpUnit/Tests/ProcessIsolationTest.php b/src/Symfony/Bridge/PhpUnit/Tests/ProcessIsolationTest.php new file mode 100644 index 0000000000000..d8677097e5c26 --- /dev/null +++ b/src/Symfony/Bridge/PhpUnit/Tests/ProcessIsolationTest.php @@ -0,0 +1,23 @@ + Date: Fri, 13 Oct 2017 15:21:24 +0200 Subject: [PATCH 2/2] [BrowserKit] Handle deprecations triggered in insulated requests --- src/Symfony/Component/BrowserKit/Client.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/Symfony/Component/BrowserKit/Client.php b/src/Symfony/Component/BrowserKit/Client.php index 2f2b08d81894d..fb7a254e2c56d 100644 --- a/src/Symfony/Component/BrowserKit/Client.php +++ b/src/Symfony/Component/BrowserKit/Client.php @@ -343,9 +343,23 @@ public function request($method, $uri, array $parameters = array(), array $files */ protected function doRequestInProcess($request) { + $deprecationsFile = tempnam(sys_get_temp_dir(), 'deprec'); + putenv('SYMFONY_DEPRECATIONS_SERIALIZE='.$deprecationsFile); $process = new PhpProcess($this->getScript($request), null, null); $process->run(); + if (file_exists($deprecationsFile)) { + $deprecations = file_get_contents($deprecationsFile); + unlink($deprecationsFile); + foreach ($deprecations ? unserialize($deprecations) : array() as $deprecation) { + if ($deprecation[0]) { + trigger_error($deprecation[1], E_USER_DEPRECATED); + } else { + @trigger_error($deprecation[1], E_USER_DEPRECATED); + } + } + } + if (!$process->isSuccessful() || !preg_match('/^O\:\d+\:/', $process->getOutput())) { throw new \RuntimeException(sprintf('OUTPUT: %s ERROR OUTPUT: %s', $process->getOutput(), $process->getErrorOutput())); }