Changeset 753
- Timestamp:
- 07/02/07 22:59:00 (1 year ago)
- Files:
-
- trunk/src/main/php/net/stubbles/ipo/request/stubAbstractRequest.php (modified) (19 diffs)
- trunk/src/main/php/net/stubbles/ipo/request/stubRequest.php (modified) (16 diffs)
- trunk/src/main/php/net/stubbles/ipo/request/stubRequestPrefixDecorator.php (modified) (13 diffs)
- trunk/src/test/php/net/stubbles/ipo/request/stubAbstractRequestTestCase.php (modified) (17 diffs)
- trunk/src/test/php/net/stubbles/ipo/request/stubRequestPrefixDecoratorTestCase.php (modified) (18 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/main/php/net/stubbles/ipo/request/stubAbstractRequest.php
r427 r753 63 63 */ 64 64 protected $isCancelled = false; 65 65 66 66 /** 67 67 * constructor … … 71 71 $this->doConstuct(); 72 72 } 73 73 74 74 /** 75 75 * template method for child classes to do the real construction … … 86 86 throw new stubException('Cloning of request is not allowed!'); 87 87 } 88 88 89 89 /** 90 90 * checks whether a request value is set or not … … 99 99 return isset($data[$valueName]); 100 100 } 101 101 102 /** 103 * add a value error for a request value 104 * 105 * @param stubRequestValueError $valueError 106 * @param string $valueName 107 * @param int $source 108 */ 109 public function addValueError(stubRequestValueError $valueError, $valueName, $source = self::SOURCE_PARAM) 110 { 111 $error =& $this->getErrors($source); 112 if (isset($error[$valueName]) == false) { 113 $error[$valueName] = array($valueError); 114 } else { 115 $ids = array(); 116 foreach ($error[$valueName] as $errorValue) { 117 $ids[] = $errorValue->getId(); 118 } 119 120 if (in_array($valueError->getId(), $ids) == false) { 121 $error[$valueName][] = $valueError; 122 } 123 } 124 } 125 102 126 /** 103 127 * checks whether a request value has an error after a filter was applied … … 112 136 return isset($error[$valueName]); 113 137 } 114 138 115 139 /** 116 140 * returns a list of errors for given request value … … 129 153 return array(); 130 154 } 131 155 132 156 /** 133 157 * checks whether there are any value errors … … 140 164 return (count($this->getErrors($source)) > 0); 141 165 } 142 166 143 167 /** 144 168 * returns a list of all request value names with their errors … … 151 175 return $this->getErrors($source); 152 176 } 153 177 154 178 /** 155 179 * returns the array with errors from requested source … … 174 198 } 175 199 } 176 200 177 201 /** 178 202 * cancels the request, e.g. if it was detected that it is invalid … … 197 221 $dispatcher->triggerEvent($event, true); 198 222 } 199 223 200 224 /** 201 225 * checks whether the request has been cancelled or not … … 207 231 return $this->isCancelled; 208 232 } 209 233 210 234 /** 211 235 * checks whether raw data is valid or not … … 218 242 return $validator->validate($this->getRawData()); 219 243 } 220 244 221 245 /** 222 246 * returns the validated raw data … … 236 260 return null; 237 261 } 238 262 239 263 /** 240 264 * returns the raw data filtered … … 257 281 return $filter->execute($rawData); 258 282 } 259 283 260 284 /** 261 285 * returns the raw data … … 264 288 */ 265 289 protected abstract function getRawData(); 266 290 267 291 /** 268 292 * checks whether a request value is valid or not … … 282 306 return false; 283 307 } 284 308 285 309 /** 286 310 * returns the validated request value … … 326 350 $value = $filter->execute($data[$valueName]); 327 351 } catch (stubFilterException $fe) { 328 $error =& $this->getErrors($source); 329 if (isset($error[$valueName]) == false) { 330 $error[$valueName] = array($fe->getError()); 331 } else { 332 $ids = array(); 333 foreach ($error[$valueName] as $errorValue) { 334 $ids[] = $errorValue->getId(); 335 } 336 337 if (in_array($fe->getError()->getId(), $ids) == false) { 338 $error[$valueName][] = $fe->getError(); 339 } 340 } 341 352 $this->addValueError($fe->getError(), $valueName, $source); 342 353 return null; 343 354 } … … 345 356 return $value; 346 357 } 347 358 348 359 /** 349 360 * return an array of all keys registered in this request … … 355 366 return array_keys($this->getValues($source)); 356 367 } 357 368 358 369 /** 359 370 * returns the array with data from requested source trunk/src/main/php/net/stubbles/ipo/request/stubRequest.php
r425 r753 9 9 stubClassLoader::load('net.stubbles.events.stubEventDispatcher', 10 10 'net.stubbles.util.validators.stubValidator', 11 'net.stubbles.ipo.request.filters.stubFilter' 11 'net.stubbles.ipo.request.filters.stubFilter', 12 'net.stubbles.ipo.request.stubRequestValueError' 12 13 ); 13 14 /** … … 40 41 */ 41 42 const SOURCE_PARAM = 4; 42 43 43 44 /** 44 45 * checks whether a request value is set or not … … 49 50 */ 50 51 public function hasValue($valueName, $source = self::SOURCE_PARAM); 51 52 53 /** 54 * add a value error for a request value 55 * 56 * @param stubRequestValueError $valueError 57 * @param string $valueName 58 * @param int $source 59 */ 60 public function addValueError(stubRequestValueError $valueError, $valueName, $source = self::SOURCE_PARAM); 61 52 62 /** 53 63 * checks whether a request value has an error after a filter was applied … … 58 68 */ 59 69 public function hasValueError($valueName, $source = self::SOURCE_PARAM); 60 70 61 71 /** 62 72 * returns a list of errors for given request value … … 67 77 */ 68 78 public function getValueError($valueName, $source = self::SOURCE_PARAM); 69 79 70 80 /** 71 81 * checks whether there are any value errors … … 75 85 */ 76 86 public function hasValueErrors($source = self::SOURCE_PARAM); 77 87 78 88 /** 79 89 * returns a list of all request value names with their errors … … 83 93 */ 84 94 public function getValueErrors($source = self::SOURCE_PARAM); 85 95 86 96 /** 87 97 * cancels the request, e.g. if it was detected that it is invalid … … 92 102 */ 93 103 public function cancel(stubEventDispatcher $dispatcher = null); 94 104 95 105 /** 96 106 * checks whether the request has been cancelled or not … … 99 109 */ 100 110 public function isCancelled(); 101 111 102 112 /** 103 113 * returns the request method … … 106 116 */ 107 117 public function getMethod(); 108 118 109 119 /** 110 120 * returns the uri of the request … … 113 123 */ 114 124 public function getURI(); 115 125 116 126 /** 117 127 * checks whether raw data is valid or not … … 121 131 */ 122 132 public function validateRawData(stubValidator $validator); 123 133 124 134 /** 125 135 * returns the validated raw data … … 131 141 */ 132 142 public function getValidatedRawData(stubValidator $validator); 133 143 134 144 /** 135 145 * returns the raw data filtered … … 140 150 */ 141 151 public function getFilteredRawData(stubFilter $filter); 142 152 143 153 /** 144 154 * checks whether a request value is valid or nor … … 150 160 */ 151 161 public function validateValue(stubValidator $validator, $valueName, $source = self::SOURCE_PARAM); 152 162 153 163 /** 154 164 * returns the validated request value … … 173 183 */ 174 184 public function getFilteredValue(stubFilter $filter, $valueName, $source = self::SOURCE_PARAM); 175 185 176 186 /** 177 187 * return an array of all keys registered in this request trunk/src/main/php/net/stubbles/ipo/request/stubRequestPrefixDecorator.php
r425 r753 44 44 */ 45 45 protected $sources; 46 46 47 47 /** 48 48 * constructor … … 58 58 $this->sources = $sources; 59 59 } 60 60 61 61 /** 62 62 * sets the prefix to another value … … 68 68 $this->prefix = $prefix; 69 69 } 70 70 71 71 /** 72 72 * checks whether a request value is set or not … … 84 84 return $this->request->hasValue($valueName, $source); 85 85 } 86 86 87 /** 88 * add a value error for a request value 89 * 90 * @param stubRequestValueError $valueError 91 * @param string $valueName 92 * @param int $source 93 */ 94 public function addValueError(stubRequestValueError $valueError, $valueName, $source = self::SOURCE_PARAM) 95 { 96 if ($this->applyPrefix($source) == true) { 97 $valueName = $this->prefix . '_' . $valueName; 98 } 99 100 $this->request->addValueError($valueError, $valueName, $source); 101 } 102 87 103 /** 88 104 * checks whether a request value has an error after a filter was applied … … 163 179 $this->request->cancel($dispatcher); 164 180 } 165 181 166 182 /** 167 183 * checks whether the request has been cancelled or not … … 173 189 return $this->request->isCancelled(); 174 190 } 175 191 176 192 /** 177 193 * returns the request method … … 183 199 return $this->request->getMethod(); 184 200 } 185 201 186 202 /** 187 203 * returns the uri of the request … … 193 209 return $this->request->getURI(); 194 210 } 195 211 196 212 /** 197 213 * checks whether raw data is valid or not … … 204 220 return $this->request->validateRawData($validator); 205 221 } 206 222 207 223 /** 208 224 * returns the validated raw data … … 217 233 return $this->request->getValidatedRawData($validator); 218 234 } 219 235 220 236 /** 221 237 * returns the raw data filtered … … 229 245 return $this->request->getFilteredRawData($filter); 230 246 } 231 247 232 248 /** 233 249 * checks whether a request value is valid or nor … … 283 299 return $this->request->getFilteredValue($filter, $valueName, $source); 284 300 } 285 301 286 302 /** 287 303 * return an array of all keys registered in this request … … 306 322 return $returnedValueKeys; 307 323 } 308 324 309 325 /** 310 326 * check whether the prefix has to be applied for requested source trunk/src/test/php/net/stubbles/ipo/request/stubAbstractRequestTestCase.php
r425 r753 1 1 <?php 2 2 /** 3 * Tests for ipo.request.stubAbstractRequest3 * Tests for net.stubbles.ipo.request.stubAbstractRequest 4 4 * 5 5 * @author Frank Kleine <mikey@stubbles.net> 6 6 * @package stubbles 7 * @subpackage ipo_ test7 * @subpackage ipo_request_test 8 8 */ 9 9 stubClassLoader::load('net.stubbles.ipo.request.stubAbstractRequest', … … 50 50 return $this->_rawData; 51 51 } 52 53 public function setValueError($valueName, stubRequestValueError $error)54 {55 $this->paramErrors[$valueName] = array($error);56 }57 58 public function setHeaderError($headerName, stubRequestValueError $error)59 {60 $this->headerErrors[$headerName] = array($error);61 }62 63 public function setCookieError($cookieName, stubRequestValueError $error)64 {65 $this->cookieErrors[$cookieName] = array($error);66 }67 52 } 68 53 /** 69 * Tests for ipo.request.stubAbstractRequest54 * Tests for net.stubbles.ipo.request.stubAbstractRequest 70 55 * 71 56 * @package stubbles 72 * @subpackage ipo_ test57 * @subpackage ipo_request_test 73 58 */ 74 59 class stubAbstractRequestTestCase extends UnitTestCase … … 80 65 */ 81 66 protected $request; 82 67 83 68 /** 84 69 * set up test environment … … 164 149 $this->assertEqual($this->request->getFilteredValue($mockFilter, 'foo', 'dummy'), 'bam'); 165 150 } 166 151 167 152 /** 168 153 * test that headers are handles as expected … … 198 183 $this->assertEqual($this->request->getFilteredValue($mockFilter, 'bar', stubRequest::SOURCE_HEADER), 'bam'); 199 184 } 200 185 201 186 /** 202 187 * test that headers are handles as expected … … 232 217 $this->assertEqual($this->request->getFilteredValue($mockFilter, 'baz', stubRequest::SOURCE_COOKIE), 'bam'); 233 218 } 234 219 235 220 /** 236 221 * assure that cancelling the request works as expected … … 248 233 stubEventDispatcher::destroyInstance('__test'); 249 234 } 250 235 251 236 /** 252 237 * assure that value error handling works correct … … 268 253 269 254 $errorValue = new stubRequestValueError('bar', array()); 270 $this->request-> setValueError('foo', $errorValue);255 $this->request->addValueError($errorValue, 'foo'); 271 256 $this->assertTrue($this->request->hasValueError('foo')); 272 257 $this->assertEqual($this->request->getValueError('foo'), array($errorValue)); … … 284 269 $this->assertEqual($this->request->getValueErrors('foo', 'dummy'), array('foo' => array($errorValue))); 285 270 } 286 271 287 272 /** 288 273 * assure that value error handling works correct … … 296 281 297 282 $errorValue = new stubRequestValueError('bar', array()); 298 $this->request-> setHeaderError('foo', $errorValue);283 $this->request->addValueError($errorValue, 'foo', stubRequest::SOURCE_HEADER); 299 284 $this->assertTrue($this->request->hasValueError('foo', stubRequest::SOURCE_HEADER)); 300 285 $this->assertEqual($this->request->getValueError('foo', stubRequest::SOURCE_HEADER), array($errorValue)); … … 302 287 $this->assertEqual($this->request->getValueErrors(stubRequest::SOURCE_HEADER), array('foo' => array($errorValue))); 303 288 } 304 289 305 290 /** 306 291 * assure that value error handling works correct … … 314 299 315 300 $errorValue = new stubRequestValueError('bar', array()); 316 $this->request-> setCookieError('foo', $errorValue);301 $this->request->addValueError($errorValue, 'foo', stubRequest::SOURCE_COOKIE); 317 302 $this->assertTrue($this->request->hasValueError('foo', stubRequest::SOURCE_COOKIE)); 318 303 $this->assertEqual($this->request->getValueError('foo', stubRequest::SOURCE_COOKIE), array($errorValue)); … … 320 305 $this->assertEqual($this->request->getValueErrors(stubRequest::SOURCE_COOKIE), array('foo' => array($errorValue))); 321 306 } 322 307 323 308 /** 324 309 * assure that the same error occurs only once in list of errors for a value … … 327 312 { 328 313 $errorValue = new stubRequestValueError('foo', array()); 329 $this->request-> setValueError('foo', $errorValue);314 $this->request->addValueError($errorValue, 'foo'); 330 315 $this->assertEqual($this->request->getValueError('foo'), array($errorValue)); 331 316 $this->request->getFilteredValue(new stubTestExceptionFilter(), 'foo'); 332 317 $this->assertEqual($this->request->getValueError('foo'), array($errorValue)); 333 318 } 334 319 335 320 /** 336 321 * assure that value keys are delivered correct … … 343 328 $this->assertEqual($this->request->getValueKeys(stubRequest::SOURCE_COOKIE), array('baz')); 344 329 } 345 330 346 331 /** 347 332 * assure that raw data is handles correct … … 355 340 $this->assertFalse($this->request->validateRawData($mockValidator)); 356 341 } 357 342 358 343 /** 359 344 * assure that raw data is handles correct … … 367 352 $this->assertNull($this->request->getValidatedRawData($mockValidator)); 368 353 } 369 354 370 355 /** 371 356 * assure that raw data is handles correct trunk/src/test/php/net/stubbles/ipo/request/stubRequestPrefixDecoratorTestCase.php
r425 r753 31 31 */ 32 32 protected $mockRequest; 33 33 34 34 /** 35 35 * set up test environment … … 40 40 $this->request = new stubRequestPrefixDecorator($this->mockRequest, 'test'); 41 41 } 42 42 43 43 /** 44 44 * test that values are prefixed as expected … … 60 60 $this->request->getFilteredValue($mockFilter, 'foo'); 61 61 } 62 62 63 63 /** 64 64 * test that values are prefixed as expected … … 81 81 $this->request->getFilteredValue($mockFilter, 'foo'); 82 82 } 83 83 84 84 /** 85 85 * test that headers are handles as expected … … 101 101 $this->request->getFilteredValue($mockFilter, 'foo', stubRequest::SOURCE_HEADER); 102 102 } 103 103 104 104 /** 105 105 * test that headers are handles as expected … … 121 121 $this->request->getFilteredValue($mockFilter, 'foo', stubRequest::SOURCE_COOKIE); 122 122 } 123 123 124 124 /** 125 125 * assure that value error handling works correct … … 127 127 public function testValueError() 128 128 { 129 $errorValue = new stubRequestValueError('bar', array()); 130 $this->mockRequest->expect('addValueError', array($errorValue, 'test_foo', stubRequest::SOURCE_PARAM)); 131 $this->request->addValueError($errorValue, 'foo'); 132 129 133 $this->mockRequest->expect('hasValueError', array('test_foo', stubRequest::SOURCE_PARAM)); 130 134 $this->request->hasValueError('foo'); … … 139 143 $this->assertEqual($this->request->getValueErrors(), array('foo' => array())); 140 144 } 141 145 142 146 /** 143 147 * assure that value error handling works correct … … 145 149 public function testHeaderError() 146 150 { 147 $this->mockRequest->expect('getValueError', array('foo', stubRequest::SOURCE_HEADER)); 151 $errorValue = new stubRequestValueError('bar', array()); 152 $this->mockRequest->expect('addValueError', array($errorValue, 'foo', stubRequest::SOURCE_HEADER)); 153 $this->request->addValueError($errorValue, 'foo', stubRequest::SOURCE_HEADER); 154 155 $this->mockRequest->expect('hasValueError', array('foo', stubRequest::SOURCE_HEADER)); 148 156 $this->request->hasValueError('foo', stubRequest::SOURCE_HEADER); 149 157 … … 158 166 $this->assertEqual($this->request->getValueErrors(stubRequest::SOURCE_HEADER), array('test_foo' => array(), 'bar' => array())); 159 167 } 160 168 161 169 /** 162 170 * assure that value error handling works correct … … 164 172 public function testCookieError() 165 173 { 166 $this->mockRequest->expect('getValueError', array('foo', stubRequest::SOURCE_COOKIE)); 174 $errorValue = new stubRequestValueError('bar', array()); 175 $this->mockRequest->expect('addValueError', array($errorValue, 'foo', stubRequest::SOURCE_COOKIE)); 176 $this->request->addValueError($errorValue, 'foo', stubRequest::SOURCE_COOKIE); 177 178 $this->mockRequest->expect('hasValueError', array('foo', stubRequest::SOURCE_COOKIE)); 167 179 $this->request->hasValueError('foo', stubRequest::SOURCE_COOKIE); 168 180 … … 177 189 $this->assertEqual($this->request->getValueErrors(stubRequest::SOURCE_COOKIE), array('test_foo' => array(), 'bar' => array())); 178 190 } 179 191 180 192 /** 181 193 * assure that the cancel methods are called … … 189 201 $this->assertTrue($this->request->isCancelled()); 190 202 } 191 203 192 204 /** 193 205 * assure that getMethod() is called … … 199 211 $this->assertEqual($this->request->getMethod(), 'test'); 200 212 } 201 213 202 214 /** 203 215 * assure that getURI() is called … … 209 221 $this->assertEqual($this->request->getURI(), 'test'); 210 222 } 211 223 212 224 /** 213 225 * assure that raw data is handles correct … … 220 232 $this->assertFalse($this->request->validateRawData($mockValidator)); 221 233 } 222 234 223 235 /** 224 236 * assure that raw data is handles correct … … 231 243 $this->assertEqual($this->request->getValidatedRawData($mockValidator), 'foo'); 232 244 } 233 245 234 246 /** 235 247 * assure that raw data is handles correct … … 242 254 $this->assertEqual($this->request->getFilteredRawData($mockFilter), 'foo'); 243 255 } 244 256 245 257 /** 246 258 * assure that value keys are delivered correct
