Skip to content

Commit c01ddf3

Browse files
committed
fix #135 assertion counter not increased when expectation on thrown exception not met
1 parent 83d3dcf commit c01ddf3

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 8.1.2 (2025-11-30)
4+
5+
* Fixed #135 assertion counter not increased when expectation on thrown exception not met
6+
37
## 8.1.1 (2025-11-29)
48

59
* Ensured compatibility with sebastian/comparator 7.1, sebastian/exporter 7.0 and phpunit/phpunit 12.4 while keeping compatibility with their predecessor versions 6.2, 6.3 and 11.5

src/main/php/Expectation.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public function throws(string|Throwable|null $expected = null): CaughtThrowable
6565
{
6666
$this->runCode();
6767
if (null === $this->exception) {
68+
increaseAssertionCounter(1);
6869
throw new AssertionFailure(
6970
'Failed asserting that '
7071
. (null !== $expected

src/test/php/ExpectationTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use TypeError;
2222

2323
use function bovigo\assert\predicate\{
24+
equals,
2425
isFalse,
2526
isInstanceOf,
2627
isSameAs,
@@ -438,4 +439,25 @@ public function outputOfUnexpectedErrorIsHelpful(): void
438439
. ' on line ' . $line . ' matches expected error "E_USER_NOTICE".'
439440
);
440441
}
442+
443+
#[Test]
444+
#[Group('issue_135')]
445+
public function assertionCounterIsIncreasedWhenExpectationOfThrownExceptionNotMet(): void
446+
{
447+
if (!class_exists('\PHPUnit\Framework\Assert')) {
448+
$this->markTestSkipped('Can not test this without PHPUnit');
449+
}
450+
451+
$countBeforeAssertion = \PHPUnit\Framework\Assert::getCount();
452+
453+
try {
454+
expect(fn() => null)
455+
->throws(Exception::class);
456+
} catch (AssertionFailure $af) {
457+
assertThat(
458+
\PHPUnit\Framework\Assert::getCount(),
459+
equals($countBeforeAssertion + 1)
460+
);
461+
}
462+
}
441463
}

0 commit comments

Comments
 (0)