Skip to content

Commit 7aed961

Browse files
Refactor
1 parent 15d73c4 commit 7aed961

File tree

3 files changed

+22
-11
lines changed

3 files changed

+22
-11
lines changed

src/Event/Value/Test/TestMethodBuilder.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@
99
*/
1010
namespace PHPUnit\Event\Code;
1111

12-
use const DEBUG_BACKTRACE_IGNORE_ARGS;
13-
use const DEBUG_BACKTRACE_PROVIDE_OBJECT;
1412
use function assert;
15-
use function debug_backtrace;
1613
use function is_numeric;
1714
use PHPUnit\Event\TestData\DataFromDataProvider;
1815
use PHPUnit\Event\TestData\DataFromTestDependency;
@@ -21,6 +18,7 @@
2118
use PHPUnit\Metadata\Parser\Registry as MetadataRegistry;
2219
use PHPUnit\Util\Exporter;
2320
use PHPUnit\Util\Reflection;
21+
use PHPUnit\Util\Test as TestUtil;
2422

2523
/**
2624
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
@@ -53,13 +51,7 @@ public static function fromTestCase(TestCase $testCase): TestMethod
5351
*/
5452
public static function fromCallStack(): TestMethod
5553
{
56-
foreach (debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT | DEBUG_BACKTRACE_IGNORE_ARGS) as $frame) {
57-
if (isset($frame['object']) && $frame['object'] instanceof TestCase) {
58-
return $frame['object']->valueObjectForEvents();
59-
}
60-
}
61-
62-
throw new NoTestCaseObjectOnCallStackException;
54+
return TestUtil::currentTestCase()->valueObjectForEvents();
6355
}
6456

6557
private static function dataFor(TestCase $testCase): TestDataCollection

src/Util/Test.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@
99
*/
1010
namespace PHPUnit\Util;
1111

12+
use const DEBUG_BACKTRACE_IGNORE_ARGS;
13+
use const DEBUG_BACKTRACE_PROVIDE_OBJECT;
14+
use function debug_backtrace;
1215
use function str_starts_with;
16+
use PHPUnit\Event\Code\NoTestCaseObjectOnCallStackException;
17+
use PHPUnit\Framework\TestCase;
1318
use PHPUnit\Metadata\Parser\Registry;
1419
use ReflectionMethod;
1520

@@ -20,6 +25,20 @@
2025
*/
2126
final readonly class Test
2227
{
28+
/**
29+
* @throws NoTestCaseObjectOnCallStackException
30+
*/
31+
public static function currentTestCase(): TestCase
32+
{
33+
foreach (debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT | DEBUG_BACKTRACE_IGNORE_ARGS) as $frame) {
34+
if (isset($frame['object']) && $frame['object'] instanceof TestCase) {
35+
return $frame['object'];
36+
}
37+
}
38+
39+
throw new NoTestCaseObjectOnCallStackException;
40+
}
41+
2342
public static function isTestMethod(ReflectionMethod $method): bool
2443
{
2544
if (!$method->isPublic()) {

tests/end-to-end/event/test-method-builder-cannot-find-testcase-object.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ require __DIR__ . '/../../bootstrap.php';
1010

1111
\PHPUnit\Event\Code\TestMethodBuilder::fromCallStack();
1212
--EXPECTF--
13-
Fatal error: Uncaught PHPUnit\Event\Code\NoTestCaseObjectOnCallStackException: Cannot find TestCase object on call stack in %sTestMethodBuilder.php:%d
13+
Fatal error: Uncaught PHPUnit\Event\Code\NoTestCaseObjectOnCallStackException: Cannot find TestCase object on call stack in %s:%d
1414
Stack trace:
1515
%a

0 commit comments

Comments
 (0)