Reputation: 275
I am using CodeIgniter 1.7.2 and CIUnit v.0.17
I am fairly new to PHPunit so please bear with me on this one. I have set-up phpUnit project extended with CIUnit to test an app built in the codeigniter framework. My problem is that when I run a test I have written the method being called from the test checks a session variable ‘LOGGED_IN’ which checks to see if a user has logged into the system. When I run my test this is obviously falling over at this point as the session variable has not been set as it is being called via the command line and not via a http request which would have initialised the session.
In my opinion, PHPunit cannot read Sessions when I run test through commandline...
How would I solve this?
Upvotes: 3
Views: 1428
Reputation: 4419
In the code block that sets the session var / checks credentials include this code:
if(php_sapi_name() == 'cli' && empty($_SERVER['REMOTE_ADDR'])) {
//code to say logged in probably return true;
return true;
} else {
//normal login checking of credentials
}
This will ensure it is being run from command line and is safe-ish to route around credential checking (probably.)
Also note that, even in that oldish version of CI you should really be using the $this->db->session->userdata()
function to check session vars, not accessing the $_SESSION variable directly.
Upvotes: 1