One thing that I haven’t seen any of the tutorials cover is how to handle running PHPUnit in different environments. Most people are just setting a new APPLICATION_ENV called ‘testing’, put a section in application.ini called ‘testing’ as well, and leave it at that….
What I’ve found, is that I’m using various things that will change in different environments.
For example, on local I just create a shortcut domain in my hosts file so I can easily play. So if I’m building a site for someverylongexample.com, I just make x.com my localhost. Which works great for me. See, I’m lazy.
Then some clients like to have a different domain for staging so they can point to different databases, or whatever reason.
And then you may just want to run a few tests with an environment of ‘production’ or ‘development’. For example, I only log to firebug on local or dev.
Frustrated, I just modified the usual settings you see.
Before going any further, make sure you check out a basic tutorial on Zend Framework and PHPUnit. For example:
So here’s how I modified the files to cover these requirements.
1. instead of running phpunit off of phpunit.xml, I renamed that file phpunit_local.xml.
So on the command line, you’ll run this:
phpunit –configuration phpunit_local.xml
The contents of that file are (I’ve had it w/ WordPress messing up my formatting. Why do they have a CODE tag if it doesn’t handle much of anything very well? I’m going to put this on pastie.org and pastebin.com & hopefully one of them will survive as long as this post stays relevant):
2. You probably noticed that instead of pointing to the bootstrap.php file, I renamed it bootstrap_local.php.
Here are the contents:
3. I wanted to be able to override the APPLICATION_ENV to test production/dev/local from time to time. So I modified the ControllerTestCase.php as follows:
Now you can test whatever environment you want by just overriding _setAppEnv().
This way, when you switch to prod, you’ll have a robust test to run that things work in the new environment. This happens to be one of the most important aspects of Unit Tests that no one seems to speak about. You usually have a slightly different environment and it can break things. If you’re only running your unit tests on local, you’re really missing one of the key benefits of all this effort.
If you think I’m way off, or have ideas on improving this code, leave a comment or ping me on twitter.
P.S. I just took a look at Matthew’s post on Zend_Test, which I had just found through a Google search, and noticed he uses a TestHelper.php. You can certainly do something very similar with the TestHelper…which is analogous to the bootstrap.php file I was using.
2nd Update: Forgot to mention, obviously you can’t redefine constants, so you may just have a subset you want to test. I’m assuming that you’re organizing things with the wonderful @group functions of PHPUnit and that you run this intelligently.