[Bug]: Exception assertion does not match full Exception-Message
What Happened
When I use it()->throws() then only the first part of the expected Exception Message gets evaluated.
How to Reproduce
// helpers.php
function shouldFail(){
throw new Exception("abcdef");
}
// helpersTest.php
it("Passes but should fail", function() {
shouldFail();
})->throws(\Exception::class,"a");
it("Passes but should fail", function() {
shouldFail();
})->throws(\Exception::class,"ab");
it("fails correctly", function() {
shouldFail();
})->throws(\Exception::class,"abcdefghijk");
Sample Repository
No response
Pest Version
3.7.4
PHP Version
8.3.15
Operation System
Linux
Notes
I am not sure if this is a big issue but it could cause problems if people require the exception-messages to be exact.
Hi @jackmorizo,
This is not a bug.
throws(\Exception::class,"your-sub-string"))
This checks if the exception message contains "your-sub-string".
If you want to match the exception message exactly you can do it like follows:
function shouldFail(){
throw new Exception("abcdef");
}
it("Fails correctly", function() {
try {
shouldFail();
}
catch(Exception $e){
expect( $e->getMessage())->toBe('a');
}
});
it("Passes correctly", function() {
try {
shouldFail();
}
catch(Exception $e){
expect( $e->getMessage())->toBe('abcdef');
}
});
Hey @maintainers, after investigating this issue, it seems to be expected behavior. I have mentioned the solution to @jackmorizo query. I believe this issue can be closed. What do you think?
Thanks @alihaiderx and sorry for the late response. Are you sure this is expected behaviour or just an undefined/undocumented edge-case?
I have no opinion towards what the expected behaviour should be. However I think it would be good to write it down somewhere
I do not see where it is defined that the $exceptionMessage is a sub-string
https://github.com/pestphp/pest/blob/ed70c9dc2b6ed2ded11a2784f3a80963ddf9b87d/src/PendingCalls/TestCall.php#L149
also the Documentation does not imply that this is in fact a substring https://pestphp.com/docs/exceptions.
@jackmorizo you are right it is not documented yet. The document needs update.
@alihaiderx should I close the ticket or wait until the documentation has been updated?
@jackmorizo We should keep it open, so the documentation update is not forgotten.
Just to clarify: This is standard behavior in PHPUnit as well.
At this point, pest simply uses PHPUnit here, as far as I can tell:
src/PendingCalls/TestCall.php -> https://github.com/sebastianbergmann/phpunit/blob/main/src/Framework/TestCase.php#L1059 -> https://github.com/sebastianbergmann/phpunit/blob/main/src/Framework/TestCase.php#L2200