Skip to content

Commit 5992bf0

Browse files
bug #37515 [PhpUnitBridge] Fix expectDeprecation() in isolation (alexpott)
This PR was squashed before being merged into the 5.1 branch. Discussion ---------- [PhpUnitBridge] Fix expectDeprecation() in isolation | Q | A | ------------- | --- | Branch? | 5.1 | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | Fix #... <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT | Doc PR | symfony/symfony-docs#... <!-- required for new features --> Tests like ``` /** * 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); } ``` will fail due to: ``` Testing Symfony\Bridge\PhpUnit\Tests\ExpectDeprecationTraitTest R 1 / 1 (100%)R Time: 111 ms, Memory: 6.00 MB There were 2 risky tests: 1) Symfony\Bridge\PhpUnit\Tests\ExpectDeprecationTraitTest::testOneInIsolation This test did not perform any assertions /Users/alex/dev/symfony/src/Symfony/Bridge/PhpUnit/Tests/ExpectDeprecationTraitTest.php:38 2) Symfony\Bridge\PhpUnit\Tests\ExpectDeprecationTraitTest::testOneInIsolation This test did not perform any assertions OK, but incomplete, skipped, or risky tests! Tests: 1, Assertions: 0, Risky: 2. ``` Commits ------- e7e2ee7 Expect deprecations in isolation
2 parents f07b187 + e7e2ee7 commit 5992bf0

File tree

7 files changed

+141
-0
lines changed

7 files changed

+141
-0
lines changed

src/Symfony/Bridge/PhpUnit/Legacy/ExpectDeprecationTraitBeforeV8_4.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,21 @@ trait ExpectDeprecationTraitBeforeV8_4
2323
*/
2424
protected function expectDeprecation($message)
2525
{
26+
// Expected deprecations set by isolated tests need to be written to a file
27+
// so that the test running process can take account of them.
28+
if ($file = getenv('SYMFONY_EXPECTED_DEPRECATIONS_SERIALIZE')) {
29+
$this->getTestResultObject()->beStrictAboutTestsThatDoNotTestAnything(false);
30+
$expectedDeprecations = file_get_contents($file);
31+
if ($expectedDeprecations) {
32+
$expectedDeprecations = array_merge(unserialize($expectedDeprecations), [$message]);
33+
} else {
34+
$expectedDeprecations = [$message];
35+
}
36+
file_put_contents($file, serialize($expectedDeprecations));
37+
38+
return;
39+
}
40+
2641
if (!SymfonyTestsListenerTrait::$previousErrorHandler) {
2742
SymfonyTestsListenerTrait::$previousErrorHandler = set_error_handler([SymfonyTestsListenerTrait::class, 'handleError']);
2843
}

src/Symfony/Bridge/PhpUnit/Legacy/ExpectDeprecationTraitForV8_4.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,21 @@ public function expectDeprecation(): void
2525
throw new \InvalidArgumentException(sprintf('The "%s()" method requires the string $message argument.', __FUNCTION__));
2626
}
2727

28+
// Expected deprecations set by isolated tests need to be written to a file
29+
// so that the test running process can take account of them.
30+
if ($file = getenv('SYMFONY_EXPECTED_DEPRECATIONS_SERIALIZE')) {
31+
$this->getTestResultObject()->beStrictAboutTestsThatDoNotTestAnything(false);
32+
$expectedDeprecations = file_get_contents($file);
33+
if ($expectedDeprecations) {
34+
$expectedDeprecations = array_merge(unserialize($expectedDeprecations), [$message]);
35+
} else {
36+
$expectedDeprecations = [$message];
37+
}
38+
file_put_contents($file, serialize($expectedDeprecations));
39+
40+
return;
41+
}
42+
2843
if (!SymfonyTestsListenerTrait::$previousErrorHandler) {
2944
SymfonyTestsListenerTrait::$previousErrorHandler = set_error_handler([SymfonyTestsListenerTrait::class, 'handleError']);
3045
}

src/Symfony/Bridge/PhpUnit/Legacy/SymfonyTestsListenerTrait.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ public function startTest($test)
208208
if ($this->willBeIsolated($test)) {
209209
$this->runsInSeparateProcess = tempnam(sys_get_temp_dir(), 'deprec');
210210
putenv('SYMFONY_DEPRECATIONS_SERIALIZE='.$this->runsInSeparateProcess);
211+
putenv('SYMFONY_EXPECTED_DEPRECATIONS_SERIALIZE='.tempnam(sys_get_temp_dir(), 'expectdeprec'));
211212
}
212213

