Changeset 1376

Show
Ignore:
Timestamp:
02/26/08 17:06:40 (9 months ago)
Author:
mikey
Message:

more flexibility with error ids

Files:

Legend:

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

    r1369 r1376  
    159159     * @param   numeric            $min 
    160160     * @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) 
    165167    { 
    166168        if (null !== $min || null !== $max) { 
     
    171173            $filter = new stubRangeFilterDecorator($this->getDecoratedFilter(), $this->getRVEFactory()); 
    172174            if (null !== $min) { 
    173                 $filter->setMinValidator(new stubMinNumberValidator($min)); 
     175                $filter->setMinValidator(new stubMinNumberValidator($min), $minErrorId); 
    174176            } 
    175177             
    176178            if (null !== $max) { 
    177                 $filter->setMaxValidator(new stubMaxNumberValidator($max)); 
     179                $filter->setMaxValidator(new stubMaxNumberValidator($max), $maxErrorId); 
    178180            } 
    179181             
     
    193195     * @param   numeric            $minLength 
    194196     * @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) 
    199203    { 
    200204        if (null !== $minLength || null !== $maxLength) { 
     
    205209            $filter = new stubLengthFilterDecorator($this->getDecoratedFilter(), $this->getRVEFactory()); 
    206210            if (null !== $minLength) { 
    207                 $filter->setMinLengthValidator(new stubMinLengthValidator($minLength)); 
     211                $filter->setMinLengthValidator(new stubMinLengthValidator($minLength), $minLengthErrorId); 
    208212            } 
    209213             
    210214            if (null !== $maxLength) { 
    211                 $filter->setMaxLengthValidator(new stubMaxLengthValidator($maxLength)); 
     215                $filter->setMaxLengthValidator(new stubMaxLengthValidator($maxLength), $maxLengthErrorId); 
    212216            } 
    213217             
  • trunk/src/main/php/net/stubbles/ipo/request/filters/stubLengthFilterDecorator.php

    r1323 r1376  
    3434     * @var  stubValidator 
    3535     */ 
    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'; 
    3743    /** 
    3844     * validator for maximum length of string 
     
    4147     */ 
    4248    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'; 
    4355 
    4456    /** 
     
    5870     * 
    5971     * @param  stubValidator  $minLength 
     72     * @param  string         $minLengthErrorId  optional  error id to use in case validation fails 
    6073     */ 
    61     public function setMinLengthValidator(stubValidator $minLength
     74    public function setMinLengthValidator(stubValidator $minLength, $minLengthErrorId = null
    6275    { 
    63         $this->minLength = $minLength; 
     76        $this->minLength        = $minLength; 
     77        if (null !== $minLengthErrorId) { 
     78            $this->minLengthErrorId = $minLengthErrorId; 
     79        } 
    6480    } 
    6581 
     
    7591 
    7692    /** 
     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    /** 
    77103     * set a max length validator 
    78104     * 
    79105     * @param  stubValidator  $maxLength 
     106     * @param  string         $maxLengthErrorId  optional  error id to use in case validation fails 
    80107     */ 
    81     public function setMaxLengthValidator(stubValidator $maxLength
     108    public function setMaxLengthValidator(stubValidator $maxLength, $maxLengthErrorId = null
    82109    { 
    83110        $this->maxLength = $maxLength; 
     111        if (null !== $maxLengthErrorId) { 
     112            $this->maxLengthErrorId = $maxLengthErrorId; 
     113        } 
    84114    } 
    85115 
     
    95125 
    96126    /** 
     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    /** 
    97137     * execute the filter 
    98138     * 
     
    105145        if (null != $this->minLength && $this->minLength->validate($value) === false) { 
    106146            // 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())); 
    108148        } elseif (null != $this->maxLength && $this->maxLength->validate($value) === false) { 
    109149            // 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())); 
    111151        } 
    112152         
  • trunk/src/main/php/net/stubbles/ipo/request/filters/stubRangeFilterDecorator.php

    r1369 r1376  
    3535    protected $minValidator = null; 
    3636    /** 
     37     * the error id to use in case min validation fails 
     38     * 
     39     * @var  string 
     40     */ 
     41    protected $minErrorId   = 'VALUE_TOO_SMALL'; 
     42    /** 
    3743     * validator for maximum values 
    3844     * 
     
    4046     */ 
    4147    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'; 
    4254 
    4355    /** 
     
    5769     * 
    5870     * @param  stubValidator  $minValidator 
     71     * @param  string         $minErrorId    optional  error id to use in case validation fails 
    5972     */ 
    60     public function setMinValidator(stubValidator $minValidator
     73    public function setMinValidator(stubValidator $minValidator, $minErrorId = null
    6174    { 
    6275        $this->minValidator = $minValidator; 
     76        if (null !== $minErrorId) { 
     77            $this->minErrorId   = $minErrorId; 
     78        } 
    6379    } 
    6480 
     
    7490 
    7591    /** 
     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    /** 
    76102     * sets the validator for maximum values 
    77103     * 
    78104     * @param  stubValidator  $maxValidator 
     105     * @param  string         $maxErrorId    optional  error id to use in case validation fails 
    79106     */ 
    80     public function setMaxValidator(stubValidator $maxValidator
     107    public function setMaxValidator(stubValidator $maxValidator, $maxErrorId = null
    81108    { 
    82109        $this->maxValidator = $maxValidator; 
     110        if (null !== $maxErrorId) { 
     111            $this->maxErrorId   = $maxErrorId; 
     112        } 
    83113    } 
    84114 
     
    94124 
    95125    /** 
     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    /** 
    96136     * checks if given value exceeds borders 
    97137     * 
     
    104144        if (null !== $value && null !== $this->minValidator && $this->minValidator->validate($value) !== true) { 
    105145             // 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())); 
    107147        } elseif (null !== $value && null !== $this->maxValidator && $this->maxValidator->validate($value) !== true) { 
    108148            // 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())); 
    110150        } 
    111151 
  • trunk/src/test/php/net/stubbles/ipo/request/filters/stubFilterFactoryTestCase.php

    r1346 r1376  
    5151        $this->assertType('stubRangeFilterDecorator', $rangeFilterDecorator); 
    5252        $this->assertEquals(1, $rangeFilterDecorator->getMinValidator()->getValue()); 
     53        $this->assertEquals('VALUE_TOO_SMALL', $rangeFilterDecorator->getMinErrorId()); 
    5354        $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()); 
    5474        $this->assertType('stubIntegerFilter', $rangeFilterDecorator->getDecoratedFilter()); 
    5575    } 
     
    150170        $this->assertType('stubLengthFilterDecorator', $lengthFilterDecorator); 
    151171        $this->assertEquals(2, $lengthFilterDecorator->getMinLengthValidator()->getValue()); 
     172        $this->assertEquals('STRING_TOO_SHORT', $lengthFilterDecorator->getMinLengthErrorId()); 
    152173        $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()); 
    153193        $this->assertType('stubTextFilter', $lengthFilterDecorator->getDecoratedFilter()); 
    154194    } 
  • trunk/src/test/php/net/stubbles/ipo/request/filters/stubLengthFilterDecoratorTestCase.php

    r1323 r1376  
    106106                               ->will($this->returnValue(array())); 
    107107        $this->lengthFilterDecorator->setMinLengthValidator($mockMinLengthValidaror); 
     108        $this->assertEquals('STRING_TOO_SHORT', $this->lengthFilterDecorator->getMinLengthErrorId()); 
    108109        $this->assertSame($mockMinLengthValidaror, $this->lengthFilterDecorator->getMinLengthValidator()); 
    109110        $this->assertNull($this->lengthFilterDecorator->getMaxLengthValidator()); 
     
    111112                                           ->method('create') 
    112113                                           ->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')) 
    113142                                           ->will($this->returnValue(new stubRequestValueError('foo', array('en_EN' => 'Something wrent wrong.')))); 
    114143        $this->lengthFilterDecorator->callDoExecute("minLengthTest"); 
     
    152181                               ->will($this->returnValue(array())); 
    153182        $this->lengthFilterDecorator->setMaxLengthValidator($mockMaxLengthValidaror); 
     183        $this->assertEquals('STRING_TOO_LONG', $this->lengthFilterDecorator->getMaxLengthErrorId()); 
    154184        $this->assertNull($this->lengthFilterDecorator->getMinLengthValidator()); 
    155185        $this->assertSame($mockMaxLengthValidaror, $this->lengthFilterDecorator->getMaxLengthValidator()); 
     
    157187                                           ->method('create') 
    158188                                           ->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')) 
    159217                                           ->will($this->returnValue(new stubRequestValueError('foo', array('en_EN' => 'Something wrent wrong.')))); 
    160218        $this->lengthFilterDecorator->callDoExecute("maxLengthTest"); 
  • trunk/src/test/php/net/stubbles/ipo/request/filters/stubRangeFilterDecoratorTestCase.php

    r1329 r1376  
    117117                               ->will($this->returnValue(array())); 
    118118        $this->rangeFilterDecorator->setMinValidator($this->mockValidatorMin); 
     119        $this->assertEquals('VALUE_TOO_SMALL', $this->rangeFilterDecorator->getMinErrorId()); 
    119120        $this->mockRequestValueErrorFactory->expects($this->once()) 
    120121                                           ->method('create') 
    121122                                           ->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')) 
    122146                                           ->will($this->returnValue(new stubRequestValueError('foo', array('en_EN' => 'Something wrent wrong.')))); 
    123147        $this->rangeFilterDecorator->callDoExecute(-11); 
     
    156180                               ->will($this->returnValue(array())); 
    157181        $this->rangeFilterDecorator->setMaxValidator($this->mockValidatorMax); 
     182        $this->assertEquals('VALUE_TOO_GREAT', $this->rangeFilterDecorator->getMaxErrorId()); 
    158183        $this->mockRequestValueErrorFactory->expects($this->once()) 
    159184                                           ->method('create') 
    160185                                           ->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')) 
    161209                                           ->will($this->returnValue(new stubRequestValueError('foo', array('en_EN' => 'Something wrent wrong.')))); 
    162210        $this->rangeFilterDecorator->callDoExecute(11);