Mockery is a PHP extension that offers a superior mocking experience, particularly when compared to PHPUnit. While PHPUnit’s mocking framework is powerful, Mockery offers a more natural language with a Hamcrest-like set of matchers. In this article, I’ll compare the two mocking frameworks and highlight the best features of Mockery.
He walks you through the installation of Mockery and includes the code for a sample PHPUnit test that creates a Mockery instance and mocks out a class object for testing. He moves on to more examples including:
- Adding expects for method calls (single and multiple)
- Returning values from the methods
- Calling multiple methods
- Returning different things based on parameters
- Partial mocks
- Handling constructor parameters