SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Guru phantom007's Avatar
    Join Date
    May 2008
    Posts
    739
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Question PHPUnit - Testing Cookies and Sessions

    Hi

    As you know that cookies and sessions does not work on command line, so when testing cases with phpunit how do you test functionality that uses cookies and sessions?


    Thanks in advance for any help

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,075
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Instead of using $_COOKIE and $_SESSION directly, you create objects with methods like Session::get('some_var');. Then when it runs on the website you access $_SESSION, but when run for tests you use a simple array. Ideally you would make different adapters for the different environments.

    As a very basic example:

    PHP Code:
    class Session
    {
        private 
    $adapter;
        public static function 
    init(SessionAdapter $adapter)
        {
            
    self::$adapter $adapter;
        }
        public static function 
    get($var)
        {
            return 
    self::$adapter->get($var);
        }
        public static function 
    set($var$value)
        {
            return 
    self::$adapter->set($var$value);
        }
    }

    interface 
    SessionAdapter
    {
        public function 
    get($var);
        public function 
    set($var$value);
    }

    public class 
    PhpSessionAdapter implements SessionAdapter
    {
        public function 
    get($var)
        {
            return isset(
    $_SESSION[$var]) ? $_SESSION[$var] : null;
        }
        public function 
    set($var$value)
        {
            
    $_SESSION[$var] = $value;
        }
    }

    public class 
    MemorySessionAdapter implements SessionAdapter
    {
        private 
    $session = array();
        public function 
    get($var)
        {
            return isset(
    $this->session[$var]) ? $this->session[$var] : null;
        }
        public function 
    set($var$value)
        {
            
    $this->session[$var] = $value;
        }

    Then in your code for the website you start with Session::init(new PhpSessionAdapter());, and for testing you use Session::init(new MemorySessionAdapter());. Then in the rest of the code just use Session::get and Session::set without worrying the data actually goes / comes from.
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    SitePoint Guru phantom007's Avatar
    Join Date
    May 2008
    Posts
    739
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Hi

    Sorry for the late reply. The problem here is that I cannot change the main code, I am just allowed to test the code using phpunit. Do we have an alternate solution to this without altering main code?


    Thanks


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •