I’ve been writing PHP code for years now. I’ve written complete systems that I’ve forgotten I’ve written. I generally know what I’m doing.

But, sometimes I’m writing something and I need to make sure some kind of complicated logic will work correctly. Or I can’t remember exactly how a certain function will behave with certain inputs. Or I need to run some kind of one-off report for someone. How do I do all of those things without creating tons of files for each one?

Create a test.php file. In that file, include your common file so you have database access and all your standard classes. You can optionally include visuals or whatever else you need to perform your testing. Then go ahead and test your one-line items, or write one-time use reports, or test some concepts.

What I like to do is write my code, then put an “exit” at the end of it. I leave it on the development server I work on all the time so it’s always available. The next time I need to test some piece of code, I add my code above my previous test code and add another “exit” at the end of my current test code. All of my various tests and one-time reports are in this file and it’s really easy to grab examples from previous work or see how I did something later. It’s like a functional scratch pad.

Recently the file got so big it was taking a couple seconds to upload and download it to the server. Because I also have a /test/ directory, I duplicated my test file, named it test_archive.php, and then deleted everything except the required common includes out of my test.php file and started over.

I love having my test.php file. It lets me take complicated systems and test individual little components to verify they’re working without needing to run large complicated tests on the entire new set of code I might be writing. It also lets me do real time one-off reporting as needed without setting up and formalizing actual report files.

August 28th, 2013

Posted In: Shortcuts

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: