Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
PHP Test Doubles FrameworkPhake –
Sometimes it is just plain hard to test the SUT because it depends on other components that cannot be used in the test environment. Such a situation may arise because those components aren’t available, because they will not return the results needed for the test, or because executing them would have undesirable side effects. In other cases, our test strategy requires us to have more control over or visibility of the internal behaviour of the SUT.
When we are writing a test in which we cannot (or choose not to) use a real depended-on component (DOC), we can replace it with a Test Double. The Test Double doesn’t have to behave exactly like the real DOC; it merely has to provide the same API as the real DOC so that the SUT thinks it is the real one.
Phake is a framework for PHP that aims to provide the above functionality. Phake was inspired by a lack of flexibility and ease of use in the current mocking frameworks combined with a recent experience with Mockito for Java. A key conceptual difference in mocking between Phake and most of php mocking frameworks (ie: mock functionality in PHPUnit, PHPMock, and mock functionality in SimpleTest) is that Phake (like Mockito) employs a verification strategy to ensure that calls get made. That is to say, you call your code as normal and then after you have finished the code being tested you can verify whether or not expected methods were called. This is very different from the aforementioned products for php which use an expectation strategy where you lay out your expectations prior to any calls being made.
While it really does boil down to a matter of preference I have found the verification strategy to be incredibly simple and very appealing. It also provides for alot of flexibility that you cannot easily have with an expectation strategy.
1 xUnit Test Patterns: Refactoring Test Code. Gerard Meszaros. Copyright © 2007. Addison-Wesley. ISBN 978-0131495050.