Changeset 1419

Show
Ignore:
Timestamp:
03/14/08 11:53:22 (7 months ago)
Author:
mikey
Message:

implemented refactoring #131: move transformation of dom tree with skin from xml post interceptor to xml processor

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/config/xml/interceptors.xml

    r1219 r1419  
    99  <postInterceptors> 
    1010    <postInterceptor type="net::stubbles::websites::xml::stubShowLastXMLInterceptor" /> 
    11     <postInterceptor type="net::stubbles::websites::xml::stubXMLPostInterceptor" /> 
    1211  </postInterceptors> 
    1312</xj:configuration> 
  • trunk/examples/config/xml/interceptors-xml.xml

    r1392 r1419  
    1111  <postInterceptors> 
    1212    <postInterceptor type="net::stubbles::websites::xml::stubShowLastXMLInterceptor"/> 
    13     <postInterceptor type="net::stubbles::websites::xml::stubXMLPostInterceptor"/> 
    1413  </postInterceptors> 
    1514</xj:configuration> 
  • trunk/src/main/php/net/stubbles/websites/xml/stubXMLProcessor.php

    r1418 r1419  
    88 * @subpackage  websites_xml 
    99 */ 
    10 stubClassLoader::load('net::stubbles::websites::processors::stubAbstractPageProcessor', 
    11                       'net::stubbles::websites::xml::stubXMLResponse', 
    12                       'net::stubbles::ioc::stubBinder', 
     10stubClassLoader::load('net::stubbles::ioc::stubBinder', 
    1311                      'net::stubbles::lang::exceptions::stubRuntimeException', 
    1412                      'net::stubbles::util::stubRegistry', 
     13                      'net::stubbles::websites::processors::stubAbstractPageProcessor', 
     14                      'net::stubbles::websites::xml::stubSkinGenerator', 
     15                      'net::stubbles::websites::xml::stubXMLResponse', 
    1516                      'net::stubbles::xml::stubXMLStreamWriterFactory', 
    1617                      'net::stubbles::xml::serializer::stubXMLSerializer' 
     
    8485        } 
    8586 
     87        $xmlStreamWriter->writeEndElement(); // end document 
    8688        $this->response->setXMLStreamWriter($xmlStreamWriter); 
     89        $xslProcessor = $this->createXSLProcessor(); 
     90        $xslProcessor->importXSLStylesheet($this->createSkinGenerator()->generate($this->session, $this->response)); 
     91        $xslProcessor->setXMLDocument($xmlStreamWriter->asDOM()); 
     92        $this->response->replaceData(str_replace(' xmlns=""', '', preg_replace('/ xml:base="(.*)"/U', '', $xslProcessor->transformToXML()))); 
    8793    } 
    8894 
     
    9298     * @return  stubXMLStreamWriter 
    9399     */ 
     100    // @codeCoverageIgnoreStart 
    94101    protected function createXMLStreamWriter() 
    95102    { 
    96103        return stubXMLStreamWriterFactory::createAsAvailable(); 
    97104    } 
     105    // @codeCoverageIgnoreEnd 
    98106 
    99107    /** 
     
    102110     * @return  stubXMLSerializer 
    103111     */ 
     112    // @codeCoverageIgnoreStart 
    104113    protected function createXMLSerializer() 
    105114    { 
    106115        return new stubXMLSerializer(); 
    107116    } 
     117    // @codeCoverageIgnoreEnd 
     118 
     119    /** 
     120     * creates a new skin generator instance 
     121     * 
     122     * @return  stubSkinGenerator 
     123     */ 
     124    // @codeCoverageIgnoreStart 
     125    protected function createSkinGenerator() 
     126    { 
     127        return new stubSkinGenerator(); 
     128    } 
     129    // @codeCoverageIgnoreEnd 
     130 
     131    /** 
     132     * creates a stubXSLProcessor instance 
     133     * 
     134     * @return  stubXSLProcessor 
     135     */ 
     136    // @codeCoverageIgnoreStart 
     137    protected function createXSLProcessor() 
     138    { 
     139        return new stubXSLProcessor(); 
     140    } 
     141    // @codeCoverageIgnoreEnd 
    108142} 
    109143?> 
  • trunk/src/test/php/net/stubbles/integration/InterceptorTestCase.php

    r1308 r1419  
    4040        $postInterceptors = $interceptorXJConfInitializer->getPostInterceptors(); 
    4141        $this->assertType('stubShowLastXMLInterceptor', $postInterceptors[0]); 
    42         $this->assertType('stubXMLPostInterceptor', $postInterceptors[1]); 
    4342         
    4443        // cached 
     
    4847        $postInterceptors = $interceptorXJConfInitializer->getPostInterceptors(); 
    4948        $this->assertType('stubShowLastXMLInterceptor', $postInterceptors[0]); 
    50         $this->assertType('stubXMLPostInterceptor', $postInterceptors[1]); 
    5149    } 
    5250} 
  • trunk/src/test/php/net/stubbles/websites/WebsitesTestSuite.php

    r1415 r1419  
    5050        $suite->addTestFile($dir . '/xml/stubShowLastXMLInterceptorTestCase.php'); 
    5151        $suite->addTestFile($dir . '/xml/stubSkinGeneratorTestCase.php'); 
    52         $suite->addTestFile($dir . '/xml/stubXMLPostInterceptorTestCase.php'); 
    5352        $suite->addTestFile($dir . '/xml/stubXMLProcessorTestCase.php'); 
    5453        $suite->addTestFile($dir . '/xml/stubXMLResponseTestCase.php'); 
  • trunk/src/test/php/net/stubbles/websites/xml/stubXMLProcessorTestCase.php

    r1417 r1419  
    99stubClassLoader::load('net::stubbles::websites::xml::stubXMLProcessor'); 
    1010/** 
    11  * Helper class for the test. 
    12  * 
    13  * @package     stubbles 
    14  * @subpackage  websites_xml_test 
    15  */ 
    16 class TeststubXMLProcessor extends stubXMLProcessor 
    17 { 
    18     /** 
    19      * xml stream writer instance to be used 
    20      * 
    21      * @var  stubXMLStreamWriter 
    22      */ 
    23     protected $xmlStreamWriter; 
    24     /** 
    25      * xml serializer instance to be used 
    26      * 
    27      * @var  stubXMLSerializer 
    28      */ 
    29     protected $xmlSerializer; 
    30  
    31     /** 
    32      * sets the xml stream writer instance to be used 
    33      * 
    34      * @param  stubXMLStreamWriter  $xmlStreamWriter 
    35      */ 
    36     public function setXMLStreamWriter($xmlStreamWriter) 
    37     { 
    38         $this->xmlStreamWriter = $xmlStreamWriter; 
    39     } 
    40  
    41     /** 
    42      * sets the  xml serializer instance to be used 
    43      * 
    44      * @param  stubXMLSerializer  $xmlSerializer 
    45      */ 
    46     public function setXMLSerializer($xmlSerializer) 
    47     { 
    48         $this->xmlSerializer = $xmlSerializer; 
    49     } 
    50  
    51     /** 
    52      * returns a xml stream writer 
    53      * 
    54      * @return  stubXMLStreamWriter 
    55      */ 
    56     protected function createXMLStreamWriter() 
    57     { 
    58         return $this->xmlStreamWriter; 
    59     } 
    60  
    61     /** 
    62      * returns the xml serializer 
    63      * 
    64      * @return  stubXMLSerializer 
    65      */ 
    66     protected function createXMLSerializer() 
    67     { 
    68         return $this->xmlSerializer; 
    69     } 
    70 } 
    71 /** 
    7211 * Tests for net::stubbles::websites::xml::stubXMLProcessor. 
    7312 * 
     
    12564     */ 
    12665    protected $page; 
     66    /** 
     67     * mocked injector instance 
     68     * 
     69     * @var  PHPUnit_Framework_MockObject_MockObject 
     70     */ 
     71    protected $mockInjector; 
    12772 
    12873    /** 
     
    13176    public function setUp() 
    13277    { 
    133         $this->markTestIncomplete('Tests have to be reworked.'); 
     78        $this->mockInjector = $this->getMock('stubInjector'); 
     79        stubRegistry::set(stubBinder::REGISTRY_KEY, new stubBinder($this->mockInjector)); 
    13480        $this->mockRequest         = $this->getMock('stubRequest'); 
    13581        $this->mockRequest->expects($this->any())->method('getValueErrors')->will($this->returnValue(array())); 
     
    13783        $this->mockResponse        = $this->getMock('stubResponse'); 
    13884        $this->mockPageFactory     = $this->getMock('stubPageFactory'); 
    139         $this->xmlProcessor        = new TeststubXMLProcessor($this->mockRequest, $this->mockSession, $this->mockResponse, $this->mockPageFactory); 
     85        $this->xmlProcessor        = $this->getMock('stubXMLProcessor', 
     86                                                    array('createXMLStreamWriter', 
     87                                                          'createXMLSerializer', 
     88                                                          'createSkinGenerator', 
     89                                                          'createXSLProcessor' 
     90                                                    ), 
     91                                                    array($this->mockRequest, $this->mockSession, $this->mockResponse, $this->mockPageFactory) 
     92                                     ); 
    14093        $this->mockXMLStreamWriter = $this->getMock('stubXMLStreamWriter'); 
    14194        $this->mockXMLSerializer   = $this->getMock('stubXMLSerializer'); 
    142         $this->xmlProcessor->setXMLStreamWriter($this->mockXMLStreamWriter); 
    143         $this->xmlProcessor->setXMLSerializer($this->mockXMLSerializer); 
    14495        $this->page = new stubPage(); 
    14596        $this->mockPageFactory->expects($this->any())->method('getPage')->will($this->returnValue($this->page)); 
     
    14798 
    14899    /** 
    149      * assure that processing works as expected 
    150      * 
    151      * @test 
    152      */ 
    153     /*public function defaultPageWithoutElements() 
    154     { 
     100     * clean up test environment 
     101     */ 
     102    public function tearDown() 
     103    { 
     104        stubRegistry::remove(stubBinder::REGISTRY_KEY); 
     105    } 
     106 
     107    /** 
     108     * no binder available > runtime exception 
     109     * 
     110     * @test 
     111     * @expectedException  stubRuntimeException 
     112     */ 
     113    public function processWithoutBinderInRegistryThrowsRuntimeException() 
     114    { 
     115        stubRegistry::remove(stubBinder::REGISTRY_KEY); 
    155116        $this->mockRequest->expects($this->once())->method('hasValue')->will($this->returnValue(false)); 
    156117        $this->mockPageFactory->expects($this->once())->method('getPage')->with($this->equalTo('conf/index')); 
     118        $this->xmlProcessor->expects($this->never())->method('createXMLStreamWriter'); 
     119        $this->xmlProcessor->expects($this->never())->method('createXMLSerializer'); 
     120        $this->xmlProcessor->expects($this->never())->method('createSkinGenerator'); 
     121        $this->xmlProcessor->expects($this->never())->method('createXSLProcessor'); 
     122        $this->xmlProcessor->process(); 
     123    } 
     124 
     125    /** 
     126     * assure that processing works as expected 
     127     * 
     128     * @test 
     129     */ 
     130    public function defaultPage() 
     131    { 
     132        $this->mockRequest->expects($this->once())->method('hasValue')->will($this->returnValue(false)); 
     133        $this->mockPageFactory->expects($this->once())->method('getPage')->with($this->equalTo('conf/index')); 
     134        $this->xmlProcessor->expects($this->once())->method('createXMLStreamWriter')->will($this->returnValue($this->mockXMLStreamWriter)); 
     135        $this->xmlProcessor->expects($this->once())->method('createXMLSerializer')->will($this->returnValue($this->mockXMLSerializer)); 
    157136        $this->mockXMLStreamWriter->expects($this->once())->method('writeStartElement'); 
    158         $this->mockXMLStreamWriter->expects($this->never())->method('writeEndElement'); 
    159         $this->mockXMLSerializer->expects($this->once())->method('serialize'); 
    160         $return = $this->xmlProcessor->process(); 
    161         $this->assertSame($this->xmlProcessor, $return); 
    162         $response = $this->xmlProcessor->getResponse(); 
    163         $this->assertType('stubResponse', $response); 
    164     }*/ 
     137        $this->mockXMLStreamWriter->expects($this->once())->method('writeAttribute')->with($this->equalTo('page'), $this->equalTo('index')); 
     138        $this->mockXMLStreamWriter->expects($this->once())->method('writeEndElement'); 
     139        $mockGlobalXMLElement = $this->getMock('stubGlobalXMLElement'); 
     140        $this->mockInjector->expects($this->exactly(3))->method('getInstance')->will($this->returnValue($mockGlobalXMLElement)); 
     141        $mockGlobalXMLElement->expects($this->exactly(3))->method('append')->with($this->equalTo($this->mockXMLStreamWriter), $this->equalTo($this->mockXMLSerializer)); 
     142        $resultXSL         = new DOMDocument(); 
     143        $mockSkinGenerator = $this->getMock('stubSkinGenerator'); 
     144        $mockSkinGenerator->expects($this->once()) 
     145                          ->method('generate') 
     146                          ->will($this->returnValue($resultXSL)); 
     147        $this->xmlProcessor->expects($this->once())->method('createSkinGenerator')->will($this->returnValue($mockSkinGenerator)); 
     148         
     149        $mockXSLProcessor = $this->getMock('stubXSLProcessor'); 
     150        $mockXSLProcessor->expects($this->once())->method('importXSLStylesheet')->with($this->equalTo($resultXSL)); 
     151        $mockXSLProcessor->expects($this->once())->method('transformToXML')->will($this->returnValue('<html><head><title>Test</title></head><body><p>Hello world.</p></body></html>')); 
     152        $this->xmlProcessor->expects($this->once())->method('createXSLProcessor')->will($this->returnValue($mockXSLProcessor)); 
     153         
     154        $domDocument0 = new DOMDocument(); 
     155        $domDocument0->createElement('bar', 'foo'); 
     156        $this->mockXMLStreamWriter->expects($this->any())->method('asDOM')->will($this->returnValue($domDocument0)); 
     157        $this->mockResponse->expects($this->once())->method('replaceData')->with($this->equalTo('<html><head><title>Test</title></head><body><p>Hello world.</p></body></html>')); 
     158        $this->xmlProcessor->process(); 
     159    } 
    165160 
    166161    /** 
     
    169164     * @test 
    170165     */ 
    171     /*public function fallbackToDefaultPageWithElements() 
     166    public function fallbackToDefaultPage() 
    172167    { 
    173168        $this->mockRequest->expects($this->once())->method('hasValue')->will($this->returnValue(true)); 
    174169        $this->mockRequest->expects($this->once())->method('getValidatedValue')->will($this->returnValue(null)); 
    175170        $this->mockPageFactory->expects($this->once())->method('getPage')->with($this->equalTo('conf/index')); 
    176         $pageElement1 = $this->getMock('stubPageElement'); 
    177         $pageElement1->expects($this->any())->method('getName')->will($this->returnValue('foo')); 
    178         $pageElement1->expects($this->once())->method('process')->will($this->returnValue('foo')); 
    179         $pageElement1->expects($this->once())->method('isAvailable')->will($this->returnValue(true)); 
    180         $pageElement2 = $this->getMock('stubPageElement'); 
    181         $pageElement2->expects($this->any())->method('getName')->will($this->returnValue('bar')); 
    182         $pageElement2->expects($this->once())->method('process')->will($this->returnValue('bar')); 
    183         $pageElement2->expects($this->once())->method('isAvailable')->will($this->returnValue(true)); 
    184         $pageElement3 = $this->getMock('stubPageElement'); 
    185         $pageElement3->expects($this->any())->method('getName')->will($this->returnValue('baz')); 
    186         $pageElement3->expects($this->never())->method('process'); 
    187         $pageElement3->expects($this->once())->method('isAvailable')->will($this->returnValue(false)); 
    188         $this->page->addElement($pageElement1); 
    189         $this->page->addElement($pageElement2); 
    190         $this->page->addElement($pageElement3); 
     171        $this->xmlProcessor->expects($this->once())->method('createXMLStreamWriter')->will($this->returnValue($this->mockXMLStreamWriter)); 
     172        $this->xmlProcessor->expects($this->once())->method('createXMLSerializer')->will($this->returnValue($this->mockXMLSerializer)); 
    191173        $this->mockXMLStreamWriter->expects($this->once())->method('writeStartElement'); 
    192         $this->mockXMLStreamWriter->expects($this->never())->method('writeEndElement'); 
    193         $this->mockXMLSerializer->expects($this->at(0)) 
    194                                 ->method('serialize') 
    195                                 ->with($this->equalTo('foo'), 
    196                                        $this->equalTo($this->mockXMLStreamWriter), 
    197                                        $this->equalTo(array(stubXMLSerializer::OPT_ROOT_TAG => 'foo')) 
    198                                   ); 
    199         $this->mockXMLSerializer->expects($this->at(1)) 
    200                                 ->method('serialize') 
    201                                 ->with($this->equalTo('bar'), 
    202                                        $this->equalTo($this->mockXMLStreamWriter), 
    203                                        $this->equalTo(array(stubXMLSerializer::OPT_ROOT_TAG => 'bar')) 
    204                                   ); 
    205         $return = $this->xmlProcessor->process(); 
    206         $this->assertSame($this->xmlProcessor, $return); 
    207         $response = $this->xmlProcessor->getResponse(); 
    208         $this->assertType('stubResponse', $response); 
    209     }*/ 
     174        $this->mockXMLStreamWriter->expects($this->once())->method('writeAttribute')->with($this->equalTo('page'), $this->equalTo('index')); 
     175        $this->mockXMLStreamWriter->expects($this->once())->method('writeEndElement'); 
     176        $mockGlobalXMLElement = $this->getMock('stubGlobalXMLElement'); 
     177        $this->mockInjector->expects($this->exactly(3))->method('getInstance')->will($this->returnValue($mockGlobalXMLElement)); 
     178        $mockGlobalXMLElement->expects($this->exactly(3))->method('append')->with($this->equalTo($this->mockXMLStreamWriter), $this->equalTo($this->mockXMLSerializer)); 
     179        $resultXSL         = new DOMDocument(); 
     180        $mockSkinGenerator = $this->getMock('stubSkinGenerator'); 
     181        $mockSkinGenerator->expects($this->once()) 
     182                          ->method('generate') 
     183                          ->will($this->returnValue($resultXSL)); 
     184        $this->xmlProcessor->expects($this->once())->method('createSkinGenerator')->will($this->returnValue($mockSkinGenerator)); 
     185         
     186        $mockXSLProcessor = $this->getMock('stubXSLProcessor'); 
     187        $mockXSLProcessor->expects($this->once())->method('importXSLStylesheet')->with($this->equalTo($resultXSL)); 
     188        $mockXSLProcessor->expects($this->once())->method('transformToXML')->will($this->returnValue('<html><head><title>Test</title></head><body><p>Hello world.</p></body></html>')); 
     189        $this->xmlProcessor->expects($this->once())->method('createXSLProcessor')->will($this->returnValue($mockXSLProcessor)); 
     190         
     191        $domDocument0 = new DOMDocument(); 
     192        $domDocument0->createElement('bar', 'foo'); 
     193        $this->mockXMLStreamWriter->expects($this->any())->method('asDOM')->will($this->returnValue($domDocument0)); 
     194        $this->mockResponse->expects($this->once())->method('replaceData')->with($this->equalTo('<html><head><title>Test</title></head><body><p>Hello world.</p></body></html>')); 
     195        $this->xmlProcessor->process(); 
     196    } 
    210197 
    211198    /** 
     
    214201     * @test 
    215202     */ 
    216     /*public function correctPage() 
     203   public function correctPage() 
    217204    { 
    218205        $this->mockRequest->expects($this->once())->method('hasValue')->will($this->returnValue(true)); 
     
    220207        $this->mockPageFactory->expects($this->once())->method('getPage')->with($this->equalTo('conf/baz')); 
    221208        $this->mockPageFactory->expects($this->once())->method('hasPage')->will($this->returnValue(true)); 
    222         $this->xmlProcessor->process(); 
    223     }*/ 
     209        $this->xmlProcessor->expects($this->once())->method('createXMLStreamWriter')->will($this->returnValue($this->mockXMLStreamWriter)); 
     210        $this->xmlProcessor->expects($this->once())->method('createXMLSerializer')->will($this->returnValue($this->mockXMLSerializer)); 
     211        $this->mockXMLStreamWriter->expects($this->once())->method('writeStartElement'); 
     212        $this->mockXMLStreamWriter->expects($this->once())->method('writeAttribute')->with($this->equalTo('page'), $this->equalTo('baz')); 
     213        $this->mockXMLStreamWriter->expects($this->once())->method('writeEndElement'); 
     214        $mockGlobalXMLElement = $this->getMock('stubGlobalXMLElement'); 
     215        $this->mockInjector->expects($this->exactly(3))->method('getInstance')->will($this->returnValue($mockGlobalXMLElement)); 
     216        $mockGlobalXMLElement->expects($this->exactly(3))->method('append')->with($this->equalTo($this->mockXMLStreamWriter), $this->equalTo($this->mockXMLSerializer)); 
     217        $resultXSL         = new DOMDocument(); 
     218        $mockSkinGenerator = $this->getMock('stubSkinGenerator'); 
     219        $mockSkinGenerator->expects($this->once()) 
     220                          ->method('generate') 
     221                          ->will($this->returnValue($resultXSL)); 
     222        $this->xmlProcessor->expects($this->once())->method('createSkinGenerator')->will($this->returnValue($mockSkinGenerator)); 
     223         
     224        $mockXSLProcessor = $this->getMock('stubXSLProcessor'); 
     225        $mockXSLProcessor->expects($this->once())->method('importXSLStylesheet')->with($this->equalTo($resultXSL)); 
     226        $mockXSLProcessor->expects($this->once())->method('transformToXML')->will($this->returnValue('<html><head><title>Test</title></head><body><p>Hello world.</p></body></html>')); 
     227        $this->xmlProcessor->expects($this->once())->method('createXSLProcessor')->will($this->returnValue($mockXSLProcessor)); 
     228         
     229        $domDocument0 = new DOMDocument(); 
     230        $domDocument0->createElement('bar', 'foo'); 
     231        $this->mockXMLStreamWriter->expects($this->any())->method('asDOM')->will($this->returnValue($domDocument0)); 
     232        $this->mockResponse->expects($this->once())->method('replaceData')->with($this->equalTo('<html><head><title>Test</title></head><body><p>Hello world.</p></body></html>')); 
     233        $this->xmlProcessor->process(); 
     234    } 
    224235} 
    225236?>