Welcome Guest, Not a member yet? Register   Sign In
Testing with session
#1

Hello,

I'm trying to test an "Auth" library I created. Obviously, that library uses the session.

My first test looks like this:

PHP Code:
public function testConstructor()
{
    $auth = new Auth();
    $this->assertInstanceOf(\App\Libraries\Auth\Auth::class, $auth);
    $this->assertNull(service('session')->get('user'));


See the line where I ask if $_SESSION['user'] does not exist? That line produces an error:

Code:
ErrorException: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

If I understand it correctly, that means "session_start()" hasn't been called before the first "echo".

I see two possibilities:
Which is the correct solution? What are the best practices? and how do I do that?

Thanks in advance!
Reply
#2

SteeveDroz
> Which is the correct solution? What are the best practices? and how do I do that?

Check out the PHP Online Manual for solutions.

A simple test:

PHP Code:
if( isset($_SESSION) ) :
  
// do nothing
else:  
  
session_start();
endif; 
Reply
#3

Just to be clear: I'm not a newbie with basic questions (I actually teach PHP and answer those basic questions all day long).

So first, your code should be:

PHP Code:
if (!isset($_SESSION))
{
    session_start();


Second, according to the PHP manual, session_start() "Start new or resume existing session", which means I wouldn't even need the if as calling that function is idempotent.

Third, thank you for telling me I should use session_start(), but as you may have read in my question, I am in the testing context, within CodeIgniter. I very well know how to start a session, my question is where? In what class? What method? Maybe in a constructor? Or as a protected attribute? Do I change something in .env?

Thank you for reading the question properly before answering.
Reply
#4

(10-27-2019, 10:31 PM)SteeveDroz Wrote: Just to be clear: I'm not a newbie with basic questions (I actually teach PHP and answer those basic questions all day long).

So first, your code should be:

PHP Code:
if (!isset($_SESSION))
{
    session_start();


Second, according to the PHP manual, session_start() "Start new or resume existing session", which means I wouldn't even need the if as calling that function is idempotent.

Third, thank you for telling me I should use session_start(), but as you may have read in my question, I am in the testing context, within CodeIgniter. I very well know how to start a session, my question is where? In what class? What method? Maybe in a constructor? Or as a protected attribute? Do I change something in .env?

Thank you for reading the question properly before answering.
Ok, point taken, if I am going to use sessions I would put session_start(); near the start of index.php.
Reply
#5

CodeIgniter ships with a MockSession class you can use. For an example of using it, check out my Myth:Auth package.
Reply
#6

(10-28-2019, 06:34 AM)kilishan Wrote: CodeIgniter ships with a MockSession class you can use. For an example of using it, check out my Myth:Auth package.

OK, it seems undocumented, but now I know it exists, I'll figure it out.

Thanks a lot, that helps! Your example is also a great guide.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB