Comments (1)
Hey @iwasherefirst2,
Great question!
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()
andandSet()
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()
orset()
:
$property = 'role';
$value = 2;
$mock = \Mockery::mock('User');
$mock->shouldReceive('setRole')->andSet($property, $value);
// or
$mock->shouldReceive('setRole')->set($property, $value);
https://docs.mockery.io/en/latest/reference/expectations.html#expectations-setting-public-properties
Note:
set
&andSet
is not designed to be used with static properties.
Currently, there are no plans to introduce new methods for mocking virtual properties, those relying on __get()
and __set()
magic methods.
However, if you have any suggestions on how we can enhance this feature, we are open to reviewing and accepting pull requests.
from mockery.
Related Issues (20)
- mockery/mockery: 1.4.x-dev not found HOT 3
- Factory methods for `IsEqual` and `IsSame` matchers HOT 1
- support for PHPUnit constraints HOT 2
- Disabled travis-ci and sensiolabs webhooks
- [New feature] Running code after mock method was called HOT 2
- InvalidCountException even though the mock method is called HOT 1
- [Feature request] Add array-like objects support to argument matchers
- PHP return type mismatch error when mocking method with static return type HOT 2
- Deprecations about drop support Metadata doc-comment in PHPUnit 12 HOT 2
- Latest release totally broken HOT 14
- PHPStan error with mock method in 1.6.8 HOT 1
- Invalid `Mockery::mock()`/`spy()` phpdoc (= phpstan errors) HOT 6
- Issues with partial construction trait methods on 1.6.10 - change in behaviour HOT 1
- Cannot use 'false' as class name as it is reserved HOT 2
- Typings for setConstantsMap does not allow array HOT 1
- Mocking final classes reports unresolvable type by PHPStan HOT 1
- Error when mocking anonymous classes HOT 5
- Received `*::__construct()` but no expectations were specified HOT 1
- 1.6.12 typehint issue with spy methods HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mockery.