Skip to content

Commit ea80433

Browse files
Merge branch '11.5' into 12.0
2 parents fc976c5 + 49dcba0 commit ea80433

File tree

6 files changed

+33
-27
lines changed

6 files changed

+33
-27
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/Framework/MockObject/Runtime/Rule/Parameters.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use PHPUnit\Framework\Constraint\IsEqual;
1919
use PHPUnit\Framework\ExpectationFailedException;
2020
use PHPUnit\Framework\MockObject\Invocation as BaseInvocation;
21+
use PHPUnit\Util\Test;
2122

2223
/**
2324
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
@@ -105,6 +106,8 @@ private function doVerify(): bool
105106
$message .= "\nTo allow 0 or more parameters with any value, omit ->with() or use ->withAnyParameters() instead.";
106107
}
107108

109+
$this->incrementAssertionCount();
110+
108111
throw new ExpectationFailedException(
109112
sprintf($message, $this->invocation->toString()),
110113
);
@@ -117,6 +120,8 @@ private function doVerify(): bool
117120
$other = $this->invocation->parameters()[$i];
118121
}
119122

123+
$this->incrementAssertionCount();
124+
120125
$parameter->evaluate(
121126
$other,
122127
sprintf(
@@ -141,4 +146,9 @@ private function guardAgainstDuplicateEvaluationOfParameterConstraints(): bool
141146

142147
return (bool) $this->parameterVerificationResult;
143148
}
149+
150+
private function incrementAssertionCount(): void
151+
{
152+
Test::currentTestCase()->addToAssertionCount(1);
153+
}
144154
}

src/Framework/TestRunner/TestRunner.php

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -177,21 +177,6 @@ public function run(TestCase $test): void
177177

178178
ErrorHandler::instance()->disable();
179179

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-
195180
if (!$error &&
196181
!$incomplete &&
197182
!$skipped &&

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

tests/end-to-end/regression/5891.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ Runtime: %s
1717

1818
Time: %s, Memory: %s MB
1919

20-
OK (2 tests, 2 assertions)
20+
OK (2 tests, 4 assertions)

0 commit comments

Comments
 (0)