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 8370105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:49:58+00:00 2026-06-09T13:49:58+00:00

I’m building a session management class and associated unit test. In order to separate

  • 0

I’m building a session management class and associated unit test. In order to separate the class from the global state of $_SESSION I’m using an extremely simple class to manage the binding between the class and the session data.

The entire source of the Binding class is as follows:

class Binding implements iface\Binding
{
    public function &getNamespace ($namespace)
    {
        return $_SESSION [$namespace];
    }
}

In the consuming Session class I have the following:

protected function initStorage ()
{
    // Check that storage hasn't already been bound to the session
    if ($this -> storage === NULL)
    {
        // Attempt to start the session if it hasn't already been started
        if (($this -> sessionId () !== '')
        || ((!$this -> headersSent ())
        && ($this -> startSession ())))
        {
            // Bind the storage to the session
            $this -> storage    =& $this -> binding -> getNamespace ($this -> namespace);
            // Make sure the session is in a usable state
            if (!$this -> hasData ())
            {
                $this -> reset ();
            }
        }
        else
        {
            // We couldn't start the session
            throw new \RuntimeException (__METHOD__ . ': Unable to initiate session storage at this time');
        }
    }

    return $this;
}

sessionId, headersSent and startSession are simple one-line functions that serve as “test seams”, which I can easily replace with mocks in PHPUnit.

I realised whilst writing the test that I could do more with a mock binding class than just detach the session from the class, I could also use it as a way of observing a non-public property of the class without having to actually expose any internal state and thus making the class fragile. As the class operates on a reference to an array rather than directly on an array I can observe the array being referenced.

I was hoping to do this with PHPUnit’s mocking API, but I can’t figure out how to do it.

I know I can create a mock that returns an array like this:

$mock = $this -> getMock ('iface\Binding');

$mock -> expects ($this -> any ())
      -> method ('getNamespace')
      -> will ($this -> returnValue (array ()));

It’s not useful for observing the state changes though, as it returns a different array every time. What I need is a mock that returns a reference to the same array every time.

In the end I wrote a class to take the place of the real Binding class and use that instead:

class BindingMock implements iface\Binding
{
    protected $storage = array ();

    public function &getNamespace ($namespace)
    {
        return $this -> storage [$namespace];
    }
}

Using this class lets me examine the contents of $storage before and after calling something in the Session API because I can look at what’s in the storage array without having to expose non-public state in the Session class. Here’s an example test that uses the technique:

public function testCreateItem ()
{
    $storage    =& $this -> binding -> getNamespace ('unittest');
    $this -> assertEmpty ($storage);
    $this -> object ->  createItem ('This is a test', 'test');
    $this -> assertNotEmpty ($storage);
    $this -> assertEquals ('This is a test', $storage ['test']);
}

I’d rather be able to generate the substitute class using PHPUnit, though, having the additional class just for the unit test seems like the wrong way to do it unless there’s no way of achieving the same thing in PHPUnit.

  • 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-06-09T13:50:00+00:00Added an answer on June 9, 2026 at 1:50 pm

    I think you could return an ArrayObject that exposes the internal array to your tests, but I’d prefer in this case to avoid exposing the array from the Binding at all. Instead, the Binding interface should provide methods to get, set and clear values along with other higher-level operations. Then you could pass in a mock object that expects those calls rather than one that returns a raw array.

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

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

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.