Expect deprecations in isolation

This commit is contained in:
Alex Pott 2020-07-07 17:46:44 +01:00 committed by Nicolas Grekas
parent ce0a9d2f09
commit e7e2ee75d3
7 changed files with 142 additions and 1 deletions

View File

@ -23,6 +23,21 @@ trait ExpectDeprecationTraitBeforeV8_4
*/
protected function expectDeprecation($message)
{
// Expected deprecations set by isolated tests need to be written to a file
// so that the test running process can take account of them.
if ($file = getenv('SYMFONY_EXPECTED_DEPRECATIONS_SERIALIZE')) {
$this->getTestResultObject()->beStrictAboutTestsThatDoNotTestAnything(false);
$expectedDeprecations = file_get_contents($file);
if ($expectedDeprecations) {
$expectedDeprecations = array_merge(unserialize($expectedDeprecations), [$message]);
} else {
$expectedDeprecations = [$message];
}
file_put_contents($file, serialize($expectedDeprecations));
return;
}
if (!SymfonyTestsListenerTrait::$previousErrorHandler) {
SymfonyTestsListenerTrait::$previousErrorHandler = set_error_handler([SymfonyTestsListenerTrait::class, 'handleError']);
}

View File

@ -25,6 +25,21 @@ trait ExpectDeprecationTraitForV8_4
throw new \InvalidArgumentException(sprintf('The "%s()" method requires the string $message argument.', __FUNCTION__));
}
// Expected deprecations set by isolated tests need to be written to a file
// so that the test running process can take account of them.
if ($file = getenv('SYMFONY_EXPECTED_DEPRECATIONS_SERIALIZE')) {
$this->getTestResultObject()->beStrictAboutTestsThatDoNotTestAnything(false);
$expectedDeprecations = file_get_contents($file);
if ($expectedDeprecations) {
$expectedDeprecations = array_merge(unserialize($expectedDeprecations), [$message]);
} else {
$expectedDeprecations = [$message];
}
file_put_contents($file, serialize($expectedDeprecations));
return;
}
if (!SymfonyTestsListenerTrait::$previousErrorHandler) {
SymfonyTestsListenerTrait::$previousErrorHandler = set_error_handler([SymfonyTestsListenerTrait::class, 'handleError']);
}

View File

@ -204,6 +204,7 @@ class SymfonyTestsListenerTrait
if ($this->willBeIsolated($test)) {
$this->runsInSeparateProcess = tempnam(sys_get_temp_dir(), 'deprec');
putenv('SYMFONY_DEPRECATIONS_SERIALIZE='.$this->runsInSeparateProcess);
putenv('SYMFONY_EXPECTED_DEPRECATIONS_SERIALIZE='.tempnam(sys_get_temp_dir(), 'expectdeprec'));
}
$groups = Test::getGroups(\get_class($test), $test->getName(false));
@ -245,6 +246,17 @@ class SymfonyTestsListenerTrait
public function endTest($test, $time)
{
if ($file = getenv('SYMFONY_EXPECTED_DEPRECATIONS_SERIALIZE')) {
putenv('SYMFONY_EXPECTED_DEPRECATIONS_SERIALIZE');
$expectedDeprecations = file_get_contents($file);
if ($expectedDeprecations) {
self::$expectedDeprecations = array_merge(self::$expectedDeprecations, unserialize($expectedDeprecations));
if (!self::$previousErrorHandler) {
self::$previousErrorHandler = set_error_handler([self::class, 'handleError']);
}
}
}
if (class_exists(DebugClassLoader::class, false)) {
DebugClassLoader::checkClasses();
}

View File

@ -29,6 +29,18 @@ final class ExpectDeprecationTraitTest extends TestCase
@trigger_error('foo', E_USER_DEPRECATED);
}
/**
* Do not remove this test in the next major version.
*
* @group legacy
* @runInSeparateProcess
*/
public function testOneInIsolation()
{
$this->expectDeprecation('foo');
@trigger_error('foo', E_USER_DEPRECATED);
}
/**
* Do not remove this test in the next major version.
*

View File

@ -0,0 +1,49 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bridge\PhpUnit\Tests\FailTests;
use PHPUnit\Framework\TestCase;
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
/**
* Class ExpectDeprecationTraitTestFail.
*
* This class is deliberately suffixed with *TestFail.php so that it is ignored
* by PHPUnit. This test is designed to fail. See ../expectdeprecationfail.phpt.
*/
final class ExpectDeprecationTraitTestFail extends TestCase
{
use ExpectDeprecationTrait;
/**
* Do not remove this test in the next major version.
*
* @group legacy
*/
public function testOne()
{
$this->expectDeprecation('foo');
@trigger_error('bar', E_USER_DEPRECATED);
}
/**
* Do not remove this test in the next major version.
*
* @group legacy
* @runInSeparateProcess
*/
public function testOneInIsolation()
{
$this->expectDeprecation('foo');
@trigger_error('bar', E_USER_DEPRECATED);
}
}

View File

@ -0,0 +1,37 @@
--TEST--
Test ExpectDeprecationTrait failing tests
--FILE--
<?php
$test = realpath(__DIR__ . '/FailTests/ExpectDeprecationTraitTestFail.php');
passthru(getenv('SYMFONY_SIMPLE_PHPUNIT_BIN_DIR') . '/simple-phpunit --colors=never ' . $test);
?>
--EXPECTF--
PHPUnit %s by Sebastian Bergmann and contributors.
Testing Symfony\Bridge\PhpUnit\Tests\FailTests\ExpectDeprecationTraitTestFail
FF 2 / 2 (100%)
Time: %s, Memory: %s
There were 2 failures:
1) Symfony\Bridge\PhpUnit\Tests\FailTests\ExpectDeprecationTraitTestFail::testOne
Failed asserting that string matches format description.
--- Expected
+++ Actual
@@ @@
@expectedDeprecation:
-%A foo
+ bar
2) Symfony\Bridge\PhpUnit\Tests\FailTests\ExpectDeprecationTraitTestFail::testOneInIsolation
Failed asserting that string matches format description.
--- Expected
+++ Actual
@@ @@
@expectedDeprecation:
-%A foo
+ bar
FAILURES!
Tests: 2, Assertions: 2, Failures: 2.

View File

@ -134,6 +134,7 @@ $defaultEnvs = [
'COMPOSER' => 'composer.json',
'COMPOSER_VENDOR_DIR' => 'vendor',
'COMPOSER_BIN_DIR' => 'bin',
'SYMFONY_SIMPLE_PHPUNIT_BIN_DIR' => __DIR__,
];
foreach ($defaultEnvs as $envName => $envValue) {
@ -193,7 +194,7 @@ if (!file_exists("$PHPUNIT_DIR/$PHPUNIT_VERSION_DIR/phpunit") || $configurationH
'requires' => ['php' => '*'],
];
if (1 === \count($info['versions'])) {
if (1 === count($info['versions'])) {
$passthruOrFail("$COMPOSER create-project --ignore-platform-reqs --no-install --prefer-dist --no-scripts --no-plugins --no-progress -s dev phpunit/phpunit $PHPUNIT_VERSION_DIR \"$PHPUNIT_VERSION.*\"");
} else {
$passthruOrFail("$COMPOSER create-project --ignore-platform-reqs --no-install --prefer-dist --no-scripts --no-plugins --no-progress phpunit/phpunit $PHPUNIT_VERSION_DIR \"$PHPUNIT_VERSION.*\"");