Consider this example class which in encrypting some data and sending it to some other service.
Consider the test for redirect
method.
This is how you consume the class:
<?php
$safeLink = new SafeLink('some_secret_key');
$safeLink->redirect('http://external.service.com/receiver.php', $someData);
and the class is smt. like:
<?php
class SafeLink
{
public function encrypt($data)
{
return ....
}
public function redirect($url, $data)
{
$encryptedData = $this->encrypt($data);
header('HTTP/1.0 302', true, 302);
header('Location: http://example.com?' . $encryptedData, true);
exit;
}
}
So I have two questions:
- How would you test this class considering the header and the exit directives...
What I'd normally do to test this would be sending in $url
and $data
and get the redirected url somehow (probably by using some wrapper class for header
) and test if the data is encrypted correctly.
I was experimenting with phpspec on this and I couldn't mock things and while investigating I found in this article that unit tests are not supposed to be used for testing states, that would be an integration test actually. That makes sense to me now but I'm a little bit confused...
- So if that's the case, what would you be testing for this redirect method, the only option I can think of is testing if it's callable with
$url
and$data
, is that all for unit testing?
$link
is undefined. and you are doing nothing with$encryptedData
i think you should reexamine your example.