Skip to content

Commit e7e2ee7

Browse files
alexpottnicolas-grekas
authored andcommitted
Expect deprecations in isolation
1 parent ce0a9d2 commit e7e2ee7

File tree

7 files changed

+142
-1
lines changed

7 files changed

+142
-1
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
@@ -204,6 +204,7 @@ public function startTest($test)
204204
if ($this->willBeIsolated($test)) {
205205
$this->runsInSeparateProcess = tempnam(sys_get_temp_dir(), 'deprec');
206206
putenv('SYMFONY_DEPRECATIONS_SERIALIZE='.$this->runsInSeparateProcess);
207+
putenv('SYMFONY_EXPECTED_DEPRECATIONS_SERIALIZE='.tempnam(sys_get_temp_dir(), 'expectdeprec'));
207208
}
208209

209210
$groups = Test::getGroups(\get_class($test), $test->getName(false));
@@ -245,6 +246,17 @@ public function startTest($test)
245246

246247
public function endTest($test, $time)
247248
{
249+
if ($file = getenv('SYMFONY_EXPECTED_DEPRECATIONS_SERIALIZE')) {
250+
putenv('SYMFONY_EXPECTED_DEPRECATIONS_SERIALIZE');
251+
$expectedDeprecations = file_get_contents($file);
252+
if ($expectedDeprecations) {
253+
self::$expectedDeprecations = array_merge(self::$expectedDeprecations, unserialize($expectedDeprecations));
254+
if (!self::$previousErrorHandler) {
255+
self::$previousErrorHandler = set_error_handler([self::class, 'handleError']);
256+
}
257+
}
258+
}
259+
248260
if (class_exists(DebugClassLoader::class, false)) {
249261
DebugClassLoader::checkClasses();
250262
}

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: 2 additions & 1 deletion
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) {
@@ -193,7 +194,7 @@
193194
'requires' => ['php' => '*'],
194195
];
195196

196-
if (1 === \count($info['versions'])) {
197+
if (1 === count($info['versions'])) {
197198
$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.*\"");
198199
} else {
199200
$passthruOrFail("$COMPOSER create-project --ignore-platform-reqs --no-install --prefer-dist --no-scripts --no-plugins --no-progress phpunit/phpunit $PHPUNIT_VERSION_DIR \"$PHPUNIT_VERSION.*\"");

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