You Are Here: Home Documentation Code Snippets Mock Property Calls

Enhance PHP Unit Testing And Mocking Framework improve the quality

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:

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:

It is not valid to use the returns() method on a setProperty expectation.