Skip to content
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

[PhpUnitBridge] Fix expectDeprecation() in isolation #37515

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -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']);
}
Expand Down
Expand Up @@ -25,6 +25,21 @@ public function expectDeprecation(): void
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;
alexpott marked this conversation as resolved.
Show resolved Hide resolved
}

if (!SymfonyTestsListenerTrait::$previousErrorHandler) {
SymfonyTestsListenerTrait::$previousErrorHandler = set_error_handler([SymfonyTestsListenerTrait::class, 'handleError']);
}
Expand Down
12 changes: 12 additions & 0 deletions src/Symfony/Bridge/PhpUnit/Legacy/SymfonyTestsListenerTrait.php
Expand Up @@ -204,6 +204,7 @@ public function startTest($test)
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));
Expand Down Expand Up @@ -245,6 +246,17 @@ public function startTest($test)

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();
}
Expand Down
12 changes: 12 additions & 0 deletions src/Symfony/Bridge/PhpUnit/Tests/ExpectDeprecationTraitTest.php
Expand Up @@ -29,6 +29,18 @@ public function testOne()
@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.
*
Expand Down
@@ -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);
}
}
37 changes: 37 additions & 0 deletions src/Symfony/Bridge/PhpUnit/Tests/expectdeprecationfail.phpt
@@ -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.
3 changes: 2 additions & 1 deletion src/Symfony/Bridge/PhpUnit/bin/simple-phpunit.php
Expand Up @@ -134,6 +134,7 @@
'COMPOSER' => 'composer.json',
'COMPOSER_VENDOR_DIR' => 'vendor',
'COMPOSER_BIN_DIR' => 'bin',
'SYMFONY_SIMPLE_PHPUNIT_BIN_DIR' => __DIR__,
];

foreach ($defaultEnvs as $envName => $envValue) {
Expand Down Expand Up @@ -193,7 +194,7 @@
'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.*\"");
Expand Down