diff --git a/src/Symfony/Bridge/PhpUnit/Legacy/ExpectDeprecationTraitBeforeV8_4.php b/src/Symfony/Bridge/PhpUnit/Legacy/ExpectDeprecationTraitBeforeV8_4.php index 47c8226e6e..03368b5597 100644 --- a/src/Symfony/Bridge/PhpUnit/Legacy/ExpectDeprecationTraitBeforeV8_4.php +++ b/src/Symfony/Bridge/PhpUnit/Legacy/ExpectDeprecationTraitBeforeV8_4.php @@ -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']); } diff --git a/src/Symfony/Bridge/PhpUnit/Legacy/ExpectDeprecationTraitForV8_4.php b/src/Symfony/Bridge/PhpUnit/Legacy/ExpectDeprecationTraitForV8_4.php index ceaefdb0b3..7347652524 100644 --- a/src/Symfony/Bridge/PhpUnit/Legacy/ExpectDeprecationTraitForV8_4.php +++ b/src/Symfony/Bridge/PhpUnit/Legacy/ExpectDeprecationTraitForV8_4.php @@ -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']); } diff --git a/src/Symfony/Bridge/PhpUnit/Legacy/SymfonyTestsListenerTrait.php b/src/Symfony/Bridge/PhpUnit/Legacy/SymfonyTestsListenerTrait.php index 3951b60705..5efc35a805 100644 --- a/src/Symfony/Bridge/PhpUnit/Legacy/SymfonyTestsListenerTrait.php +++ b/src/Symfony/Bridge/PhpUnit/Legacy/SymfonyTestsListenerTrait.php @@ -208,6 +208,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)); @@ -249,6 +250,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(); } diff --git a/src/Symfony/Bridge/PhpUnit/Tests/ExpectDeprecationTraitTest.php b/src/Symfony/Bridge/PhpUnit/Tests/ExpectDeprecationTraitTest.php index 2d3f0e7a8b..2830daef6e 100644 --- a/src/Symfony/Bridge/PhpUnit/Tests/ExpectDeprecationTraitTest.php +++ b/src/Symfony/Bridge/PhpUnit/Tests/ExpectDeprecationTraitTest.php @@ -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. * diff --git a/src/Symfony/Bridge/PhpUnit/Tests/FailTests/ExpectDeprecationTraitTestFail.php b/src/Symfony/Bridge/PhpUnit/Tests/FailTests/ExpectDeprecationTraitTestFail.php new file mode 100644 index 0000000000..43a3e34f96 --- /dev/null +++ b/src/Symfony/Bridge/PhpUnit/Tests/FailTests/ExpectDeprecationTraitTestFail.php @@ -0,0 +1,49 @@ + + * + * 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); + } +} diff --git a/src/Symfony/Bridge/PhpUnit/Tests/expectdeprecationfail.phpt b/src/Symfony/Bridge/PhpUnit/Tests/expectdeprecationfail.phpt new file mode 100644 index 0000000000..5af138691c --- /dev/null +++ b/src/Symfony/Bridge/PhpUnit/Tests/expectdeprecationfail.phpt @@ -0,0 +1,37 @@ +--TEST-- +Test ExpectDeprecationTrait failing tests +--FILE-- + +--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. diff --git a/src/Symfony/Bridge/PhpUnit/bin/simple-phpunit.php b/src/Symfony/Bridge/PhpUnit/bin/simple-phpunit.php index 7c1c3123e1..67f6c78c7c 100644 --- a/src/Symfony/Bridge/PhpUnit/bin/simple-phpunit.php +++ b/src/Symfony/Bridge/PhpUnit/bin/simple-phpunit.php @@ -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) {