Take a look at the idea of Dependency injection. In a nutshell you should feed your code what it needs as opposed to it getting the data it needs... Here's an example:
example without Dependency Injection
function sanitize1() {
foreach($_POST as $k => $v) {
// code to sanitize $v
}
}
sanitize1();
example with Dependency Injection
function sanitize2(array &$formData) {
foreach($formData as $k => $v) {
// code to sanitize $v
}
}
sanitize2($_POST);
See the difference? In your PHPUnit test you can pass sanitize2()
an associative array of your choice; you've injected the dependency. Whereas sanitize1()
is coupled with $_POST
. $_POST
and $_GET
are assoc arrays anyways so in your production code you can pass $_GET
or $_POST
to your function but in your unit tests you'd hard code some expected data.
Unit test example:
function testSanitize() {
$fakeFormData = array ('bio' => 'hi i\'m arin', 'location' => 'San Francisco');
sanitize($fakeFormData);
// assert something
}