File tree Expand file tree Collapse file tree 6 files changed +33
-27
lines changed Expand file tree Collapse file tree 6 files changed +33
-27
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 18
18
use PHPUnit \Framework \Constraint \IsEqual ;
19
19
use PHPUnit \Framework \ExpectationFailedException ;
20
20
use PHPUnit \Framework \MockObject \Invocation as BaseInvocation ;
21
+ use PHPUnit \Util \Test ;
21
22
22
23
/**
23
24
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
@@ -105,6 +106,8 @@ private function doVerify(): bool
105
106
$ message .= "\nTo allow 0 or more parameters with any value, omit ->with() or use ->withAnyParameters() instead. " ;
106
107
}
107
108
109
+ $ this ->incrementAssertionCount ();
110
+
108
111
throw new ExpectationFailedException (
109
112
sprintf ($ message , $ this ->invocation ->toString ()),
110
113
);
@@ -117,6 +120,8 @@ private function doVerify(): bool
117
120
$ other = $ this ->invocation ->parameters ()[$ i ];
118
121
}
119
122
123
+ $ this ->incrementAssertionCount ();
124
+
120
125
$ parameter ->evaluate (
121
126
$ other ,
122
127
sprintf (
@@ -141,4 +146,9 @@ private function guardAgainstDuplicateEvaluationOfParameterConstraints(): bool
141
146
142
147
return (bool ) $ this ->parameterVerificationResult ;
143
148
}
149
+
150
+ private function incrementAssertionCount (): void
151
+ {
152
+ Test::currentTestCase ()->addToAssertionCount (1 );
153
+ }
144
154
}
Original file line number Diff line number Diff line change @@ -177,21 +177,6 @@ public function run(TestCase $test): void
177
177
178
178
ErrorHandler::instance ()->disable ();
179
179
180
- /**
181
- * Workaround for tests that fail due to mock object expectations
182
- * that are verified while the test is running and not after the
183
- * test has finished running.
184
- *
185
- * @see https://github.com/sebastianbergmann/phpunit/issues/6138
186
- */
187
- if ($ failure &&
188
- !$ error &&
189
- !$ incomplete &&
190
- !$ skipped &&
191
- $ test ->numberOfAssertionsPerformed () === 0 ) {
192
- $ test ->addToAssertionCount (1 );
193
- }
194
-
195
180
if (!$ error &&
196
181
!$ incomplete &&
197
182
!$ skipped &&
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
Original file line number Diff line number Diff line change @@ -17,4 +17,4 @@ Runtime: %s
17
17
18
18
Time: %s, Memory: %s MB
19
19
20
- OK (2 tests, 2 assertions)
20
+ OK (2 tests, 4 assertions)
You can’t perform that action at this time.
0 commit comments