90% of our code is linear in nature. We have functions spread around in some places, but our code generally looks like this:
<?php
// gather some data
// do something with that data
// instantiate a bunch of globals
// output a bunch of stuff to the browser
include_once "file.php";
// output some more stuff
include_once "file2.php";
// ad nauseum
then in file.php
<?php
// do something with the globals from above
// gather some more data
// do something with this newfound data
// output more stuff to the browser
As part of moving to a cleaner code base, I want to begin testing this, but I’m unsure as to the proper way to do that. Any suggestions? I’m at a loss as to the proper method.
As others have commented, you’ve essentially written procedural code. That type of code is typically not very conducive to Unit Testing or Test Driven Development. To begin with, you should probably become familiar with Object Oriented Programming and start grouping related pieces of functionality into appropriate abstractions.
You could try looking for Link Seams and other such tricks, but you’re likely in for a world of hurt unless you start changing the paradigm. Unless you can break your procedural PHP into enough tiny files with set input and output points that you can test each in isolation. But that will definitely require creating methods and eliminating as many of the GLOBALS as possible.
The first thing you need to do is probably read Chapter 19 of Michael Feathers’ Working Effectively with Legacy Code. In fact, read the whole book. Because the “seemingly simple bit” of adding tests is going to require a paradigm shift.