Changeset 1413
- Timestamp:
- 03/11/08 18:37:13 (4 months ago)
- Files:
-
- trunk/src/main/php/net/stubbles/websites/xml/stubSkinGenerator.php (added)
- trunk/src/main/php/net/stubbles/websites/xml/stubXMLPostInterceptor.php (modified) (5 diffs)
- trunk/src/test/php/net/stubbles/websites/WebsitesTestSuite.php (modified) (1 diff)
- trunk/src/test/php/net/stubbles/websites/xml/stubSkinGeneratorTestCase.php (added)
- trunk/src/test/php/net/stubbles/websites/xml/stubXMLPostInterceptorTestCase.php (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/main/php/net/stubbles/websites/xml/stubXMLPostInterceptor.php
r1360 r1413 7 7 * @subpackage websites_xml 8 8 */ 9 stubClassLoader::load('net::stubbles::ioc::stubBinder', 10 'net::stubbles::util::stubFactory', 11 'net::stubbles::util::stubRegistry', 9 stubClassLoader::load('net::stubbles::websites::xml::stubSkinGenerator', 12 10 'net::stubbles::websites::xml::stubXMLAbstractPostInterceptor', 13 'net::stubbles::xml::stubXMLXIncludeStreamWrapper',14 'net::stubbles::xml::stubXMLStreamWriterFactory',15 11 'net::stubbles::xml::xsl::stubXSLProcessor' 16 12 ); 17 13 /** 18 14 * Post interceptor that transforms the xml result document into html. 19 *20 * When using this post interceptor please make sure that within the registry21 * a value accessible with key 'net::stubbles::ioc.stubBinder' is configured and22 * an instance of net::stubbles::ioc.stubBinder. If the pre interceptor23 * net::stubbles::ioc.stubIOCPreInterceptor is configured as pre interceptor this24 * one will put a stubBinder instance into the registry.25 * Please make sure as well that a file named xsl-callbacks.ini resides in the26 * base config directory configured in stubConfig::getConfigPath().27 15 * 28 16 * @package stubbles … … 31 19 class stubXMLPostInterceptor extends stubXMLAbstractPostInterceptor 32 20 { 33 /** 34 * constructor 35 */ 36 public function __construct() 37 { 38 stubXMLXIncludeStreamWrapper::register(); 39 stubXMLXIncludeStreamWrapper::setIncludePath(stubConfig::getPagePath() . '/txt'); 40 stubXMLXIncludeStreamWrapper::setCachePath(stubConfig::getCachePath()); 41 } 42 21 43 22 /** 44 23 * does the postprocessing stuff … … 51 30 protected function doPostProcess(stubRequest $request, stubSession $session, stubXMLResponse $response) 52 31 { 53 $binder = stubRegistry::get(stubBinder::REGISTRY_KEY);54 if (($binder instanceof stubBinder) === false) {55 throw new stubRuntimeException('No instance of net::stubbles::ioc::stubBinder in registry.');56 }57 58 $binder->bind('stubXMLStreamWriter')->to(stubXMLStreamWriterFactory::getFqClassNameAsAvailable());59 $binder->bindConstant()->named('imagePath')->to(getcwd());60 32 $xslProcessor = $this->createXSLProcessor(); 61 $injector = $binder->getInjector(); 62 foreach ($this->getCallbackList() as $callbackName => $callbackClass) { 63 $xslProcessor->registerCallback($callbackName, $injector->getInstance($callbackClass)); 64 } 65 66 $xslProcessor->importXSLStylesheet($this->createXSLStylesheet()); 67 $page = $response->getPage(); 68 $xslProcessor->setParameter('', 'page', $page->getProperty('name')); 69 if ($session->hasValue('net.stubbles.language') == true) { 70 $language = $session->getValue('net.stubbles.language'); 71 } elseif ($page->hasProperty('language') == true) { 72 $language = $page->getProperty('language'); 73 } elseif (stubRegistry::hasConfig('net.stubbles.language') == true) { 74 $language = stubRegistry::getConfig('net.stubbles.language'); 75 } else { 76 $language = 'en_EN'; 77 } 78 79 $xslProcessor->setParameter('', 'lang', $language); 80 // use same xsl processor for xincludes 81 $this->setXIncludeProcessor($xslProcessor); 82 83 // first we create the xsl with applying the master.xsl on the skin and the page 84 $skin = (($page->hasProperty('skin') === true) ? ($page->getProperty('skin')) : ('default')); 85 $xslProcessor->setXMLDocument($this->createXMLSkinDocument($skin)); 86 $resultXSL = $xslProcessor->transformToDoc(); 87 @$resultXSL->xinclude(); 88 89 // now we use the created xsl to transform the xml document created by the application 90 $xslProcessor = $this->createXSLProcessor(); 91 $xslProcessor->importXSLStylesheet($resultXSL); 33 $xslProcessor->importXSLStylesheet($this->getSkinGenerator()->generate($session, $response)); 92 34 $xmlStreamWriter = $response->getXMLStreamWriter(); 93 35 if ($xmlStreamWriter->isFinished() === false) { … … 100 42 101 43 /** 102 * use same xsl processor for the xincludes44 * creates a new skin generator instance 103 45 * 104 * @ param stubXSLProcessor $xslProcessor46 * @return stubSkinGenerator 105 47 */ 106 protected function setXIncludeProcessor($xslProcessor)48 protected function getSkinGenerator() 107 49 { 108 stubXMLXIncludeStreamWrapper::setXSLProcessor($xslProcessor);50 return new stubSkinGenerator(); 109 51 } 110 52 … … 116 58 protected function createXSLProcessor() 117 59 { 118 $xslProcessor = new stubXSLProcessor(); 119 return $xslProcessor; 120 } 121 122 /** 123 * creates a stubXSLImageDimensions 124 * 125 * @return array 126 * @throws stubRuntimeException 127 */ 128 protected function getCallbackList() 129 { 130 $iniFile = stubConfig::getConfigPath() . '/xsl-callbacks.ini'; 131 if (file_exists($iniFile) === false) { 132 throw new stubRuntimeException('Configuration file ' . $iniFile . ' for XSL callback configuration is missing.'); 133 } 134 135 return parse_ini_file($iniFile); 136 } 137 138 /** 139 * creates the xsl stylesheet 140 * 141 * @return DOMDocument 142 * @todo fix selection of uri 143 */ 144 protected function createXSLStylesheet() 145 { 146 $uris = stubFactory::getResourceURIs('xsl/master.xsl'); 147 return DOMDocument::load($uris[0]); 148 } 149 150 /** 151 * creates the skin document 152 * 153 * @param string $skin 154 * @return DOMDocument 155 */ 156 protected function createXMLSkinDocument($skin) 157 { 158 return DOMDocument::load(stubConfig::getPagePath() . '/skin/' . $skin . '.xml'); 60 return new stubXSLProcessor(); 159 61 } 160 62 } trunk/src/test/php/net/stubbles/websites/WebsitesTestSuite.php
r1402 r1413 49 49 // xml tests 50 50 $suite->addTestFile($dir . '/xml/stubShowLastXMLInterceptorTestCase.php'); 51 $suite->addTestFile($dir . '/xml/stubSkinGeneratorTestCase.php'); 51 52 $suite->addTestFile($dir . '/xml/stubXMLPostInterceptorTestCase.php'); 52 53 $suite->addTestFile($dir . '/xml/stubXMLProcessorTestCase.php'); trunk/src/test/php/net/stubbles/websites/xml/stubXMLPostInterceptorTestCase.php
r1360 r1413 8 8 */ 9 9 stubClassLoader::load('net::stubbles::websites::xml::stubXMLPostInterceptor'); 10 class TestDOMDocument extends DOMDocument11 {12 // intentionally empty13 }14 10 /** 15 11 * Tests for net::stubbles::websites::xml::stubXMLPostInterceptor. … … 33 29 protected $xmlResponse; 34 30 /** 35 * instance to be used for tests36 *37 * @var PHPUnit_Framework_MockObject_MockObject38 */39 protected $mockXMLStreamWriter;40 /**41 31 * mocked request instance 42 32 * … … 51 41 protected $mockResponse; 52 42 /** 53 * the page instance54 *55 * @var stubPage56 */57 protected $page;58 /**59 43 * mocked session instance 60 44 * … … 62 46 */ 63 47 protected $mockSession; 64 /**65 * mocked xsl processor66 *67 * @var PHPUnit_Framework_MockObject_MockObject68 */69 protected $mockXSLProcessor;70 /**71 * mocked image callback72 *73 * @var PHPUnit_Framework_MockObject_MockObject74 */75 protected $mockImageCallback;76 48 77 49 /** … … 81 53 { 82 54 $this->xmlPostInterceptor = $this->getMock('stubXMLPostInterceptor', 83 array('setXIncludeProcessor', 84 'createXSLProcessor', 85 'getCallbackList', 86 /* 'createXSLStylesheet',*/ 87 'createXMLSkinDocument' 55 array('getSkinGenerator', 56 'createXSLProcessor' 88 57 ) 89 58 ); 90 59 $this->mockRequest = $this->getMock('stubRequest'); 91 60 $this->mockResponse = $this->getMock('stubResponse'); 92 $this->mockXMLStreamWriter = $this->getMock('stubXMLStreamWriter'); 93 $domDocument0 = new DOMDocument(); 94 $domDocument0->createElement('bar', 'foo'); 95 $this->mockXMLStreamWriter->expects($this->any())->method('isFinished')->will($this->returnValue(true)); 96 $this->mockXMLStreamWriter->expects($this->any())->method('asDOM')->will($this->returnValue($domDocument0)); 97 $this->xmlResponse = new stubXMLResponse($this->mockResponse); 98 $this->xmlResponse->setXMLStreamWriter($this->mockXMLStreamWriter); 99 $this->page = new stubPage(); 100 $this->xmlResponse->setPage($this->page); 101 $this->mockSession = $this->getMock('stubSession'); 102 $this->mockXSLProcessor = $this->getMock('stubXSLProcessor'); 103 $domDocument = new DOMDocument(); 104 $domDocument->createElement('bar', 'foo'); 105 $this->mockXSLProcessor->expects($this->any())->method('transformToDoc')->will($this->returnValue($domDocument)); 106 $this->mockXSLProcessor->expects($this->any())->method('transformToXML')->will($this->returnValue('<html><head><title>Test</title></head><body><p>Hello world.</p></body></html>')); 107 $this->xmlPostInterceptor->expects($this->any())->method('createXSLProcessor')->will($this->returnValue($this->mockXSLProcessor)); 108 $this->xmlPostInterceptor->expects($this->any())->method('getCallbackList')->will($this->returnValue(array())); 109 if (stubRegistry::hasConfig('net.stubbles.language') == true) { 110 stubRegistry::setConfig('net.stubbles.language', null); 111 } 61 $this->xmlResponse = new stubXMLResponse($this->mockResponse); 62 $this->mockSession = $this->getMock('stubSession'); 112 63 113 stubRegistry::set(stubBinder::REGISTRY_KEY, new stubBinder());114 }115 116 /**117 * clear test environment118 */119 public function tearDown()120 {121 stubRegistry::remove(stubBinder::REGISTRY_KEY);122 64 } 123 65 … … 140 82 public function correctResponse() 141 83 { 142 $this->page->setProperty('name', 'baz'); 143 $this->xmlPostInterceptor->expects($this->once()) 144 ->method('createXMLSkinDocument') 145 ->with($this->equalTo('default')) 146 ->will($this->returnValue($this->getMock('DOMDocument'))); 147 $this->mockXSLProcessor->expects($this->at(1))->method('setParameter')->with($this->equalTo(''), $this->equalTo('page'), $this->equalTo('baz')); 148 $this->mockXSLProcessor->expects($this->at(2))->method('setParameter')->with($this->equalTo(''), $this->equalTo('lang'), $this->equalTo('en_EN')); 84 $resultXSL = new DOMDocument(); 85 $mockSkinGenerator = $this->getMock('stubSkinGenerator'); 86 $mockSkinGenerator->expects($this->once()) 87 ->method('generate') 88 ->with($this->equalTo($this->mockSession), $this->equalTo($this->xmlResponse)) 89 ->will($this->returnValue($resultXSL)); 90 $this->xmlPostInterceptor->expects($this->once())->method('getSkinGenerator')->will($this->returnValue($mockSkinGenerator)); 91 92 $mockXSLProcessor = $this->getMock('stubXSLProcessor'); 93 $mockXSLProcessor->expects($this->once())->method('importXSLStylesheet')->with($this->equalTo($resultXSL)); 94 $mockXSLProcessor->expects($this->once())->method('transformToXML')->will($this->returnValue('<html><head><title>Test</title></head><body><p>Hello world.</p></body></html>')); 95 $this->xmlPostInterceptor->expects($this->once())->method('createXSLProcessor')->will($this->returnValue($mockXSLProcessor)); 96 97 $domDocument0 = new DOMDocument(); 98 $domDocument0->createElement('bar', 'foo'); 99 $mockXMLStreamWriter = $this->getMock('stubXMLStreamWriter'); 100 $mockXMLStreamWriter->expects($this->any())->method('isFinished')->will($this->returnValue(true)); 101 $mockXMLStreamWriter->expects($this->any())->method('asDOM')->will($this->returnValue($domDocument0)); 102 $this->xmlResponse->setXMLStreamWriter($mockXMLStreamWriter); 103 149 104 $this->mockResponse->expects($this->once())->method('replaceData')->with($this->equalTo('<html><head><title>Test</title></head><body><p>Hello world.</p></body></html>')); 150 105 $this->assertTrue($this->xmlPostInterceptor->postProcess($this->mockRequest, $this->mockSession, $this->xmlResponse)); 151 106 } 152 153 /**154 * assure that another skin will be used155 *156 * @test157 */158 public function skin()159 {160 $this->page->setProperty('skin', 'another');161 $this->xmlPostInterceptor->expects($this->once())162 ->method('createXMLSkinDocument')163 ->with($this->equalTo('another'))164 ->will($this->returnValue($this->getMock('DOMDocument')));165 $this->assertTrue($this->xmlPostInterceptor->postProcess($this->mockRequest, $this->mockSession, $this->xmlResponse));166 }167 168 /**169 * assure that the language value from the registry is taken170 *171 * @test172 */173 public function registryLanguage()174 {175 stubRegistry::setConfig('net.stubbles.language', 'foo');176 $this->xmlPostInterceptor->expects($this->any())177 ->method('createXMLSkinDocument')178 ->will($this->returnValue($this->getMock('DOMDocument')));179 $this->mockXSLProcessor->expects($this->at(2))->method('setParameter')->with($this->equalTo(''), $this->equalTo('lang'), $this->equalTo('foo'));180 $this->assertTrue($this->xmlPostInterceptor->postProcess($this->mockRequest, $this->mockSession, $this->xmlResponse));181 }182 183 /**184 * assure that the language value from the page is taken185 *186 * @test187 */188 public function pageLanguage()189 {190 $this->page->setProperty('language', 'bar');191 stubRegistry::setConfig('net.stubbles.language', 'foo');192 $this->xmlPostInterceptor->expects($this->any())193 ->method('createXMLSkinDocument')194 ->will($this->returnValue($this->getMock('DOMDocument')));195 $this->mockXSLProcessor->expects($this->at(2))->method('setParameter')->with($this->equalTo(''), $this->equalTo('lang'), $this->equalTo('bar'));196 $this->assertTrue($this->xmlPostInterceptor->postProcess($this->mockRequest, $this->mockSession, $this->xmlResponse));197 }198 199 /**200 * assure that the language value from the session is taken201 *202 * @test203 */204 public function sessionLanguage()205 {206 $this->xmlPostInterceptor->expects($this->any())207 ->method('createXMLSkinDocument')208 ->will($this->returnValue($this->getMock('DOMDocument')));209 $this->mockSession->expects($this->once())->method('hasValue')->will($this->returnValue(true));210 $this->mockSession->expects($this->once())->method('getValue')->will($this->returnValue('baz'));211 $this->page->setProperty('language', 'bar');212 stubRegistry::setConfig('net.stubbles.language', 'foo');213 $this->mockXSLProcessor->expects($this->at(2))->method('setParameter')->with($this->equalTo(''), $this->equalTo('lang'), $this->equalTo('baz'));214 $this->assertTrue($this->xmlPostInterceptor->postProcess($this->mockRequest, $this->mockSession, $this->xmlResponse));215 }216 217 /**218 * assure that missing binder triggers an exception219 *220 * @test221 * @expectedException stubRuntimeException222 */223 public function withoutBinderInRegistry()224 {225 stubRegistry::remove(stubBinder::REGISTRY_KEY);226 $this->xmlPostInterceptor->postProcess($this->mockRequest, $this->mockSession, $this->xmlResponse);227 }228 107 } 229 108 ?>
