Reputation: 67703
I'm writing Yet Another PHP Framework, and now that I've coded a good deal of stuff, I'm starting to think about unit tests (yes, I know, bad form).
Some actions make the framework to modify HTTP behavior, such as create cookies, redirect the browser. Another major feature is that the the visited URI can affect the way the framework should behave.
In a nutshell, I need to be able to test stuff that affect, or are affected by, the environment, and not only the return values of functions/methods. How can I do this the easiest?
Upvotes: 4
Views: 1854
Reputation: 117457
(yes, I know, bad form).
Yes. If you had started with the tests, you would have written your framework in a way, such that external dependencies could be mocked out. What you can do now, is to replace all calls to functions that manipulate global state, with a call to a global proxy object. You can then mock this out during tests. One particular troublesome change of global state, is calls to exit
/die
. You could try to replace this with an exception in your mock.
Upvotes: 0
Reputation: 78518
You should make HTTP requests to the server in your unit tests, and then check that cookies and headers are set correctly in the response.
In PHP, I would use Zend_Http_Client. Documentation can be found here, in the package Zend_Http.
Upvotes: 1
Reputation: 311496
In a nutshell, I need to be able to test stuff that affect, or are affected by, the environment, and not only the return values of functions/methods. How can I do this the easiest?
If the environment can be simulated easily and can be represented completely with a small amount of state, then you should look into mock testing. Mock tests help you write tests without the mess of having to use real domain objects, if you can get away with simply making sure that things were called in the right order. Here's a good overview of the principles.
If that's not the case, then you're talking about an integration test, which is significantly more expensive. Integration tests help you see if your system is cohesively formed and usually run with a live, full-stack copy of your application. Here's some more reading on integration tests.
Upvotes: 2
Reputation: 5593
So it sounds as though you are writing integration tests and not unit tests (by definition anything which touches the environment is an integration test).
Would this be easier to test through the stack through the browser? If so have a look at a browser automation framework, maybe something like http://wtr.rubyforge.org/
Upvotes: 0