Changeset 1031
- Timestamp:
- 11/12/07 15:37:58 (8 months ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/main/php/net/stubbles/ipo/request/filters/stubNumberFilter.php
r731 r1031 24 24 { 25 25 /** 26 * default error id if field was empty 27 */ 28 const FIELD_EMPTY = 'FIELD_EMPTY'; 29 /** 30 * default error id if value is too small 31 */ 32 const VALUE_TOO_SMALL = 'VALUE_TOO_SMALL'; 33 /** 34 * default error id if value is too great 35 */ 36 const VALUE_TOO_GREAT = 'VALUE_TOO_GREAT'; 37 /** 26 38 * validator for minimum values 27 39 * … … 35 47 */ 36 48 protected $maxValidator = null; 49 /** 50 * list of error ids to be used by this filter 51 * 52 * @var array<string,string> 53 */ 54 protected $errorIds = array(self::FIELD_EMPTY => self::FIELD_EMPTY, 55 self::VALUE_TOO_SMALL => self::VALUE_TOO_SMALL, 56 self::VALUE_TOO_GREAT => self::VALUE_TOO_GREAT 57 ); 37 58 38 59 /** … … 71 92 72 93 /** 94 * sets the error id to be used in case the filter finds an error in the value 95 * 96 * @param string $type type of error, one of the stubHTTPURLFilter::URL_* constants 97 * @param string $errorId the id of the error object to use 98 */ 99 public function setErrorId($type, $errorId) 100 { 101 if (isset($this->errorIds[$type]) === false) { 102 throw new stubIllegalArgumentException('Illegal type ' . $type . ' given. Allowed types are ' . join(', ', array_keys($this->errorIds)) . '.'); 103 } 104 105 $this->errorIds[$type] = $errorId; 106 } 107 108 /** 73 109 * checks if given value exceeds borders 74 110 * … … 92 128 protected function doExecute($value) 93 129 { 94 if ((null === $value || strlen($value) == 0) && true== $this->isRequired) {130 if ((null === $value || strlen($value) === 0) && true === $this->isRequired) { 95 131 // add error message if there is no input and input is needed 96 throw new stubFilterException($this->rveFactory->create( 'FIELD_EMPTY'));97 } elseif (null !== $value && null != $this->minValidator && $this->minValidator->validate($value) != true) {132 throw new stubFilterException($this->rveFactory->create($this->errorIds[self::FIELD_EMPTY])); 133 } elseif (null !== $value && null !== $this->minValidator && $this->minValidator->validate($value) !== true) { 98 134 // add error message if input is smaller than minimum value 99 throw new stubFilterException($this->rveFactory->create( 'VALUE_TOO_SMALL')->setValues($this->minValidator->getCriteria()));100 } elseif (null !== $value && null != $this->maxValidator && $this->maxValidator->validate($value) != true) {135 throw new stubFilterException($this->rveFactory->create($this->errorIds[self::VALUE_TOO_SMALL])->setValues($this->minValidator->getCriteria())); 136 } elseif (null !== $value && null !== $this->maxValidator && $this->maxValidator->validate($value) !== true) { 101 137 // add error message if input is greater than maximum value 102 throw new stubFilterException($this->rveFactory->create( 'VALUE_TOO_GREAT')->setValues($this->maxValidator->getCriteria()));138 throw new stubFilterException($this->rveFactory->create($this->errorIds[self::VALUE_TOO_GREAT])->setValues($this->maxValidator->getCriteria())); 103 139 } 104 140 trunk/src/test/php/net/stubbles/ipo/request/filters/stubFloatFilterTestCase.php
r525 r1031 79 79 public function testWithUnsetWhenRequired() 80 80 { 81 $this->mockStubRequestValueErrorFactory->expect('create', array('FIELD_EMPTY')); 81 82 $floatFilter = new stubFloatFilter($this->mockStubRequestValueErrorFactory); 82 83 $floatFilter->setRequired(true); … … 93 94 public function testWithUnsetEmptyStringWhenRequired() 94 95 { 96 $this->mockStubRequestValueErrorFactory->expect('create', array('FIELD_EMPTY_TEST')); 95 97 $floatFilter = new stubFloatFilter($this->mockStubRequestValueErrorFactory); 96 98 $floatFilter->setRequired(true); 99 $floatFilter->setErrorId(stubNumberFilter::FIELD_EMPTY, 'FIELD_EMPTY_TEST'); 97 100 $this->expectException('stubFilterException'); 98 101 $floatFilter->execute(''); … … 121 124 $this->mockStubValidatorMin->setReturnValueAt(1, 'validate', false); 122 125 $this->mockStubValidatorMin->setReturnValue('getCriteria', array()); 126 $this->mockStubRequestValueErrorFactory->expect('create', array('VALUE_TOO_SMALL')); 123 127 124 128 $floatFilter = new stubFloatFilter($this->mockStubRequestValueErrorFactory, $this->mockStubValidatorMin); 125 129 $this->assertEqual($floatFilter->execute(-10), -10000); 130 $this->expectException('stubFilterException'); 131 $floatFilter->execute(-11); 132 } 133 134 /** 135 * assure that an FilterException is thrown when value smaller then $min 136 */ 137 public function testWithMinValidatorWithDifferentErrorId() 138 { 139 $this->mockStubValidatorMin->setReturnValue('validate', false); 140 $this->mockStubValidatorMin->setReturnValue('getCriteria', array()); 141 $this->mockStubRequestValueErrorFactory->expect('create', array('VALUE_TOO_SMALL_TEST')); 142 143 $floatFilter = new stubFloatFilter($this->mockStubRequestValueErrorFactory, $this->mockStubValidatorMin); 144 $floatFilter->setErrorId(stubNumberFilter::VALUE_TOO_SMALL, 'VALUE_TOO_SMALL_TEST'); 126 145 $this->expectException('stubFilterException'); 127 146 $floatFilter->execute(-11); … … 136 155 $this->mockStubValidatorMax->setReturnValueAt(1, 'validate', false); 137 156 $this->mockStubValidatorMax->setReturnValue('getCriteria', array()); 157 $this->mockStubRequestValueErrorFactory->expect('create', array('VALUE_TOO_GREAT')); 138 158 139 159 $floatFilter = new stubFloatFilter($this->mockStubRequestValueErrorFactory, null, $this->mockStubValidatorMax); 140 160 $this->assertEqual($floatFilter->execute(10), 10000); 161 $this->expectException('stubFilterException'); 162 $floatFilter->execute(11); 163 } 164 165 /** 166 * assure that an FilterException is thrown when value greater then $max 167 */ 168 public function testWithMaxValidatorWithDifferentErrorId() 169 { 170 $this->mockStubValidatorMax->setReturnValue('validate', false); 171 $this->mockStubValidatorMax->setReturnValue('getCriteria', array()); 172 $this->mockStubRequestValueErrorFactory->expect('create', array('VALUE_TOO_GREAT_TEST')); 173 174 $floatFilter = new stubFloatFilter($this->mockStubRequestValueErrorFactory, null, $this->mockStubValidatorMax); 175 $floatFilter->setErrorId(stubNumberFilter::VALUE_TOO_GREAT, 'VALUE_TOO_GREAT_TEST'); 141 176 $this->expectException('stubFilterException'); 142 177 $floatFilter->execute(11); trunk/src/test/php/net/stubbles/ipo/request/filters/stubIntegerFilterTestCase.php
r696 r1031 64 64 public function testWithUnsetWhenRequired() 65 65 { 66 $this->mockStubRequestValueErrorFactory->expect('create', array('FIELD_EMPTY')); 66 67 $integerFilter = new stubIntegerFilter($this->mockStubRequestValueErrorFactory); 67 68 $integerFilter->setRequired(true); … … 78 79 public function testWithUnsetEmptyStringWhenRequired() 79 80 { 81 $this->mockStubRequestValueErrorFactory->expect('create', array('FIELD_EMPTY_TEST')); 80 82 $integerFilter = new stubIntegerFilter($this->mockStubRequestValueErrorFactory); 81 83 $integerFilter->setRequired(true); 84 $integerFilter->setErrorId(stubNumberFilter::FIELD_EMPTY, 'FIELD_EMPTY_TEST'); 82 85 $this->expectException('stubFilterException'); 83 86 $integerFilter->execute(''); … … 106 109 $this->mockStubValidatorMin->setReturnValueAt(1, 'validate', false); 107 110 $this->mockStubValidatorMin->setReturnValue('getCriteria', array()); 111 $this->mockStubRequestValueErrorFactory->expect('create', array('VALUE_TOO_SMALL')); 108 112 109 113 $integerFilter = new stubIntegerFilter($this->mockStubRequestValueErrorFactory, $this->mockStubValidatorMin); 110 114 $this->assertEqual($integerFilter->execute(-10), -10); 115 $this->expectException('stubFilterException'); 116 $integerFilter->execute(-11); 117 } 118 119 /** 120 * assure that an FilterException is thrown when value smaller then $min 121 */ 122 public function testWithMinValidatorWithDifferentErrorId() 123 { 124 $this->mockStubValidatorMin->setReturnValue('validate', false); 125 $this->mockStubValidatorMin->setReturnValue('getCriteria', array()); 126 $this->mockStubRequestValueErrorFactory->expect('create', array('VALUE_TOO_SMALL_TEST')); 127 128 $integerFilter = new stubIntegerFilter($this->mockStubRequestValueErrorFactory, $this->mockStubValidatorMin); 129 $integerFilter->setErrorId(stubNumberFilter::VALUE_TOO_SMALL, 'VALUE_TOO_SMALL_TEST'); 111 130 $this->expectException('stubFilterException'); 112 131 $integerFilter->execute(-11); … … 121 140 $this->mockStubValidatorMax->setReturnValueAt(1, 'validate', false); 122 141 $this->mockStubValidatorMax->setReturnValue('getCriteria', array()); 142 $this->mockStubRequestValueErrorFactory->expect('create', array('VALUE_TOO_GREAT')); 123 143 124 144 $integerFilter = new stubIntegerFilter($this->mockStubRequestValueErrorFactory, null, $this->mockStubValidatorMax); 125 145 $this->assertEqual($integerFilter->execute(10), 10); 146 $this->expectException('stubFilterException'); 147 $integerFilter->execute(11); 148 } 149 150 /** 151 * assure that an FilterException is thrown when value greater then $max 152 */ 153 public function testWithMaxValidatorWithDifferentErrorId() 154 { 155 $this->mockStubValidatorMax->setReturnValue('validate', false); 156 $this->mockStubValidatorMax->setReturnValue('getCriteria', array()); 157 $this->mockStubRequestValueErrorFactory->expect('create', array('VALUE_TOO_GREAT_TEST')); 158 159 $integerFilter = new stubIntegerFilter($this->mockStubRequestValueErrorFactory, null, $this->mockStubValidatorMax); 160 $integerFilter->setErrorId(stubNumberFilter::VALUE_TOO_GREAT, 'VALUE_TOO_GREAT_TEST'); 126 161 $this->expectException('stubFilterException'); 127 162 $integerFilter->execute(11);
