Changeset 1031

Show
Ignore:
Timestamp:
11/12/07 15:37:58 (8 months ago)
Author:
mikey
Message:

implemented enhancement #96 for net.stubbles.ipo.request.filters.stubFloatFilter and net.stubbles.ipo.request.filters.stubIntegerFilter

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/main/php/net/stubbles/ipo/request/filters/stubNumberFilter.php

    r731 r1031  
    2424{ 
    2525    /** 
     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    /** 
    2638     * validator for minimum values 
    2739     * 
     
    3547     */ 
    3648    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                              ); 
    3758 
    3859    /** 
     
    7192 
    7293    /** 
     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    /** 
    73109     * checks if given value exceeds borders 
    74110     * 
     
    92128    protected function doExecute($value) 
    93129    { 
    94         if ((null === $value || strlen($value) == 0) && true == $this->isRequired) { 
     130        if ((null === $value || strlen($value) === 0) && true === $this->isRequired) { 
    95131            // 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) { 
    98134             // 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) { 
    101137            // 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())); 
    103139        } 
    104140 
  • trunk/src/test/php/net/stubbles/ipo/request/filters/stubFloatFilterTestCase.php

    r525 r1031  
    7979    public function testWithUnsetWhenRequired() 
    8080    { 
     81        $this->mockStubRequestValueErrorFactory->expect('create', array('FIELD_EMPTY')); 
    8182        $floatFilter = new stubFloatFilter($this->mockStubRequestValueErrorFactory); 
    8283        $floatFilter->setRequired(true); 
     
    9394    public function testWithUnsetEmptyStringWhenRequired() 
    9495    { 
     96        $this->mockStubRequestValueErrorFactory->expect('create', array('FIELD_EMPTY_TEST')); 
    9597        $floatFilter = new stubFloatFilter($this->mockStubRequestValueErrorFactory); 
    9698        $floatFilter->setRequired(true); 
     99        $floatFilter->setErrorId(stubNumberFilter::FIELD_EMPTY, 'FIELD_EMPTY_TEST'); 
    97100        $this->expectException('stubFilterException'); 
    98101        $floatFilter->execute(''); 
     
    121124        $this->mockStubValidatorMin->setReturnValueAt(1, 'validate', false); 
    122125        $this->mockStubValidatorMin->setReturnValue('getCriteria', array()); 
     126        $this->mockStubRequestValueErrorFactory->expect('create', array('VALUE_TOO_SMALL')); 
    123127         
    124128        $floatFilter = new stubFloatFilter($this->mockStubRequestValueErrorFactory, $this->mockStubValidatorMin); 
    125129        $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'); 
    126145        $this->expectException('stubFilterException'); 
    127146        $floatFilter->execute(-11); 
     
    136155        $this->mockStubValidatorMax->setReturnValueAt(1, 'validate', false); 
    137156        $this->mockStubValidatorMax->setReturnValue('getCriteria', array()); 
     157        $this->mockStubRequestValueErrorFactory->expect('create', array('VALUE_TOO_GREAT')); 
    138158         
    139159        $floatFilter = new stubFloatFilter($this->mockStubRequestValueErrorFactory, null, $this->mockStubValidatorMax); 
    140160        $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'); 
    141176        $this->expectException('stubFilterException'); 
    142177        $floatFilter->execute(11); 
  • trunk/src/test/php/net/stubbles/ipo/request/filters/stubIntegerFilterTestCase.php

    r696 r1031  
    6464    public function testWithUnsetWhenRequired() 
    6565    { 
     66        $this->mockStubRequestValueErrorFactory->expect('create', array('FIELD_EMPTY')); 
    6667        $integerFilter = new stubIntegerFilter($this->mockStubRequestValueErrorFactory); 
    6768        $integerFilter->setRequired(true); 
     
    7879    public function testWithUnsetEmptyStringWhenRequired() 
    7980    { 
     81        $this->mockStubRequestValueErrorFactory->expect('create', array('FIELD_EMPTY_TEST')); 
    8082        $integerFilter = new stubIntegerFilter($this->mockStubRequestValueErrorFactory); 
    8183        $integerFilter->setRequired(true); 
     84        $integerFilter->setErrorId(stubNumberFilter::FIELD_EMPTY, 'FIELD_EMPTY_TEST'); 
    8285        $this->expectException('stubFilterException'); 
    8386        $integerFilter->execute(''); 
     
    106109        $this->mockStubValidatorMin->setReturnValueAt(1, 'validate', false); 
    107110        $this->mockStubValidatorMin->setReturnValue('getCriteria', array()); 
     111        $this->mockStubRequestValueErrorFactory->expect('create', array('VALUE_TOO_SMALL')); 
    108112         
    109113        $integerFilter = new stubIntegerFilter($this->mockStubRequestValueErrorFactory, $this->mockStubValidatorMin); 
    110114        $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'); 
    111130        $this->expectException('stubFilterException'); 
    112131        $integerFilter->execute(-11); 
     
    121140        $this->mockStubValidatorMax->setReturnValueAt(1, 'validate', false); 
    122141        $this->mockStubValidatorMax->setReturnValue('getCriteria', array()); 
     142        $this->mockStubRequestValueErrorFactory->expect('create', array('VALUE_TOO_GREAT')); 
    123143         
    124144        $integerFilter = new stubIntegerFilter($this->mockStubRequestValueErrorFactory, null, $this->mockStubValidatorMax); 
    125145        $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'); 
    126161        $this->expectException('stubFilterException'); 
    127162        $integerFilter->execute(11);