I’m trying to get familiar with PHPUnit testing within Kohana. At the moment, I seem to be having problems with Request::current()->redirect calls in my code.
For example, I am trying to test the login functionality. Once our user is successfully logged in, we redirect it to its home page using the above Request redirect line. The problem is that when that line is there, the test seems to stop there and never return the results.
Here is how my tests is written at the moment:
class SampleTest extends Kohana_UnitTest_TestCase
{
protected $session;
public function setUp() {
parent::setUp();
$this->session = Session::instance();
}
public function testLogin()
{
$request = new Request('/login');
$request->method(HTTP_Request::POST)
->post(array('username' => 'username', 'password' => 'password'));
$request->execute();
$this->assertEquals($this->session->get('username'), 'password');
}
}
If I comment out the following line in my login controller, everything works great:
Request::current()->redirect(); //redirect to home
What am I doing wrong?
The order of operations for a standard request (check your index.php) is:
You hijacked the request in the middle of your execute and redirected the process. Your test simply follows that code since it is all part of that execute.
Instead, defer your redirect by adding it to the Request headers which get executed in send_headers and you won’t hit that code in your unittest. Replace your Request::current()->redirect() line with the proper way to redirect users: