Skip to content

Commit

Permalink
Merge branch '11.5' into 12.0
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Feb 25, 2025
2 parents fc976c5 + 49dcba0 commit ea80433
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 27 deletions.
12 changes: 2 additions & 10 deletions src/Event/Value/Test/TestMethodBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
*/
namespace PHPUnit\Event\Code;

use const DEBUG_BACKTRACE_IGNORE_ARGS;
use const DEBUG_BACKTRACE_PROVIDE_OBJECT;
use function assert;
use function debug_backtrace;
use function is_numeric;
use PHPUnit\Event\TestData\DataFromDataProvider;
use PHPUnit\Event\TestData\DataFromTestDependency;
Expand All @@ -21,6 +18,7 @@
use PHPUnit\Metadata\Parser\Registry as MetadataRegistry;
use PHPUnit\Util\Exporter;
use PHPUnit\Util\Reflection;
use PHPUnit\Util\Test as TestUtil;

/**
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
Expand Down Expand Up @@ -53,13 +51,7 @@ public static function fromTestCase(TestCase $testCase): TestMethod
*/
public static function fromCallStack(): TestMethod
{
foreach (debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT | DEBUG_BACKTRACE_IGNORE_ARGS) as $frame) {
if (isset($frame['object']) && $frame['object'] instanceof TestCase) {
return $frame['object']->valueObjectForEvents();
}
}

throw new NoTestCaseObjectOnCallStackException;
return TestUtil::currentTestCase()->valueObjectForEvents();
}

private static function dataFor(TestCase $testCase): TestDataCollection
Expand Down
10 changes: 10 additions & 0 deletions src/Framework/MockObject/Runtime/Rule/Parameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use PHPUnit\Framework\Constraint\IsEqual;
use PHPUnit\Framework\ExpectationFailedException;
use PHPUnit\Framework\MockObject\Invocation as BaseInvocation;
use PHPUnit\Util\Test;

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

$this->incrementAssertionCount();

throw new ExpectationFailedException(
sprintf($message, $this->invocation->toString()),
);
Expand All @@ -117,6 +120,8 @@ private function doVerify(): bool
$other = $this->invocation->parameters()[$i];
}

$this->incrementAssertionCount();

$parameter->evaluate(
$other,
sprintf(
Expand All @@ -141,4 +146,9 @@ private function guardAgainstDuplicateEvaluationOfParameterConstraints(): bool

return (bool) $this->parameterVerificationResult;
}

private function incrementAssertionCount(): void
{
Test::currentTestCase()->addToAssertionCount(1);
}
}
15 changes: 0 additions & 15 deletions src/Framework/TestRunner/TestRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,21 +177,6 @@ public function run(TestCase $test): void

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

/**
* Workaround for tests that fail due to mock object expectations
* that are verified while the test is running and not after the
* test has finished running.
*
* @see https://github.com/sebastianbergmann/phpunit/issues/6138
*/
if ($failure &&
!$error &&
!$incomplete &&
!$skipped &&
$test->numberOfAssertionsPerformed() === 0) {
$test->addToAssertionCount(1);
}

if (!$error &&
!$incomplete &&
!$skipped &&
Expand Down
19 changes: 19 additions & 0 deletions src/Util/Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
*/
namespace PHPUnit\Util;

use const DEBUG_BACKTRACE_IGNORE_ARGS;
use const DEBUG_BACKTRACE_PROVIDE_OBJECT;
use function debug_backtrace;
use function str_starts_with;
use PHPUnit\Event\Code\NoTestCaseObjectOnCallStackException;
use PHPUnit\Framework\TestCase;
use PHPUnit\Metadata\Parser\Registry;
use ReflectionMethod;

Expand All @@ -20,6 +25,20 @@
*/
final readonly class Test
{
/**
* @throws NoTestCaseObjectOnCallStackException
*/
public static function currentTestCase(): TestCase
{
foreach (debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT | DEBUG_BACKTRACE_IGNORE_ARGS) as $frame) {
if (isset($frame['object']) && $frame['object'] instanceof TestCase) {
return $frame['object'];
}
}

throw new NoTestCaseObjectOnCallStackException;
}

public static function isTestMethod(ReflectionMethod $method): bool
{
if (!$method->isPublic()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ require __DIR__ . '/../../bootstrap.php';

\PHPUnit\Event\Code\TestMethodBuilder::fromCallStack();
--EXPECTF--
Fatal error: Uncaught PHPUnit\Event\Code\NoTestCaseObjectOnCallStackException: Cannot find TestCase object on call stack in %sTestMethodBuilder.php:%d
Fatal error: Uncaught PHPUnit\Event\Code\NoTestCaseObjectOnCallStackException: Cannot find TestCase object on call stack in %s:%d
Stack trace:
%a
2 changes: 1 addition & 1 deletion tests/end-to-end/regression/5891.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ Runtime: %s

Time: %s, Memory: %s MB

OK (2 tests, 2 assertions)
OK (2 tests, 4 assertions)

0 comments on commit ea80433

Please sign in to comment.