Changeset 1433
- Timestamp:
- 03/18/08 14:59:53 (6 months ago)
- Files:
-
- trunk/src/main/php/net/stubbles/websites/xml/generator/stubPageXMLGenerator.php (modified) (5 diffs)
- trunk/src/main/php/net/stubbles/websites/xml/generator/stubRequestXMLGenerator.php (modified) (2 diffs)
- trunk/src/main/php/net/stubbles/websites/xml/generator/stubSessionXMLGenerator.php (modified) (1 diff)
- trunk/src/main/php/net/stubbles/websites/xml/generator/stubXMLGenerator.php (modified) (1 diff)
- trunk/src/test/php/net/stubbles/websites/xml/generator/stubPageXMLGeneratorTestCase.php (modified) (7 diffs)
- trunk/src/test/php/net/stubbles/websites/xml/generator/stubRequestXMLGeneratorTestCase.php (modified) (1 diff)
- trunk/src/test/php/net/stubbles/websites/xml/generator/stubSessionXMLGeneratorTestCase.php (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/main/php/net/stubbles/websites/xml/generator/stubPageXMLGenerator.php
r1427 r1433 10 10 'net::stubbles::ipo::request::stubRequest', 11 11 'net::stubbles::ipo::request::stubRequestPrefixDecorator', 12 'net::stubbles::ipo::response::stubResponse', 12 13 'net::stubbles::ipo::session::stubSession', 13 14 'net::stubbles::lang::exceptions::stubRuntimeException', 14 'net::stubbles::util::stubRegistry',15 15 'net::stubbles::websites::stubPage', 16 16 'net::stubbles::websites::xml::generator::stubXMLGenerator', … … 50 50 protected $page; 51 51 /** 52 * list of available page elements 53 * 54 * @var array<string,stubPageElement> 55 */ 56 protected $pageElements = array(); 57 /** 52 58 * injector instance to be used 53 59 * … … 55 61 */ 56 62 protected $injector; 63 /** 64 * switch whether document part is cachable or not 65 * 66 * @var bool 67 */ 68 protected $isCachable = true; 69 /** 70 * list of cache variables for this page 71 * 72 * @var array<string,scalar> 73 */ 74 protected $cacheVars = array(); 75 /** 76 * list of used files for this page 77 * 78 * @var array<string> 79 */ 80 protected $usedFiles = array(); 57 81 58 82 /** … … 62 86 * @param stubSession $session 63 87 * @param stubResponse $response 64 * @param stubPage $page65 88 * @param stubInjector $injector 66 89 * @Inject 67 90 */ 68 public function __construct(stubRequest $request, stubSession $session, stubResponse $response, stub Page $page, stubInjector $injector)91 public function __construct(stubRequest $request, stubSession $session, stubResponse $response, stubInjector $injector) 69 92 { 70 $this->request = $request;93 $this->request = new stubRequestPrefixDecorator($request, ''); 71 94 $this->session = $session; 72 95 $this->response = $response; 73 $this->page = $page;74 96 $this->injector = $injector; 97 } 98 99 /** 100 * sets the page 101 * 102 * @param stubPage $page 103 * @Inject 104 */ 105 public function setPage(stubPage $page) 106 { 107 $this->page = $page; 108 if (count($this->page->getResources()) > 0) { 109 $this->isCachable = false; 110 } 111 112 foreach ($this->page->getElements() as $name => $element) { 113 $this->request->setPrefix($name); 114 $this->injector->handleInjections($element); 115 $element->init($this->request, $this->session, $this->response); 116 if ($element->isAvailable() === true) { 117 $this->pageElements[$name] = $element; 118 // we can spare this if the page is not cachable 119 if (true === $this->isCachable) { 120 if ($element->isCachable() === false) { 121 $this->isCachable = false; 122 } elseif (true === $this->isCachable) { 123 $this->cacheVars = array_merge($this->cacheVars, $element->getCacheVars()); 124 $this->usedFiles = array_merge($this->usedFiles, $element->getUsedFiles()); 125 } 126 } 127 } 128 } 129 } 130 131 /** 132 * checks whether document part is cachable or not 133 * 134 * Document part is cachable if page has no session resources and all page 135 * elements are cachable. 136 * 137 * @return bool 138 */ 139 public function isCachable() 140 { 141 return $this->isCachable; 142 } 143 144 /** 145 * returns a list of variables that have an influence on caching 146 * 147 * @return array<string,scalar> 148 */ 149 public function getCacheVars() 150 { 151 return $this->cacheVars; 152 } 153 154 /** 155 * returns a list of files used to create the content 156 * 157 * @return array<string> 158 */ 159 public function getUsedFiles() 160 { 161 return $this->usedFiles; 75 162 } 76 163 … … 83 170 public function generate(stubXMLStreamWriter $xmlStreamWriter, stubXMLSerializer $xmlSerializer) 84 171 { 85 $prefixRequest = new stubRequestPrefixDecorator($this->request, ''); 86 $formValues = array(); 87 foreach ($this->page->getElements() as $name => $element) { 88 $prefixRequest->setPrefix($name); 89 $this->injector->handleInjections($element); 90 $element->init($prefixRequest, $this->session, $this->response); 91 if ($element->isAvailable() === true) { 92 $data = $element->process(); 93 if ($prefixRequest->isCancelled() === true) { 94 return; 95 } 96 97 $xmlSerializer->serialize($data, $xmlStreamWriter, array(stubXMLSerializer::OPT_ROOT_TAG => $name)); 98 if ($element instanceof stubXMLPageElement) { 99 $formValues[$name] = $element->getFormValues(); 100 } 172 $formValues = array(); 173 foreach ($this->pageElements as $name => $element) { 174 $this->request->setPrefix($name); 175 $element->init($this->request, $this->session, $this->response); 176 $data = $element->process(); 177 if ($this->request->isCancelled() === true) { 178 return; 179 } 180 181 $xmlSerializer->serialize($data, $xmlStreamWriter, array(stubXMLSerializer::OPT_ROOT_TAG => $name)); 182 if ($element instanceof stubXMLPageElement) { 183 $formValues[$name] = $element->getFormValues(); 101 184 } 102 185 } trunk/src/main/php/net/stubbles/websites/xml/generator/stubRequestXMLGenerator.php
r1423 r1433 34 34 * Concrete request values will not be written into the result document. 35 35 * 36 * The serializing of the request should take place after page elements were 37 * processed - only these generate the request value errors stored in the 38 * request. Additionally those page elements should take care of whether a page 39 * is cachable or not and the required cache variables. 40 * 36 41 * @package stubbles 37 42 * @subpackage websites_xml_generator … … 58 63 59 64 /** 65 * checks whether document part is cachable or not 66 * 67 * @return bool 68 */ 69 public function isCachable() 70 { 71 return true; 72 } 73 74 /** 75 * returns a list of variables that have an influence on caching 76 * 77 * @return array<string,scalar> 78 */ 79 public function getCacheVars() 80 { 81 return array(); 82 } 83 84 /** 85 * returns a list of files used to create the content 86 * 87 * @return array<string> 88 */ 89 public function getUsedFiles() 90 { 91 return array(); 92 } 93 94 /** 60 95 * serializes request data into result document 61 96 * trunk/src/main/php/net/stubbles/websites/xml/generator/stubSessionXMLGenerator.php
r1432 r1433 67 67 68 68 /** 69 * checks whether document part is cachable or not 70 * 71 * @return bool 72 */ 73 public function isCachable() 74 { 75 return true; 76 } 77 78 /** 79 * returns a list of variables that have an influence on caching 80 * 81 * @return array<string,scalar> 82 */ 83 public function getCacheVars() 84 { 85 return array('isNew' => $this->session->isNew(), 86 'variant' => (string) $this->session->getValue('net.stubbles.websites.variantmanager.variant.name'), 87 'acceptsCookies' => $this->request->acceptsCookies() 88 ); 89 } 90 91 /** 92 * returns a list of files used to create the content 93 * 94 * @return array<string> 95 */ 96 public function getUsedFiles() 97 { 98 return array(); 99 } 100 101 /** 69 102 * serializes session data into result document 70 103 * trunk/src/main/php/net/stubbles/websites/xml/generator/stubXMLGenerator.php
r1423 r1433 19 19 { 20 20 /** 21 * checks whether document part is cachable or not 22 * 23 * @return bool 24 */ 25 public function isCachable(); 26 27 /** 28 * returns a list of variables that have an influence on caching 29 * 30 * @return array<string,scalar> 31 */ 32 public function getCacheVars(); 33 34 /** 35 * returns a list of files used to create the content 36 * 37 * @return array<string> 38 */ 39 public function getUsedFiles(); 40 41 /** 21 42 * serializes something 22 43 * trunk/src/test/php/net/stubbles/websites/xml/generator/stubPageXMLGeneratorTestCase.php
r1427 r1433 75 75 $this->page = new stubPage(); 76 76 $this->mockInjector = $this->getMock('stubInjector'); 77 $this->pageXMLGenerator = new stubPageXMLGenerator($this->mockRequest, $this->mockSession, $this->mockResponse, $this->page, $this->mockInjector); 77 $this->pageXMLGenerator = new stubPageXMLGenerator($this->mockRequest, $this->mockSession, $this->mockResponse, $this->mockInjector); 78 $this->pageXMLGenerator->setPage($this->page); 78 79 $this->mockXMLStreamWriter = $this->getMock('stubXMLStreamWriter'); 79 80 $this->mockXMLSerializer = $this->getMock('stubXMLSerializer'); … … 102 103 $this->mockXMLStreamWriter->expects($this->once())->method('writeStartElement'); 103 104 $this->mockXMLStreamWriter->expects($this->once())->method('writeEndElement'); 105 $this->assertTrue($this->pageXMLGenerator->isCachable()); 106 $this->assertEquals(array(), $this->pageXMLGenerator->getCacheVars()); 107 $this->assertEquals(array(), $this->pageXMLGenerator->getUsedFiles()); 104 108 $this->pageXMLGenerator->generate($this->mockXMLStreamWriter, $this->mockXMLSerializer); 105 109 } … … 116 120 $pageElement1 = $this->getMock('stubPageElement'); 117 121 $pageElement1->expects($this->any())->method('getName')->will($this->returnValue('foo')); 122 $pageElement1->expects($this->once())->method('isAvailable')->will($this->returnValue(true)); 123 $pageElement1->expects($this->once())->method('isCachable')->will($this->returnValue(true)); 124 $pageElement1->expects($this->once())->method('getCacheVars')->will($this->returnValue(array())); 125 $pageElement1->expects($this->once())->method('getUsedFiles')->will($this->returnValue(array())); 118 126 $pageElement1->expects($this->once())->method('process')->will($this->returnValue('foo')); 119 $pageElement1->expects($this->once())->method('isAvailable')->will($this->returnValue(true));120 127 $pageElement2 = $this->getMock('stubPageElement'); 121 128 $pageElement2->expects($this->any())->method('getName')->will($this->returnValue('bar')); 129 $pageElement2->expects($this->once())->method('isAvailable')->will($this->returnValue(false)); 130 $pageElement2->expects($this->never())->method('isCachable'); 131 $pageElement2->expects($this->never())->method('getCacheVars'); 132 $pageElement2->expects($this->never())->method('getUsedFiles'); 122 133 $pageElement2->expects($this->never())->method('process'); 123 $pageElement2->expects($this->once())->method('isAvailable')->will($this->returnValue(false));124 134 $pageElement3 = $this->getMock('stubXMLPageElement'); 125 135 $pageElement3->expects($this->any())->method('getName')->will($this->returnValue('baz')); 136 $pageElement3->expects($this->once())->method('isAvailable')->will($this->returnValue(true)); 137 $pageElement3->expects($this->once())->method('isCachable')->will($this->returnValue(true)); 138 $pageElement3->expects($this->once())->method('getCacheVars')->will($this->returnValue(array())); 139 $pageElement3->expects($this->once())->method('getUsedFiles')->will($this->returnValue(array())); 126 140 $pageElement3->expects($this->once())->method('process')->will($this->returnValue('baz')); 127 $pageElement3->expects($this->once())->method('isAvailable')->will($this->returnValue(true));128 141 $pageElement3->expects($this->once())->method('getFormValues')->will($this->returnValue(array('foo'))); 129 142 $this->page->addElement($pageElement1); 130 143 $this->page->addElement($pageElement2); 131 144 $this->page->addElement($pageElement3); 145 $this->pageXMLGenerator->setPage($this->page); 132 146 133 147 $this->mockXMLSerializer->expects($this->at(0)) … … 142 156 $this->mockXMLStreamWriter->expects($this->once())->method('writeStartElement'); 143 157 $this->mockXMLStreamWriter->expects($this->once())->method('writeEndElement'); 158 $this->assertTrue($this->pageXMLGenerator->isCachable()); 159 $this->assertEquals(array(), $this->pageXMLGenerator->getCacheVars()); 160 $this->assertEquals(array(), $this->pageXMLGenerator->getUsedFiles()); 144 161 $this->pageXMLGenerator->generate($this->mockXMLStreamWriter, $this->mockXMLSerializer); 145 162 } … … 153 170 { 154 171 $this->mockRequest->expects($this->once())->method('isCancelled')->will($this->returnValue(true)); 155 $this->mockInjector->expects($this-> once())->method('handleInjections');172 $this->mockInjector->expects($this->exactly(2))->method('handleInjections'); 156 173 $pageElement1 = $this->getMock('stubPageElement'); 157 174 $pageElement1->expects($this->any())->method('getName')->will($this->returnValue('foo')); 175 $pageElement1->expects($this->once())->method('isAvailable')->will($this->returnValue(true)); 176 $pageElement1->expects($this->once())->method('isCachable')->will($this->returnValue(true)); 177 $pageElement1->expects($this->once())->method('getCacheVars')->will($this->returnValue(array('foo' => 'bar'))); 178 $pageElement1->expects($this->once())->method('getUsedFiles')->will($this->returnValue(array('foo.xml'))); 158 179 $pageElement1->expects($this->once())->method('process')->will($this->returnValue('foo')); 159 $pageElement1->expects($this->once())->method('isAvailable')->will($this->returnValue(true));160 180 $pageElement2 = $this->getMock('stubPageElement'); 161 181 $pageElement2->expects($this->any())->method('getName')->will($this->returnValue('bar')); 182 $pageElement2->expects($this->once())->method('isAvailable')->will($this->returnValue(true)); 183 $pageElement2->expects($this->once())->method('isCachable')->will($this->returnValue(true)); 184 $pageElement2->expects($this->once())->method('getCacheVars')->will($this->returnValue(array('bar' => 313))); 185 $pageElement2->expects($this->once())->method('getUsedFiles')->will($this->returnValue(array())); 162 186 $pageElement2->expects($this->never())->method('process'); 163 $pageElement2->expects($this->never())->method('isAvailable');164 187 $this->page->addElement($pageElement1); 165 188 $this->page->addElement($pageElement2); 189 $this->pageXMLGenerator->setPage($this->page); 166 190 167 191 $this->mockXMLSerializer->expects($this->never())->method('serialize'); 168 192 $this->mockXMLStreamWriter->expects($this->never())->method('writeStartElement'); 169 193 $this->mockXMLStreamWriter->expects($this->never())->method('writeEndElement'); 194 $this->assertTrue($this->pageXMLGenerator->isCachable()); 195 $this->assertEquals(array('foo' => 'bar', 'bar' => 313), $this->pageXMLGenerator->getCacheVars()); 196 $this->assertEquals(array('foo.xml'), $this->pageXMLGenerator->getUsedFiles()); 197 $this->pageXMLGenerator->generate($this->mockXMLStreamWriter, $this->mockXMLSerializer); 198 } 199 200 /** 201 * page with cancelling elements: stop processing 202 * 203 * @test 204 */ 205 public function pageWithNonCachableElement() 206 { 207 $this->mockRequest->expects($this->exactly(2))->method('isCancelled')->will($this->returnValue(false)); 208 $this->mockInjector->expects($this->exactly(2))->method('handleInjections'); 209 $pageElement1 = $this->getMock('stubPageElement'); 210 $pageElement1->expects($this->any())->method('getName')->will($this->returnValue('foo')); 211 $pageElement1->expects($this->once())->method('isAvailable')->will($this->returnValue(true)); 212 $pageElement1->expects($this->once())->method('isCachable')->will($this->returnValue(false)); 213 $pageElement1->expects($this->never())->method('getCacheVars'); 214 $pageElement1->expects($this->never())->method('getUsedFiles'); 215 $pageElement1->expects($this->once())->method('process')->will($this->returnValue('foo')); 216 $pageElement2 = $this->getMock('stubPageElement'); 217 $pageElement2->expects($this->any())->method('getName')->will($this->returnValue('bar')); 218 $pageElement2->expects($this->once())->method('isAvailable')->will($this->returnValue(true)); 219 $pageElement2->expects($this->never())->method('isCachable'); 220 $pageElement1->expects($this->never())->method('getCacheVars'); 221 $pageElement1->expects($this->never())->method('getUsedFiles'); 222 $pageElement2->expects($this->once())->method('process')->will($this->returnValue('bar')); 223 $this->page->addElement($pageElement1); 224 $this->page->addElement($pageElement2); 225 $this->pageXMLGenerator->setPage($this->page); 226 227 $this->mockXMLSerializer->expects($this->at(0)) 228 ->method('serialize') 229 ->with($this->equalTo('foo'), $this->equalTo($this->mockXMLStreamWriter), $this->equalTo(array(stubXMLSerializer::OPT_ROOT_TAG => 'foo'))); 230 $this->mockXMLSerializer->expects($this->at(1)) 231 ->method('serialize') 232 ->with($this->equalTo('bar'), $this->equalTo($this->mockXMLStreamWriter), $this->equalTo(array(stubXMLSerializer::OPT_ROOT_TAG => 'bar'))); 233 $this->mockXMLStreamWriter->expects($this->once())->method('writeStartElement'); 234 $this->mockXMLStreamWriter->expects($this->once())->method('writeEndElement'); 235 $this->assertFalse($this->pageXMLGenerator->isCachable()); 236 $this->assertEquals(array(), $this->pageXMLGenerator->getCacheVars()); 237 $this->assertEquals(array(), $this->pageXMLGenerator->getUsedFiles()); 170 238 $this->pageXMLGenerator->generate($this->mockXMLStreamWriter, $this->mockXMLSerializer); 171 239 } … … 179 247 { 180 248 $this->page->setResources(array('foo' => 'stdClass')); 249 $this->pageXMLGenerator->setPage($this->page); 181 250 182 251 $this->mockRequest->expects($this->never())->method('isCancelled'); … … 192 261 $this->mockXMLStreamWriter->expects($this->once())->method('writeStartElement'); 193 262 $this->mockXMLStreamWriter->expects($this->once())->method('writeEndElement'); 194 $this->pageXMLGenerator->generate($this->mockXMLStreamWriter, $this->mockXMLSerializer); 195 263 $this->assertFalse($this->pageXMLGenerator->isCachable()); 264 $this->assertEquals(array(), $this->pageXMLGenerator->getCacheVars()); 265 $this->assertEquals(array(), $this->pageXMLGenerator->getUsedFiles()); 266 $this->pageXMLGenerator->generate($this->mockXMLStreamWriter, $this->mockXMLSerializer); 196 267 } 197 268 } trunk/src/test/php/net/stubbles/websites/xml/generator/stubRequestXMLGeneratorTestCase.php
r1423 r1433 53 53 54 54 /** 55 * the request data is always cachable - it is important that the relevant 56 * page elements decide about cachability and cache variables 57 * 58 * @test 59 */ 60 public function cachingMethods() 61 { 62 $this->assertTrue($this->requestXMLGenerator->isCachable()); 63 $this->assertEquals(array(), $this->requestXMLGenerator->getCacheVars()); 64 $this->assertEquals(array(), $this->requestXMLGenerator->getUsedFiles()); 65 66 } 67 68 /** 55 69 * no request value errors: request elements stays empty 56 70 * trunk/src/test/php/net/stubbles/websites/xml/generator/stubSessionXMLGeneratorTestCase.php
r1432 r1433 23 23 * @var stubSessionXMLGenerator 24 24 */ 25 protected $ requestXMLGenerator;25 protected $sessionXMLGenerator; 26 26 /** 27 27 * mocked request instance … … 56 56 $this->mockRequest = $this->getMock('stubRequest'); 57 57 $this->mockSession = $this->getMock('stubSession'); 58 $this-> requestXMLGenerator = new stubSessionXMLGenerator($this->mockRequest, $this->mockSession);58 $this->sessionXMLGenerator = new stubSessionXMLGenerator($this->mockRequest, $this->mockSession); 59 59 $this->xmlStreamWriter = stubXMLStreamWriterFactory::createAsAvailable(); 60 60 $this->mockXMLSerializer = $this->getMock('stubXMLSerializer'); 61 } 62 63 /** 64 * cache variables should be whether session is new, the name of the variant 65 * and if the requestor accepts cookies or not 66 * 67 * @test 68 */ 69 public function isCachable() 70 { 71 $this->assertTrue($this->sessionXMLGenerator->isCachable()); 72 } 73 74 /** 75 * cache variables should be whether session is new, the name of the variant 76 * and if the requestor accepts cookies or not 77 * 78 * @test 79 */ 80 public function cacheVars() 81 { 82 $this->mockRequest->expects($this->once())->method('acceptsCookies')->will($this->returnValue(true)); 83 $this->mockSession->expects($this->once())->method('isNew')->will($this->returnValue(true)); 84 $this->mockSession->expects($this->once())->method('getValue')->will($this->returnValue('variant_name')); 85 $this->assertEquals(array('isNew' => true, 86 'variant' => 'variant_name', 87 'acceptsCookies' => true 88 ), 89 $this->sessionXMLGenerator->getCacheVars() 90 ); 91 } 92 93 /** 94 * no files used for generating the cache content 95 * 96 * @test 97 */ 98 public function getUsedFiles() 99 { 100 $this->assertEquals(array(), $this->sessionXMLGenerator->getUsedFiles()); 61 101 } 62 102 … … 73 113 $this->mockSession->expects($this->once())->method('isNew')->will($this->returnValue(true)); 74 114 $this->mockSession->expects($this->exactly(2))->method('getValue')->will($this->onConsecutiveCalls('variant_name', 'variant_alias')); 75 $this-> requestXMLGenerator->generate($this->xmlStreamWriter, $this->mockXMLSerializer);115 $this->sessionXMLGenerator->generate($this->xmlStreamWriter, $this->mockXMLSerializer); 76 116 $doc = $this->xmlStreamWriter->asXML(); 77 117 $this->assertEquals('<?xml version="1.0" encoding="UTF-8"?>' . "\n" . … … 100 140 $this->mockSession->expects($this->once())->method('isNew')->will($this->returnValue(false)); 101 141 $this->mockSession->expects($this->exactly(2))->method('getValue')->will($this->returnValue(null)); 102 $this-> requestXMLGenerator->generate($this->xmlStreamWriter, $this->mockXMLSerializer);142 $this->sessionXMLGenerator->generate($this->xmlStreamWriter, $this->mockXMLSerializer); 103 143 $doc = $this->xmlStreamWriter->asXML(); 104 144 $this->assertEquals('<?xml version="1.0" encoding="UTF-8"?>' . "\n" .
