Changeset 1136

Show
Ignore:
Timestamp:
12/11/07 12:18:30 (9 months ago)
Author:
mikey
Message:

net::stubbles::websites::memphis::stubMemphisProcessor is now reference implementation for enhancement #116

Files:

Legend:

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

    r1110 r1136  
    88 */ 
    99stubClassLoader::load('net.stubbles.ipo.request.stubRequestPrefixDecorator', 
    10                       'net.stubbles.ipo.response.stubBaseResponse', 
    1110                      'net.stubbles.lang.stubMode', 
    1211                      'net.stubbles.util.stubRegistry', 
    1312                      'net.stubbles.util.validators.stubEqualValidator', 
    1413                      'net.stubbles.util.validators.stubPreSelectValidator', 
     14                      'net.stubbles.websites.cache.stubCachableProcessor', 
    1515                      'net.stubbles.websites.processors.stubAbstractProcessor', 
    1616                      'net.stubbles.websites.memphis.stubMemphisConfig', 
     
    2323 * @subpackage  websites_memphis 
    2424 */ 
    25 class stubMemphisProcessor extends stubAbstractProcessor 
     25class stubMemphisProcessor extends stubAbstractProcessor implements stubCachableProcessor 
    2626{ 
    2727    /** 
     
    3737     */ 
    3838    protected $config; 
     39    /** 
     40     * website cache to be used 
     41     * 
     42     * @var  stubWebsiteCache 
     43     */ 
     44    protected $cache; 
    3945 
    4046    /** 
     
    5157        parent::__construct($request, $session, $response, $pageFactory); 
    5258        $this->config   = $this->createConfig(); 
    53         $this->template = $this->createTemplate(); 
     59    } 
     60 
     61    /** 
     62     * sets the website cache to be used by the processor 
     63     * 
     64     * @param  stubWebsiteCache  $websiteCache 
     65     */ 
     66    public function setWebsiteCache(stubWebsiteCache $websiteCache) 
     67    { 
     68        $this->cache = $websiteCache; 
    5469    } 
    5570 
     
    7287    protected function createTemplate() 
    7388    { 
    74         $template = new stubMemphisTemplate(stubRegistry::getConfig('net.stubbles.websites.memphis.templateDir', stubConfig::getPagePath() . '/../templates')); 
     89        $template = new stubMemphisTemplate(stubRegistry::getConfig('net.stubbles.websites.memphis.templateDir', stubConfig::getPagePath() . '/../templates')); 
    7590        if (stubMode::$CURRENT->isCacheEnabled() === true) { 
    7691            $template->enableCache(); 
     
    85100    protected function doProcess() 
    86101    { 
    87         try { 
    88             $page = $this->pageFactory->getPage($this->getPageName()); 
    89         } catch (stubPageConfigurationException $pce) { 
    90             $this->response->replaceData((string) $pce); 
    91             return; 
    92         } 
    93          
     102        $pageName      = $this->getPageName(); 
     103        $page          = $this->getPage($pageName); 
    94104        $elements      = $page->getElements(); 
    95105        $prefixRequest = new stubRequestPrefixDecorator($this->request, ''); 
    96         $content       = array(); 
    97         $context       = array('part'     => null, 
    98                                'template' => $this->template, 
    99                                'page'     => $page 
     106        $context       = array('part' => null, 
     107                               'page' => $page 
    100108                         ); 
    101          
    102         $this->template->readTemplatesFromFile($this->config->getFrame($this->getFrameId($page))); 
    103         $this->setTemplateVars($page); 
     109        $frameId       = $this->getFrameId($page); 
     110        if (null !== $this->cache) { 
     111            $this->processCacheVars($prefixRequest, $elements, $pageName, $frameId, $context); 
     112            if ($this->cache->retrieve($this->request, $this->response, $pageName) === true) { 
     113                return; 
     114            } 
     115        } 
     116         
     117        $this->template      = $this->createTemplate(); 
     118        $context['template'] = $this->template; 
     119        $this->template->readTemplatesFromInput($this->config->getFrame($frameId)); 
     120        $this->setTemplateVars($page, $pageName, $frameId); 
     121        $this->processPageElements($prefixRequest, $elements, $context); 
     122        if (null !== $this->cache) { 
     123            $this->cache->store($this->request, $this->response, $pageName); 
     124        } 
     125    } 
     126 
     127    /** 
     128     * retrieves the page instance for given page name 
     129     * 
     130     * Exception is only thrown when mode is not PROD, in mode PROD the page 
     131     * will not have any elements and the frame 404. 
     132     * 
     133     * @param   string    $pageName 
     134     * @return  stubPage 
     135     * @throws  stubPageConfigurationException 
     136     */ 
     137    protected function getPage($pageName) 
     138    { 
     139        try { 
     140            $page = $this->pageFactory->getPage($pageName); 
     141        } catch (stubPageConfigurationException $pce) { 
     142            if (stubMode::$CURRENT->name() !== 'PROD') { 
     143                throw $pce; 
     144            } 
     145             
     146            header('HTTP/1.0 404 Not Found'); 
     147            $page = new stubPage(); 
     148            $page->setProperty('frame', '404'); 
     149            $page->setProperty('frame_fixed', true); 
     150        } 
     151         
     152        return $page; 
     153    } 
     154 
     155    /** 
     156     * helper method to set the cache variables 
     157     * 
     158     * @param  stubRequest  $prefixRequest 
     159     * @param  array        $elements 
     160     * @param  string       $pageName 
     161     * @param  string       $frameId 
     162     * @param  array        $context 
     163     */ 
     164    protected function processCacheVars(stubRequest $prefixRequest, array $elements, $pageName, $frameId, array $context) 
     165    { 
     166        $this->cache->addCacheVar('page', $pageName); 
     167        $this->cache->addCacheVar('frame', $frameId); 
     168        $this->cache->addCacheVar('variant', $this->session->getValue('net.stubbles.websites.variantmanager.variant', '')); 
     169        foreach ($this->config->getParts() as $part) { 
     170            $context['part'] = $part; 
     171            foreach ($this->config->getDefaultElements($part) as $defaultElement) { 
     172                $prefixRequest->setPrefix($defaultElement->getName()); 
     173                $defaultElement->init($prefixRequest, $this->session, $this->response, $context); 
     174                if ($defaultElement->isAvailable() === false) { 
     175                    continue; 
     176                } 
     177                 
     178                $this->cache->addCacheVars($defaultElement->getCacheVars()); 
     179                $this->cache->addUsedFiles($defaultElement->getUsedFiles()); 
     180            } 
     181             
     182            foreach ($elements as $name => $element) { 
     183                $prefixRequest->setPrefix($name); 
     184                $element->init($prefixRequest, $this->session, $this->response, $context); 
     185                if ($element->isAvailable() === false) { 
     186                    continue; 
     187                } 
     188                 
     189                $this->cache->addCacheVars($element->getCacheVars()); 
     190                $this->cache->addUsedFiles($element->getUsedFiles()); 
     191            } 
     192        } 
     193    } 
     194 
     195    /** 
     196     * helper method to process the page elements 
     197     * 
     198     * @param  stubRequest  $prefixRequest 
     199     * @param  array        $elements 
     200     * @param  array        $context 
     201     */ 
     202    protected function processPageElements(stubRequest $prefixRequest, array $elements, array $context) 
     203    { 
    104204        foreach ($this->config->getParts() as $part) { 
    105205            $content         = ''; 
     
    107207            foreach ($this->config->getDefaultElements($part) as $defaultElement) { 
    108208                $prefixRequest->setPrefix($defaultElement->getName()); 
    109                 $content .= $this->processElement($defaultElement, $prefixRequest, $context); 
     209                $defaultElement->init($prefixRequest, $this->session, $this->response, $context); 
     210                if ($defaultElement->isAvailable() === false) { 
     211                    continue; 
     212                } 
     213                 
     214                $content .= $defaultElement->process(); 
    110215                if ($prefixRequest->isCancelled() === true) { 
    111216                    return; 
     
    115220            foreach ($elements as $name => $element) { 
    116221                $prefixRequest->setPrefix($name); 
    117                 $content .= $this->processElement($element, $prefixRequest, $context); 
     222                $element->init($prefixRequest, $this->session, $this->response, $context); 
     223                if ($element->isAvailable() === false) { 
     224                    continue; 
     225                } 
     226                 
     227                $content .= $element->process(); 
    118228                if ($prefixRequest->isCancelled() === true) { 
    119229                    return; 
     
    128238 
    129239    /** 
    130      * helper method to process a page element 
    131      * 
    132      * @param   stubPageElement  $element 
    133      * @param   array            $context 
    134      * @return  string 
    135      * @todo    better error handling 
    136      */ 
    137     protected function processElement(stubPageElement $element, stubRequest $request, array $context) 
    138     { 
    139         $element->init($request, $this->session, $this->response, $context); 
    140         if ($element->isAvailable() === false) { 
    141             return ''; 
    142         } 
    143                  
    144         try { 
    145             $content = $element->process(); 
    146         } catch (Exception $e) { 
    147             $content = 'An error occurred: ' . $e->getMessage(); 
    148         } 
    149           
    150         return $content; 
    151     } 
    152  
    153     /** 
    154240     * helper method to get the name of the frame to use 
    155241     * 
     
    159245    protected function getFrameId(stubPage $page) 
    160246    { 
    161         if ($this->request->hasValue('frame') == true) { 
     247        if ($page->hasProperty('frame_fixed') === true && $page->getProperty('frame_fixed') === true) { 
     248            return $page->getProperty('frame'); 
     249        } 
     250 
     251        if ($this->request->hasValue('frame') === true) { 
    162252            $frame = $this->request->getValidatedValue(new stubPreSelectValidator(array_keys($this->config->getFrames())), 'frame'); 
    163         } elseif ($this->session->hasValue('net.stubbles.websites.memphis.frame') == true) { 
     253        } elseif ($this->session->hasValue('net.stubbles.websites.memphis.frame') === true) { 
    164254            $frame = $this->session->getValue('net.stubbles.websites.memphis.frame'); 
    165255        } else { 
     
    178268     * 
    179269     * @param  stubPage  $page 
    180      */ 
    181     protected function setTemplateVars(stubPage $page) 
    182     { 
    183         $this->template->addGlobalVar('UCUO_FRAME', $this->getFrameId($page)); 
     270     * @param  string    $pageName 
     271     * @param  string    $frameId 
     272     */ 
     273    protected function setTemplateVars(stubPage $page, $pageName, $frameId) 
     274    { 
     275        $this->template->addGlobalVar('UCUO_FRAME', $frameId); 
    184276        $this->template->addGlobalVar('PAGE_TITLE', htmlentities($page->getProperty('title'))); 
    185         $this->template->addGlobalVar('PAGE_NAME', $this->getPageName()); 
     277        $this->template->addGlobalVar('PAGE_NAME', $pageName); 
    186278        $this->template->addGlobalVar('VARIANT', $this->session->getValue('net.stubbles.websites.variantmanager.variant', '')); 
    187279        $this->template->addGlobalVar('SID', $this->session->getName() . '=' . $this->session->getId()); 
     
    190282         
    191283        // check for user data 
    192         if ($this->session->hasValue('_userData') == true) { 
     284        if ($this->session->hasValue('_userData') === true) { 
    193285            $this->template->addGlobalVars($this->session->getValue('_userData'), 'USER_'); 
    194286        } 
     
    200292         
    201293        $sslMode = 'no'; 
    202         if ($this->request->validateValue(new stubEqualValidator(443), 'SERVER_PORT', stubRequest::SOURCE_HEADER) == true) { 
     294        if ($this->request->validateValue(new stubEqualValidator(443), 'SERVER_PORT', stubRequest::SOURCE_HEADER) === true) { 
    203295            $sslMode = 'yes'; 
    204296        } 
  • trunk/src/test/php/net/stubbles/websites/WebsitesTestSuite.php

    r1130 r1136  
    3434        $this->addTestFile($dir . '/memphis/stubMemphisIncludeFilePageElementTestCase.php'); 
    3535        $this->addTestFile($dir . '/memphis/stubMemphisPageElementTestCase.php'); 
    36        # $this->addTestFile($dir . '/memphis/stubMemphisProcessorTestCase.php'); 
     36        $this->addTestFile($dir . '/memphis/stubMemphisProcessorTestCase.php'); 
    3737 
    3838        // processors tests 
  • trunk/src/test/php/net/stubbles/websites/memphis/stubMemphisProcessorTestCase.php

    r317 r1136  
    11<?php 
    22/** 
    3  * Tests for net.stubbles.websites.memphis.stubMemphisProcessor 
     3 * Tests for net::stubbles::websites::memphis::stubMemphisProcessor. 
    44 * 
    55 * @author      Frank Kleine <mikey@stubbles.net> 
     
    1313Mock::generate('stubPageElement'); 
    1414Mock::generate('stubPageFactory'); 
     15Mock::generate('stubMemphisConfig'); 
     16Mock::generate('stubMemphisTemplate'); 
     17Mock::generate('stubWebsiteCache'); 
     18Mock::generatePartial('stubMemphisProcessor', 
     19                      'PartialMockstubMemphisProcessor', 
     20                      array('createTemplate', 
     21                            'getPageName', 
     22                            'getPage', 
     23                            'getFrameId', 
     24                            'processCacheVars', 
     25                            'setTemplateVars', 
     26                            'processPageElements' 
     27                      ) 
     28); 
    1529/** 
    16  * Tests for net.stubbles.websites.memphis.stubMemphisProcessor 
     30 * Page factory that will always throw an exception when a page is requested. 
     31 * 
     32 * @package     stubbles 
     33 * @subpackage  websites_memphis_test 
     34 */ 
     35class stubExceptionPageFactory extends stubBaseObject implements stubPageFactory 
     36
     37    /** 
     38     * checks whether the page factory knows the page or not 
     39     * 
     40     * @param   string  $configSource  source of the page configuration to use 
     41     * @return  bool 
     42     */ 
     43    public function hasPage($configSource) 
     44    { 
     45        return true; 
     46    } 
     47     
     48    /** 
     49     * returns the configured stubPage instance 
     50     * 
     51     * @param   string    $configSource  source of the page configuration to use 
     52     * @return  stubPage 
     53     * @throws  stubPageConfigurationException 
     54     */ 
     55    public function getPage($configSource) 
     56    { 
     57        throw new stubPageConfigurationException('Page not found'); 
     58    } 
     59
     60/** 
     61 * Extend tested class to be able to inject mock instances. 
     62 * 
     63 * @package     stubbles 
     64 * @subpackage  websites_memphis_test 
     65 */ 
     66class TeststubMemphisProcessor extends stubMemphisProcessor 
     67
     68    /** 
     69     * sets the page factory to be used 
     70     * 
     71     * @param  stubPageFactory  $pageFactory 
     72     */ 
     73    public function setPageFactory(stubPageFactory $pageFactory) 
     74    { 
     75        $this->pageFactory = $pageFactory; 
     76    } 
     77 
     78    /** 
     79     * sets the memphis config instance (no type hint to allow mocks from concrete class) 
     80     * 
     81     * @param  stubMemphisConfig  $config 
     82     */ 
     83    public function setConfig($config) 
     84    { 
     85        $this->config = $config; 
     86    } 
     87 
     88    /** 
     89     * helper method to create the config object 
     90     * 
     91     * @return  stubMemphisConfig 
     92     * @throws  stubException 
     93     */ 
     94    protected function createConfig() 
     95    { 
     96        return null; 
     97    } 
     98 
     99    /** 
     100     * sets the memphis template instance (no type hint to allow mocks from concrete class) 
     101     * 
     102     * @param  stubMemphisTemplate  $template 
     103     */ 
     104    public function setTemplate($template) 
     105    { 
     106        $this->template = $template; 
     107    } 
     108 
     109    /** 
     110     * helper method to create the template 
     111     * 
     112     * @return  stubMemphisTemplate 
     113     */ 
     114    protected function createTemplate() 
     115    { 
     116        return $this->template; 
     117    } 
     118 
     119    /** 
     120     * helper method to access protected method processCacheVars() 
     121     * 
     122     * @param   string    $pageName 
     123     * @return  stubPage 
     124     * @throws  stubPageConfigurationException 
     125     */ 
     126    public function callGetPage($pageName) 
     127    { 
     128        return $this->getPage($pageName); 
     129    } 
     130 
     131    /** 
     132     * helper method to access protected method processCacheVars() 
     133     * 
     134     * @param  stubRequest  $prefixRequest 
     135     * @param  array        $elements 
     136     * @param  string       $pageName 
     137     * @param  string       $frameId 
     138     * @param  array        $context 
     139     */ 
     140    public function callProcessCacheVars(stubRequest $prefixRequest, array $elements, $pageName, $frameId, array $context) 
     141    { 
     142        $this->processCacheVars($prefixRequest, $elements, $pageName, $frameId, $context); 
     143    } 
     144 
     145    /** 
     146     * helper method to access protected method processPageElements() 
     147     * 
     148     * @param  stubRequest  $prefixRequest 
     149     * @param  array        $elements 
     150     * @param  array        $context 
     151     */ 
     152    public function callProcessPageElements(stubRequest $prefixRequest, array $elements, array $context) 
     153    { 
     154        $this->processPageElements($prefixRequest, $elements, $context); 
     155    } 
     156 
     157    /** 
     158     * helper method to access protected method getFrameId() 
     159     * 
     160     * @param   stubPage  $page 
     161     * @return  string 
     162     */ 
     163    public function callGetFrameId(stubPage $page) 
     164    { 
     165        return $this->getFrameId($page); 
     166    } 
     167    /** 
     168     * helper method to access protected method setTemplateVars() 
     169     * 
     170     * @param  stubPage  $page 
     171     * @param  string    $pageName 
     172     * @param  string    $frameId 
     173     */ 
     174    public function callSetTemplateVars(stubPage $page, $pageName, $frameId) 
     175    { 
     176        return $this->setTemplateVars($page, $pageName, $frameId); 
     177    } 
     178
     179/** 
     180 * Extend tested class to be able to test processing algorithm. 
     181 * 
     182 * @package     stubbles 
     183 * @subpackage  websites_memphis_test 
     184 */ 
     185class TestPartialMockstubMemphisProcessor extends PartialMockstubMemphisProcessor 
     186
     187    /** 
     188     * constructor 
     189     * 
     190     * @param   stubRequest      $request      the current request 
     191     * @param   stubSession      $session      the current session 
     192     * @param   stubResponse     $response     the current response 
     193     * @param   stubPageFactory  $pageFactory  page factory to use to read the page configuration 
     194     */ 
     195    public function __construct(stubRequest $request, stubSession $session, stubResponse $response, stubPageFactory $pageFactory) 
     196    { 
     197        $this->request     = $request; 
     198        $this->session     = $session; 
     199        $this->response    = $response; 
     200        $this->pageFactory = $pageFactory; 
     201        parent::__construct(); 
     202    } 
     203 
     204    /** 
     205     * sets the memphis config instance (no type hint to allow mocks from concrete class) 
     206     * 
     207     * @param  stubMemphisConfig  $config 
     208     */ 
     209    public function setConfig($config) 
     210    { 
     211        $this->config = $config; 
     212    } 
     213 
     214    /** 
     215     * helper method to call protected method doProcess() 
     216     */ 
     217    public function callDoProcess() 
     218    { 
     219        $this->doProcess(); 
     220    } 
     221
     222/** 
     223 * Tests for net::stubbles::websites::memphis::stubMemphisProcessor. 
     224 * 
     225 * We need to test the single methods because else the unit to test becomes to 
     226 * great. 
    17227 * 
    18228 * @package     stubbles 
     
    57267     */ 
    58268    protected $mockPage; 
    59      
     269    /** 
     270     * mocked memphis config instance 
     271     * 
     272     * @var  SimpleMock 
     273     */ 
     274    protected $mockMemphisConfig; 
     275    /** 
     276     * mocked memphis template instance 
     277     * 
     278     * @var  SimpleMock 
     279     */ 
     280    protected $mockMemphisTemplate; 
     281    /** 
     282     * current mode for restoring 
     283     * 
     284     * @var  stubMode 
     285     */ 
     286    protected $currentMode; 
     287 
    60288    /** 
    61289     * set up test environment 
     
    63291    public function setUp() 
    64292    { 
    65         $this->mockRequest      = new MockstubRequest(); 
    66         $this->mockSession      = new MockstubSession(); 
    67         $this->mockResponse     = new MockstubResponse(); 
    68         $this->mockPageFactory  = new MockstubPageFactory(); 
    69         $this->memphisProcessor = new stubMemphisProcessor($this->mockRequest, $this->mockSession, $this->mockResponse, $this->mockPageFactory); 
     293        $this->mockRequest       = new MockstubRequest(); 
     294        $this->mockSession       = new MockstubSession(); 
     295        $this->mockResponse      = new MockstubResponse(); 
     296        $this->mockPageFactory   = new MockstubPageFactory(); 
     297        $this->memphisProcessor  = new TeststubMemphisProcessor($this->mockRequest, $this->mockSession, $this->mockResponse, $this->mockPageFactory); 
     298        $this->mockMemphisConfig = new MockstubMemphisConfig(); 
     299        $this->memphisProcessor->setConfig($this->mockMemphisConfig); 
     300        $this->mockMemphisTemplate = new MockstubMemphisTemplate(); 
     301        $this->memphisProcessor->setTemplate($this->mockMemphisTemplate); 
    70302        $this->mockPage = new MockstubPage(); 
    71303        $this->mockPageFactory->setReturnValue('getPage', $this->mockPage); 
    72     } 
    73      
    74     /** 
    75      * assure that processing works as expected 
    76      */ 
    77     public function testDefaultPageWithoutElements() 
    78     { 
     304        $this->currentMode = stubMode::$CURRENT; 
     305    } 
     306 
     307    /** 
     308     * clean up test environment 
     309     */ 
     310    public function tearDown() 
     311    { 
     312        stubMode::setCurrent($this->currentMode); 
     313    } 
     314 
     315    /** 
     316     * test the processing algorithm without using a cache 
     317     */ 
     318    public function testDoProcessWithoutCache() 
     319    { 
     320        $memphisProcessor = new TestPartialMockstubMemphisProcessor($this->mockRequest, $this->mockSession, $this->mockResponse, $this->mockPageFactory); 
     321        $memphisProcessor->setConfig($this->mockMemphisConfig); 
     322        $memphisProcessor->setReturnValue('createTemplate', $this->mockMemphisTemplate); 
     323        $memphisProcessor->setReturnValue('getPageName', 'foo'); 
     324        $memphisProcessor->setReturnValue('getFrameId', 'bar'); 
     325        $memphisProcessor->setReturnValue('getPage', $this->mockPage); 
     326        $mockPageElement = new MockstubPageElement(); 
     327        $this->mockPage->setReturnValue('getElements', array('baz' => $mockPageElement)); 
     328        $memphisProcessor->expectNever('processCacheVars'); 
     329        $this->mockMemphisConfig->expectOnce('getFrame', array('bar')); 
     330        $this->mockMemphisConfig->setReturnValue('getFrame', 'frame/default.tmpl'); 
     331        $this->mockMemphisTemplate->expectOnce('readTemplatesFromInput', array('frame/default.tmpl')); 
     332        $memphisProcessor->expectOnce('setTemplateVars', array('*', 'foo', 'bar')); 
     333        $memphisProcessor->expectOnce('processPageElements', array('*', array('baz' => $mockPageElement), '*')); 
     334        $memphisProcessor->callDoProcess(); 
     335    } 
     336 
     337    /** 
     338     * test the processing algorithm using a cache 
     339     */ 
     340    public function testDoProcessWithCacheButUncached() 
     341    { 
     342        $memphisProcessor = new TestPartialMockstubMemphisProcessor($this->mockRequest, $this->mockSession, $this->mockResponse, $this->mockPageFactory); 
     343        $memphisProcessor->setConfig($this->mockMemphisConfig); 
     344        $memphisProcessor->setReturnValue('createTemplate', $this->mockMemphisTemplate); 
     345        $memphisProcessor->setReturnValue('getPageName', 'foo'); 
     346        $memphisProcessor->setReturnValue('getFrameId', 'bar'); 
     347        $memphisProcessor->setReturnValue('getPage', $this->mockPage); 
     348        $mockPageElement = new MockstubPageElement(); 
     349        $this->mockPage->setReturnValue('getElements', array('baz' => $mockPageElement)); 
     350        $memphisProcessor->expectOnce('processCacheVars', array('*', array('baz' => $mockPageElement), 'foo', 'bar', '*')); 
     351        $this->mockMemphisConfig->expectOnce('getFrame', array('bar')); 
     352        $this->mockMemphisConfig->setReturnValue('getFrame', 'frame/default.tmpl'); 
     353        $this->mockMemphisTemplate->expectOnce('readTemplatesFromInput', array('frame/default.tmpl')); 
     354        $memphisProcessor->expectOnce('setTemplateVars', array('*', 'foo', 'bar')); 
     355        $memphisProcessor->expectOnce('processPageElements', array('*', array('baz' => $mockPageElement), '*')); 
     356        $mockCache = new MockstubWebsiteCache(); 
     357        $mockCache->expectOnce('retrieve', array($this->mockRequest, $this->mockResponse, 'foo')); 
     358        $mockCache->setReturnValue('retrieve', false); 
     359        $mockCache->expectOnce('store', array($this->mockRequest, $this->mockResponse, 'foo')); 
     360        $memphisProcessor->setWebsiteCache($mockCache); 
     361        $memphisProcessor->callDoProcess(); 
     362    } 
     363 
     364    /** 
     365     * test the processing algorithm using a cache 
     366     */ 
     367    public function testDoProcessWithCacheAndCached() 
     368    { 
     369        $memphisProcessor = new TestPartialMockstubMemphisProcessor($this->mockRequest, $this->mockSession, $this->mockResponse, $this->mockPageFactory); 
     370        $memphisProcessor->setConfig($this->mockMemphisConfig); 
     371        $memphisProcessor->expectNever('createTemplate'); 
     372        $memphisProcessor->setReturnValue('getPageName', 'foo'); 
     373        $memphisProcessor->setReturnValue('getFrameId', 'bar'); 
     374        $memphisProcessor->setReturnValue('getPage', $this->mockPage); 
     375        $mockPageElement = new MockstubPageElement(); 
     376        $this->mockPage->setReturnValue('getElements', array('baz' => $mockPageElement)); 
     377        $memphisProcessor->expectOnce('processCacheVars', array('*', array('baz' => $mockPageElement), 'foo', 'bar', '*')); 
     378        $this->mockMemphisConfig->expectNever('getFrame'); 
     379        $this->mockMemphisTemplate->expectNever('readTemplatesFromInput'); 
     380        $memphisProcessor->expectNever('setTemplateVars'); 
     381        $memphisProcessor->expectNever('processPageElements'); 
     382        $mockCache = new MockstubWebsiteCache(); 
     383        $mockCache->expectOnce('retrieve', array($this->mockRequest, $this->mockResponse, 'foo')); 
     384        $mockCache->setReturnValue('retrieve', true); 
     385        $mockCache->expectNever('store'); 
     386        $memphisProcessor->setWebsiteCache($mockCache); 
     387        $memphisProcessor->callDoProcess(); 
     388    } 
     389 
     390    /** 
     391     * test that correct page instance is created 
     392     */ 
     393    public function testGetPage() 
     394    { 
     395        $this->mockPageFactory->expectOnce('getPage', array('foo')); 
     396        $this->assertEqual($this->memphisProcessor->callGetPage('foo'), $this->mockPage); 
     397    } 
     398 
     399    /** 
     400     * test that correct page instance is created 
     401     */ 
     402    public function testGetPageButNotFoundModeNotProd() 
     403    { 
     404        stubMode::setCurrent(stubMode::$DEV); 
     405        $pageFactory = new stubExceptionPageFactory(); 
     406        $this->memphisProcessor->setPageFactory($pageFactory); 
     407        $this->expectException('stubPageConfigurationException'); 
     408        $this->memphisProcessor->callGetPage('foo'); 
     409    } 
     410 
     411    /** 
     412     * test that correct page instance is created 
     413     */ 
     414    public function testGetPageButNotFoundModeIsProd() 
     415    { 
     416        stubMode::setCurrent(stubMode::$PROD); 
     417        $pageFactory = new stubExceptionPageFactory(); 
     418        $this->memphisProcessor->setPageFactory($pageFactory); 
     419        $page = $this->memphisProcessor->callGetPage('foo'); 
     420        $this->assertIsA($page, 'stubPage'); 
     421        $this->assertEqual($page->getProperty('frame'), '404'); 
     422        $this->assertTrue($page->getProperty('frame_fixed')); 
     423    } 
     424 
     425    /** 
     426     * test that collecting the cache variables works as expected 
     427     */ 
     428    public function testProcessCacheVars() 
     429    { 
     430        $prefixRequest = new stubRequestPrefixDecorator($this->mockRequest, ''); 
     431        $this->mockMemphisConfig->setReturnValue('getParts', array('content', 'teaser')); 
     432        $mockDefaultPageElement1 = new MockstubPageElement(); 
     433        $mockDefaultPageElement1->setReturnValue('getName', 'defaultMock1'); 
     434        $mockDefaultPageElement1->expectCallCount('init', 2); 
     435        $mockDefaultPageElement1->expectAt(0, 'init', array($prefixRequest, $this->mockSession, $this->mockResponse, array('part' => 'content'))); 
     436        $mockDefaultPageElement1->expectAt(1, 'init', array($prefixRequest, $this->mockSession, $this->mockResponse, array('part' => 'teaser'))); 
     437        $mockDefaultPageElement1->setReturnValue('isAvailable', false); 
     438        $mockDefaultPageElement1->expectNever('getCacheVars'); 
     439        $mockDefaultPageElement1->expectNever('getUsedFiles'); 
     440        $mockDefaultPageElement2 = new MockstubPageElement(); 
     441        $mockDefaultPageElement2->setReturnValue('getName', 'defaultMock2'); 
     442        $mockDefaultPageElement2->expectCallCount('init', 2); 
     443        $mockDefaultPageElement2->expectAt(0, 'init', array($prefixRequest, $this->mockSession, $this->mockResponse, array('part' => 'content'))); 
     444        $mockDefaultPageElement2->expectAt(1, 'init', array($prefixRequest, $this->mockSession, $this->mockResponse, array('part' => 'teaser'))); 
     445        $mockDefaultPageElement2->setReturnValue('isAvailable', true); 
     446        $mockDefaultPageElement2->expectCallCount('getCacheVars', 2); 
     447        $mockDefaultPageElement2->setReturnValue('getCacheVars', array('one' => 'defaultMock2')); 
     448        $mockDefaultPageElement2->expectCallCount('getUsedFiles', 2); 
     449        $mockDefaultPageElement2->setReturnValue('getUsedFiles', array('defaultMock2.tmpl')); 
     450        $this->mockMemphisConfig->setReturnValue('getDefaultElements', array($mockDefaultPageElement1, $mockDefaultPageElement2)); 
     451        $mockPageElement1 = new MockstubPageElement(); 
     452        $mockPageElement1->setReturnValue('getName', 'Mock1'); 
     453        $mockPageElement1->expectCallCount('init', 2); 
     454        $mockPageElement1->expectAt(0, 'init', array($prefixRequest, $this->mockSession, $this->mockResponse, array('part' => 'content'))); 
     455        $mockPageElement1->expectAt(1, 'init', array($prefixRequest, $this->mockSession, $this->mockResponse, array('part' => 'teaser'))); 
     456        $mockPageElement1->setReturnValue('isAvailable', false); 
     457        $mockPageElement1->expectNever('getCacheVars'); 
     458        $mockPageElement1->expectNever('getUsedFiles'); 
     459        $mockPageElement2 = new MockstubPageElement(); 
     460        $mockPageElement2->setReturnValue('getName', 'Mock2'); 
     461        $mockPageElement2->expectCallCount('init', 2); 
     462        $mockPageElement2->expectAt(0, 'init', array($prefixRequest, $this->mockSession, $this->mockResponse, array('part' => 'content'))); 
     463        $mockPageElement2->expectAt(1, 'init', array($prefixRequest, $this->mockSession, $this->mockResponse, array('part' => 'teaser'))); 
     464        $mockPageElement2->setReturnValue('isAvailable', true); 
     465        $mockPageElement2->expectCallCount('getCacheVars', 2); 
     466        $mockPageElement2->setReturnValue('getCacheVars', array('two' => 'Mock2')); 
     467        $mockPageElement2->expectCallCount('getUsedFiles', 2); 
     468        $mockPageElement2->setReturnValue('getUsedFiles', array('Mock2.tmpl')); 
     469        $mockCache = new MockstubWebsiteCache(); 
     470        $mockCache->expectAt(0, 'addCacheVar', array('page', 'foo')); 
     471        $mockCache->expectAt(1, 'addCacheVar', array('frame', 'bar')); 
     472        $mockCache->expectAt(2, 'addCacheVar', array('variant', null)); 
     473        $mockCache->expectAt(0, 'addCacheVars', array(array('one' => 'defaultMock2'))); 
     474        $mockCache->expectAt(1, 'addCacheVars', array(array('two' => 'Mock2'))); 
     475        $mockCache->expectAt(2, 'addCacheVars', array(array('one' => 'defaultMock2'))); 
     476        $mockCache->expectAt(3, 'addCacheVars', array(array('two' => 'Mock2'))); 
     477        $mockCache->expectCallcount('addCacheVar', 3); 
     478        $mockCache->expectCallcount('addCacheVars', 4); 
     479        $mockCache->expectAt(0, 'addUsedFiles', array(array('defaultMock2.tmpl'))); 
     480        $mockCache->expectAt(1, 'addUsedFiles', array(array('Mock2.tmpl'))); 
     481        $mockCache->expectAt(2, 'addUsedFiles', array(array('defaultMock2.tmpl'))); 
     482        $mockCache->expectAt(3, 'addUsedFiles', array(array('Mock2.tmpl'))); 
     483        $mockCache->expectCallcount('addUsedFiles', 4); 
     484        $this->memphisProcessor->setWebsiteCache($mockCache); 
     485        $this->memphisProcessor->callProcessCacheVars($prefixRequest, array('mock1' => $mockPageElement1, 'mock2' => $mockPageElement2), 'foo', 'bar', array()); 
     486    } 
     487 
     488    /** 
     489     * test that processing of page elements works as expected 
     490     */ 
     491    public function testProcessPageElements() 
     492    { 
     493        $prefixRequest = new stubRequestPrefixDecorator($this->mockRequest, ''); 
     494        $this->mockRequest->setReturnValue('isCancelled', false); 
     495        $this->mockMemphisConfig->setReturnValue('getParts', array('content', 'teaser')); 
     496        $mockDefaultPageElement1 = new MockstubPageElement(); 
     497        $mockDefaultPageElement1->setReturnValue('getName', 'defaultMock1'); 
     498        $mockDefaultPageElement1->expectCallCount('init', 2); 
     499        $mockDefaultPageElement1->expectAt(0, 'init', array($prefixRequest, $this->mockSession, $this->mockResponse, array('part' => 'content'))); 
     500        $mockDefaultPageElement1->expectAt(1, 'init', array($prefixRequest, $this->mockSession, $this->mockResponse, array('part' => 'teaser'))); 
     501        $mockDefaultPageElement1->setReturnValue('isAvailable', false); 
     502        $mockDefaultPageElement1->expectNever('process'); 
     503        $mockDefaultPageElement2 = new MockstubPageElement(); 
     504        $mockDefaultPageElement2->setReturnValue('getName', 'defaultMock2'); 
     505        $mockDefaultPageElement2->expectCallCount('init', 2); 
     506        $mockDefaultPageElement2->expectAt(0, 'init', array($prefixRequest, $this->mockSession, $this->mockResponse, array('part' => 'content'))); 
     507        $mockDefaultPageElement2->expectAt(1, 'init', array($prefixRequest, $this->mockSession, $this->mockResponse, array('part' => 'teaser'))); 
     508        $mockDefaultPageElement2->setReturnValue('isAvailable', true); 
     509        $mockDefaultPageElement2->expectCallCount('process', 2); 
     510        $mockDefaultPageElement2->setReturnValue('process', 'defaultMock2'); 
     511        $this->mockMemphisConfig->setReturnValue('getDefaultElements', array($mockDefaultPageElement1, $mockDefaultPageElement2)); 
     512        $mockPageElement1 = new MockstubPageElement(); 
     513        $mockPageElement1->setReturnValue('getName', 'Mock1'); 
     514        $mockPageElement1->expectCallCount('init', 2); 
     515        $mockPageElement1->expectAt(0, 'init', array($prefixRequest, $this->mockSession, $this->mockResponse, array('part' => 'content'))); 
     516        $mockPageElement1->expectAt(1, 'init', array($prefixRequest, $this->mockSession, $this->mockResponse, array('part' => 'teaser'))); 
     517        $mockPageElement1->setReturnValue('isAvailable', false); 
     518        $mockPageElement1->expectNever('process'); 
     519        $mockPageElement2 = new MockstubPageElement(); 
     520        $mockPageElement2->setReturnValue('getName', 'Mock2'); 
     521        $mockPageElement2->expectCallCount('init', 2); 
     522        $mockPageElement2->expectAt(0, 'init', array($prefixRequest, $this->mockSession, $this->mockResponse, array('part' => 'content'))); 
     523        $mockPageElement2->expectAt(1, 'init', array($prefixRequest, $this->mockSession, $this->mockResponse, array('part' => 'teaser'))); 
     524        $mockPageElement2->setReturnValue('isAvailable', true); 
     525        $mockPageElement2->expectCallCount('process', 2); 
     526        $mockPageElement2->setReturnValue('process', 'Mock2'); 
     527        $this->mockMemphisTemplate->expectAt(0, 'addGlobalVar', array('content', 'defaultMock2Mock2')); 
     528        $this->mockMemphisTemplate->expectAt(1, 'addGlobalVar', array('teaser', 'defaultMock2Mock2')); 
     529        $this->mockMemphisTemplate->setReturnValue('getParsedTemplate', 'defaultMock2Mock2defaultMock2Mock2'); 
     530        $this->mockResponse->expectOnce('write', array('defaultMock2Mock2defaultMock2Mock2')); 
     531        $this->memphisProcessor->callProcessPageElements($prefixRequest, array('mock1' => $mockPageElement1, 'mock2' => $mockPageElement2), array()); 
     532    } 
     533 
     534    /** 
     535     * test that processing of page elements works as expected 
     536     */ 
     537    public function testProcessPageElementsWithRequestCancelledByPageElement() 
     538    { 
     539        $prefixRequest = new stubRequestPrefixDecorator($this->mockRequest, ''); 
     540        // just two, as the first default element is not available 
     541        $this->mockRequest->setReturnValueAt(0, 'isCancelled', false); 
     542        $this->mockRequest->setReturnValueAt(1, 'isCancelled', true); 
     543        $this->mockMemphisConfig->setReturnValue('getParts', array('content', 'teaser')); 
     544        $mockDefaultPageElement1 = new MockstubPageElement(); 
     545        $mockDefaultPageElement1->setReturnValue('getName', 'defaultMock1'); 
     546        $mockDefaultPageElement1->expectOnce('init'); 
     547        $mockDefaultPageElement1->expect('init', array($prefixRequest, $this->mockSession, $this->mockResponse, array('part' => 'content'))); 
     548        $mockDefaultPageElement1->setReturnValue('isAvailable', false); 
     549        $mockDefaultPageElement1->expectNever('process'); 
     550        $mockDefaultPageElement2 = new MockstubPageElement(); 
     551        $mockDefaultPageElement2->setReturnValue('getName', 'defaultMock2'); 
     552        $mockDefaultPageElement2->expectOnce('init'); 
     553        $mockDefaultPageElement2->expect('init', array($prefixRequest, $this->mockSession, $this->mockResponse, array('part' => 'content'))); 
     554        $mockDefaultPageElement2->setReturnValue('isAvailable', true); 
     555        $mockDefaultPageElement2->expectOnce('process'); 
     556        $mockDefaultPageElement2->setReturnValue('process', 'defaultMock2'); 
     557        $this->mockMemphisConfig->setReturnValue('getDefaultElements', array($mockDefaultPageElement1, $mockDefaultPageElement2)); 
     558        $mockPageElement1 = new MockstubPageElement(); 
     559        $mockPageElement1->setReturnValue('getName', 'Mock1'); 
     560        $mockPageElement1->expectOnce('init'); 
     561        $mockPageElement1->expect('init', array($prefixRequest, $this->mockSession, $this->mockResponse, array('part' => 'content'))); 
     562        $mockPageElement1->setReturnValue('isAvailable', true); 
     563        $mockPageElement1->expectOnce('process'); 
     564        $mockPageElement2 = new MockstubPageElement(); 
     565        $mockPageElement2->setReturnValue('getName', 'Mock2'); 
     566        $mockPageElement2->expectNever('init'); 
     567        $mockPageElement2->expectNever('isAvailable'); 
     568        $mockPageElement2->expectNever('process'); 
     569        $this->mockMemphisTemplate->expectNever('addGlobalVar'); 
     570        $this->mockResponse->expectNever('write'); 
     571        $this->memphisProcessor->callProcessPageElements($prefixRequest, array('mock1' => $mockPageElement1, 'mock2' => $mockPageElement2), array()); 
     572    } 
     573 
     574    /** 
     575     * test that processing of page elements works as expected 
     576     */ 
     577    public function testProcessPageElementsWithRequestCancelledByDefaultPageElement() 
     578    { 
     579        $prefixRequest = new stubRequestPrefixDecorator($this->mockRequest, ''); 
     580        $this->mockRequest->setReturnValue('isCancelled', true); 
     581        $this->mockMemphisConfig->setReturnValue('getParts', array('content', 'teaser')); 
     582        $mockDefaultPageElement1 = new MockstubPageElement(); 
     583        $mockDefaultPageElement1->setReturnValue('getName', 'defaultMock1'); 
     584        $mockDefaultPageElement1->expectOnce('init'); 
     585        $mockDefaultPageElement1->expect('init', array($prefixRequest, $this->mockSession, $this->mockResponse, array('part' => 'content'))); 
     586        $mockDefaultPageElement1->setReturnValue('isAvailable', true); 
     587        $mockDefaultPageElement1->expectOnce('process'); 
     588        $mockDefaultPageElement2 = new MockstubPageElement(); 
     589        $mockDefaultPageElement2->setReturnValue('getName', 'defaultMock2'); 
     590        $mockDefaultPageElement2->expectNever('init'); 
     591        $mockDefaultPageElement2->expectNever('isAvailable'); 
     592        $mockDefaultPageElement2->expectNever('process'); 
     593        $this->mockMemphisConfig->setReturnValue('getDefaultElements', array($mockDefaultPageElement1, $mockDefaultPageElement2)); 
     594        $mockPageElement1 = new MockstubPageElement(); 
     595        $mockPageElement1->setReturnValue('getName', 'Mock1'); 
     596        $mockPageElement1->expectNever('init'); 
     597        $mockPageElement1->expectNever('isAvailable'); 
     598        $mockPageElement1->expectNever('process'); 
     599        $mockPageElement2 = new MockstubPageElement(); 
     600        $mockPageElement2->setReturnValue('getName', 'Mock2'); 
     601        $mockPageElement2->expectNever('init'); 
     602        $mockPageElement2->expectNever('isAvailable'); 
     603        $mockPageElement2->expectNever('process'); 
     604        $this->mockMemphisTemplate->expectNever('addGlobalVar'); 
     605        $this->mockResponse->expectNever('write'); 
     606        $this->memphisProcessor->callProcessPageElements($prefixRequest, array('mock1' => $mockPageElement1, 'mock2' => $mockPageElement2), array()); 
     607    } 
     608 
     609    /** 
     610     * test that request is responsible for choosing the frame 
     611     */ 
     612    public function testGetFrameIdFromRequest() 
     613    { 
     614        $this->mockMemphisConfig->setReturnValue('getFrames', array('default' => 'frame/default.tmpl')); 
     615        $page = new stubPage(); 
     616        $this->mockRequest->setReturnValue('hasValue', true); 
     617        $this->mockRequest->setReturnValueAt(0, 'getValidatedValue', 'another'); 
     618        $this->mockRequest->setReturnValueAt(1, 'getValidatedValue', ''); 
     619        $this->mockRequest->setReturnValueAt(2, 'getValidatedValue', null); 
     620        $this->mockSession->expectNever('hasValue'); 
     621        $this->assertEqual($this->memphisProcessor->callGetFrameId($page), 'another'); 
     622        $this->assertEqual($this->memphisProcessor->callGetFrameId($page), 'default'); 
     623        $this->assertEqual($this->memphisProcessor->callGetFrameId($page), 'default'); 
     624    } 
     625 
     626    /** 
     627     * test that session is responsible for choosing the frame 
     628     */ 
     629    public function testGetFrameIdFromSession() 
     630    { 
     631        $page = new stubPage(); 
    79632        $this->mockRequest->setReturnValue('hasValue', false); 
    80         $this->mockPage->setReturnValue('getElements', array()); 
    81         $this->mockPageFactory->expectOnce('getPage', array('memphis/index')); 
    82         $return = $this->memphisProcessor->process(); 
    83         $this->assertReference($this->memphisProcessor, $return); 
    84         $response = $this->memphisProcessor->getResponse(); 
    85         $this->assertIsA($response, 'stubResponse'); 
    86         $this->assertEqual($response->getData(), ''); 
    87     } 
    88      
    89     /** 
    90      * assure that processing works as expected 
    91      */ 
    92     public function testFallbackToDefaultPageWithElements() 
    93     { 
    94         $this->mockRequest->setReturnValue('hasValue', true); 
    95         $this->mockRequest->setReturnValue('getValidatedValue', null); 
    96         $this->mockPageFactory->expectOnce('getPage', array('memphis/index')); 
    97         $pageElement1 = new MockstubPageElement(); 
    98         $response = $this->memphisProcessor->getResponse(); 
    99         $pageElement1->setReturnValue('isAvailable', true); 
    100         $pageElement1->expectOnce('process'); 
    101         $pageElement2 = new MockstubPageElement(); 
    102         $pageElement2->setReturnValue('isAvailable', true); 
    103         $pageElement2->expectOnce('process'); 
    104         $pageElement3 = new MockstubPageElement(); 
    105         $pageElement3->setReturnValue('isAvailable', false); 
    106         $pageElement3->expectNever('process'); 
    107         $this->mockPage->setReturnValue('getElements', array('foo' => $pageElement1, 'bar' => $pageElement2, 'baz' => $pageElement3)); 
    108         $return = $this->memphisProcessor->process(); 
    109         $this->assertReference($this->memphisProcessor, $return); 
    110     } 
    111      
    112     /** 
    113      * assure that processing works as expected 
    114      */ 
    115     public function testCorrectPage() 
    116     { 
    117         $this->mockRequest->setReturnValue('hasValue', true); 
    118         $this->mockRequest->setReturnValue('getValidatedValue', 'baz'); 
    119         $this->mockPageFactory->expectOnce('getPage', array('memphis/baz')); 
    120         $this->mockPageFactory->setReturnValue('hasPage', true); 
    121         $this->mockPage->setReturnValue('getElements', array()); 
    122         $this->memphisProcessor->process(); 
     633        $this->mockRequest->expectNever('getValidatedValue'); 
     634        $this->mockSession->setReturnValue('hasValue', true); 
     635        $this->mockSession->setReturnValueAt(0, 'getValue', 'another'); 
     636        $this->mockSession->setReturnValueAt(1, 'getValue', ''); 
     637        $this->mockSession->setReturnValueAt(2, 'getValue', null); 
     638        $this->asser