Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5986977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:44:56+00:00 2026-05-22T22:44:56+00:00

I am having an problem writing unit tests for an simple session wrapper. The

  • 0

I am having an problem writing unit tests for an simple session wrapper.

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

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

When writing my unit sets, I have the following setUp and tearDown methods. I have these because I want every test that runs to have a fresh session environment.

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

Now to the problem, I want an test method which fails when I attempt to use set when I don’t have an session started. In order to do this, I will have to destroy the session started in setUp. Like this:

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

This however throws an warning ‘Trying to destroy uninitialized session’. When I put an echo session_id() right in front of session_destroy though – it shows me that I have an valid session.

Does anyone have experience unit testing session wrappers?

Additional information:

  1. PHP Version 5.3.6
  2. Linux
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T22:44:57+00:00Added an answer on May 22, 2026 at 10:44 pm

    It happens because at this point:

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

    there is an exception and session_start(); isn’t called anymore.

    My suggestion would be to change to change your tearDown to only call session_destory when there is an active session. So in the tearDown “Only clean up if you have to”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having some difficulty writing some Unit Tests to test a custom ModelBinder that
I'm having a problem writing Norwegian characters into an XML file using C#. I
The problem I'm having with writing a web application architecture is that I want
So, I previously wasn't really in the practice of writing unit tests - now
I am having trouble writing a simple if statement in a BMC Bladelogic NSH
I am having a validation problem when writing a Redmine plugin. I'm writing a
I seem to be having a problem. I want to write some code that
We're having problem with a huge number of legacy stored procedures at work. Do
I'm having problem in the following line: rd.PrintOptions.PaperSize = PaperSize.PaperFanfoldStdGerman; it throws an exception
Having a problem getting a TreeView control to display node images. The code below

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.