You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In general, Mockery does not support mocking any magic methods since these are generally not considered a public API.
So it’s recommend to mock virtual properties (those relying on __get() and __set()) as if they were actually declared on the class.
Mockery allows us to mock properties in several ways.
One way is that we can set a public property and its value on any mock object.
$mock = \Mockery::mock('User');
$mock->role = 2;
The second is that we can use the expectation methods set() and andSet() to set property values if that expectation is met.
Used with an expectation so that when a matching method is called, we can cause a mock object’s public property to be set to a specified value, by using andSet() or set():
Mockery Version
1.6.6
PHP Version
PHP 8.2
Issue Description
It is not possible to set properties to objects who have a __set() method, as this is always called instead on the mocked object. The only workaround that I know, is that one has to mock the methods inside the original class'es __set() method, see https://stackoverflow.com/a/26555404/2311074 which is not recommended https://docs.mockery.io/en/latest/reference/magic_methods.html
Steps to Reproduce
$mock = Mockery::mock('User');
$mock->roles = 2; // Calls __set() methods, which may lead to errors, like for any Laravel Eloquent object
Expected Behavior
Should change property of roles to 2
Actual Behavior
An exception is thrown instead
Exception or Error
BadMethodCallException: Method Mockery_0_User::setAttribute() does not exist on this mock object
Additional Information
Having a mockery set method like:
$mock = Mockery::mock('User');
$mock->addProperty('roles', 2);
would be great.
The text was updated successfully, but these errors were encountered: