File tree Expand file tree Collapse file tree 3 files changed +22
-11
lines changed
Expand file tree Collapse file tree 3 files changed +22
-11
lines changed Original file line number Diff line number Diff line change 99 */
1010namespace PHPUnit \Event \Code ;
1111
12- use const DEBUG_BACKTRACE_IGNORE_ARGS ;
13- use const DEBUG_BACKTRACE_PROVIDE_OBJECT ;
1412use function assert ;
15- use function debug_backtrace ;
1613use function is_numeric ;
1714use PHPUnit \Event \TestData \DataFromDataProvider ;
1815use PHPUnit \Event \TestData \DataFromTestDependency ;
2118use PHPUnit \Metadata \Parser \Registry as MetadataRegistry ;
2219use PHPUnit \Util \Exporter ;
2320use 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
Original file line number Diff line number Diff line change 99 */
1010namespace PHPUnit \Util ;
1111
12+ use const DEBUG_BACKTRACE_IGNORE_ARGS ;
13+ use const DEBUG_BACKTRACE_PROVIDE_OBJECT ;
14+ use function debug_backtrace ;
1215use function str_starts_with ;
16+ use PHPUnit \Event \Code \NoTestCaseObjectOnCallStackException ;
17+ use PHPUnit \Framework \TestCase ;
1318use PHPUnit \Metadata \Parser \Registry ;
1419use ReflectionMethod ;
1520
2025 */
2126final 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 ()) {
Original file line number Diff line number Diff 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
1414Stack trace:
1515%a
You can’t perform that action at this time.
0 commit comments