Skip to content

Conversation

VincentLanglet
Copy link

  • expectException works with a Throwable class and is typed class-string<Throwable>
  • Throwable has both Message and Code

So this method could works on Throwable.

@staabm
Copy link
Contributor

staabm commented Oct 16, 2025

Do you have a real world use-case in mind?

@VincentLanglet
Copy link
Author

Do you have a real world use-case in mind?

Sure, multiple.

Let's say I have a test with something like

$this->someSubService
            ->expects($this->once())
            ->method('execute')
            ->willThrowException($thrownException);

if (null !== $expectedException) {
     $this->expectExceptionObject($expectedException);
}
$this->myHandler->__invoke($message);

I'm restricted to the typehint

public function testThrowException(\Throwable $thrownException, ?\Exception $expectedException): void

because even if the Throwable is accepted by willThrowException it is not by expectExceptionObject.

For the same reason, I cannot do expectation on Error, like TypeError, ValueError, and so on while I can configure my mock to throw one.

I can still use expectException which supports Throwable, and combine it with expectExceptionMessage.
But I don't see a reason to not support it in expectExceptionObject too then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants