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 9
9
*/
10
10
namespace PHPUnit \Event \Code ;
11
11
12
- use const DEBUG_BACKTRACE_IGNORE_ARGS ;
13
- use const DEBUG_BACKTRACE_PROVIDE_OBJECT ;
14
12
use function assert ;
15
- use function debug_backtrace ;
16
13
use function is_numeric ;
17
14
use PHPUnit \Event \TestData \DataFromDataProvider ;
18
15
use PHPUnit \Event \TestData \DataFromTestDependency ;
21
18
use PHPUnit \Metadata \Parser \Registry as MetadataRegistry ;
22
19
use PHPUnit \Util \Exporter ;
23
20
use PHPUnit \Util \Reflection ;
21
+ use PHPUnit \Util \Test as TestUtil ;
24
22
25
23
/**
26
24
* @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
53
51
*/
54
52
public static function fromCallStack (): TestMethod
55
53
{
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 ();
63
55
}
64
56
65
57
private static function dataFor (TestCase $ testCase ): TestDataCollection
Original file line number Diff line number Diff line change 9
9
*/
10
10
namespace PHPUnit \Util ;
11
11
12
+ use const DEBUG_BACKTRACE_IGNORE_ARGS ;
13
+ use const DEBUG_BACKTRACE_PROVIDE_OBJECT ;
14
+ use function debug_backtrace ;
12
15
use function str_starts_with ;
16
+ use PHPUnit \Event \Code \NoTestCaseObjectOnCallStackException ;
17
+ use PHPUnit \Framework \TestCase ;
13
18
use PHPUnit \Metadata \Parser \Registry ;
14
19
use ReflectionMethod ;
15
20
20
25
*/
21
26
final readonly class Test
22
27
{
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
+
23
42
public static function isTestMethod (ReflectionMethod $ method ): bool
24
43
{
25
44
if (!$ method ->isPublic ()) {
Original file line number Diff line number Diff line change @@ -10,6 +10,6 @@ require __DIR__ . '/../../bootstrap.php';
10
10
11
11
\PHPUnit \Event \Code \TestMethodBuilder::fromCallStack ();
12
12
--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
14
14
Stack trace:
15
15
%a
You can’t perform that action at this time.
0 commit comments