Support for mocking functions #77

wants to merge 3 commits into from

5 participants



today I had the idea to mock functions. This pull request implements the idea, and is meant as a base for discussion of whether there is any interest in integrating it into phpunit-mock-objects.

With the help of function mocks I was able to increase code coverage of a class from below 30% to 100% which has not been possible before as the class uses socket functionality. See the class to test, and test cases starting at line 200 using this functionality.

The way it works is to create a new function with the same signature, but inside the same namespace of the class we want to test. Now if the class uses relative function calls this will lead the class to call the mock functions instead of the original ones. The original idea is described in Mocking Global Php 5.3 Functions Using Namespaces. Therefore this only works with PHP 5.3 and if the class to test is inside a namespace.



I accidentally found this PR. We made a small library for function mocking, you can look at it here:

As a referance how to use it you can look at an integration test:


Thanks for the hint @makasim, I wasn't aware of it. I'd prefer a directly integrated version, but in case this PR doesn't get through I'll have a deeper look at it.


@sebastianbergmann Do we mant to merge this for a mock objects 1.3? If so I'd rebase it if not we could close it.

I personally don't see much value in having it at this point in time but you know the use cases better :)


Does it work? Reliably?


Does it work? If so, I do not having anything against merging it to master (apart from adding yet-another-feature to make legacy code testable ...).


Also, you won't be able to unmock the function for further tests in the same namespace. While you might be able to live with that, wouldn't it be safer to use the more reliable solution with runkit or php-test-helpers?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment