Wednesday, November 9, 2011

Continuous Integration Pt II - Unit Testing and Codeigniter

I'm still plugging away at getting our shop set up for continuous integration. We use Codeigniter for our application, and this presents a huge can of worms with regards to unit testing. First of all, Codeigniter does include some minimal unit testing support within the app, but, by all accounts, it's pretty darn small. The holy grail for unit testing in PHP, IMHO, is PHPUnit. It's pretty much the standard, and it's well supported. So, it makes sense to use that, since our tests will continue to be relevant, whether we change versions of Codeigniter, whether developers come and go, etc. It's a nice constant to shoot for.

The big problem is that PHPUnit doesn't just naturally hook into Codeigniter. Things get even more complicated when you start looking for solutions for bootstrapping it, as it greatly varies depending on the version of Codeigniter you're using. Ellis Labs (the developers of Codeigniter) is working on integrating PHPUnit into the framework at some point in the future, which will be version 2.?. In the meantime, another developer has started with their code and completed it to make it work, and it works with version 2.0.3. Well, we're on version 2.0.2. So, that's a bummer. The earlier attempts to integrate PHPUnit with Codeigniter are usually for 1.7.x and below.

In the end, I managed to get another developer's much simpler bootstrapping code to work. You can find it here. That's a Codeigniter forum post. Look for CarloGI's post about his technique. He includes a download of example files. Download the files, put the where they belong, and make sure you modify the _getDBObject function of the bootstrap.php file to match your mysqli settings for your app. You'll want to include the --stderr argument when you run phpunit so that you don't get the output buffers error. One other thing I had to do was to comment out an echo statement in myControllerTest.php. Otherwise, it will cause the output buffer error, as well.

Sorry these instructions are so cryptic. I wanted to get them out there (mostly for myself), but they might help someone else. If I have time, I will add more detail.

No comments:

Post a Comment