Skip to content

Commit 83f6449

Browse files
staabmsebastianbergmann
authored andcommitted
Relate deprecations from DataProvider with corresponding tests
1 parent c1c1438 commit 83f6449

File tree

5 files changed

+111
-6
lines changed

5 files changed

+111
-6
lines changed

src/Framework/TestBuilder.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use PHPUnit\Metadata\ExcludeStaticPropertyFromBackup;
2222
use PHPUnit\Metadata\Parser\Registry as MetadataRegistry;
2323
use PHPUnit\Metadata\PreserveGlobalState;
24+
use PHPUnit\Runner\ErrorHandler;
2425
use PHPUnit\TextUI\Configuration\Registry as ConfigurationRegistry;
2526
use ReflectionClass;
2627

@@ -45,7 +46,12 @@ public function build(ReflectionClass $theClass, string $methodName, array $grou
4546
$data = null;
4647

4748
if ($this->requirementsSatisfied($className, $methodName)) {
48-
$data = (new DataProvider)->providedData($className, $methodName);
49+
try {
50+
ErrorHandler::instance()->enterTestCaseContext($methodName);
51+
$data = (new DataProvider)->providedData($className, $methodName);
52+
} finally {
53+
ErrorHandler::instance()->leaveTestCaseContext();
54+
}
4955
}
5056

5157
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: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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. 3 / 3 (100%)
18+
19+
Time: %s, Memory: %s
20+
21+
1 test triggered 1 deprecation:
22+
23+
1) %s/end-to-end/regression/6279/TriggersDeprecationInDataProviderTest.php:24
24+
some deprecation
25+
26+
OK, but there were issues!
27+
Tests: 3, Assertions: 3, Deprecations: 1.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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 function trigger_error;
16+
use PHPUnit\Framework\Attributes\DataProvider;
17+
use PHPUnit\Framework\Attributes\Test;
18+
use PHPUnit\Framework\TestCase;
19+
20+
class TriggersDeprecationInDataProviderTest extends TestCase
21+
{
22+
public static function dataProvider(): iterable
23+
{
24+
@trigger_error('some deprecation', E_USER_DEPRECATED);
25+
26+
yield [true];
27+
}
28+
29+
#[Test]
30+
public function method1(): void
31+
{
32+
$this->assertTrue(true);
33+
}
34+
35+
#[Test]
36+
#[DataProvider('dataProvider')]
37+
public function method2(bool $value): void
38+
{
39+
$this->assertTrue($value);
40+
}
41+
42+
#[Test]
43+
public function method3(): void
44+
{
45+
$this->assertTrue(true);
46+
}
47+
}

0 commit comments

Comments
 (0)