Changeset 1563

Show
Ignore:
Timestamp:
04/24/08 17:09:12 (3 weeks ago)
Author:
mikey
Message:

enhanced net::stubbles::ipo::session::stubNoneDurableSession to be more session-like

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/main/php/net/stubbles/ipo/session/stubNoneDurableSession.php

    r1549 r1563  
    77 * @subpackage  ipo_session 
    88 */ 
    9 stubClassLoader::load('net::stubbles::ipo::session::stubAbstractSession'); 
     9stubClassLoader::load('net::stubbles::ipo::request::validator::stubRegexValidator', 
     10                      'net::stubbles::ipo::session::stubAbstractSession' 
     11); 
    1012/** 
    1113 * Session class that is not durable for more than one request. 
     
    2123     * @var  int 
    2224     */ 
    23     protected $id   = 312
     25    protected $id
    2426    /** 
    2527     * the data 
     
    2729     * @var  array 
    2830     */ 
    29     protected $data = array(); 
     31    protected $data        = array(); 
     32    /** 
     33     * the response instance 
     34     * 
     35     * @var  stubResponse 
     36     */ 
     37    protected $response; 
     38    /** 
     39     * regular expression to validate the session id 
     40     */ 
     41    const REGEX_SESSION_ID = '/^([a-zA-Z0-9]{32})$/D'; 
    3042 
    3143    /** 
     
    3951    protected function doConstruct(stubRequest $request, stubResponse $response, $sessionName) 
    4052    { 
     53        $this->response = $response; 
     54        if ($request->hasValue($sessionName) === true) { 
     55            $this->id = $request->getValidatedValue(new stubRegexValidator(self::REGEX_SESSION_ID), $sessionName); 
     56            $this->data = array(stubSession::START_TIME  => time(), 
     57                                stubSession::FINGERPRINT => '', 
     58                                stubSession::NEXT_TOKEN  => '' 
     59                          ); 
     60        } elseif ($request->hasValue($sessionName, stubRequest::SOURCE_COOKIE) === true) { 
     61            $this->id = $request->getValidatedValue(new stubRegexValidator(self::REGEX_SESSION_ID), $sessionName, stubRequest::SOURCE_COOKIE); 
     62            $this->data = array(stubSession::START_TIME  => time(), 
     63                                stubSession::FINGERPRINT => '', 
     64                                stubSession::NEXT_TOKEN  => '' 
     65                          ); 
     66        } else { 
     67            $this->id = md5(uniqid(rand(), true)); 
     68        } 
     69         
     70        $this->response->setCookie(stubCookie::create($sessionName, $this->id)->forPath('/')); 
    4171        return true; 
    4272    } 
     
    5787     * @return  string  the session id 
    5888     */ 
    59     public function getID() 
     89    public function getId() 
    6090    { 
    6191        return $this->id; 
     
    6595     * regenerates the session id but leaves session data 
    6696     */ 
    67     public function regenerateID() 
     97    public function regenerateId() 
    6898    { 
    69         $this->id++; 
     99        $this->id = md5(uniqid(rand(), true)); 
     100        $this->response->setCookie(stubCookie::create($this->sessionName, $this->id)->forPath('/')); 
    70101    } 
    71102 
  • trunk/src/test/php/net/stubbles/ipo/session/stubNoneDurableSessionTestCase.php

    r1531 r1563  
    2222     */ 
    2323    protected $session; 
     24    /** 
     25     * mocked request instance 
     26     * 
     27     * @var  PHPUnit_Framework_MockObject_MockObject 
     28     */ 
     29    protected $mockRequest; 
     30    /** 
     31     * response instance 
     32     * 
     33     * @var  stubResponse 
     34     */ 
     35    protected $response; 
    2436 
    2537    /** 
     
    2840    public function setUp() 
    2941    { 
    30         $this->session = new stubNoneDurableSession($this->getMock('stubRequest'), $this->getMock('stubResponse'), 'test'); 
     42        $this->mockRequest = $this->getMock('stubRequest'); 
     43        $this->response    = new stubBaseResponse(); 
     44        $this->session     = new stubNoneDurableSession($this->mockRequest, $this->response, 'test'); 
     45    } 
     46 
     47    /** 
     48     * session id from request should be used 
     49     * 
     50     * @test 
     51     */ 
     52    public function useSessionIdFromRequest() 
     53    { 
     54        $this->mockRequest->expects($this->once()) 
     55                          ->method('hasValue') 
     56                          ->with($this->equalTo('test')) 
     57                          ->will($this->returnValue(true)); 
     58        $this->mockRequest->expects($this->once()) 
     59                          ->method('getValidatedValue') 
     60                          ->will($this->returnValue('313')); 
     61        $session = new stubNoneDurableSession($this->mockRequest, $this->response, 'test'); 
     62        $this->assertEquals('313', $session->getId()); 
     63        $cookies = $this->response->getCookies(); 
     64        $this->assertType('stubCookie', $cookies['test']); 
     65        $this->assertEquals('test', $cookies['test']->getName()); 
     66        $this->assertEquals('313', $cookies['test']->getValue()); 
     67    } 
     68 
     69    /** 
     70     * session id from cookie should be used 
     71     * 
     72     * @test 
     73     */ 
     74    public function useSessionIdFromCookie() 
     75    { 
     76        $this->mockRequest->expects($this->exactly(2)) 
     77                          ->method('hasValue') 
     78                          ->will($this->onConsecutiveCalls(false, true)); 
     79        $this->mockRequest->expects($this->once()) 
     80                          ->method('getValidatedValue') 
     81                          ->will($this->returnValue('313')); 
     82        $session = new stubNoneDurableSession($this->mockRequest, $this->response, 'test'); 
     83        $this->assertEquals('313', $session->getId()); 
     84        $cookies = $this->response->getCookies(); 
     85        $this->assertType('stubCookie', $cookies['test']); 
     86        $this->assertEquals('test', $cookies['test']->getName()); 
     87        $this->assertEquals('313', $cookies['test']->getValue()); 
     88    } 
     89 
     90    /** 
     91     * new session id should be generated 
     92     * 
     93     * @test 
     94     */ 
     95    public function generateNewSessionId() 
     96    { 
     97        $this->mockRequest->expects($this->exactly(2)) 
     98                          ->method('hasValue') 
     99                          ->will($this->returnValue(false)); 
     100        $this->mockRequest->expects($this->never()) 
     101                          ->method('getValidatedValue'); 
     102        $session = new stubNoneDurableSession($this->mockRequest, $this->response, 'test'); 
     103        $cookies = $this->response->getCookies(); 
     104        $this->assertType('stubCookie', $cookies['test']); 
     105        $this->assertEquals('test', $cookies['test']->getName()); 
     106        $this->assertEquals($session->getId(), $cookies['test']->getValue()); 
    31107    } 
    32108 
     
    41117        $this->session->regenerateId(); 
    42118        $this->assertNotEquals($id, $this->session->getId()); 
     119        $cookies = $this->response->getCookies(); 
     120        $this->assertType('stubCookie', $cookies['test']); 
     121        $this->assertEquals('test', $cookies['test']->getName()); 
     122        $this->assertNotEquals($id, $cookies['test']->getValue()); 
     123        $this->assertEquals($this->session->getId(), $cookies['test']->getValue()); 
    43124    } 
    44125