简体   繁体   中英

Unit testing Zend_Session

i have problem with testing zend_session, my test look like that:

Bootstrap.php

public function _initSession() {
    try
    {
        Zend_Session::setSaveHandler(new APP_Session_SaveHandler_Memcached());            
        Zend_Session::start();

        $session = new Zend_Session_Namespace('APP');
        Zend_Registry::set('session', $session);
    }
    catch(Exception $e) 
    {
        Zend_Registry::get('log')->err($e);
    }
}  

SessionTest.php

public function testSession() { 
    $session = Zend_Registry::get('session');
    $session->setExpirationSeconds(1);
    $session->testIndex = "testValue";

    $this->assertEquals('testValue', $session->testIndex);

    sleep(3);

    $this->assertEquals(null, $session->testIndex);
}

first assertion is ok, but the second always say that value of $session->testIndex is "testValue", when it should be null right?

If i'am wrong, please tell me, because I can't figure it out.

The expiry time isn't evaluated every time you access data from the session class, just when it is first loaded. A typical PHP script should never take over a second to execute, so the case you are trying to test shouldn't ever happen in a real world situation.

I'd also add that there's not a lot of point testing functionality that exists in ZF like this - the ZF classes are already unit tested. Test your application, not the framework.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM