Hello,
I'm trying to write a unit test using F3's mock() and PHPUnit. I have a function that I want to test that looks like this:
public function logout($f3)
{
$f3->clear('SESSION.user');
$f3->reroute('@loginPage');
}
I'm trying to test it doing something like:
public function testLogout()
{
$this->f3->QUIET = true;
$this->f3->HALT = false;
$this->f3->mock('GET /logout');
//Make sure SESSION.user is cleared
//Make sure user gets rerouted
}
The problem is that when reroute() is called, reroute() will call "die" which will terminate the execution of my test suite. Here is a code snippet from the end of the reroute() function in base.php where "die" gets called.
if (!$this->hive['CLI']) {
header('Location: '.$url);
$this->status($permanent?301:302);
die;
}
$this->mock('GET '.$url);
I did see in some places (even in F3 too, see snippet above) that you can call mock() instead. I was able to get this to work and test my function successfully if I change my logout() function to be:
public function logout($f3)
{
$f3->clear('SESSION.user');
$f3->mock('GET @loginPage');
}
However, this feels like cheating to me. mock() is meant for testing, not production code--unless F3 has a unique way to use mock(). I don't like changing production code to accommodate my tests unless my testing discovers an actual problem. Is there some better way to test that when I call this function that the user gets redirected as expected? Is there a different way to reroute someone? Is mock() really the right way to redirect and not have "die" get called?
I have opinions about the use of "die" and "exit" in PHP code and I think they should only get called in extreme circumstances. What role does "die" play in the reroute() function, is it because it sends a 301 or 302 status instead of 200? Is it possible to reroute someone with a 200 status?
Thanks!