Take a look at the idea of including dependencies . In a nutshell, you should feed your code what it needs, and not get the data it needs ... Here is an example:
example without dependency injection
function sanitize1() { foreach($_POST as $k => $v) {
dependency injection example
function sanitize2(array &$formData) { foreach($formData as $k => $v) {
See the difference? In your PHPUnit test, you can pass the sanitize2() associative array of your choice; you addicted. While sanitize1() is associated with $_POST . $_POST and $_GET are anyway arrays, so in your production code you can pass $_GET or $_POST to your function, but in your unit tests it will be difficult for you to copy some expected data.
Unit test example:
function testSanitize() { $fakeFormData = array ('bio' => 'hi i\'m arin', 'location' => 'San Francisco'); sanitize($fakeFormData);
phatduckk Sep 25 '08 at 22:57 2008-09-25 22:57
source share