Changeset 1329

Show
Ignore:
Timestamp:
02/03/08 23:36:00 (7 months ago)
Author:
mikey
Message:

reworked numeric filters to be able to use simple versions without a request value error factory

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/main/php/net/stubbles/ipo/request/broker/annotations/stubFloatFilterAnnotation.php

    r1223 r1329  
    99stubClassLoader::load('net::stubbles::reflection::annotations::stubAnnotation', 
    1010                      'net::stubbles::ipo::request::filters::stubFloatFilter', 
     11                      'net::stubbles::ipo::request::filters::stubRangeFilterDecorator', 
    1112                      'net::stubbles::ipo::request::broker::annotations::stubAbstractFilterAnnotation', 
    1213                      'net::stubbles::util::validators::stubMaxNumberValidator', 
     
    5758     * returns the filter defined by the annotation 
    5859     * 
    59      * @return  stubFloatFilter 
    60      * @throws  stubRequestBrokerException 
     60     * @return  stubFilter 
    6161     */ 
    6262    protected function doGetFilter() 
    6363    { 
    64         $minValidator = ((null !== $this->minValue) ? (new stubMinNumberValidator($this->minValue)) : (null)); 
    65         $maxValidator = ((null !== $this->maxValue) ? (new stubMaxNumberValidator($this->maxValue)) : (null)); 
    66         $floatFilter  = new stubFloatFilter($this->createRVEFactory(), $minValidator, $maxValidator); 
    67         return $floatFilter; 
     64        $filter = new stubFloatFilter(); 
     65        if (null !== $this->minValue || null !== $this->maxValue) { 
     66            $filter = new stubRangeFilterDecorator($filter, $this->createRVEFactory()); 
     67            if (null !== $this->minValue) { 
     68                $filter->setMinValidator(new stubMinNumberValidator($this->minValue)); 
     69            } 
     70             
     71            if (null !== $this->maxValue) { 
     72                $filter->setMaxValidator(new stubMaxNumberValidator($this->maxValue)); 
     73            } 
     74        } 
     75         
     76        return $filter; 
    6877    } 
    6978} 
  • trunk/src/main/php/net/stubbles/ipo/request/broker/annotations/stubIntegerFilterAnnotation.php

    r1223 r1329  
    99stubClassLoader::load('net::stubbles::reflection::annotations::stubAnnotation', 
    1010                      'net::stubbles::ipo::request::filters::stubIntegerFilter', 
     11                      'net::stubbles::ipo::request::filters::stubRangeFilterDecorator', 
    1112                      'net::stubbles::ipo::request::broker::annotations::stubAbstractFilterAnnotation', 
    1213                      'net::stubbles::util::validators::stubMaxNumberValidator', 
     
    5758     * returns the filter defined by the annotation 
    5859     * 
    59      * @return  stubIntegerFilter 
    60      * @throws  stubRequestBrokerException 
     60     * @return  stubFilter 
    6161     */ 
    6262    protected function doGetFilter() 
    6363    { 
    64         $minValidator = ((null !== $this->minValue) ? (new stubMinNumberValidator($this->minValue)) : (null)); 
    65         $maxValidator = ((null !== $this->maxValue) ? (new stubMaxNumberValidator($this->maxValue)) : (null)); 
    66         $integerFilter  = new stubIntegerFilter($this->createRVEFactory(), $minValidator, $maxValidator); 
    67         return $integerFilter; 
     64        $filter = new stubIntegerFilter(); 
     65        if (null !== $this->minValue || null !== $this->maxValue) { 
     66            $filter = new stubRangeFilterDecorator($filter, $this->createRVEFactory()); 
     67            if (null !== $this->minValue) { 
     68                $filter->setMinValidator(new stubMinNumberValidator($this->minValue)); 
     69            } 
     70             
     71            if (null !== $this->maxValue) { 
     72                $filter->setMaxValidator(new stubMaxNumberValidator($this->maxValue)); 
     73            } 
     74        } 
     75         
     76        return $filter; 
    6877    } 
    6978} 
  • trunk/src/main/php/net/stubbles/ipo/request/filters/stubFloatFilter.php

    r1328 r1329  
    77 * @subpackage  ipo_request_filters 
    88 */ 
    9 stubClassLoader::load('net::stubbles::ipo::request::filters::stubNumberFilter', 
     9stubClassLoader::load('net::stubbles::ipo::request::filters::stubFilter', 
    1010                      'net::stubbles::util::stubRegistry' 
    1111); 
     
    1313 * Filters on request variables of type double / float. 
    1414 *  
    15  * This filter takes any value, casts it to float and checks if it complies 
    16  * with the min and/or the max validator. Afterwards its multiplied with 10^x 
    17  * (x is configureable via the registry) to get an integer value that can be 
    18  * used for mathematical operations for accuracy
     15 * This filter takes any value and casts it to float. Afterwards its multiplied 
     16 * with 10^x (x is configureable via the registry) to get an integer value that 
     17 * can be used for mathematical operations for accuracy. If no value for x is 
     18 * configured in the registry the value is returned as is after the cast
    1919 * 
    2020 * @package     stubbles 
    2121 * @subpackage  ipo_request_filters 
    2222 */ 
    23 class stubFloatFilter extends stubNumberFilter 
     23class stubFloatFilter extends stubBaseObject implements stubFilter 
    2424{ 
    2525    /** 
     
    3131     * checks if given value is double, transfers into int with $decimalPlaces 
    3232     * 
    33      * @param   mixed                $value  value to filter 
    34      * @return  int                  filtered value 
    35      * @throws  stubFilterException  in case $value has errors 
     33     * @param   mixed  $value  value to filter 
     34     * @return  float 
    3635     */ 
    3736    function execute($value) 
    3837    { 
    3938        settype($value, 'float'); 
    40         $value    = parent::doExecute($value); 
    4139        $decimals = stubRegistry::getConfig(self::DECIMALS_REGISTRY_KEY); 
    4240        if (null == $decimals) { 
  • trunk/src/main/php/net/stubbles/ipo/request/filters/stubIntegerFilter.php

    r1328 r1329  
    77 * @subpackage  ipo_request_filters 
    88 */ 
    9 stubClassLoader::load('net::stubbles::ipo::request::filters::stubNumberFilter'); 
     9stubClassLoader::load('net::stubbles::ipo::request::filters::stubFilter'); 
    1010/** 
    1111 * Basic class for filters on request variables of type integer. 
    1212 * 
    13  * This filter takes any value, casts it to int and checks if it complies 
    14  * with the min and/or the max validator. 
     13 * This filter takes any value and casts it to int. 
    1514 * 
    1615 * @package     stubbles 
    1716 * @subpackage  ipo_request_filters 
    1817 */ 
    19 class stubIntegerFilter extends stubNumberFilter 
     18class stubIntegerFilter extends stubBaseObject implements stubFilter 
    2019{ 
    2120    /** 
    2221     * checks if given value is an integer 
    2322     * 
    24      * @param   mixed                $value  value to filter 
    25      * @return  int                  filtered value 
    26      * @throws  stubFilterException  in case $value has errors 
     23     * @param   mixed  $value  value to filter 
     24     * @return  int 
    2725     */ 
    2826    function execute($value) 
    2927    { 
    3028        settype($value, 'integer'); 
    31         return parent::doExecute($value)
     29        return $value
    3230    } 
    3331} 
  • trunk/src/main/php/net/stubbles/ipo/request/filters/stubRangeFilterDecorator.php

    r1323 r1329  
    88 */ 
    99stubClassLoader::load('net::stubbles::ipo::request::stubRequestValueErrorFactory', 
    10                       'net::stubbles::ipo::request::filters::stubFilter', 
     10                      'net::stubbles::ipo::request::filters::stubStrategyFilterDecorator', 
    1111                      'net::stubbles::util::validators::stubValidator' 
    1212); 
     
    2020 * @subpackage  ipo_request_filters 
    2121 */ 
    22 class stubNumberFilter extends stubBaseObject implements stubFilte
     22class stubRangeFilterDecorator extends stubStrategyFilterDecorato
    2323{ 
    2424    /** 
     
    4444     * constructor 
    4545     * 
     46     * @param  stubFilter                    $filter      decorated filter 
    4647     * @param  stubRequestValueErrorFactory  $rveFactory  factory to create RequestValueErrors 
    47      * @param  stubValidator                 $min         validator for minimum values 
    48      * @param  stubValidator                 $max         validator for maximum values 
    4948     */ 
    50     public function __construct(stubRequestValueErrorFactory $rveFactory, stubValidator $min = null, stubValidator $max = null
     49    public function __construct(stubFilter $filter, stubRequestValueErrorFactory $rveFactory
    5150    { 
     51        $this->setDecoratedFilter($filter); 
    5252        $this->rveFactory   = $rveFactory; 
    53         $this->minValidator = $min; 
    54         $this->maxValidator = $max; 
     53    } 
     54 
     55    /** 
     56     * sets the validator for minimum values 
     57     * 
     58     * @return  stubValidator  $minValidator 
     59     */ 
     60    public function setMinValidator(stubValidator $minValidator) 
     61    { 
     62        $this->minValidator = $minValidator; 
    5563    } 
    5664 
     
    6674 
    6775    /** 
     76     * sets the validator for maximum values 
     77     * 
     78     * @return  stubValidator  $maxValidator 
     79     */ 
     80    public function setMaxValidator(stubValidator $maxValidator) 
     81    { 
     82        $this->maxValidator = $maxValidator; 
     83    } 
     84 
     85    /** 
    6886     * returns the validator for maximum values 
    6987     * 
     
    7391    { 
    7492        return $this->maxValidator; 
    75     } 
    76  
    77     /** 
    78      * checks if given value exceeds borders 
    79      * 
    80      * @param   numeric              $value  value to filter 
    81      * @return  numeric              filtered value 
    82      * @throws  stubFilterException  in case $value has errors 
    83      */ 
    84     public function execute($value) 
    85     { 
    86         settype($value, 'float'); 
    87         return $this->doExecute($value); 
    8893    } 
    8994 
  • trunk/src/test/php/net/stubbles/ipo/IPOTestSuite.php

    r1327 r1329  
    5050        $suite->addTestFile($dir . '/request/filters/stubLengthFilterDecoratorTestCase.php'); 
    5151        $suite->addTestFile($dir . '/request/filters/stubMailFilterTestCase.php'); 
    52         $suite->addTestFile($dir . '/request/filters/stubNumberFilterTestCase.php'); 
    5352        $suite->addTestFile($dir . '/request/filters/stubPassThruFilterTestCase.php'); 
    5453        $suite->addTestFile($dir . '/request/filters/stubPasswordFilterTestCase.php'); 
     54        $suite->addTestFile($dir . '/request/filters/stubRangeFilterDecoratorTestCase.php'); 
    5555        $suite->addTestFile($dir . '/request/filters/stubRegexFilterDecoratorTestCase.php'); 
    5656        $suite->addTestFile($dir . '/request/filters/stubRequiredFilterDecoratorTestCase.php'); 
  • trunk/src/test/php/net/stubbles/ipo/request/broker/annotations/stubFloatFilterAnnotationTestCase.php

    r1323 r1329  
    3939    public function instance() 
    4040    { 
    41         $floatFilter = $this->floatFilterAnnotation->getFilter(); 
    42         $this->assertType('stubFloatFilter', $floatFilter); 
    43         $this->assertNull($floatFilter->getMinValidator()); 
    44         $this->assertNull($floatFilter->getMaxValidator()); 
     41        $this->assertType('stubFloatFilter', $this->floatFilterAnnotation->getFilter()); 
    4542    } 
    4643 
     
    5047     * @test 
    5148     */ 
    52     public function withValidators() 
     49    public function withMinNumValidator() 
     50    { 
     51        $this->floatFilterAnnotation->setMinValue(1); 
     52        $filter = $this->floatFilterAnnotation->getFilter(); 
     53        $this->assertType('stubRangeFilterDecorator', $filter); 
     54        $this->assertType('stubMinNumberValidator', $filter->getMinValidator()); 
     55        $this->assertNull($filter->getMaxValidator()); 
     56        $this->assertEquals(1, $filter->getMinValidator()->getValue()); 
     57        $this->assertType('stubFloatFilter', $filter->getDecoratedFilter()); 
     58    } 
     59 
     60    /** 
     61     * test that the correct filter is created 
     62     * 
     63     * @test 
     64     */ 
     65    public function withMaxNumValidators() 
     66    { 
     67        $this->floatFilterAnnotation->setMaxValue(2); 
     68        $filter = $this->floatFilterAnnotation->getFilter(); 
     69        $this->assertType('stubRangeFilterDecorator', $filter); 
     70        $this->assertNull($filter->getMinValidator()); 
     71        $this->assertType('stubMaxNumberValidator', $filter->getMaxValidator()); 
     72        $this->assertEquals(2, $filter->getMaxValidator()->getValue()); 
     73        $this->assertType('stubFloatFilter', $filter->getDecoratedFilter()); 
     74    } 
     75 
     76    /** 
     77     * test that the correct filter is created 
     78     * 
     79     * @test 
     80     */ 
     81    public function withBothValidators() 
    5382    { 
    5483        $this->floatFilterAnnotation->setMinValue(1); 
    5584        $this->floatFilterAnnotation->setMaxValue(2); 
    56         $floatFilter = $this->floatFilterAnnotation->getFilter(); 
    57         $this->assertType('stubFloatFilter', $floatFilter); 
    58         $this->assertType('stubMinNumberValidator', $floatFilter->getMinValidator()); 
    59         $this->assertType('stubMaxNumberValidator', $floatFilter->getMaxValidator()); 
    60         $this->assertEquals(1, $floatFilter->getMinValidator()->getValue()); 
    61         $this->assertEquals(2, $floatFilter->getMaxValidator()->getValue()); 
     85        $filter = $this->floatFilterAnnotation->getFilter(); 
     86        $this->assertType('stubRangeFilterDecorator', $filter); 
     87        $this->assertType('stubMinNumberValidator', $filter->getMinValidator()); 
     88        $this->assertType('stubMaxNumberValidator', $filter->getMaxValidator()); 
     89        $this->assertEquals(1, $filter->getMinValidator()->getValue()); 
     90        $this->assertEquals(2, $filter->getMaxValidator()->getValue()); 
     91        $this->assertType('stubFloatFilter', $filter->getDecoratedFilter()); 
    6292    } 
    6393} 
  • trunk/src/test/php/net/stubbles/ipo/request/broker/annotations/stubIntegerFilterAnnotationTestCase.php

    r1323 r1329  
    3939    public function instance() 
    4040    { 
    41         $integerFilter = $this->integerFilterAnnotation->getFilter(); 
    42         $this->assertType('stubIntegerFilter', $integerFilter); 
    43         $this->assertNull($integerFilter->getMinValidator()); 
    44         $this->assertNull($integerFilter->getMaxValidator()); 
     41        $this->assertType('stubIntegerFilter', $this->integerFilterAnnotation->getFilter()); 
    4542    } 
    4643 
     
    5047     * @test 
    5148     */ 
    52     public function withValidators() 
     49    public function withMinNumValidator() 
     50    { 
     51        $this->integerFilterAnnotation->setMinValue(1); 
     52        $filter = $this->integerFilterAnnotation->getFilter(); 
     53        $this->assertType('stubRangeFilterDecorator', $filter); 
     54        $this->assertType('stubMinNumberValidator', $filter->getMinValidator()); 
     55        $this->assertNull($filter->getMaxValidator()); 
     56        $this->assertEquals(1, $filter->getMinValidator()->getValue()); 
     57        $this->assertType('stubIntegerFilter', $filter->getDecoratedFilter()); 
     58    } 
     59 
     60    /** 
     61     * test that the correct filter is created 
     62     * 
     63     * @test 
     64     */ 
     65    public function withMaxNumValidators() 
     66    { 
     67        $this->integerFilterAnnotation->setMaxValue(2); 
     68        $filter = $this->integerFilterAnnotation->getFilter(); 
     69        $this->assertType('stubRangeFilterDecorator', $filter); 
     70        $this->assertNull($filter->getMinValidator()); 
     71        $this->assertType('stubMaxNumberValidator', $filter->getMaxValidator()); 
     72        $this->assertEquals(2, $filter->getMaxValidator()->getValue()); 
     73        $this->assertType('stubIntegerFilter', $filter->getDecoratedFilter()); 
     74    } 
     75 
     76    /** 
     77     * test that the correct filter is created 
     78     * 
     79     * @test 
     80     */ 
     81    public function withBothValidators() 
    5382    { 
    5483        $this->integerFilterAnnotation->setMinValue(1); 
    5584        $this->integerFilterAnnotation->setMaxValue(2); 
    56         $integerFilter = $this->integerFilterAnnotation->getFilter(); 
    57         $this->assertType('stubIntegerFilter', $integerFilter); 
    58         $this->assertType('stubMinNumberValidator', $integerFilter->getMinValidator()); 
    59         $this->assertType('stubMaxNumberValidator', $integerFilter->getMaxValidator()); 
    60         $this->assertEquals(1, $integerFilter->getMinValidator()->getValue()); 
    61         $this->assertEquals(2, $integerFilter->getMaxValidator()->getValue()); 
     85        $filter = $this->integerFilterAnnotation->getFilter(); 
     86        $this->assertType('stubRangeFilterDecorator', $filter); 
     87        $this->assertType('stubMinNumberValidator', $filter->getMinValidator()); 
     88        $this->assertType('stubMaxNumberValidator', $filter->getMaxValidator()); 
     89        $this->assertEquals(1, $filter->getMinValidator()->getValue()); 
     90        $this->assertEquals(2, $filter->getMaxValidator()->getValue()); 
     91        $this->assertType('stubIntegerFilter', $filter->getDecoratedFilter()); 
    6292    } 
    6393} 
  • trunk/src/test/php/net/stubbles/ipo/request/filters/stubFloatFilterTestCase.php

    r1323 r1329  
    1717{ 
    1818    /** 
    19      * a mock to be used for the rveFactory 
    20      * 
    21      * @var  PHPUnit_Framework_MockObject_MockObject 
    22      */ 
    23     protected $mockRequestValueErrorFactory; 
    24     /** 
    25      * a mock to be used for the minimum validator 
    26      * 
    27      * @var  PHPUnit_Framework_MockObject_MockObject 
    28      */ 
    29     protected $mockValidatorMin; 
    30     /** 
    31      * a mock to be used for the maximum validator 
    32      * 
    33      * @var  PHPUnit_Framework_MockObject_MockObject 
    34      */ 
    35     protected $mockValidatorMax; 
    36      
    37     /** 
    3819     * set up test environment 
    3920     */ 
     
    4122    { 
    4223        stubRegistry::setConfig(stubFloatFilter::DECIMALS_REGISTRY_KEY, 3); 
    43         $this->mockRequestValueErrorFactory = $this->getMock('stubRequestValueErrorFactory'); 
    44         $this->mockValidatorMin             = $this->getMock('stubValidator'); 
    45         $this->mockValidatorMax             = $this->getMock('stubValidator'); 
    4624    } 
    4725 
     
    6139    public function value() 
    6240    { 
    63         $floatFilter = new stubFloatFilter($this->mockRequestValueErrorFactory); 
     41        $floatFilter = new stubFloatFilter(); 
    6442        $this->assertEquals(8453, $floatFilter->execute('8.4533')); 
    6543        $this->assertEquals(8453, $floatFilter->execute('8.4538')); 
     
    8058    public function unsetOrOtherValues() 
    8159    { 
    82         $floatFilter = new stubFloatFilter($this->mockRequestValueErrorFactory); 
     60        $floatFilter = new stubFloatFilter(); 
    8361        $this->assertEquals(0, $floatFilter->execute(null)); 
    8462        $this->assertEquals(0, $floatFilter->execute('')); 
    8563        $this->assertEquals(1000, $floatFilter->execute(true)); 
    8664        $this->assertEquals(0, $floatFilter->execute(false)); 
    87     } 
    88  
    89     /** 
    90      * assure that an FilterException is thrown when value smaller then $min 
    91      * 
    92      * @test 
    93      */ 
    94     public function withMinValidator() 
    95     { 
    96         $this->mockValidatorMin->expects($this->once()) 
    97                                ->method('validate') 
    98                                ->will($this->returnValue(true)); 
    99         $this->mockValidatorMin->expects($this->never()) 
    100                                ->method('getCriteria'); 
    101         $floatFilter = new stubFloatFilter($this->mockRequestValueErrorFactory, $this->mockValidatorMin); 
    102         $this->assertEquals(-10000, $floatFilter->execute(-10)); 
    103     } 
    104  
    105     /** 
    106      * assure that an FilterException is thrown when value smaller then $min 
    107      * 
    108      * @test 
    109      * @expectedException  stubFilterException 
    110      */ 
    111     public function withMinValidatorFails() 
    112     { 
    113         $this->mockValidatorMin->expects($this->once()) 
    114                                ->method('validate') 
    115                                ->will($this->returnValue(false)); 
    116         $this->mockValidatorMin->expects($this->once()) 
    117                                ->method('getCriteria') 
    118                                ->will($this->returnValue(array())); 
    119         $floatFilter = new stubFloatFilter($this->mockRequestValueErrorFactory, $this->mockValidatorMin); 
    120         $this->mockRequestValueErrorFactory->expects($this->once()) 
    121                                            ->method('create') 
    122                                            ->with($this->equalTo('VALUE_TOO_SMALL')) 
    123                                            ->will($this->returnValue(new stubRequestValueError('foo', array('en_EN' => 'Something wrent wrong.')))); 
    124         $floatFilter->execute(-11); 
    125     } 
    126  
    127     /** 
    128      * assure that an FilterException is thrown when value greater then $max 
    129      * 
    130      * @test 
    131      */ 
    132     public function withMaxValidator() 
    133     { 
    134         $this->mockValidatorMax->expects($this->once()) 
    135                                ->method('validate') 
    136                                ->will($this->returnValue(true)); 
    137         $this->mockValidatorMax->expects($this->never()) 
    138                                ->method('getCriteria'); 
    139         $floatFilter = new stubFloatFilter($this->mockRequestValueErrorFactory, null, $this->mockValidatorMax); 
    140         $this->assertEquals($floatFilter->execute(10), 10000); 
    141     } 
    142  
    143     /** 
    144      * assure that an FilterException is thrown when value greater then $max 
    145      * 
    146      * @test 
    147      * @expectedException  stubFilterException 
    148      */ 
    149     public function withMaxValidatorFails() 
    150     { 
    151         $this->mockValidatorMax->expects($this->once()) 
    152                                ->method('validate') 
    153                                ->will($this->returnValue(false)); 
    154         $this->mockValidatorMax->expects($this->once()) 
    155                                ->method('getCriteria') 
    156                                ->will($this->returnValue(array())); 
    157         $floatFilter = new stubFloatFilter($this->mockRequestValueErrorFactory, null, $this->mockValidatorMax); 
    158         $this->mockRequestValueErrorFactory->expects($this->once()) 
    159                                            ->method('create') 
    160                                            ->with($this->equalTo('VALUE_TOO_GREAT')) 
    161                                            ->will($this->returnValue(new stubRequestValueError('foo', array('en_EN' => 'Something wrent wrong.')))); 
    162         $floatFilter->execute(11); 
    16365    } 
    16466 
     
    17173    { 
    17274        stubRegistry::setConfig(stubFloatFilter::DECIMALS_REGISTRY_KEY, 2); 
    173         $floatFilter = new stubFloatFilter($this->mockRequestValueErrorFactory); 
     75        $floatFilter = new stubFloatFilter(); 
    17476        $this->assertEquals(156, $floatFilter->execute('1.564')); 
    17577    } 
     
    18385    { 
    18486        stubRegistry::setConfig(stubFloatFilter::DECIMALS_REGISTRY_KEY, 0); 
    185         $floatFilter = new stubFloatFilter($this->mockRequestValueErrorFactory); 
     87        $floatFilter = new stubFloatFilter(); 
    18688        $this->assertEquals(1.564, $floatFilter->execute('1.564')); 
    18789    } 
     
    19597    { 
    19698        stubRegistry::removeConfig(stubFloatFilter::DECIMALS_REGISTRY_KEY); 
    197         $floatFilter = new stubFloatFilter($this->mockRequestValueErrorFactory); 
     99        $floatFilter = new stubFloatFilter(); 
    198100        $this->assertEquals(1.564, $floatFilter->execute('1.564')); 
    199101    } 
  • trunk/src/test/php/net/stubbles/ipo/request/filters/stubIntegerFilterTestCase.php

    r1323 r1329  
    1717{ 
    1818    /** 
    19      * a mock to be used for the rveFactory 
    20      * 
    21      * @var  PHPUnit_Framework_MockObject_MockObject 
    22      */ 
    23     protected $mockRequestValueErrorFactory; 
    24     /** 
    25      * a mock to be used for the minimum validator 
    26      * 
    27      * @var  PHPUnit_Framework_MockObject_MockObject 
    28      */ 
    29     protected $mockValidatorMin; 
    30     /** 
    31      * a mock to be used for the maximum validator 
    32      * 
    33      * @var  PHPUnit_Framework_MockObject_MockObject 
    34      */ 
    35     protected $mockValidatorMax; 
    36      
    37     /** 
    38      * create test environment 
    39      * 
    40      */ 
    41     public function setUp() 
    42     { 
    43         $this->mockRequestValueErrorFactory = $this->getMock('stubRequestValueErrorFactory'); 
    44         $this->mockValidatorMin             = $this->getMock('stubValidator'); 
    45         $this->mockValidatorMax             = $this->getMock('stubValidator'); 
    46     } 
    47      
    48     /** 
    4919     * assure that values are returned the expected way 
    5020     * 
     
    5323    public function value() 
    5424    { 
    55         $integerFilter = new stubIntegerFilter($this->mockRequestValueErrorFactory); 
     25        $integerFilter = new stubIntegerFilter(); 
    5626        $this->assertEquals(8, $integerFilter->execute(8)); 
    5727    } 
    5828 
    5929    /** 
    60      * assure that 0 or 1 is returned when value not set or empty when no value 
     30     * assure that 0 is returned when value not set or empty when no value 
    6131     * is required 
    6232     * 
    6333     * @test 
    6434     */ 
    65     public function unsetWhenNotRequired() 
     35    public function unsetOrOtherValues() 
    6636    { 
    67         $integerFilter = new stubIntegerFilter($this->mockRequestValueErrorFactory); 
     37        $integerFilter = new stubIntegerFilter(); 
    6838        $this->assertEquals(0, $integerFilter->execute(null)); 
    6939        $this->assertEquals(0, $integerFilter->execute('')); 
    7040        $this->assertEquals(1, $integerFilter->execute(true)); 
    7141        $this->assertEquals(0, $integerFilter->execute(false)); 
    72     } 
    73  
    74     /** 
    75      * assure that an FilterException is thrown when value smaller then $min 
    76      * 
    77      * @test 
    78      */ 
    79     public function withMinValidator() 
    80     { 
    81         $this->mockValidatorMin->expects($this->once()) 
    82                                ->method('validate') 
    83                                ->will($this->returnValue(true)); 
    84         $this->mockValidatorMin->expects($this->never()) 
    85                                ->method('getCriteria'); 
    86         $integerFilter = new stubIntegerFilter($this->mockRequestValueErrorFactory, $this->mockValidatorMin); 
    87         $this->assertEquals(-10, $integerFilter->execute(-10)); 
    88     } 
    89  
    90     /** 
    91      * assure that an FilterException is thrown when value smaller then $min 
    92      * 
    93      * @test 
    94      * @expectedException  stubFilterException 
    95      */ 
    96     public function withMinValidatorFails() 
    97     { 
    98         $this->mockValidatorMin->expects($this->once()) 
    99                                ->method('validate') 
    100                                ->will($this->returnValue(false)); 
    101         $this->mockValidatorMin->expects($this->once()) 
    102                                ->method('getCriteria') 
    103                                ->will($this->returnValue(array())); 
    104         $integerFilter = new stubIntegerFilter($this->mockRequestValueErrorFactory, $this->mockValidatorMin); 
    105         $this->mockRequestValueErrorFactory->expects($this->once()) 
    106                                            ->method('create') 
    107                                            ->with($this->equalTo('VALUE_TOO_SMALL')) 
    108                                            ->will($this->returnValue(new stubRequestValueError('foo', array('en_EN' => 'Something wrent wrong.')))); 
    109         $integerFilter->execute(-11); 
    110     } 
    111  
    112     /** 
    113      * assure that an FilterException is thrown when value greater then $max 
    114      * 
    115      * @test 
    116      */ 
    117     public function withMaxValidator() 
    118     { 
    119         $this->mockValidatorMax->expects($this->once()) 
    120                                ->method('validate') 
    121                                ->will($this->returnValue(true)); 
    122         $this->mockValidatorMax->expects($this->never()) 
    123                                ->method('getCriteria'); 
    124         $integerFilter = new stubIntegerFilter($this->mockRequestValueErrorFactory, null, $this->mockValidatorMax); 
    125         $this->assertEquals(10, $integerFilter->execute(10)); 
    126     } 
    127  
    128     /** 
    129      * assure that an FilterException is thrown when value greater then $max 
    130      * 
    131      * @test 
    132      * @expectedException  stubFilterException 
    133      */ 
    134     public function withMaxValidatorFails() 
    135     { 
    136         $this->mockValidatorMax->expects($this->once()) 
    137                                ->method('validate') 
    138                                ->will($this->returnValue(false)); 
    139         $this->mockValidatorMax->expects($this->once()) 
    140                                ->method('getCriteria') 
    141                                ->will($this->returnValue(array())); 
    142         $integerFilter = new stubIntegerFilter($this->mockRequestValueErrorFactory, null, $this->mockValidatorMax); 
    143         $this->mockRequestValueErrorFactory->expects($this->once()) 
    144                                            ->method('create') 
    145                                            ->with($this->equalTo('VALUE_TOO_GREAT')) 
    146                                            ->will($this->returnValue(new stubRequestValueError('foo', array('en_EN' => 'Something wrent wrong.')))); 
    147         $integerFilter->execute(11); 
    14842    } 
    14943 
     
    15549    public function float() 
    15650    { 
    157         $integerFilter = new stubIntegerFilter($this->mockRequestValueErrorFactory); 
     51        $integerFilter = new stubIntegerFilter(); 
    15852        $this->assertEquals(1, $integerFilter->execute(1.564)); 
    15953    } 
  • trunk/src/test/php/net/stubbles/ipo/request/filters/stubRangeFilterDecoratorTestCase.php

    r1323 r1329  
    11<?php 
    22/** 
    3  * Tests for net::stubbles::ipo::request::filters::stubNumberFilter. 
     3 * Tests for net::stubbles::ipo::request::filters::stubRangeFilterDecorator. 
    44 * 
    55 * @author      Frank Kleine <mikey@stubbles.net> 
     
    77 * @subpackage  ipo_request_filters_test 
    88 */ 
    9 stubClassLoader::load('net::stubbles::ipo::request::filters::stubNumberFilter'); 
     9stubClassLoader::load('net::stubbles::ipo::request::filters::stubRangeFilterDecorator'); 
    1010/** 
    11  * Tests for net::stubbles::ipo::request::filters::stubNumberFilter
     11 * Helper class for the test
    1212 * 
    1313 * @package     stubbles 
    1414 * @subpackage  ipo_request_filters_test 
    1515 */ 
    16 class stubNumberFilterTestCase extends PHPUnit_Framework_TestCase 
     16class TeststubRangeFilterDecorator extends stubRangeFilterDecorator 
     17
     18    /** 
     19     * helper method for direct access to protected doExecute() 
     20     * 
     21     * @param   mixed  $value 
     22     * @return  mixed 
     23     */ 
     24    public function callDoExecute($value) 
     25    { 
     26        return $this->doExecute($value); 
     27    } 
     28
     29/** 
     30 * Tests for net::stubbles::ipo::request::filters::stubRangeFilterDecorator. 
     31 * 
     32 * @package     stubbles 
     33 * @subpackage  ipo_request_filters_test 
     34 */ 
     35class stubRangeFilterDecoratorTestCase extends PHPUnit_Framework_TestCase 
    1736{ 
    1837    /** 
     
    3453     */ 
    3554    protected $mockValidatorMax; 
     55    /** 
     56     * instance to test 
     57     * 
     58     * @var  TeststubRangeFilterDecorator 
     59     */ 
     60    protected $rangeFilterDecorator; 
    3661     
    3762    /** 
     
    4368        $this->mockValidatorMin             = $this->getMock('stubValidator'); 
    4469        $this->mockValidatorMax             = $this->getMock('stubValidator'); 
     70        $this->rangeFilterDecorator         = new TeststubRangeFilterDecorator($this->getMock('stubFilter'), $this->mockRequestValueErrorFactory); 
    4571    } 
    4672 
    4773    /** 
    48      * assure that values are returned the expected way 
     74     * no validator does not do any harm 
    4975     * 
    5076     * @test 
    5177     */ 
    52     public function value() 
     78    public function noValidatorReturnsValueAsIs() 
    5379    { 
    54         $numberFilter = new stubNumberFilter($this->mockRequestValueErrorFactory); 
    55         $this->assertEquals(8.4533, $numberFilter->execute('8.4533')); 
    56         $this->assertEquals(8.4538, $numberFilter->execute('8.4538')); 
    57         $this->assertEquals(8.45, $numberFilter->execute('8.45')); 
    58         $this->assertEquals(8, $numberFilter->execute('8')); 
    59         $this->assertEquals(8.4533, $numberFilter->execute(8.4533)); 
    60         $this->assertEquals(8.4538, $numberFilter->execute(8.4538)); 
    61         $this->assertEquals(8.45, $numberFilter->execute(8.45)); 
    62         $this->assertEquals(8, $numberFilter->execute(8)); 
    63         $this->assertNull($numberFilter->getMinValidator()); 
    64         $this->assertNull($numberFilter->getMaxValidator()); 
    65     } 
    66  
    67     /** 
    68      * assure that 0 is returned when value not set or empty when no value 
    69      * is required 
    70      * 
    71      * @test 
    72      */ 
    73     public function unsetOrOtherValues() 
    74     { 
    75         $numberFilter = new stubNumberFilter($this->mockRequestValueErrorFactory); 
    76         $this->assertEquals(0, $numberFilter->execute(null)); 
    77         $this->assertEquals(0, $numberFilter->execute('')); 
    78         $this->assertEquals(1, $numberFilter->execute(true)); 
    79         $this->assertEquals(0, $numberFilter->execute(false)); 
     80        $this->assertNull($this->rangeFilterDecorator->callDoExecute(null)); 
     81        $this->assertEquals('', $this->rangeFilterDecorator->callDoExecute('')); 
     82        $this->assertEquals(true, $this->rangeFilterDecorator->callDoExecute(true)); 
     83        $this->assertEquals(false, $this->rangeFilterDecorator->callDoExecute(false)); 
     84        $this->assertEquals(313, $this->rangeFilterDecorator->callDoExecute(313)); 
    8085    } 
    8186 
     
    9297        $this->mockValidatorMin->expects($this->never()) 
    9398                               ->method('getCriteria'); 
    94         $numberFilter = new stubNumberFilter($this->mockRequestValueErrorFactory, $this->mockValidatorMin); 
    95         $this->assertSame($this->mockValidatorMin, $numberFilter->getMinValidator()); 
    96         $this->assertEquals(-10, $numberFilter->execute(-10)); 
     99        $this->rangeFilterDecorator->setMinValidator($this->mockValidatorMin); 
     100        $this->assertSame($this->mockValidatorMin, $this->rangeFilterDecorator->getMinValidator()); 
     101        $this->assertEquals(-10, $this->rangeFilterDecorator->callDoExecute(-10)); 
    97102    } 
    98103 
     
    111116                               ->method('getCriteria') 
    112117                               ->will($this->returnValue(array())); 
    113         $numberFilter = new stubNumberFilter($this->mockRequestValueErrorFactory, $this->mockValidatorMin); 
     118        $this->rangeFilterDecorator->setMinValidator($this->mockValidatorMin); 
    114119        $this->mockRequestValueErrorFactory->expects($this->once()) 
    115120                                           ->method('create') 
    116121                                           ->with($this->equalTo('VALUE_TOO_SMALL')) 
    117122                                           ->will($this->returnValue(new stubRequestValueError('foo', array('en_EN' => 'Something wrent wrong.')))); 
    118         $numberFilter->execute(-11); 
     123        $this->rangeFilterDecorator->callDoExecute(-11); 
    119124    } 
    120125 
     
    131136        $this->mockValidatorMax->expects($this->never()) 
    132137                               ->method('getCriteria'); 
    133         $numberFilter = new stubNumberFilter($this->mockRequestValueErrorFactory, null, $this->mockValidatorMax); 
    134         $this->assertSame($this->mockValidatorMax, $numberFilter->getMaxValidator()); 
    135         $this->assertEquals(10, $numberFilter->execute(10)); 
     138        $this->rangeFilterDecorator->setMaxValidator($this->mockValidatorMax); 
     139        $this->assertSame($this->mockValidatorMax, $this->rangeFilterDecorator->getMaxValidator()); 
     140        $this->assertEquals(10, $this->rangeFilterDecorator->callDoExecute(10)); 
    136141    } 
    137142 
     
    150155                               ->method('getCriteria') 
    151156                               ->will($this->returnValue(array())); 
    152         $numberFilter = new stubNumberFilter($this->mockRequestValueErrorFactory, null, $this->mockValidatorMax); 
     157        $this->rangeFilterDecorator->setMaxValidator($this->mockValidatorMax); 
    153158        $this->mockRequestValueErrorFactory->expects($this->once()) 
    154159                                           ->method('create') 
    155160                                           ->with($this->equalTo('VALUE_TOO_GREAT')) 
    156161                                           ->will($this->returnValue(new stubRequestValueError('foo', array('en_EN' => 'Something wrent wrong.')))); 
    157         $numberFilter->execute(11); 
     162        $this->rangeFilterDecorator->callDoExecute(11); 
    158163    } 
    159164}