I'm unit-testing some PHP code with SimpleTest and I've run into trouble. In my tests of a database class I want to be able to set an expectation for PHPs mysql
functions. In my tests of a wrapper class for the mail
function I want to mock PHPs mail
function. These are just some examples.
The point is: I don't (always) want to test if my Mail class sends e-mail, I want to test how it calls the mail
function. I want to be able to control what these functions return. I want to be able to test my Database class without needing a database, fixtures and that whole lot.
I've got some experience with testing Ruby code, and Test::Unit and RSpec make it very easy to test code in isolation. I'm new to testing PHP and it feels like I'm testing a lot more than I should need to, in order to get my tests to pass.
Is there a way in SimpleTest or PhpUnit or some other testing framework that makes this possible or easier?
Not in an automated way. What you can do, is to write your code in a way such that external dependencies are wrapped in objects that are passed in from the outside. In your production environment you'll just wire up the real adapters, but during testing, you can wire it to stubs or mocks.
If you really insist, you can use the runkit extension which changes php's programming model so that you can redefine classes and functions at runtime. This is an external and nonstandard extensions however, so keept that in mind. The defacto standard is a manual approach as I described above.