Changeset 1413

Show
Ignore:
Timestamp:
03/11/08 18:37:13 (4 months ago)
Author:
mikey
Message:

implemented refactoring #130: move skin generation from xml post interceptor into its own class

Files:

Legend:

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

    r1360 r1413  
    77 * @subpackage  websites_xml 
    88 */ 
    9 stubClassLoader::load('net::stubbles::ioc::stubBinder', 
    10                       'net::stubbles::util::stubFactory', 
    11                       'net::stubbles::util::stubRegistry', 
     9stubClassLoader::load('net::stubbles::websites::xml::stubSkinGenerator', 
    1210                      'net::stubbles::websites::xml::stubXMLAbstractPostInterceptor', 
    13                       'net::stubbles::xml::stubXMLXIncludeStreamWrapper', 
    14                       'net::stubbles::xml::stubXMLStreamWriterFactory', 
    1511                      'net::stubbles::xml::xsl::stubXSLProcessor' 
    1612); 
    1713/** 
    1814 * Post interceptor that transforms the xml result document into html. 
    19  *  
    20  * When using this post interceptor please make sure that within the registry 
    21  * a value accessible with key 'net::stubbles::ioc.stubBinder' is configured and 
    22  * an instance of net::stubbles::ioc.stubBinder. If the pre interceptor 
    23  * net::stubbles::ioc.stubIOCPreInterceptor is configured as pre interceptor this 
    24  * one will put a stubBinder instance into the registry. 
    25  * Please make sure as well that a file named xsl-callbacks.ini resides in the 
    26  * base config directory configured in stubConfig::getConfigPath(). 
    2715 * 
    2816 * @package     stubbles 
     
    3119class stubXMLPostInterceptor extends stubXMLAbstractPostInterceptor 
    3220{ 
    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     
    4322    /** 
    4423     * does the postprocessing stuff 
     
    5130    protected function doPostProcess(stubRequest $request, stubSession $session, stubXMLResponse $response) 
    5231    { 
    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()); 
    6032        $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)); 
    9234        $xmlStreamWriter = $response->getXMLStreamWriter(); 
    9335        if ($xmlStreamWriter->isFinished() === false) { 
     
    10042 
    10143    /** 
    102      * use same xsl processor for the xincludes 
     44     * creates a new skin generator instance 
    10345     * 
    104      * @param  stubXSLProcessor  $xslProcessor 
     46     * @return  stubSkinGenerator 
    10547     */ 
    106     protected function setXIncludeProcessor($xslProcessor
     48    protected function getSkinGenerator(
    10749    { 
    108         stubXMLXIncludeStreamWrapper::setXSLProcessor($xslProcessor); 
     50        return new stubSkinGenerator(); 
    10951    } 
    11052 
     
    11658    protected function createXSLProcessor() 
    11759    { 
    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(); 
    15961    } 
    16062} 
  • trunk/src/test/php/net/stubbles/websites/WebsitesTestSuite.php

    r1402 r1413  
    4949        // xml tests 
    5050        $suite->addTestFile($dir . '/xml/stubShowLastXMLInterceptorTestCase.php'); 
     51        $suite->addTestFile($dir . '/xml/stubSkinGeneratorTestCase.php'); 
    5152        $suite->addTestFile($dir . '/xml/stubXMLPostInterceptorTestCase.php'); 
    5253        $suite->addTestFile($dir . '/xml/stubXMLProcessorTestCase.php'); 
  • trunk/src/test/php/net/stubbles/websites/xml/stubXMLPostInterceptorTestCase.php

    r1360 r1413  
    88 */ 
    99stubClassLoader::load('net::stubbles::websites::xml::stubXMLPostInterceptor'); 
    10 class TestDOMDocument extends DOMDocument 
    11 { 
    12     // intentionally empty 
    13 } 
    1410/** 
    1511 * Tests for net::stubbles::websites::xml::stubXMLPostInterceptor. 
     
    3329    protected $xmlResponse; 
    3430    /** 
    35      * instance to be used for tests 
    36      * 
    37      * @var  PHPUnit_Framework_MockObject_MockObject 
    38      */ 
    39     protected $mockXMLStreamWriter; 
    40     /** 
    4131     * mocked request instance 
    4232     * 
     
    5141    protected $mockResponse; 
    5242    /** 
    53      * the page instance 
    54      * 
    55      * @var  stubPage 
    56      */ 
    57     protected $page; 
    58     /** 
    5943     * mocked session instance 
    6044     * 
     
    6246     */ 
    6347    protected $mockSession; 
    64     /** 
    65      * mocked xsl processor 
    66      * 
    67      * @var  PHPUnit_Framework_MockObject_MockObject 
    68      */ 
    69     protected $mockXSLProcessor; 
    70     /** 
    71      * mocked image callback 
    72      * 
    73      * @var  PHPUnit_Framework_MockObject_MockObject 
    74      */ 
    75     protected $mockImageCallback; 
    7648 
    7749    /** 
     
    8153    { 
    8254        $this->xmlPostInterceptor = $this->getMock('stubXMLPostInterceptor', 
    83                                                    array('setXIncludeProcessor', 
    84                                                          'createXSLProcessor', 
    85                                                          'getCallbackList', 
    86                                                         /* 'createXSLStylesheet',*/ 
    87                                                          'createXMLSkinDocument' 
     55                                                   array('getSkinGenerator', 
     56                                                         'createXSLProcessor' 
    8857                                                   ) 
    8958                                    ); 
    9059        $this->mockRequest         = $this->getMock('stubRequest'); 
    9160        $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'); 
    11263         
    113         stubRegistry::set(stubBinder::REGISTRY_KEY, new stubBinder()); 
    114     } 
    115  
    116     /** 
    117      * clear test environment 
    118      */ 
    119     public function tearDown() 
    120     { 
    121         stubRegistry::remove(stubBinder::REGISTRY_KEY); 
    12264    } 
    12365 
     
    14082    public function correctResponse() 
    14183    { 
    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         
    149104        $this->mockResponse->expects($this->once())->method('replaceData')->with($this->equalTo('<html><head><title>Test</title></head><body><p>Hello world.</p></body></html>')); 
    150105        $this->assertTrue($this->xmlPostInterceptor->postProcess($this->mockRequest, $this->mockSession, $this->xmlResponse)); 
    151106    } 
    152  
    153     /** 
    154      * assure that another skin will be used 
    155      * 
    156      * @test 
    157      */ 
    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 taken 
    170      * 
    171      * @test 
    172      */ 
    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 taken 
    185      * 
    186      * @test 
    187      */ 
    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 taken 
    201      * 
    202      * @test 
    203      */ 
    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 exception 
    219      * 
    220      * @test 
    221      * @expectedException  stubRuntimeException 
    222      */ 
    223     public function withoutBinderInRegistry() 
    224     { 
    225         stubRegistry::remove(stubBinder::REGISTRY_KEY); 
    226         $this->xmlPostInterceptor->postProcess($this->mockRequest, $this->mockSession, $this->xmlResponse); 
    227     } 
    228107} 
    229108?>