Home
PHP

How to Use a URI to Test Functions with a Test Script in Php

May 11, 2012 01:30 AM

For the PHP tester out there

There are many things you can do with PHP!  A while back I was doing some testing on various functions in a script that my main application would call through jquery asynchronously and I found myself either constantly rewriting the global script code as I switched from function to function checking outputs or modifying multiple files to see the execution ( changes could result in modifying the application file(s) then modifying the worker file(s) ).  Needless to say I got really tired of it really quickly, which lead to this small script which allows you to execute a function by modifying the URI in your browser.

Warnings

  • Do not put this in production scripts!

function function_name(){ echo "Awesomeness!"; } 

if( isset($_GET['function'] ) && !empty( $_GET['function'] )){ if(is_callable( $_GET['function'] ) && function_exists( $_GET['function'] ) ){ $_GET['function'](); }  }

Obviously there are numerous things to concern yourself about with the execution of this code; however, if you are testing many functions and changing them around during development it is much faster to just concern yourself with the actual function you are working on then making multiple modifications to your overall application just to test a single function.  With the above code you will be able to execute a function through some URI like http://localhost/filename.php?function=function_name by placing the code in the global scope of your script. Furthermore, you could place this code in its own file and use PHP's include statement with the name of the file with the functions you wish to test.  This way you don’t add any unwanted code to your production script. I hope this helps, enjoy!

Comments

No Comments Exist

Be the first, drop a comment!