CodeIgniter Forums
testing redirect in controllers - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: testing redirect in controllers (/showthread.php?tid=67967)



testing redirect in controllers - aaldhahe - 05-02-2017

here is the following method inside the controller I am trying to test:
function index(){
if($this->session->userdata('userid')){
$data['username'] = $this->session->userdata('username');
$data['path'] = $this->session->userdata('logo');
$this->load->view('home', $data);

}
else{
//If no session, redirect to login page
redirect('login', 'refresh');
}
}


here is the test method:
public function test_index($userid, $username, $logo_path, $permissions, $tour, $is_admin, $expected){

        //to skip this test uncomment below line
        //$this->markTestSkipped();

        $_SESSION = [
            'userid' => $userid,
            'username' => $username,
            'logo' => $logo_path,
            'permissions' => $permissions,
            'tour' => $tour,
            'is_admin' => $is_admin
        ];

        $output = $this->request('GET', ['Home', 'index']);

        //check if redirect or not
        if($output)
            $this->assertContains($expected, $output);
        else
            $this->assertRedirect('login');
    }


I keep getting the following error: 
Risky Test: Test code or tested code did not (only) close its own output buffers. 
I don't know what am I doing wrong? Can someone please help


RE: testing redirect in controllers - aaldhahe - 05-11-2017

Well, I guess I found a temporary solution. The ci_phpunit-test framework throws an exception and causes phpunit to consider it as a risky test. What I did is I did not throw the exception and saved in the $_SERVER variable that the redirect function was called. I am a newbie and this is my first internship and I believe there are better solutions out there.