Shells and Sandboxes - The Grumpy Programmer's Guide To Building Testable PHP Applications (2015)

The Grumpy Programmer's Guide To Building Testable PHP Applications (2015)

7. Shells and Sandboxes

This content orginally appeared on my blog in 2009. It covers a tool that is used constantly in languages like Python and Ruby but is pretty much ignored in PHP. Hope you enjoy it.

Now that I have become a born-again tester, I’ve started looking at the testing I’m doing at work from more of a high-level point of view. What are tools like SimpleTest and PHPUnit *really* doing? They are providing a mechanism for the automating of running of tests. Such tools also exist for frameworks like Django and Ruby on Rails. I cannot speak for Django, but the Rails community has made a very large commitment to testing tools and testing best practices. But there is also another very powerful testing tool that Ruby and Python make available that PHP does not.

Both Ruby and Python offer a Read-eval-print loop that can be run from the command line to allow people to do the opposite of what conventional testing tools do: allow you to run interactive tests of your code. I used the REPL available in Python (you know, just typing ‘python’ from my command line) to test out how to use various geocoding libraries for a side project. My favourite Ruby programmer uses irb all the time to work on stuff.

So in a way, a REPL is the anti-automated test. I think that if there was a good REPL available for PHP, testing best-practices might take better hold. Perhaps there is hope for a good PHP REPL in Alan Pinstein’s iphp project. I remember trying to continue work on the testing console I had created a number of years ago for CakePHP, and quickly realized that what I needed was a REPL.

Since I did not have the desire to write my own limted PHP parser by messing around with eval (although, to be honest I did try playing with it and realized I did not know what the hell I was doing), the testing console was dead. Besides, how many PHP devs really use PHP on the command line? 10%? 1%? Hard to know, as the group I follow is a self-selecting one and I’m sure most of them have used PHP on the command line. Hard to become an advanced PHP developer without trying to do stuff on the command line with it.

I was also pleased to find out (after a year of using it, no less) that eXist had it’s own REPL for XQuery - they call it the “sandbox”. It’s awesome because I can take my “wrote this 6 months ago and can’t remember if it still works” XQuery scripts and test them out. Sort of like a command line client to your RDBMS. It even tells me when I’ve got syntax or formatting errors in them BEFORE I try and execute them. Talk about saving a developer time.

Do things like this exist for RDBMs like MySQL and Postgres? Imagine an interactive client that is checking your syntax out while you type it. If such a thing exists, please tell me!

Once again, I see I have started to diverge. Back on track again.

So, from a testing perspective it’s easy to see how shells and sandboxes can become essential tools. For example, all the good advice I have ever seen about unit testing clearly advocates a sandbox approach. You create fixtures containing data. You create mock objects to simulate functionality that might potentially be destructive. You might even create a separate database for doing test operations on. Clearly, the goal here is to create an environment that is the same as the one your code will eventually be running in, but separate from the “real” one.

A REPL, on the other hand, is very powerful and very dangerous. If you’re not careful, you could very easily insert data into your current production environment instead of your test one. As with so many other things, power comes with a price. Sure, I can run some code to figure out how to use, say, SQLAlchemy but I better make sure that I am not writing data to my production database. That would be silly.

I’m guessing the main reason PHP does not have a easy-to-use REPL is because it grew up on the web first and the command line second. Ruby and Python started on the command line first, and out to the web second. It’s really that simple I think. Will PHP ever get a widely-accepted REPL?