Testing and PHP
I’ve discovered that after a certain point, I become allergic to not having tests. It doesn’t manifest in hives or in anaphylactic shock, or any of the regular symptoms. It actually results in a brain freeze. There’s just so much code I can write before my brain refuses to go any further. In a way, maybe it’s more like a phobia. I’m simply afraid to write the next piece of code, knowing how tenuously linked to reality I am by that point, and the next code could be simply the beginning of the cascade of horrible ideas and messy disgusting code.
I wasn’t always this way. For quite a while I was completely happy with not documenting, not testing, and just writing line after line of incomprehensible code. Now, I can’t stand having poorly-documented code, and while I can still stomach writing a project for a while without tests, I hit a point eventually where I just can’t go any further without knowing what lies behind is thoroughly tested.
So it is with a project I’ve been working on that I hope will help me out at work: PGModel. I have been working on it for quite some time, though rather sporadically. It’s simply an ORM for PHP that is designed to be compatible all the way back to PHP 5.1 (I know it’s been end-of-lifed forever, but the project’s documentation goes into more detail on the “why”). And it does a few particular ORM-like things already, such as basic dataset loading and associations. These are all inspired, as the README states, by Sequel.
But while all of this is relatively simple at this point, it’s still already very complicated, and the more I write the more I worry that I’m missing critical bugs. So, I finally wrote myself a testing suite. It’s simple but I think it adheres to at least a subset of the Test Anything Protocol, and allows me to write a large number of tests fairly quickly and easily.
Unfortunately, there are some drawbacks. I could allow a function to be called à la call-user-func-array() to handle checking for exceptions, but it’s rather inelegant. As it stands, though, there’s no other way to check for exceptional cases, as closures didn’t become available until PHP 5.3. It’s also currently completely done from a memory of how testing works in Test::More from Perl, and as such is almost definitely far less feature-complete than a full testing suite.
There’s also already a unit testing suite written for PHP, but it’s sadly only compatible back to 5.2.7. So, I think, as long as I’m stupidly insistent on sticking with 5.1, I’ll keep using this. Who knows, maybe I’ll learn to love it, over time. But for now, I can finally scratch that itch and secure my code (more than it has been, at any rate) with proper testing.