Changeset 1376
- Timestamp:
- 02/26/08 17:06:40 (9 months ago)
- Files:
-
- trunk/src/main/php/net/stubbles/ipo/request/filters/stubFilterFactory.php (modified) (4 diffs)
- trunk/src/main/php/net/stubbles/ipo/request/filters/stubLengthFilterDecorator.php (modified) (6 diffs)
- trunk/src/main/php/net/stubbles/ipo/request/filters/stubRangeFilterDecorator.php (modified) (6 diffs)
- trunk/src/test/php/net/stubbles/ipo/request/filters/stubFilterFactoryTestCase.php (modified) (2 diffs)
- trunk/src/test/php/net/stubbles/ipo/request/filters/stubLengthFilterDecoratorTestCase.php (modified) (4 diffs)
- trunk/src/test/php/net/stubbles/ipo/request/filters/stubRangeFilterDecoratorTestCase.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/main/php/net/stubbles/ipo/request/filters/stubFilterFactory.php
r1369 r1376 159 159 * @param numeric $min 160 160 * @param numeric $max 161 * @param int $strategy optional strategy to be used: before or after decorated filter 162 * @return stubFilterFactory 163 */ 164 public function inRange($min, $max, $strategy = stubStrategyFilterDecorator::STRATEGY_AFTER) 161 * @param string $minErrorId optional error id for failing min validation 162 * @param string $maxErrorId optional error id for failing max validation 163 * @param int $strategy optional strategy to be used: before or after decorated filter 164 * @return stubFilterFactory 165 */ 166 public function inRange($min, $max, $minErrorId = null, $maxErrorId = null, $strategy = stubStrategyFilterDecorator::STRATEGY_AFTER) 165 167 { 166 168 if (null !== $min || null !== $max) { … … 171 173 $filter = new stubRangeFilterDecorator($this->getDecoratedFilter(), $this->getRVEFactory()); 172 174 if (null !== $min) { 173 $filter->setMinValidator(new stubMinNumberValidator($min) );175 $filter->setMinValidator(new stubMinNumberValidator($min), $minErrorId); 174 176 } 175 177 176 178 if (null !== $max) { 177 $filter->setMaxValidator(new stubMaxNumberValidator($max) );179 $filter->setMaxValidator(new stubMaxNumberValidator($max), $maxErrorId); 178 180 } 179 181 … … 193 195 * @param numeric $minLength 194 196 * @param numeric $maxLength 195 * @param int $strategy optional strategy to be used: before or after decorated filter 196 * @return stubFilterFactory 197 */ 198 public function length($minLength, $maxLength, $strategy = stubStrategyFilterDecorator::STRATEGY_AFTER) 197 * @param string $minLengthErrorId optional error id for failing min validation 198 * @param string $maxLengthErrorId optional error id for failing max validation 199 * @param int $strategy optional strategy to be used: before or after decorated filter 200 * @return stubFilterFactory 201 */ 202 public function length($minLength, $maxLength, $minLengthErrorId = null, $maxLengthErrorId = null, $strategy = stubStrategyFilterDecorator::STRATEGY_AFTER) 199 203 { 200 204 if (null !== $minLength || null !== $maxLength) { … … 205 209 $filter = new stubLengthFilterDecorator($this->getDecoratedFilter(), $this->getRVEFactory()); 206 210 if (null !== $minLength) { 207 $filter->setMinLengthValidator(new stubMinLengthValidator($minLength) );211 $filter->setMinLengthValidator(new stubMinLengthValidator($minLength), $minLengthErrorId); 208 212 } 209 213 210 214 if (null !== $maxLength) { 211 $filter->setMaxLengthValidator(new stubMaxLengthValidator($maxLength) );215 $filter->setMaxLengthValidator(new stubMaxLengthValidator($maxLength), $maxLengthErrorId); 212 216 } 213 217 trunk/src/main/php/net/stubbles/ipo/request/filters/stubLengthFilterDecorator.php
r1323 r1376 34 34 * @var stubValidator 35 35 */ 36 protected $minLength = null; 36 protected $minLength = null; 37 /** 38 * the error id to use in case min length validation fails 39 * 40 * @var string 41 */ 42 protected $minLengthErrorId = 'STRING_TOO_SHORT'; 37 43 /** 38 44 * validator for maximum length of string … … 41 47 */ 42 48 protected $maxLength = null; 49 /** 50 * the error id to use in case max length validation fails 51 * 52 * @var string 53 */ 54 protected $maxLengthErrorId = 'STRING_TOO_LONG'; 43 55 44 56 /** … … 58 70 * 59 71 * @param stubValidator $minLength 72 * @param string $minLengthErrorId optional error id to use in case validation fails 60 73 */ 61 public function setMinLengthValidator(stubValidator $minLength )74 public function setMinLengthValidator(stubValidator $minLength, $minLengthErrorId = null) 62 75 { 63 $this->minLength = $minLength; 76 $this->minLength = $minLength; 77 if (null !== $minLengthErrorId) { 78 $this->minLengthErrorId = $minLengthErrorId; 79 } 64 80 } 65 81 … … 75 91 76 92 /** 93 * returns the error id to use in case validation fails 94 * 95 * @return string 96 */ 97 public function getMinLengthErrorId() 98 { 99 return $this->minLengthErrorId; 100 } 101 102 /** 77 103 * set a max length validator 78 104 * 79 105 * @param stubValidator $maxLength 106 * @param string $maxLengthErrorId optional error id to use in case validation fails 80 107 */ 81 public function setMaxLengthValidator(stubValidator $maxLength )108 public function setMaxLengthValidator(stubValidator $maxLength, $maxLengthErrorId = null) 82 109 { 83 110 $this->maxLength = $maxLength; 111 if (null !== $maxLengthErrorId) { 112 $this->maxLengthErrorId = $maxLengthErrorId; 113 } 84 114 } 85 115 … … 95 125 96 126 /** 127 * returns the error id to use in case validation fails 128 * 129 * @return string 130 */ 131 public function getMaxLengthErrorId() 132 { 133 return $this->maxLengthErrorId; 134 } 135 136 /** 97 137 * execute the filter 98 138 * … … 105 145 if (null != $this->minLength && $this->minLength->validate($value) === false) { 106 146 // input is shorter than maximal allowed length 107 throw new stubFilterException($this->rveFactory->create( 'STRING_TOO_SHORT')->setValues($this->minLength->getCriteria()));147 throw new stubFilterException($this->rveFactory->create($this->minLengthErrorId)->setValues($this->minLength->getCriteria())); 108 148 } elseif (null != $this->maxLength && $this->maxLength->validate($value) === false) { 109 149 // input is longer than maximal allowed length 110 throw new stubFilterException($this->rveFactory->create( 'STRING_TOO_LONG')->setValues($this->maxLength->getCriteria()));150 throw new stubFilterException($this->rveFactory->create($this->maxLengthErrorId)->setValues($this->maxLength->getCriteria())); 111 151 } 112 152 trunk/src/main/php/net/stubbles/ipo/request/filters/stubRangeFilterDecorator.php
r1369 r1376 35 35 protected $minValidator = null; 36 36 /** 37 * the error id to use in case min validation fails 38 * 39 * @var string 40 */ 41 protected $minErrorId = 'VALUE_TOO_SMALL'; 42 /** 37 43 * validator for maximum values 38 44 * … … 40 46 */ 41 47 protected $maxValidator = null; 48 /** 49 * the error id to use in case max validation fails 50 * 51 * @var string 52 */ 53 protected $maxErrorId = 'VALUE_TOO_GREAT'; 42 54 43 55 /** … … 57 69 * 58 70 * @param stubValidator $minValidator 71 * @param string $minErrorId optional error id to use in case validation fails 59 72 */ 60 public function setMinValidator(stubValidator $minValidator )73 public function setMinValidator(stubValidator $minValidator, $minErrorId = null) 61 74 { 62 75 $this->minValidator = $minValidator; 76 if (null !== $minErrorId) { 77 $this->minErrorId = $minErrorId; 78 } 63 79 } 64 80 … … 74 90 75 91 /** 92 * returns the error id to use in case validation fails 93 * 94 * @return string 95 */ 96 public function getMinErrorId() 97 { 98 return $this->minErrorId; 99 } 100 101 /** 76 102 * sets the validator for maximum values 77 103 * 78 104 * @param stubValidator $maxValidator 105 * @param string $maxErrorId optional error id to use in case validation fails 79 106 */ 80 public function setMaxValidator(stubValidator $maxValidator )107 public function setMaxValidator(stubValidator $maxValidator, $maxErrorId = null) 81 108 { 82 109 $this->maxValidator = $maxValidator; 110 if (null !== $maxErrorId) { 111 $this->maxErrorId = $maxErrorId; 112 } 83 113 } 84 114 … … 94 124 95 125 /** 126 * returns the error id to use in case validation fails 127 * 128 * @return string 129 */ 130 public function getMaxErrorId() 131 { 132 return $this->maxErrorId; 133 } 134 135 /** 96 136 * checks if given value exceeds borders 97 137 * … … 104 144 if (null !== $value && null !== $this->minValidator && $this->minValidator->validate($value) !== true) { 105 145 // add error message if input is smaller than minimum value 106 throw new stubFilterException($this->rveFactory->create( 'VALUE_TOO_SMALL')->setValues($this->minValidator->getCriteria()));146 throw new stubFilterException($this->rveFactory->create($this->minErrorId)->setValues($this->minValidator->getCriteria())); 107 147 } elseif (null !== $value && null !== $this->maxValidator && $this->maxValidator->validate($value) !== true) { 108 148 // add error message if input is greater than maximum value 109 throw new stubFilterException($this->rveFactory->create( 'VALUE_TOO_GREAT')->setValues($this->maxValidator->getCriteria()));149 throw new stubFilterException($this->rveFactory->create($this->maxErrorId)->setValues($this->maxValidator->getCriteria())); 110 150 } 111 151 trunk/src/test/php/net/stubbles/ipo/request/filters/stubFilterFactoryTestCase.php
r1346 r1376 51 51 $this->assertType('stubRangeFilterDecorator', $rangeFilterDecorator); 52 52 $this->assertEquals(1, $rangeFilterDecorator->getMinValidator()->getValue()); 53 $this->assertEquals('VALUE_TOO_SMALL', $rangeFilterDecorator->getMinErrorId()); 53 54 $this->assertEquals(4, $rangeFilterDecorator->getMaxValidator()->getValue()); 55 $this->assertEquals('VALUE_TOO_GREAT', $rangeFilterDecorator->getMaxErrorId()); 56 $this->assertType('stubIntegerFilter', $rangeFilterDecorator->getDecoratedFilter()); 57 } 58 59 /** 60 * test that range filter is created decorating an integer filter 61 * 62 * @test 63 */ 64 public function rangeFilterDifferentErrorIds() 65 { 66 $filter = stubFilterFactory::forType('int')->inRange(1, 4, 'differentMin', 'differentMax'); 67 $this->assertType('stubFilterFactory', $filter); 68 $rangeFilterDecorator = $filter->getDecoratedFilter(); 69 $this->assertType('stubRangeFilterDecorator', $rangeFilterDecorator); 70 $this->assertEquals(1, $rangeFilterDecorator->getMinValidator()->getValue()); 71 $this->assertEquals('differentMin', $rangeFilterDecorator->getMinErrorId()); 72 $this->assertEquals(4, $rangeFilterDecorator->getMaxValidator()->getValue()); 73 $this->assertEquals('differentMax', $rangeFilterDecorator->getMaxErrorId()); 54 74 $this->assertType('stubIntegerFilter', $rangeFilterDecorator->getDecoratedFilter()); 55 75 } … … 150 170 $this->assertType('stubLengthFilterDecorator', $lengthFilterDecorator); 151 171 $this->assertEquals(2, $lengthFilterDecorator->getMinLengthValidator()->getValue()); 172 $this->assertEquals('STRING_TOO_SHORT', $lengthFilterDecorator->getMinLengthErrorId()); 152 173 $this->assertEquals(5, $lengthFilterDecorator->getMaxLengthValidator()->getValue()); 174 $this->assertEquals('STRING_TOO_LONG', $lengthFilterDecorator->getMaxLengthErrorId()); 175 $this->assertType('stubTextFilter', $lengthFilterDecorator->getDecoratedFilter()); 176 } 177 178 /** 179 * test that length filter is created decorating a text filter 180 * 181 * @test 182 */ 183 public function lengthFilterWithDifferentErrorIds() 184 { 185 $filter = stubFilterFactory::forType('text')->length(2, 5, 'differentMin', 'differentMax'); 186 $this->assertType('stubFilterFactory', $filter); 187 $lengthFilterDecorator = $filter->getDecoratedFilter(); 188 $this->assertType('stubLengthFilterDecorator', $lengthFilterDecorator); 189 $this->assertEquals(2, $lengthFilterDecorator->getMinLengthValidator()->getValue()); 190 $this->assertEquals('differentMin', $lengthFilterDecorator->getMinLengthErrorId()); 191 $this->assertEquals(5, $lengthFilterDecorator->getMaxLengthValidator()->getValue()); 192 $this->assertEquals('differentMax', $lengthFilterDecorator->getMaxLengthErrorId()); 153 193 $this->assertType('stubTextFilter', $lengthFilterDecorator->getDecoratedFilter()); 154 194 } trunk/src/test/php/net/stubbles/ipo/request/filters/stubLengthFilterDecoratorTestCase.php
r1323 r1376 106 106 ->will($this->returnValue(array())); 107 107 $this->lengthFilterDecorator->setMinLengthValidator($mockMinLengthValidaror); 108 $this->assertEquals('STRING_TOO_SHORT', $this->lengthFilterDecorator->getMinLengthErrorId()); 108 109 $this->assertSame($mockMinLengthValidaror, $this->lengthFilterDecorator->getMinLengthValidator()); 109 110 $this->assertNull($this->lengthFilterDecorator->getMaxLengthValidator()); … … 111 112 ->method('create') 112 113 ->with($this->equalTo('STRING_TOO_SHORT')) 114 ->will($this->returnValue(new stubRequestValueError('foo', array('en_EN' => 'Something wrent wrong.')))); 115 $this->lengthFilterDecorator->callDoExecute("minLengthTest"); 116 } 117 118 /** 119 * assure that filtering a string with minimum length is ok and a string 120 * with length shorter than minimum length throws an FilterException 121 * 122 * @test 123 * @expectedException stubFilterException 124 */ 125 public function minLengthFailsDifferentErrorId() 126 { 127 $mockMinLengthValidaror = $this->getMock('stubValidator'); 128 $mockMinLengthValidaror->expects($this->once()) 129 ->method('validate') 130 ->with($this->equalTo('minLengthTest')) 131 ->will($this->returnValue(false)); 132 $mockMinLengthValidaror->expects($this->once()) 133 ->method('getCriteria') 134 ->will($this->returnValue(array())); 135 $this->lengthFilterDecorator->setMinLengthValidator($mockMinLengthValidaror, 'differentErrorId'); 136 $this->assertEquals('differentErrorId', $this->lengthFilterDecorator->getMinLengthErrorId()); 137 $this->assertSame($mockMinLengthValidaror, $this->lengthFilterDecorator->getMinLengthValidator()); 138 $this->assertNull($this->lengthFilterDecorator->getMaxLengthValidator()); 139 $this->mockRequestValueErrorFactory->expects($this->once()) 140 ->method('create') 141 ->with($this->equalTo('differentErrorId')) 113 142 ->will($this->returnValue(new stubRequestValueError('foo', array('en_EN' => 'Something wrent wrong.')))); 114 143 $this->lengthFilterDecorator->callDoExecute("minLengthTest"); … … 152 181 ->will($this->returnValue(array())); 153 182 $this->lengthFilterDecorator->setMaxLengthValidator($mockMaxLengthValidaror); 183 $this->assertEquals('STRING_TOO_LONG', $this->lengthFilterDecorator->getMaxLengthErrorId()); 154 184 $this->assertNull($this->lengthFilterDecorator->getMinLengthValidator()); 155 185 $this->assertSame($mockMaxLengthValidaror, $this->lengthFilterDecorator->getMaxLengthValidator()); … … 157 187 ->method('create') 158 188 ->with($this->equalTo('STRING_TOO_LONG')) 189 ->will($this->returnValue(new stubRequestValueError('foo', array('en_EN' => 'Something wrent wrong.')))); 190 $this->lengthFilterDecorator->callDoExecute("maxLengthTest"); 191 } 192 193 /** 194 * assure that filtering a string with maximum length is ok and a string 195 * with length greater than maximum length throws an FilterException 196 * 197 * @test 198 * @expectedException stubFilterException 199 */ 200 public function maxLengthFailsDifferentErrorId() 201 { 202 $mockMaxLengthValidaror = $this->getMock('stubValidator'); 203 $mockMaxLengthValidaror->expects($this->once()) 204 ->method('validate') 205 ->with($this->equalTo('maxLengthTest')) 206 ->will($this->returnValue(false)); 207 $mockMaxLengthValidaror->expects($this->once()) 208 ->method('getCriteria') 209 ->will($this->returnValue(array())); 210 $this->lengthFilterDecorator->setMaxLengthValidator($mockMaxLengthValidaror, 'differentErrorId'); 211 $this->assertEquals('differentErrorId', $this->lengthFilterDecorator->getMaxLengthErrorId()); 212 $this->assertNull($this->lengthFilterDecorator->getMinLengthValidator()); 213 $this->assertSame($mockMaxLengthValidaror, $this->lengthFilterDecorator->getMaxLengthValidator()); 214 $this->mockRequestValueErrorFactory->expects($this->once()) 215 ->method('create') 216 ->with($this->equalTo('differentErrorId')) 159 217 ->will($this->returnValue(new stubRequestValueError('foo', array('en_EN' => 'Something wrent wrong.')))); 160 218 $this->lengthFilterDecorator->callDoExecute("maxLengthTest"); trunk/src/test/php/net/stubbles/ipo/request/filters/stubRangeFilterDecoratorTestCase.php
r1329 r1376 117 117 ->will($this->returnValue(array())); 118 118 $this->rangeFilterDecorator->setMinValidator($this->mockValidatorMin); 119 $this->assertEquals('VALUE_TOO_SMALL', $this->rangeFilterDecorator->getMinErrorId()); 119 120 $this->mockRequestValueErrorFactory->expects($this->once()) 120 121 ->method('create') 121 122 ->with($this->equalTo('VALUE_TOO_SMALL')) 123 ->will($this->returnValue(new stubRequestValueError('foo', array('en_EN' => 'Something wrent wrong.')))); 124 $this->rangeFilterDecorator->callDoExecute(-11); 125 } 126 127 /** 128 * assure that an FilterException is thrown when value smaller then $min 129 * 130 * @test 131 * @expectedException stubFilterException 132 */ 133 public function withMinValidatorFailsDifferentErrorId() 134 { 135 $this->mockValidatorMin->expects($this->once()) 136 ->method('validate') 137 ->will($this->returnValue(false)); 138 $this->mockValidatorMin->expects($this->once()) 139 ->method('getCriteria') 140 ->will($this->returnValue(array())); 141 $this->rangeFilterDecorator->setMinValidator($this->mockValidatorMin, 'differentErrorId'); 142 $this->assertEquals('differentErrorId', $this->rangeFilterDecorator->getMinErrorId()); 143 $this->mockRequestValueErrorFactory->expects($this->once()) 144 ->method('create') 145 ->with($this->equalTo('differentErrorId')) 122 146 ->will($this->returnValue(new stubRequestValueError('foo', array('en_EN' => 'Something wrent wrong.')))); 123 147 $this->rangeFilterDecorator->callDoExecute(-11); … … 156 180 ->will($this->returnValue(array())); 157 181 $this->rangeFilterDecorator->setMaxValidator($this->mockValidatorMax); 182 $this->assertEquals('VALUE_TOO_GREAT', $this->rangeFilterDecorator->getMaxErrorId()); 158 183 $this->mockRequestValueErrorFactory->expects($this->once()) 159 184 ->method('create') 160 185 ->with($this->equalTo('VALUE_TOO_GREAT')) 186 ->will($this->returnValue(new stubRequestValueError('foo', array('en_EN' => 'Something wrent wrong.')))); 187 $this->rangeFilterDecorator->callDoExecute(11); 188 } 189 190 /** 191 * assure that an FilterException is thrown when value greater then $max 192 * 193 * @test 194 * @expectedException stubFilterException 195 */ 196 public function withMaxValidatorFailsDifferentErrorId() 197 { 198 $this->mockValidatorMax->expects($this->once()) 199 ->method('validate') 200 ->will($this->returnValue(false)); 201 $this->mockValidatorMax->expects($this->once()) 202 ->method('getCriteria') 203 ->will($this->returnValue(array())); 204 $this->rangeFilterDecorator->setMaxValidator($this->mockValidatorMax, 'differentErrorId'); 205 $this->assertEquals('differentErrorId', $this->rangeFilterDecorator->getMaxErrorId()); 206 $this->mockRequestValueErrorFactory->expects($this->once()) 207 ->method('create') 208 ->with($this->equalTo('differentErrorId')) 161 209 ->will($this->returnValue(new stubRequestValueError('foo', array('en_EN' => 'Something wrent wrong.')))); 162 210 $this->rangeFilterDecorator->callDoExecute(11);
