Mock Property Calls
Here are some examples of mocking property calls with Enhance PHP. If you aren't interested in using the mock to verify the expectations as part of a test, you might be more interested in using a stub - the syntax for both is very similar, but the stub is not used to pass or fail a test.
Getting A Mock Class
$mock = \Enhance\MockFactory::createMock('MyClassName');
To get a mock class, simply ask the MockFactory to create a mock and pass in the name of the class.
Get Property
$mock->addExpectation(\Enhance\Expect::getProperty('MyProperty')->returns(5)->times(1));
In this example we use the following expectation:
-
getProperty('myProperty')
Expect a property named "myProperty" to be retrieved -
returns(5)
When the call is made, return the value 5. -
times(1)
Expect the property to be retrieved exactly 1 time.
It is not valid to use the with() method on a getProperty expectation.
Set Property
$mock->addExpectation(\Enhance\Expect::setProperty('myProperty')->with(4)->times(1));
In this example we use the following expectation:
-
setProperty('myProperty')
Expect a property named "myProperty" to be set -
with(5)
The value to be set is expected to be the value 5. -
times(1)
Expect the property to be set exactly 1 time.
It is not valid to use the returns() method on a setProperty expectation.