Changeset 1447
- Timestamp:
- 03/22/08 23:18:11 (2 months ago)
- Files:
-
- trunk/src/main/php/net/stubbles/websites/processors/stubAbstractProcessorResolver.php (modified) (3 diffs)
- trunk/src/main/php/net/stubbles/websites/processors/stubDefaultProcessorResolver.php (modified) (6 diffs)
- trunk/src/main/php/net/stubbles/websites/processors/stubSimpleProcessorResolver.php (modified) (2 diffs)
- trunk/src/main/php/org/stubbles/test/FooPageBasedProcessor.php (added)
- trunk/src/test/php/net/stubbles/websites/processors/stubAbstractProcessorResolverTestCase.php (modified) (2 diffs)
- trunk/src/test/php/net/stubbles/websites/processors/stubDefaultProcessorResolverTestCase.php (modified) (7 diffs)
- trunk/src/test/php/net/stubbles/websites/processors/stubSimpleProcessorResolverTestCase.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/main/php/net/stubbles/websites/processors/stubAbstractProcessorResolver.php
r1445 r1447 68 68 69 69 /** 70 * returns the page factory class for the processor71 *72 * @param stubProcessor $processor73 * @return string74 */75 protected abstract function getPageFactoryClass(stubProcessor $processor);76 77 /**78 70 * helper method to handle page based processors 79 71 * … … 93 85 } 94 86 95 stubClassLoader::load($pageFactoryClass);96 87 $nqClassName = stubClassLoader::getNonQualifiedClassName($pageFactoryClass); 88 if (class_exists($nqClassName, false) === false) { 89 stubClassLoader::load($pageFactoryClass); 90 } 91 97 92 $pageFactory = new $nqClassName(); 98 93 if (($pageFactory instanceof stubPageFactory) === false) { … … 102 97 $processor->selectPage($pageFactory); 103 98 } 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); 104 107 } 105 108 ?> trunk/src/main/php/net/stubbles/websites/processors/stubDefaultProcessorResolver.php
r1445 r1447 2 2 /** 3 3 * Default implementation for the processor resolver. 4 * 4 * 5 5 * @author Frank Kleine <mikey@stubbles.net> 6 6 * @package stubbles … … 13 13 /** 14 14 * 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. 15 34 * 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 * 17 44 * @package stubbles 18 45 * @subpackage websites_processors … … 53 80 * @param string $pageFactoryClass optional page factory class for the processor 54 81 */ 55 public function addProcessor($paramValue, $fqClassName, $interceptorDescriptor = 'interceptors', $pageFactoryClass = null)82 public function addProcessor($paramValue, $fqClassName, $interceptorDescriptor = null, $pageFactoryClass = null) 56 83 { 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 59 91 if (null !== $pageFactoryClass) { 60 92 $this->pageFactoryClasses[$fqClassName] = $pageFactoryClass; … … 83 115 protected function doResolve(stubRequest $request, stubSession $session, stubResponse $response) 84 116 { 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'); 87 120 } 88 121 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 90 127 $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 }96 128 } 97 98 $session->putValue('net ::stubbles::websites.lastProcessor', $paramValue);129 130 $session->putValue('net.stubbles.websites.lastProcessor', $paramValue); 99 131 return $this->processors[$paramValue]; 100 132 } … … 107 139 protected function configure(stubProcessor $processor) 108 140 { 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]); 111 144 } 112 145 } … … 120 153 protected function getPageFactoryClass(stubProcessor $processor) 121 154 { 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]; 124 158 } 125 159 trunk/src/main/php/net/stubbles/websites/processors/stubSimpleProcessorResolver.php
r1445 r1447 1 1 <?php 2 2 /** 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 * 5 6 * @author Frank Kleine <mikey@stubbles.net> 6 7 * @package stubbles … … 9 10 stubClassLoader::load('net::stubbles::websites::processors::stubAbstractProcessorResolver'); 10 11 /** 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 * 13 24 * @package stubbles 14 25 * @subpackage websites_processors trunk/src/test/php/net/stubbles/websites/processors/stubAbstractProcessorResolverTestCase.php
r1445 r1447 92 92 * @test 93 93 */ 94 public function correct Processor()94 public function correctNonPageBasedProcessor() 95 95 { 96 96 $this->abstractProcessorResolver->expects($this->once()) … … 100 100 $processor = $this->abstractProcessorResolver->resolve($this->mockRequest, $this->mockSession, $this->mockResponse); 101 101 $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()); 108 164 } 109 165 } trunk/src/test/php/net/stubbles/websites/processors/stubDefaultProcessorResolverTestCase.php
r1445 r1447 29 29 return $this->doResolve($request, $session, $response); 30 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); 51 } 31 52 } 32 53 /** … … 79 100 protected function addProcessors() 80 101 { 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'); 84 105 $this->defaultProcessorResolver->setDefaultProcessor('foo'); 85 106 } … … 94 115 $this->addProcessors(); 95 116 $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')); 96 120 $processor = $this->defaultProcessorResolver->getDoResolveReturnValue($this->mockRequest, $this->mockSession, $this->mockResponse); 97 121 $this->assertEquals('org::stubbles::test::FooProcessor', $processor); … … 108 132 $this->mockRequest->expects($this->once())->method('hasValue')->will($this->returnValue(true)); 109 133 $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')); 110 137 $processor = $this->defaultProcessorResolver->getDoResolveReturnValue($this->mockRequest, $this->mockSession, $this->mockResponse); 111 138 $this->assertEquals('org::stubbles::test::BarProcessor', $processor); … … 122 149 $this->mockRequest->expects($this->once())->method('hasValue')->will($this->returnValue(true)); 123 150 $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')); 124 154 $processor = $this->defaultProcessorResolver->getDoResolveReturnValue($this->mockRequest, $this->mockSession, $this->mockResponse); 125 155 $this->assertEquals($processor, 'org::stubbles::test::FooProcessor'); … … 134 164 public function noProcessors() 135 165 { 166 $this->mockSession->expects($this->never())->method('putValue'); 136 167 $this->defaultProcessorResolver->getDoResolveReturnValue($this->mockRequest, $this->mockSession, $this->mockResponse); 137 168 } … … 147 178 $this->defaultProcessorResolver->addProcessor('foo', 'org::stubbles::test::FooProcessor', 'interceptors-foo'); 148 179 $this->defaultProcessorResolver->setDefaultProcessor('bar'); 180 $this->mockSession->expects($this->never())->method('putValue'); 149 181 $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)); 150 231 } 151 232 } trunk/src/test/php/net/stubbles/websites/processors/stubSimpleProcessorResolverTestCase.php
r1252 r1447 28 28 { 29 29 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); 30 51 } 31 52 } … … 85 106 $this->assertEquals('org::stubbles::test::FooProcessor', $processor); 86 107 } 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 } 87 131 } 88 132 ?>
