I was writing a PHP demonstration yesterday, and I wanted to include a test harness with my class definitions that I could leave in place on the live code on my server, to be silently skipped over ... but which I could re-use at a later date for testing class changes, etc.
How did I do it? An environment variable, which I need to provide in order for the test harness to be run. My PHP code contains
if ($_ENV["TESTING"]) {
# Test environment - run the file
# On a production run this will be skipped!
# Test code here
}
And it runs like this:
Dorothy-2:sj grahamellis$ php staticmember.php
Dorothy-2:sj grahamellis$ export TESTING=1
Dorothy-2:sj grahamellis$ php staticmember.php
Service leaves at 05:19
Service leaves at 16:39
Service leaves at 07:02
Service leaves at 17:02
The next service will have id 5
Dorothy-2:sj grahamellis$
(written 2010-03-16)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
H115 - Designing PHP-Based Solutions: Best Practice [123] Short underground journeys and a PHP book - (2004-11-19)
[237] Crossfertilisation, PHP to Python - (2005-03-06)
[261] Putting a form online - (2005-03-29)
[340] Code and code maintainance efficiency - (2005-06-08)
[394] A year on - should we offer certified PHP courses - (2005-07-28)
[426] Robust checking of data entered by users - (2005-08-27)
[563] Merging pictures using PHP and GD - (2006-01-13)
[572] Giving the researcher power over database analysis - (2006-01-22)
[839] Reporting on the 10 largest files or 10 top scores - (2006-08-20)
[896] PHP - good coding practise and sticky radio buttons - (2006-10-17)
[936] Global, Superglobal, Session variables - scope and persistance in PHP - (2006-11-21)
[945] Code quality counts - (2006-11-26)
[1047] Maintainable code - some positive advice - (2007-01-21)
[1052] Learning to write secure, maintainable PHP - (2007-01-25)
[1166] Back button - ensuring order are not submitted twice (PHP) - (2007-04-28)
[1181] Good Programming practise - where to initialise variables - (2007-05-09)
[1182] Painting a masterpiece in PHP - (2007-05-10)
[1194] Drawing hands on a clock face - PHP - (2007-05-19)
[1321] Resetting session based tests in PHP - (2007-08-26)
[1323] Easy handling of errors in PHP - (2007-08-27)
[1381] Using a MySQL database to control mod_rewrite via PHP - (2007-10-06)
[1389] Controlling and labelling Google maps via PHP - (2007-10-13)
[1390] Converting from postal address to latitude / longitude - (2007-10-13)
[1391] Ordnance Survey Grid Reference to Latitude / Longitude - (2007-10-14)
[1482] A story about benchmarking PHP - (2007-12-23)
[1487] Efficient PHP applications - framework and example - (2007-12-28)
[1490] Software to record day to day events and keep an action list - (2007-12-31)
[1533] Short and sweet and sticky - PHP form input - (2008-02-06)
[1623] PHP Techniques - a workshop - (2008-04-26)
[1694] Defensive coding techniques in PHP? - (2008-07-02)
[1794] Refactoring - a PHP demo becomes a production page - (2008-09-12)
[2199] Improving the structure of your early PHP programs - (2009-05-25)
[2221] Adding a newsfeed for your users to a multipage PHP application - (2009-06-06)
[2430] Not just a PHP program - a good web application - (2009-09-29)
[3539] Separating program and artwork in PHP - easier maintainance, and better for the user - (2011-12-05)
[3813] Injection Attacks - PHP, SQL, HTML, Javascript - and how to neutralise them - (2012-07-22)
[3820] PHP sessions - a best practice teaching example - (2012-07-27)
[3926] Filtering PHP form inputs - three ways, but which should you use? - (2012-11-18)
[4069] Even early on, separate out your program from your HTML! - (2013-04-25)
[4118] We not only teach PHP and Python - we teach good PHP and Python Practice! - (2013-06-18)
[4326] Learning to program - comments, documentation and test code - (2014-11-22)
[4641] Using an MVC structure - even without a formal framework - (2016-02-07)
[4691] Real life PHP application using our course training MVC example - (2016-06-05)
H112 - PHP - Further Web Page and Network Handling [220] When to use Frames - (2005-02-19)
[314] What language is this written in? - (2005-05-17)
[345] Spotting a denial of service attack - (2005-06-12)
[356] Sudoku helper or sudoku cheat - (2005-06-23)
[372] Time calculation in PHP - (2005-07-08)
[376] What brings people to my web site? - (2005-07-13)
[410] Reading a news or blog feed (RSS) in your PHP page - (2005-08-12)
[425] Caching an XML feed - (2005-08-26)
[443] Server side scripting of styles to suit the browser - (2005-09-12)
[451] Accessing a page via POST from within a PHP script - (2005-09-26)
[484] Setting the file name for a downloaded document - (2005-11-03)
[537] Daily Image Santafied - (2005-12-22)
[542] Morning image, afternoon image - (2005-12-26)
[565] Using PHP to output images, XML, Style sheets, etc - (2006-01-15)
[603] PHP - setting sort order with an associative array - (2006-02-13)
[675] Adding PHP tags to an old cgi program - (2006-04-08)
[767] Finding the language preference of a web site visitor - (2006-06-18)
[789] Hot answers in PHP - (2006-07-02)
[847] Image maps for navigation - a straightforward example - (2006-08-28)
[904] Of course I'll tell you by email - (2006-10-25)
[1009] Passing GET parameters through Apache mod_rewrite - (2006-12-27)
[1114] PHP Image upload script - (2007-03-21)
[1183] Improving searches - from OR to AND? - (2007-05-11)
[1187] Updating a page strictly every minute (PHP, Perl) - (2007-05-14)
[1210] PHP header() function - uses and new restrictions - (2007-05-30)
[1355] .php or .html extension? Morally Static Pages - (2007-09-17)
[1379] Simple page password protection - PHP - (2007-10-04)
[1485] Copyright and theft of images, bandwidth and members. - (2007-12-26)
[1495] Single login and single threaded models - Java and PHP - (2008-01-04)
[1496] PHP / Web 2 logging - (2008-01-06)
[1505] Script to present commonly used images - PHP - (2008-01-13)
[1515] Keeping staff up to date on hotel room status - (2008-01-22)
[1518] Downloading data for use in Excel (from PHP / MySQL) - (2008-01-25)
[1549] http, https and ajp - comparison and choice - (2008-02-22)
[2632] Shipping a test harness with your class in PHP - (2010-02-12)
[2729] Uploading a document or image to its own URL via a browser - (2010-04-18)
[2918] Downloading a report from the web for further local analysis - (2010-08-13)
[3036] Sending out an email containing HTML from within a PHP page - (2010-11-07)
[3432] 3 digit HTTP status codes - what are they, which are most common, which should be a concern? - (2011-09-11)
[3540] Easy session example in PHP - keeping each customers data apart - (2011-12-06)
[3568] Telling which ServerAlias your visitor used - useful during merging domains - (2012-01-04)
[3918] Multiple page web applications - maintaining state - PHP - (2012-11-10)
[4070] Passing variable between PHP pages - hidden fields, cookies and sessions - (2013-04-26)
[4483] Moving from mysql to mysqli - simple worked example - (2015-05-03)
Some other Articles
Car Parking in MelkshamAdding extensions to PHP Open Source applications - callbacksTcl - a great engineering languageStatic class members in PHP - a documented exampleHow to build a test harness into your PHPReaching out to the community - a good case for supportBlowing out the winter cobwebsChanging TimesRedirecting to your main domain for correct security keysMake and makefiles - a commented example to help you learn