Skip to content

Commit ee70301

Browse files
Merge branch '12.3'
2 parents b57b0b5 + 9b15def commit ee70301

File tree

5 files changed

+151
-6
lines changed

5 files changed

+151
-6
lines changed

src/Framework/TestBuilder.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use PHPUnit\Metadata\ExcludeStaticPropertyFromBackup;
2424
use PHPUnit\Metadata\Parser\Registry as MetadataRegistry;
2525
use PHPUnit\Metadata\PreserveGlobalState;
26+
use PHPUnit\Runner\ErrorHandler;
2627
use PHPUnit\TextUI\Configuration\Registry as ConfigurationRegistry;
2728
use ReflectionClass;
2829

@@ -47,7 +48,13 @@ public function build(ReflectionClass $theClass, string $methodName, array $grou
4748
$data = null;
4849

4950
if ($this->requirementsSatisfied($className, $methodName)) {
50-
$data = (new DataProvider)->providedData($className, $methodName);
51+
try {
52+
ErrorHandler::instance()->enterTestCaseContext($methodName);
53+
54+
$data = (new DataProvider)->providedData($className, $methodName);
55+
} finally {
56+
ErrorHandler::instance()->leaveTestCaseContext();
57+
}
5158
}
5259

5360
if ($data !== null) {

src/Framework/TestRunner/TestRunner.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public function run(TestCase $test): void
8181
$skipped = false;
8282

8383
if ($this->shouldErrorHandlerBeUsed($test)) {
84-
ErrorHandler::instance()->enable();
84+
ErrorHandler::instance()->enable($test);
8585
}
8686

8787
$collectCodeCoverage = CodeCoverage::instance()->isActive() &&

src/Runner/ErrorHandler.php

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
use PHPUnit\Event\Code\IssueTrigger\IssueTrigger;
3737
use PHPUnit\Event\Code\NoTestCaseObjectOnCallStackException;
3838
use PHPUnit\Event\Code\TestMethod;
39+
use PHPUnit\Framework\TestCase;
3940
use PHPUnit\Runner\Baseline\Baseline;
4041
use PHPUnit\Runner\Baseline\Issue;
4142
use PHPUnit\TextUI\Configuration\Registry;
@@ -63,6 +64,12 @@ final class ErrorHandler
6364
*/
6465
private array $globalDeprecations = [];
6566

67+
/**
68+
* @var array<string, list<array{int, string, string, int}>>
69+
*/
70+
private array $testCaseContextDeprecations = [];
71+
private ?string $testCaseContext = null;
72+
6673
/**
6774
* @var ?array{functions: list<non-empty-string>, methods: list<array{className: class-string, methodName: non-empty-string}>}
6875
*/
@@ -207,7 +214,11 @@ public function __invoke(int $errorNumber, string $errorString, string $errorFil
207214

208215
public function deprecationHandler(int $errorNumber, string $errorString, string $errorFile, int $errorLine): bool
209216
{
210-
$this->globalDeprecations[] = [$errorNumber, $errorString, $errorFile, $errorLine];
217+
if ($this->testCaseContext !== null) {
218+
$this->testCaseContextDeprecations[$this->testCaseContext][] = [$errorNumber, $errorString, $errorFile, $errorLine];
219+
} else {
220+
$this->globalDeprecations[] = [$errorNumber, $errorString, $errorFile, $errorLine];
221+
}
211222

212223
return true;
213224
}
@@ -222,7 +233,7 @@ public function restoreDeprecationHandler(): void
222233
restore_error_handler();
223234
}
224235

225-
public function enable(): void
236+
public function enable(TestCase $test): void
226237
{
227238
if ($this->enabled) {
228239
return;
@@ -239,7 +250,7 @@ public function enable(): void
239250
$this->enabled = true;
240251
$this->originalErrorReportingLevel = error_reporting();
241252

242-
$this->triggerGlobalDeprecations();
253+
$this->triggerGlobalDeprecations($test);
243254

244255
error_reporting($this->originalErrorReportingLevel & self::UNHANDLEABLE_LEVELS);
245256
}
@@ -271,6 +282,16 @@ public function useDeprecationTriggers(array $deprecationTriggers): void
271282
$this->deprecationTriggers = $deprecationTriggers;
272283
}
273284

285+
public function enterTestCaseContext(string $methodName): void
286+
{
287+
$this->testCaseContext = $methodName;
288+
}
289+
290+
public function leaveTestCaseContext(): void
291+
{
292+
$this->testCaseContext = null;
293+
}
294+
274295
/**
275296
* @param non-empty-string $file
276297
* @param positive-int $line
@@ -450,10 +471,14 @@ private function stackTrace(): string
450471
return $buffer;
451472
}
452473

453-
private function triggerGlobalDeprecations(): void
474+
private function triggerGlobalDeprecations(TestCase $test): void
454475
{
455476
foreach ($this->globalDeprecations ?? [] as $d) {
456477
$this->__invoke(...$d);
457478
}
479+
480+
foreach ($this->testCaseContextDeprecations[$test->name()] ?? [] as $d) {
481+
$this->__invoke(...$d);
482+
}
458483
}
459484
}

tests/end-to-end/regression/6279.phpt

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
--TEST--
2+
https://github.com/sebastianbergmann/phpunit/issues/6279
3+
--FILE--
4+
<?php declare(strict_types=1);
5+
$_SERVER['argv'][] = '--do-not-cache-result';
6+
$_SERVER['argv'][] = '--no-configuration';
7+
$_SERVER['argv'][] = '--display-deprecations';
8+
$_SERVER['argv'][] = __DIR__ . '/6279/TriggersDeprecationInDataProviderTest.php';
9+
10+
require_once __DIR__ . '/../../bootstrap.php';
11+
(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
12+
--EXPECTF--
13+
PHPUnit %s by Sebastian Bergmann and contributors.
14+
15+
Runtime: %s
16+
17+
.D.DD 5 / 5 (100%)
18+
19+
Time: %s, Memory: %s
20+
21+
3 tests triggered 3 deprecations:
22+
23+
1) %sTriggersDeprecationInDataProviderTest.php:26
24+
some deprecation
25+
26+
Triggered by:
27+
28+
* PHPUnit\TestFixture\Issue6279\TriggersDeprecationInDataProviderTest::method2#0
29+
%sTriggersDeprecationInDataProviderTest.php:48
30+
31+
* PHPUnit\TestFixture\Issue6279\TriggersDeprecationInDataProviderTest::method4#0
32+
%sTriggersDeprecationInDataProviderTest.php:61
33+
34+
2) %sTriggersDeprecationInDataProviderTest.php:33
35+
first
36+
37+
3) %sTriggersDeprecationInDataProviderTest.php:34
38+
second
39+
40+
OK, but there were issues!
41+
Tests: 5, Assertions: 5, Deprecations: 3.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/*
5+
* This file is part of PHPUnit.
6+
*
7+
* (c) Sebastian Bergmann <[email protected]>
8+
*
9+
* For the full copyright and license information, please view the LICENSE
10+
* file that was distributed with this source code.
11+
*/
12+
namespace PHPUnit\TestFixture\Issue6279;
13+
14+
use const E_USER_DEPRECATED;
15+
use const E_USER_WARNING;
16+
use function trigger_error;
17+
use PHPUnit\Framework\Attributes\DataProvider;
18+
use PHPUnit\Framework\Attributes\DataProviderExternal;
19+
use PHPUnit\Framework\Attributes\Test;
20+
use PHPUnit\Framework\TestCase;
21+
22+
class TriggersDeprecationInDataProviderTest extends TestCase
23+
{
24+
public static function dataProvider(): iterable
25+
{
26+
@trigger_error('some deprecation', E_USER_DEPRECATED);
27+
28+
yield [true];
29+
}
30+
31+
public static function dataWith2Deprecations(): iterable
32+
{
33+
@trigger_error('first', E_USER_DEPRECATED);
34+
@trigger_error('second', E_USER_DEPRECATED);
35+
@trigger_error('warning', E_USER_WARNING);
36+
37+
yield [true];
38+
}
39+
40+
#[Test]
41+
public function method1(): void
42+
{
43+
$this->assertTrue(true);
44+
}
45+
46+
#[Test]
47+
#[DataProvider('dataProvider')]
48+
public function method2(bool $value): void
49+
{
50+
$this->assertTrue($value);
51+
}
52+
53+
#[Test]
54+
public function method3(): void
55+
{
56+
$this->assertTrue(true);
57+
}
58+
59+
#[Test]
60+
#[DataProvider('dataProvider')]
61+
public function method4(bool $value): void
62+
{
63+
$this->assertTrue($value);
64+
}
65+
66+
#[Test]
67+
#[DataProviderExternal(self::class, 'dataWith2Deprecations')]
68+
public function method5(bool $value): void
69+
{
70+
$this->assertTrue($value);
71+
}
72+
}

0 commit comments

Comments
 (0)