Accounting for problems with starting / closing a session

I am having trouble writing unit tests for a simple session wrapper.

The class itself has some basic functions set , get , exists , etc. All these functions have an assertSessionStart check, which performs the following actions:

 protected static function assertStarted() { if (strlen(session_id()) < 1) { throw new Exception("Some text here"); } return; } 

When writing sets of units, I have the following setUp and tearDown . I have this because I want each test to work with a new session environment.

 protected function setUp() { session_start(); } protected function tearDown() { session_destroy(); } 

Now in front of the problem, I want the testing method to fail when I try to use set when I have no session. To do this, I will have to destroy the session running in setUp . Like this:

 public function testGetWithoutSession() { session_destroy(); $this->setExpectedException('Exception'); ESL_Session::set('set', 'value'); session_start(); } 

However, this raises the warning "Attempt to destroy an uninitialized session." When I put echo session_id() right in front of session_destroy , though - it shows me that I have a live session.

Does anyone have experienced testing for session wrappers?

Additional information :

  • PHP Version 5.3.6
  • Linux
+4
source share
2 answers

This is because at this point:

  session_destroy(); $this->setExpectedException('Exception'); ESL_Session::set('set', 'value'); // HERE <---- session_start(); // This is not called anymore! 

an exception exists, and session_start(); no longer called.

My suggestion would be to change to change your tearDown to only call session_destory when there is an active session. Therefore, in tearDown "Only clean if you need to . "

+5
source

If you have a good session class wrapper, I prevent you from using session_id(); every time session_id(); for your approval.

Just put strength

 private $started = false; 

and then in

 protected function setUp() { session_start(); $this->started=true; } 

So you can make your assertStarted() based on $ start

0
source

All Articles