Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPUnit cannot read Session ( CodeIgniter and CIUnit )

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?

like image 462
Annie B. Avatar asked Nov 27 '25 01:11

Annie B.


1 Answers

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.

like image 61
Louis Avatar answered Nov 28 '25 17:11

Louis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!