213214
$groups = Test::getGroups(\get_class($test), $test->getName(false));
@@ -249,6 +250,17 @@ public function startTest($test)
249250

250251
public function endTest($test, $time)
251252
{
253+
if ($file = getenv('SYMFONY_EXPECTED_DEPRECATIONS_SERIALIZE')) {
254+
putenv('SYMFONY_EXPECTED_DEPRECATIONS_SERIALIZE');
255+
$expectedDeprecations = file_get_contents($file);
256+
if ($expectedDeprecations) {
257+
self::$expectedDeprecations = array_merge(self::$expectedDeprecations, unserialize($expectedDeprecations));
258+
if (!self::$previousErrorHandler) {
259+
self::$previousErrorHandler = set_error_handler([self::class, 'handleError']);
260+
}
261+
}
262+
}
263+
252264
if (class_exists(DebugClassLoader::class, false)) {
253265
DebugClassLoader::checkClasses();
254266
}

src/Symfony/Bridge/PhpUnit/Tests/ExpectDeprecationTraitTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@ public function testOne()
2929
@trigger_error('foo', E_USER_DEPRECATED);
3030
}
3131

32+
/**
33+
* Do not remove this test in the next major version.
34+
*
35+
* @group legacy
36+
* @runInSeparateProcess
37+
*/
38+
public function testOneInIsolation()
39+
{
40+
$this->expectDeprecation('foo');
41+
@trigger_error('foo', E_USER_DEPRECATED);
42+
}
43+
3244
/**
3345
* Do not remove this test in the next major version.
3446
*
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\PhpUnit\Tests\FailTests;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
16+
17+
/**
18+
* Class ExpectDeprecationTraitTestFail.
19+
*
20+
* This class is deliberately suffixed with *TestFail.php so that it is ignored
21+
* by PHPUnit. This test is designed to fail. See ../expectdeprecationfail.phpt.
22+
*/
23+
final class ExpectDeprecationTraitTestFail extends TestCase
24+
{
25+
use ExpectDeprecationTrait;
26+
27+
/**
28+
* Do not remove this test in the next major version.
29+
*
30+
* @group legacy
31+
*/
32+
public function testOne()
33+
{
34+
$this->expectDeprecation('foo');
35+
@trigger_error('bar', E_USER_DEPRECATED);
36+
}
37+
38+
/**
39+
* Do not remove this test in the next major version.
40+
*
41+
* @group legacy
42+
* @runInSeparateProcess
43+
*/
44+
public function testOneInIsolation()
45+
{
46+
$this->expectDeprecation('foo');
47+
@trigger_error('bar', E_USER_DEPRECATED);
48+
}
49+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
--TEST--
2+
Test ExpectDeprecationTrait failing tests
3+
--FILE--
4+
<?php
5+
$test = realpath(__DIR__ . '/FailTests/ExpectDeprecationTraitTestFail.php');
6+
passthru(getenv('SYMFONY_SIMPLE_PHPUNIT_BIN_DIR') . '/simple-phpunit --colors=never ' . $test);
7+
?>
8+
--EXPECTF--
9+
PHPUnit %s by Sebastian Bergmann and contributors.
10+
11+
Testing Symfony\Bridge\PhpUnit\Tests\FailTests\ExpectDeprecationTraitTestFail
12+
FF 2 / 2 (100%)
13+
14+
Time: %s, Memory: %s
15+
16+
There were 2 failures:
17+
18+
1) Symfony\Bridge\PhpUnit\Tests\FailTests\ExpectDeprecationTraitTestFail::testOne
19+
Failed asserting that string matches format description.
20+
--- Expected
21+
+++ Actual
22+
@@ @@
23+
@expectedDeprecation:
24+
-%A foo
25+
+ bar
26+
27+
2) Symfony\Bridge\PhpUnit\Tests\FailTests\ExpectDeprecationTraitTestFail::testOneInIsolation
28+
Failed asserting that string matches format description.
29+
--- Expected
30+
+++ Actual
31+
@@ @@
32+
@expectedDeprecation:
33+
-%A foo
34+
+ bar
35+
36+
FAILURES!
37+
Tests: 2, Assertions: 2, Failures: 2.

src/Symfony/Bridge/PhpUnit/bin/simple-phpunit.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@
134134
'COMPOSER' => 'composer.json',
135135
'COMPOSER_VENDOR_DIR' => 'vendor',
136136
'COMPOSER_BIN_DIR' => 'bin',
137+
'SYMFONY_SIMPLE_PHPUNIT_BIN_DIR' => __DIR__,
137138
];
138139

139140
foreach ($defaultEnvs as $envName => $envValue) {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy