SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict
    Join Date
    Dec 2007
    Posts
    348
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    mocking a static class (simpletest)

    Hi guys,

    If I'm creating a database session class which gets its database connection from a static factory class like so:

    Code PHP:
    class StaticDbFactory {
      static function getInstance($data = null) {
         //return an instance
      }
    }
     
    class Session {
       static $db;
       function __open() {
            self::$db = StaticDbFactory::getInstance();
        }
    }
     
    session_set_save_handler(array('Session', 'open') .... ); 
     
    Mock::generate('StaticDbFactory');
    class TestOfDbSession extends UnitTestCase {
      function setUp() {
          //set up database tables if necessary
          session_start();
      }
      //etc...
    }

    .. how do I create a MockStaticDbFactory which will return a specific test database? Since the StaticDbFactory method is called statically, I cannot instantiate a MockStaticDbFactory and use the setReturnValue() function to return the test database as I usually would.

    any help appreciated!

  2. #2
    SitePoint Addict
    Join Date
    Nov 2005
    Location
    Germany
    Posts
    235
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Refactor Session:
    PHP Code:
    class Session {
       static 
    $db;
       function 
    __open() {
            
    self::$db $this->getDbInstance()
        }
        
        function 
    getDbInstance() {
            return 
    StaticDbFactory::getInstance();
        }

    Then extend Session to MyTestSession and override getDbInstance() to return any object you want (your mock) while testing MyTestSession instead of Session.

    On the other hand you might consider getting rid of this static stuff (passing the instance to __open()?) - it makes testing hard.

  3. #3
    SitePoint Addict
    Join Date
    Dec 2007
    Posts
    348
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ideally I'd get rid of the static stuff.. but if I wanted to use a custom session handler using session_set_save_handler() don't all the methods have to be available statically? otherwise I'd definitely prefer to get rid of the static stuff, I just wasn't aware it was possible if I wanted a custom session handler.

  4. #4
    SitePoint Addict
    Join Date
    Nov 2005
    Location
    Germany
    Posts
    235
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by old_iron View Post
    ideally I'd get rid of the static stuff.. but if I wanted to use a custom session handler using session_set_save_handler() don't all the methods have to be available statically? otherwise I'd definitely prefer to get rid of the static stuff, I just wasn't aware it was possible if I wanted a custom session handler.
    Sorry, I have no clue about custom session handler callbacks, so please disregard that remark - I thought Session::__open() was completely under your jurisdiction.

  5. #5
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by old_iron View Post
    don't all the methods have to be available statically?
    It takes a callback, which can be an objects method. Eg.:
    PHP Code:
    $session = new Session();
    session_set_save_handler(array($session'open') .... ); 

  6. #6
    SitePoint Addict
    Join Date
    Dec 2007
    Posts
    348
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kyberfabrikken View Post
    It takes a callback, which can be an objects method. Eg.:
    PHP Code:
    $session = new Session();
    session_set_save_handler(array($session'open') .... ); 
    I knew it could be an object's method, I just thought it had to be available statically. Thanks kyber


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
  •