If Perl is the Swiss Army chainsaw of the internet,
PHP might be the dull butterknife from that silverware
set your Aunt Girda gave you because she felt guilty
about throwing it away. Sure, you're never going to cut down that redwood, but when you're cutting butter,
butterknives have good points too. Today's butter
is unittesting for small PHP projects.
Setting up a light unittesting setup is pretty trivial
as long as your language supports using functions as
parameters. Which, fortunately, PHP does. For example,
a none-too-impressive implementation of map could
be written like this:
If you're in the mood for some overengineering, it's
a pretty short leap from here to a flexible and extensible
unittesting framework. For a start, we can rewrite the run_tests
method to look through all user defined functions for those
that begin with test_ and then run those tests.
In either case, running the tests is as simple as reloading the page
(or could be done at the command line), and doing this exercise
has firmly dragged PHP from my mental junk bin (into some kind of halfway home for programming languages). The equivalent code
written in Python strikes me as much more pleasant, but the difference
isn't as overwhelming as I might have once imagined.