phpspec / prophecy

Highly opinionated mocking framework for PHP 5.3+
MIT License
8.53k stars 242 forks source link

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

Closed Deuchnord closed 1 year ago

Deuchnord commented 1 year ago

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 https://github.com/phpspec/prophecy/issues/154#issuecomment-184572986:

// 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.

jdreesen commented 1 year ago

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