Enhance PHP Unit Testing And Mocking Framework improve the quality

Mocks And Stubs

Enhance PHP has built-in support for mocks and stubs. You can set up expectations and supply predictable return values.

You can simply create a mock and then add an expectation and then check that the expectation has been met. Here is an example:

class ExampleDependencyClassTests extends \Enhance\TestFixture
        public function verifyWithAMock() 
                $mock = \Enhance\MockFactory::createMock('ExampleDependencyClass');
                        ->with(1, 'Arg2')
                $target = new ExampleClass($mock);
                $result = $target->doSomething();

The call to \Enhance\MockFactory::createMock just requires the name of the class you want to mock.

The call to $mock->addExpectation takes in an expectation with the following aspects:

The first call should be to one of the following:

Subsequent calls can be made to any combination of the following:

The return value supplied will only be returned when the matching call is made. The mock will not error unless you call $mock->verifyExpectations();

If you want to make a stub rather than a mock, call \Enhance\StubFactory::createStub(). You cannot call "verifyExpectations" on a Stub.

Important note - Unexpected Calls

With a mock, if a call is made to a method or property for which there is no expectation, an error will be thrown. If you want a method to be stubbed (i.e. not to be tracked so no errors are thrown) simply set a general expectation:


With a stub, you can make unexpected calls without errors.