8000 [BrowserKit][Bridge\PhpUnit] Handle deprecations triggered in separate processes by nicolas-grekas · Pull Request #24548 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[BrowserKit][Bridge\PhpUnit] Handle deprecations triggered in separate processes #24548

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
59 changes: 45 additions & 14 deletions src/Symfony/Bridge/PhpUnit/Legacy/SymfonyTestsListenerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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';
Expand All @@ -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));
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
}
}
23 changes: 23 additions & 0 deletions src/Symfony/Bridge/PhpUnit/Tests/ProcessIsolationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Symfony\Bridge\PhpUnit\Tests;

use PHPUnit\Framework\TestCase;

/**
* Don't remove this test case, it tests the legacy group.
*
* @group legacy
*
* @runTestsInSeparateProcesses
*/
class ProcessIsolationTest extends TestCase
{
/**
* @expectedDeprecation Test abc
*/
public function testIsolation()
{
@trigger_error('Test abc', E_USER_DEPRECATED);
}
}
4 changes: 4 additions & 0 deletions src/Symfony/Bridge/PhpUnit/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@

// Detect if we're loaded by an actual run of phpunit
if (!defined('PHPUNIT_COMPOSER_INSTALL') && !class_exists('PHPUnit_TextUI_Command', false) && !class_exists('PHPUnit\TextUI\Command', false)) {
if ($ser = getenv('SYMFONY_DEPRECATIONS_SERIALIZE')) {
DeprecationErrorHandler::collectDeprecations($ser);
}

return;
}

Expand Down
14 changes: 14 additions & 0 deletions src/Symfony/Component/BrowserKit/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
Expand Down
0