Changeset 1447

Show
Ignore:
Timestamp:
03/22/08 23:18:11 (2 months ago)
Author:
mikey
Message:

refactoring #137, part 8 (and final): updated tests

Files:

Legend:

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

    r1445 r1447  
    6868 
    6969    /** 
    70      * returns the page factory class for the processor 
    71      * 
    72      * @param   stubProcessor  $processor 
    73      * @return  string 
    74      */ 
    75     protected abstract function getPageFactoryClass(stubProcessor $processor); 
    76  
    77     /** 
    7870     * helper method to handle page based processors 
    7971     * 
     
    9385        } 
    9486         
    95         stubClassLoader::load($pageFactoryClass); 
    9687        $nqClassName = stubClassLoader::getNonQualifiedClassName($pageFactoryClass); 
     88        if (class_exists($nqClassName, false) === false) { 
     89            stubClassLoader::load($pageFactoryClass); 
     90        } 
     91         
    9792        $pageFactory = new $nqClassName(); 
    9893        if (($pageFactory instanceof stubPageFactory) === false) { 
     
    10297        $processor->selectPage($pageFactory); 
    10398    } 
     99 
     100    /** 
     101     * returns the page factory class for the processor 
     102     * 
     103     * @param   stubProcessor  $processor 
     104     * @return  string 
     105     */ 
     106    protected abstract function getPageFactoryClass(stubProcessor $processor); 
    104107} 
    105108?> 
  • trunk/src/main/php/net/stubbles/websites/processors/stubDefaultProcessorResolver.php

    r1445 r1447  
    22/** 
    33 * Default implementation for the processor resolver. 
    4  *  
     4 * 
    55 * @author      Frank Kleine <mikey@stubbles.net> 
    66 * @package     stubbles 
     
    1313/** 
    1414 * Default implementation for the processor resolver. 
     15 * 
     16 * The default processor resolver is able to select the processor to be used 
     17 * for the current request depending on the request parameter <em>processor</em>. 
     18 * For instance, if you add a processor with 
     19 * <code> 
     20 *   $defaultProcessor->addProcessor('foo', 'org::stubbles::test::FooProcessor'); 
     21 *   $defaultProcessor->addProcessor('bar', 'org::stubbles::test::BarProcessor'); 
     22 * </code> 
     23 * then the first added processor class named as second argument will be 
     24 * selected if the value of the request param is <em>foo</em>. 
     25 * 
     26 * To make sure that a processor gets selected even in case the parameter is not 
     27 * set or has a wrong value, one can set the default processor to be used: 
     28 * <code> 
     29 *   $defaultProcessor->setDefaultProcessor('foo'); 
     30 * </code> 
     31 * Now, the processor with this key will be selected in such cases as well. You 
     32 * should make sure that the default processor matches one of the added 
     33 * processors, else an exception will be thrown. 
    1534 *  
    16  * @static 
     35 * Additionally one can set the interceptor descriptor used by the processor and 
     36 * the page factory class to be used by the processor. While the first is used 
     37 * to determine the interceptor configuration to be used in conjunction with 
     38 * this processor, the latter determines the page factory class that will be 
     39 * injected into the processor's <code>selectPage()</code> method if the 
     40 * processor implements the 
     41 * <code>net::stubbles::websites::processors::stubPageBasesProcessor</code> 
     42 * interface. 
     43 * 
    1744 * @package     stubbles 
    1845 * @subpackage  websites_processors 
     
    5380     * @param  string  $pageFactoryClass       optional  page factory class for the processor 
    5481     */ 
    55     public function addProcessor($paramValue, $fqClassName, $interceptorDescriptor = 'interceptors', $pageFactoryClass = null) 
     82    public function addProcessor($paramValue, $fqClassName, $interceptorDescriptor = null, $pageFactoryClass = null) 
    5683    { 
    57         $this->processors[$paramValue]              = $fqClassName; 
    58         $this->interceptorDescriptors[$fqClassName] = $interceptorDescriptor; 
     84        $this->processors[$paramValue] = $fqClassName; 
     85        if (null != $interceptorDescriptor) { 
     86            $this->interceptorDescriptors[$fqClassName] = $interceptorDescriptor; 
     87        } else { 
     88            $this->interceptorDescriptors[$fqClassName] = 'interceptors'; 
     89        } 
     90         
    5991        if (null !== $pageFactoryClass) { 
    6092            $this->pageFactoryClasses[$fqClassName] = $pageFactoryClass; 
     
    83115    protected function doResolve(stubRequest $request, stubSession $session, stubResponse $response) 
    84116    { 
    85         if (isset($this->processors[$this->defaultProcessor]) == false) { 
    86             throw new stubConfigurationException('Configuration error: the default processor ' . $this->defaultProcessor . ' is not set.'); 
     117        $paramValue = null; 
     118        if ($request->hasValue('processor') === true) { 
     119            $paramValue = $request->getValidatedValue(new stubPreSelectValidator(array_keys($this->processors)), 'processor'); 
    87120        } 
    88121         
    89         if ($request->hasValue('processor') == false) { 
     122        if (null == $paramValue) { 
     123            if (null == $this->defaultProcessor || isset($this->processors[$this->defaultProcessor]) === false) { 
     124                throw new stubConfigurationException('Configuration error: the default processor ' . $this->defaultProcessor . ' is not set.'); 
     125            } 
     126             
    90127            $paramValue = $this->defaultProcessor; 
    91         } else { 
    92             $paramValue = $request->getValidatedValue(new stubPreSelectValidator(array_keys($this->processors)), 'processor'); 
    93             if (null == $paramValue) { 
    94                 $paramValue = $this->defaultProcessor; 
    95             } 
    96128        } 
    97          
    98         $session->putValue('net::stubbles::websites.lastProcessor', $paramValue); 
     129             
     130        $session->putValue('net.stubbles.websites.lastProcessor', $paramValue); 
    99131        return $this->processors[$paramValue]; 
    100132    } 
     
    107139    protected function configure(stubProcessor $processor) 
    108140    { 
    109         if (isset($this->interceptorDescriptors[$processor->getClassName()]) == true && strlen($this->interceptorDescriptors[$processor->getClassName()]) > 0) { 
    110             $processor->setInterceptorDescriptor($this->interceptorDescriptors[$processor->getClassName()]); 
     141        $processorClassName = $processor->getClassName(); 
     142        if (isset($this->interceptorDescriptors[$processorClassName]) === true && strlen($this->interceptorDescriptors[$processorClassName]) > 0) { 
     143            $processor->setInterceptorDescriptor($this->interceptorDescriptors[$processorClassName]); 
    111144        } 
    112145    } 
     
    120153    protected function getPageFactoryClass(stubProcessor $processor) 
    121154    { 
    122         if (isset($this->pageFactoryClasses[$processor->getClassName()]) === true) { 
    123             return $this->pageFactoryClasses[$processor->getClassName()]; 
     155        $processorClassName = $processor->getClassName(); 
     156        if (isset($this->pageFactoryClasses[$processorClassName]) === true) { 
     157            return $this->pageFactoryClasses[$processorClassName]; 
    124158        } 
    125159         
  • trunk/src/main/php/net/stubbles/websites/processors/stubSimpleProcessorResolver.php

    r1445 r1447  
    11<?php 
    22/** 
    3  * A very simple implementation for the processor resolver which returns always the same processor. 
    4  *  
     3 * A very simple implementation for the processor resolver which returns always 
     4 * the same processor. 
     5 * 
    56 * @author      Frank Kleine <mikey@stubbles.net> 
    67 * @package     stubbles 
     
    910stubClassLoader::load('net::stubbles::websites::processors::stubAbstractProcessorResolver'); 
    1011/** 
    11  * A very simple implementation for the processor resolver which returns always the same processor. 
    12  *  
     12 * A very simple implementation for the processor resolver which returns always 
     13 * the same processor. 
     14 * 
     15 * The processor to be returned can be set via the <code>setProcessor()</code> 
     16 * method. The interceptor descriptor will always be <em>interceptors</em>. 
     17 * 
     18 * Additionally one can set the the page factory class to be used by the 
     19 * processor, which determines the page factory class that will be injected into 
     20 * the processor's <code>selectPage()</code> method if the processor implements 
     21 * the <code>net::stubbles::websites::processors::stubPageBasesProcessor</code> 
     22 * interface. 
     23 * 
    1324 * @package     stubbles 
    1425 * @subpackage  websites_processors 
  • trunk/src/test/php/net/stubbles/websites/processors/stubAbstractProcessorResolverTestCase.php

    r1445 r1447  
    9292     * @test 
    9393     */ 
    94     public function correctProcessor() 
     94    public function correctNonPageBasedProcessor() 
    9595    { 
    9696        $this->abstractProcessorResolver->expects($this->once()) 
     
    100100        $processor = $this->abstractProcessorResolver->resolve($this->mockRequest, $this->mockSession, $this->mockResponse); 
    101101        $this->assertType('FooProcessor', $processor); 
    102         $request = $processor->getRequest(); 
    103         $this->assertSame($this->mockRequest, $request); 
    104         $session = $processor->getSession(); 
    105         $this->assertSame($this->mockSession, $session); 
    106         $response = $processor->getResponse(); 
    107         $this->assertSame($this->mockResponse, $response); 
     102        $this->assertSame($this->mockRequest, $processor->getRequest()); 
     103        $this->assertSame($this->mockSession, $processor->getSession()); 
     104        $this->assertSame($this->mockResponse, $processor->getResponse()); 
     105    } 
     106 
     107    /** 
     108     * if a page factory class is required and none is returned an exception will be thrown 
     109     * 
     110     * @test 
     111     * @expectedException  stubConfigurationException 
     112     */ 
     113    public function pageBasedProcessorNoPageFactoryClass() 
     114    { 
     115        $this->abstractProcessorResolver->expects($this->once()) 
     116                                        ->method('doResolve') 
     117                                        ->with($this->equalTo($this->mockRequest), $this->equalTo($this->mockSession), $this->equalTo($this->mockResponse)) 
     118                                        ->will($this->returnValue('org::stubbles::test::FooPageBasedProcessor')); 
     119        $this->abstractProcessorResolver->expects($this->once()) 
     120                                        ->method('getPageFactoryClass') 
     121                                        ->will($this->returnValue(null)); 
     122        $processor = $this->abstractProcessorResolver->resolve($this->mockRequest, $this->mockSession, $this->mockResponse); 
     123    } 
     124 
     125    /** 
     126     * if a page factory class is required and a wrong class is returned an exception will be thrown 
     127     * 
     128     * @test 
     129     * @expectedException  stubRuntimeException 
     130     */ 
     131    public function pageBasedProcessorNoPageFactoryInstance() 
     132    { 
     133        $this->abstractProcessorResolver->expects($this->once()) 
     134                                        ->method('doResolve') 
     135                                        ->with($this->equalTo($this->mockRequest), $this->equalTo($this->mockSession), $this->equalTo($this->mockResponse)) 
     136                                        ->will($this->returnValue('org::stubbles::test::FooPageBasedProcessor')); 
     137        $this->abstractProcessorResolver->expects($this->once()) 
     138                                        ->method('getPageFactoryClass') 
     139                                        ->will($this->returnValue('stdClass')); 
     140        $processor = $this->abstractProcessorResolver->resolve($this->mockRequest, $this->mockSession, $this->mockResponse); 
     141    } 
     142 
     143    /** 
     144     * assure that the default processor is returned and it has all required classes 
     145     * 
     146     * @test 
     147     */ 
     148    public function correctPageBasedProcessor() 
     149    { 
     150        $pageFactoryClass = get_class($this->getMock('stubPageFactory')); 
     151        $this->abstractProcessorResolver->expects($this->once()) 
     152                                        ->method('doResolve') 
     153                                        ->with($this->equalTo($this->mockRequest), $this->equalTo($this->mockSession), $this->equalTo($this->mockResponse)) 
     154                                        ->will($this->returnValue('org::stubbles::test::FooPageBasedProcessor')); 
     155        $this->abstractProcessorResolver->expects($this->once()) 
     156                                        ->method('getPageFactoryClass') 
     157                                        ->will($this->returnValue($pageFactoryClass)); 
     158        $processor = $this->abstractProcessorResolver->resolve($this->mockRequest, $this->mockSession, $this->mockResponse); 
     159        $this->assertType('FooPageBasedProcessor', $processor); 
     160        $this->assertSame($this->mockRequest, $processor->getRequest()); 
     161        $this->assertSame($this->mockSession, $processor->getSession()); 
     162        $this->assertSame($this->mockResponse, $processor->getResponse()); 
     163        $this->assertType($pageFactoryClass, $processor->getPageFactory()); 
    108164    } 
    109165} 
  • trunk/src/test/php/net/stubbles/websites/processors/stubDefaultProcessorResolverTestCase.php

    r1445 r1447  
    2929        return $this->doResolve($request, $session, $response); 
    3030    } 
     31 
     32    /** 
     33     * direct access to configure() 
     34     * 
     35     * @param  stubProcessor  $processor 
     36     */ 
     37    public function callConfigure(stubProcessor $processor) 
     38    { 
     39        $this->configure($processor); 
     40    } 
     41 
     42    /** 
     43     * direct access to getPageFactoryClass() 
     44     * 
     45     * @param   stubProcessor  $processor 
     46     * @return  string 
     47     */ 
     48    public function callGetPageFactoryClass(stubProcessor $processor) 
     49    { 
     50        return $this->getPageFactoryClass($processor); 
     51    } 
    3152} 
    3253/** 
     
    79100    protected function addProcessors() 
    80101    { 
    81         $this->defaultProcessorResolver->addProcessor('foo', 'org::stubbles::test::FooProcessor', 'interceptors-foo'); 
    82         $this->defaultProcessorResolver->addProcessor('bar', 'org::stubbles::test::BarProcessor', 'interceptors-bar'); 
    83         $this->defaultProcessorResolver->addProcessor('baz', 'org::stubbles::test::BazProcessor', 'interceptors-baz'); 
     102        $this->defaultProcessorResolver->addProcessor('foo', 'org::stubbles::test::FooProcessor', 'interceptors-foo', 'TestPageFactoryClassName1'); 
     103        $this->defaultProcessorResolver->addProcessor('bar', 'org::stubbles::test::BarProcessor', null, 'TestPageFactoryClassName2'); 
     104        $this->defaultProcessorResolver->addProcessor('baz', 'org::stubbles::test::BazProcessor'); 
    84105        $this->defaultProcessorResolver->setDefaultProcessor('foo'); 
    85106    } 
     
    94115        $this->addProcessors(); 
    95116        $this->mockRequest->expects($this->once())->method('hasValue')->will($this->returnValue(false)); 
     117        $this->mockSession->expects($this->once()) 
     118                          ->method('putValue') 
     119                          ->with($this->equalTo('net.stubbles.websites.lastProcessor'), $this->equalTo('foo')); 
    96120        $processor = $this->defaultProcessorResolver->getDoResolveReturnValue($this->mockRequest, $this->mockSession, $this->mockResponse); 
    97121        $this->assertEquals('org::stubbles::test::FooProcessor', $processor); 
     
    108132        $this->mockRequest->expects($this->once())->method('hasValue')->will($this->returnValue(true)); 
    109133        $this->mockRequest->expects($this->once())->method('getValidatedValue')->will($this->returnValue('bar')); 
     134        $this->mockSession->expects($this->once()) 
     135                          ->method('putValue') 
     136                          ->with($this->equalTo('net.stubbles.websites.lastProcessor'), $this->equalTo('bar')); 
    110137        $processor = $this->defaultProcessorResolver->getDoResolveReturnValue($this->mockRequest, $this->mockSession, $this->mockResponse); 
    111138        $this->assertEquals('org::stubbles::test::BarProcessor', $processor); 
     
    122149        $this->mockRequest->expects($this->once())->method('hasValue')->will($this->returnValue(true)); 
    123150        $this->mockRequest->expects($this->once())->method('getValidatedValue')->will($this->returnValue(null)); 
     151        $this->mockSession->expects($this->once()) 
     152                          ->method('putValue') 
     153                          ->with($this->equalTo('net.stubbles.websites.lastProcessor'), $this->equalTo('foo')); 
    124154        $processor = $this->defaultProcessorResolver->getDoResolveReturnValue($this->mockRequest, $this->mockSession, $this->mockResponse); 
    125155        $this->assertEquals($processor, 'org::stubbles::test::FooProcessor'); 
     
    134164    public function noProcessors() 
    135165    { 
     166        $this->mockSession->expects($this->never())->method('putValue'); 
    136167        $this->defaultProcessorResolver->getDoResolveReturnValue($this->mockRequest, $this->mockSession, $this->mockResponse); 
    137168    } 
     
    147178        $this->defaultProcessorResolver->addProcessor('foo', 'org::stubbles::test::FooProcessor', 'interceptors-foo'); 
    148179        $this->defaultProcessorResolver->setDefaultProcessor('bar'); 
     180        $this->mockSession->expects($this->never())->method('putValue'); 
    149181        $this->defaultProcessorResolver->getDoResolveReturnValue($this->mockRequest, $this->mockSession, $this->mockResponse); 
     182    } 
     183 
     184    /** 
     185     * processor should be condigured correctly 
     186     * 
     187     * @test 
     188     */ 
     189    public function processorIsConfiguredCorrectly() 
     190    { 
     191        $this->addProcessors(); 
     192        $mockProcessor = $this->getMock('stubProcessor'); 
     193        $mockProcessor->expects($this->once()) 
     194                      ->method('getClassName') 
     195                      ->will($this->returnValue('org::stubbles::test::FooProcessor')); 
     196        $mockProcessor->expects($this->once())->method('setInterceptorDescriptor')->with($this->equalTo('interceptors-foo')); 
     197        $this->defaultProcessorResolver->callConfigure($mockProcessor); 
     198        $mockProcessor = $this->getMock('stubProcessor'); 
     199        $mockProcessor->expects($this->once()) 
     200                      ->method('getClassName') 
     201                      ->will($this->returnValue('org::stubbles::test::BarProcessor')); 
     202        $mockProcessor->expects($this->once())->method('setInterceptorDescriptor')->with($this->equalTo('interceptors')); 
     203        $this->defaultProcessorResolver->callConfigure($mockProcessor); 
     204        $mockProcessor = $this->getMock('stubProcessor'); 
     205        $mockProcessor->expects($this->once()) 
     206                      ->method('getClassName') 
     207                      ->will($this->returnValue('org::stubbles::test::BazProcessor')); 
     208        $mockProcessor->expects($this->once())->method('setInterceptorDescriptor')->with($this->equalTo('interceptors')); 
     209        $this->defaultProcessorResolver->callConfigure($mockProcessor); 
     210    } 
     211 
     212    /** 
     213     * page factory class should be handled correct 
     214     * 
     215     * @test 
     216     */ 
     217    public function pageFactoryClass() 
     218    { 
     219        $this->addProcessors(); 
     220        $mockProcessor = $this->getMock('stubProcessor'); 
     221        $mockProcessor->expects($this->exactly(3)) 
     222                      ->method('getClassName') 
     223                      ->will($this->onConsecutiveCalls('org::stubbles::test::FooProcessor', 
     224                                                       'org::stubbles::test::BarProcessor', 
     225                                                       'org::stubbles::test::BazProcessor' 
     226                             ) 
     227                        ); 
     228        $this->assertEquals('TestPageFactoryClassName1', $this->defaultProcessorResolver->callGetPageFactoryClass($mockProcessor)); 
     229        $this->assertEquals('TestPageFactoryClassName2', $this->defaultProcessorResolver->callGetPageFactoryClass($mockProcessor)); 
     230        $this->assertNull($this->defaultProcessorResolver->callGetPageFactoryClass($mockProcessor)); 
    150231    } 
    151232} 
  • trunk/src/test/php/net/stubbles/websites/processors/stubSimpleProcessorResolverTestCase.php

    r1252 r1447  
    2828    { 
    2929        return $this->doResolve($request, $session, $response); 
     30    } 
     31 
     32    /** 
     33     * direct access to configure() 
     34     * 
     35     * @param  stubProcessor  $processor 
     36     */ 
     37    public function callConfigure(stubProcessor $processor) 
     38    { 
     39        $this->configure($processor); 
     40    } 
     41 
     42    /** 
     43     * direct access to getPageFactoryClass() 
     44     * 
     45     * @param   stubProcessor  $processor 
     46     * @return  string 
     47     */ 
     48    public function callGetPageFactoryClass(stubProcessor $processor) 
     49    { 
     50        return $this->getPageFactoryClass($processor); 
    3051    } 
    3152} 
     
    85106        $this->assertEquals('org::stubbles::test::FooProcessor', $processor); 
    86107    } 
     108 
     109    /** 
     110     * processor should be condigured correctly 
     111     * 
     112     * @test 
     113     */ 
     114    public function processorIsConfiguredCorrectly() 
     115    { 
     116        $mockProcessor = $this->getMock('stubProcessor'); 
     117        $mockProcessor->expects($this->once())->method('setInterceptorDescriptor')->with($this->equalTo('interceptors')); 
     118        $this->simpleProcessorResolver->callConfigure($mockProcessor); 
     119    } 
     120 
     121    /** 
     122     * page factory class should be handled correct 
     123     * 
     124     * @test 
     125     */ 
     126    public function pageFactoryClass() 
     127    { 
     128        $this->simpleProcessorResolver->setPageFactoryClass('TestPageFactoryClassName'); 
     129        $this->assertEquals('TestPageFactoryClassName', $this->simpleProcessorResolver->callGetPageFactoryClass($this->getMock('stubProcessor'))); 
     130    } 
    87131} 
    88132?>