Changeset 1909

Show
Ignore:
Timestamp:
10/28/08 16:51:19 (2 months ago)
Author:
mikey
Message:

make sure doInit() is only called once

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • framework/trunk/src/main/php/net/stubbles/websites/stubAbstractPageElement.php

    r1902 r1909  
    66 * @package     stubbles 
    77 * @subpackage  websites 
     8 * @version     $Id$ 
    89 */ 
    910stubClassLoader::load('net::stubbles::websites::stubPageElement'); 
     
    4647     */ 
    4748    protected $context; 
     49    /** 
     50     * switch whether internal initializing was done or not 
     51     * 
     52     * @var  bool 
     53     */ 
     54    private $initialized = false; 
    4855 
    4956    /** 
     
    9198        $this->response = $response; 
    9299        $this->context  = $context; 
    93         $this->doInit(); 
     100        if (false === $this->initialized) { 
     101            $this->doInit(); 
     102            $this->initialized = true; 
     103        } 
    94104    } 
    95105 
  • framework/trunk/src/test/php/net/stubbles/websites/stubAbstractPageElementTestCase.php

    r1763 r1909  
    66 * @package     stubbles 
    77 * @subpackage  websites_test 
     8 * @version     $Id$ 
    89 */ 
    910stubClassLoader::load('net::stubbles::websites::stubAbstractPageElement'); 
     
    1617class TeststubAbstractPageElement extends stubAbstractPageElement 
    1718{ 
     19    /** 
     20     * counter for calls of doInit() 
     21     * 
     22     * @var  int 
     23     */ 
     24    protected $initCount = 0; 
     25 
    1826    /** 
    1927     * returns the request instance 
     
    5462    { 
    5563        return $this->context; 
     64    } 
     65 
     66    /** 
     67     * method for additional initialisation 
     68     */ 
     69    protected function doInit() 
     70    { 
     71        $this->initCount++; 
     72    } 
     73 
     74    /** 
     75     * returns number of calls to doInit() 
     76     * 
     77     * @return  int 
     78     */ 
     79    public function getInitCount() 
     80    { 
     81        return $this->initCount; 
    5682    } 
    5783 
     
    129155 
    130156    /** 
     157     * doInit() should be called only once regardless of calls to init() 
     158     * 
     159     * @test 
     160     */ 
     161    public function doInitIsOnlyCalledOnce() 
     162    { 
     163        $mockRequest  = $this->getMock('stubRequest'); 
     164        $mockSession  = $this->getMock('stubSession'); 
     165        $mockResponse = $this->getMock('stubResponse'); 
     166        $this->assertEquals(0, $this->abstractPageElement->getInitCount()); 
     167        $this->abstractPageElement->init($mockRequest, $mockSession, $mockResponse, array()); 
     168        $this->assertEquals(1, $this->abstractPageElement->getInitCount()); 
     169        $this->abstractPageElement->init($mockRequest, $mockSession, $mockResponse, array()); 
     170        $this->assertEquals(1, $this->abstractPageElement->getInitCount()); 
     171    } 
     172 
     173    /** 
    131174     * by default page elements are available, cachable and do not have any cache vars 
    132175     *