Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add control over class/interface existence before trying to prophesize it #597

Merged

Conversation

Deuchnord
Copy link
Contributor

Currently, if we try to prophesize a class or an interface that doesn't exist, the mock is created anyway, but with nothing inside. This behavior is disturbing, because then, when we try to set a method behavior inside, we get a "method not found" error, as said in #154 (comment):

// The Person class does not exist
$mock = $this->prophesize(Person::class);
$mock->getName()->willReturn('Jérôme');
// Prophecy\Exception\Doubler\MethodNotFoundException: Method `Double\stdClass\P1::getName()` is not defined.

With this PR, trying to prophesize a class that does not exist will now throw a ClassNotFoundException with a comprehensive message that should help understanding what's actually happening.
This new behavior doesn't impact the creation of empty prophecy (i.e. calling prophesize without any argument). However, this might still be a BC break for people who use this method to mock inexistant classes on purpose.

spec/Prophecy/ProphetSpec.php Show resolved Hide resolved
@Deuchnord Deuchnord requested a review from stof April 18, 2023 07:51
@stof stof merged commit e0ae099 into phpspec:master Jun 21, 2023
@jdreesen
Copy link
Contributor

This is great! The current error message is very confusing.

@Deuchnord Deuchnord deleted the feat/check-class-exists-before-prophesizing branch June 22, 2023 08:58
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.

3 participants