Changeset 1329
- Timestamp:
- 02/03/08 23:36:00 (7 months ago)
- Files:
-
- trunk/src/main/php/net/stubbles/ipo/request/broker/annotations/stubFloatFilterAnnotation.php (modified) (2 diffs)
- trunk/src/main/php/net/stubbles/ipo/request/broker/annotations/stubIntegerFilterAnnotation.php (modified) (2 diffs)
- trunk/src/main/php/net/stubbles/ipo/request/filters/stubFloatFilter.php (modified) (3 diffs)
- trunk/src/main/php/net/stubbles/ipo/request/filters/stubIntegerFilter.php (modified) (1 diff)
- trunk/src/main/php/net/stubbles/ipo/request/filters/stubRangeFilterDecorator.php (moved) (moved from trunk/src/main/php/net/stubbles/ipo/request/filters/stubNumberFilter.php) (5 diffs)
- trunk/src/test/php/net/stubbles/ipo/IPOTestSuite.php (modified) (1 diff)
- trunk/src/test/php/net/stubbles/ipo/request/broker/annotations/stubFloatFilterAnnotationTestCase.php (modified) (2 diffs)
- trunk/src/test/php/net/stubbles/ipo/request/broker/annotations/stubIntegerFilterAnnotationTestCase.php (modified) (2 diffs)
- trunk/src/test/php/net/stubbles/ipo/request/filters/stubFloatFilterTestCase.php (modified) (7 diffs)
- trunk/src/test/php/net/stubbles/ipo/request/filters/stubIntegerFilterTestCase.php (modified) (3 diffs)
- trunk/src/test/php/net/stubbles/ipo/request/filters/stubRangeFilterDecoratorTestCase.php (moved) (moved from trunk/src/test/php/net/stubbles/ipo/request/filters/stubNumberFilterTestCase.php) (8 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/main/php/net/stubbles/ipo/request/broker/annotations/stubFloatFilterAnnotation.php
r1223 r1329 9 9 stubClassLoader::load('net::stubbles::reflection::annotations::stubAnnotation', 10 10 'net::stubbles::ipo::request::filters::stubFloatFilter', 11 'net::stubbles::ipo::request::filters::stubRangeFilterDecorator', 11 12 'net::stubbles::ipo::request::broker::annotations::stubAbstractFilterAnnotation', 12 13 'net::stubbles::util::validators::stubMaxNumberValidator', … … 57 58 * returns the filter defined by the annotation 58 59 * 59 * @return stubFloatFilter 60 * @throws stubRequestBrokerException 60 * @return stubFilter 61 61 */ 62 62 protected function doGetFilter() 63 63 { 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; 68 77 } 69 78 } trunk/src/main/php/net/stubbles/ipo/request/broker/annotations/stubIntegerFilterAnnotation.php
r1223 r1329 9 9 stubClassLoader::load('net::stubbles::reflection::annotations::stubAnnotation', 10 10 'net::stubbles::ipo::request::filters::stubIntegerFilter', 11 'net::stubbles::ipo::request::filters::stubRangeFilterDecorator', 11 12 'net::stubbles::ipo::request::broker::annotations::stubAbstractFilterAnnotation', 12 13 'net::stubbles::util::validators::stubMaxNumberValidator', … … 57 58 * returns the filter defined by the annotation 58 59 * 59 * @return stubIntegerFilter 60 * @throws stubRequestBrokerException 60 * @return stubFilter 61 61 */ 62 62 protected function doGetFilter() 63 63 { 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; 68 77 } 69 78 } trunk/src/main/php/net/stubbles/ipo/request/filters/stubFloatFilter.php
r1328 r1329 7 7 * @subpackage ipo_request_filters 8 8 */ 9 stubClassLoader::load('net::stubbles::ipo::request::filters::stub NumberFilter',9 stubClassLoader::load('net::stubbles::ipo::request::filters::stubFilter', 10 10 'net::stubbles::util::stubRegistry' 11 11 ); … … 13 13 * Filters on request variables of type double / float. 14 14 * 15 * This filter takes any value , casts it to float and checks if it complies16 * with the min and/or the max validator. Afterwards its multiplied with 10^x17 * (x is configureable via the registry) to get an integer value that can be18 * 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. 19 19 * 20 20 * @package stubbles 21 21 * @subpackage ipo_request_filters 22 22 */ 23 class stubFloatFilter extends stub NumberFilter23 class stubFloatFilter extends stubBaseObject implements stubFilter 24 24 { 25 25 /** … … 31 31 * checks if given value is double, transfers into int with $decimalPlaces 32 32 * 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 36 35 */ 37 36 function execute($value) 38 37 { 39 38 settype($value, 'float'); 40 $value = parent::doExecute($value);41 39 $decimals = stubRegistry::getConfig(self::DECIMALS_REGISTRY_KEY); 42 40 if (null == $decimals) { trunk/src/main/php/net/stubbles/ipo/request/filters/stubIntegerFilter.php
r1328 r1329 7 7 * @subpackage ipo_request_filters 8 8 */ 9 stubClassLoader::load('net::stubbles::ipo::request::filters::stub NumberFilter');9 stubClassLoader::load('net::stubbles::ipo::request::filters::stubFilter'); 10 10 /** 11 11 * Basic class for filters on request variables of type integer. 12 12 * 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. 15 14 * 16 15 * @package stubbles 17 16 * @subpackage ipo_request_filters 18 17 */ 19 class stubIntegerFilter extends stub NumberFilter18 class stubIntegerFilter extends stubBaseObject implements stubFilter 20 19 { 21 20 /** 22 21 * checks if given value is an integer 23 22 * 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 27 25 */ 28 26 function execute($value) 29 27 { 30 28 settype($value, 'integer'); 31 return parent::doExecute($value);29 return $value; 32 30 } 33 31 } trunk/src/main/php/net/stubbles/ipo/request/filters/stubRangeFilterDecorator.php
r1323 r1329 8 8 */ 9 9 stubClassLoader::load('net::stubbles::ipo::request::stubRequestValueErrorFactory', 10 'net::stubbles::ipo::request::filters::stub Filter',10 'net::stubbles::ipo::request::filters::stubStrategyFilterDecorator', 11 11 'net::stubbles::util::validators::stubValidator' 12 12 ); … … 20 20 * @subpackage ipo_request_filters 21 21 */ 22 class stub NumberFilter extends stubBaseObject implements stubFilter22 class stubRangeFilterDecorator extends stubStrategyFilterDecorator 23 23 { 24 24 /** … … 44 44 * constructor 45 45 * 46 * @param stubFilter $filter decorated filter 46 47 * @param stubRequestValueErrorFactory $rveFactory factory to create RequestValueErrors 47 * @param stubValidator $min validator for minimum values48 * @param stubValidator $max validator for maximum values49 48 */ 50 public function __construct(stub RequestValueErrorFactory $rveFactory, stubValidator $min = null, stubValidator $max = null)49 public function __construct(stubFilter $filter, stubRequestValueErrorFactory $rveFactory) 51 50 { 51 $this->setDecoratedFilter($filter); 52 52 $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; 55 63 } 56 64 … … 66 74 67 75 /** 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 /** 68 86 * returns the validator for maximum values 69 87 * … … 73 91 { 74 92 return $this->maxValidator; 75 }76 77 /**78 * checks if given value exceeds borders79 *80 * @param numeric $value value to filter81 * @return numeric filtered value82 * @throws stubFilterException in case $value has errors83 */84 public function execute($value)85 {86 settype($value, 'float');87 return $this->doExecute($value);88 93 } 89 94 trunk/src/test/php/net/stubbles/ipo/IPOTestSuite.php
r1327 r1329 50 50 $suite->addTestFile($dir . '/request/filters/stubLengthFilterDecoratorTestCase.php'); 51 51 $suite->addTestFile($dir . '/request/filters/stubMailFilterTestCase.php'); 52 $suite->addTestFile($dir . '/request/filters/stubNumberFilterTestCase.php');53 52 $suite->addTestFile($dir . '/request/filters/stubPassThruFilterTestCase.php'); 54 53 $suite->addTestFile($dir . '/request/filters/stubPasswordFilterTestCase.php'); 54 $suite->addTestFile($dir . '/request/filters/stubRangeFilterDecoratorTestCase.php'); 55 55 $suite->addTestFile($dir . '/request/filters/stubRegexFilterDecoratorTestCase.php'); 56 56 $suite->addTestFile($dir . '/request/filters/stubRequiredFilterDecoratorTestCase.php'); trunk/src/test/php/net/stubbles/ipo/request/broker/annotations/stubFloatFilterAnnotationTestCase.php
r1323 r1329 39 39 public function instance() 40 40 { 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()); 45 42 } 46 43 … … 50 47 * @test 51 48 */ 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() 53 82 { 54 83 $this->floatFilterAnnotation->setMinValue(1); 55 84 $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()); 62 92 } 63 93 } trunk/src/test/php/net/stubbles/ipo/request/broker/annotations/stubIntegerFilterAnnotationTestCase.php
r1323 r1329 39 39 public function instance() 40 40 { 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()); 45 42 } 46 43 … … 50 47 * @test 51 48 */ 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() 53 82 { 54 83 $this->integerFilterAnnotation->setMinValue(1); 55 84 $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()); 62 92 } 63 93 } trunk/src/test/php/net/stubbles/ipo/request/filters/stubFloatFilterTestCase.php
r1323 r1329 17 17 { 18 18 /** 19 * a mock to be used for the rveFactory20 *21 * @var PHPUnit_Framework_MockObject_MockObject22 */23 protected $mockRequestValueErrorFactory;24 /**25 * a mock to be used for the minimum validator26 *27 * @var PHPUnit_Framework_MockObject_MockObject28 */29 protected $mockValidatorMin;30 /**31 * a mock to be used for the maximum validator32 *33 * @var PHPUnit_Framework_MockObject_MockObject34 */35 protected $mockValidatorMax;36 37 /**38 19 * set up test environment 39 20 */ … … 41 22 { 42 23 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');46 24 } 47 25 … … 61 39 public function value() 62 40 { 63 $floatFilter = new stubFloatFilter( $this->mockRequestValueErrorFactory);41 $floatFilter = new stubFloatFilter(); 64 42 $this->assertEquals(8453, $floatFilter->execute('8.4533')); 65 43 $this->assertEquals(8453, $floatFilter->execute('8.4538')); … … 80 58 public function unsetOrOtherValues() 81 59 { 82 $floatFilter = new stubFloatFilter( $this->mockRequestValueErrorFactory);60 $floatFilter = new stubFloatFilter(); 83 61 $this->assertEquals(0, $floatFilter->execute(null)); 84 62 $this->assertEquals(0, $floatFilter->execute('')); 85 63 $this->assertEquals(1000, $floatFilter->execute(true)); 86 64 $this->assertEquals(0, $floatFilter->execute(false)); 87 }88 89 /**90 * assure that an FilterException is thrown when value smaller then $min91 *92 * @test93 */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 $min107 *108 * @test109 * @expectedException stubFilterException110 */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 $max129 *130 * @test131 */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 $max145 *146 * @test147 * @expectedException stubFilterException148 */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);163 65 } 164 66 … … 171 73 { 172 74 stubRegistry::setConfig(stubFloatFilter::DECIMALS_REGISTRY_KEY, 2); 173 $floatFilter = new stubFloatFilter( $this->mockRequestValueErrorFactory);75 $floatFilter = new stubFloatFilter(); 174 76 $this->assertEquals(156, $floatFilter->execute('1.564')); 175 77 } … … 183 85 { 184 86 stubRegistry::setConfig(stubFloatFilter::DECIMALS_REGISTRY_KEY, 0); 185 $floatFilter = new stubFloatFilter( $this->mockRequestValueErrorFactory);87 $floatFilter = new stubFloatFilter(); 186 88 $this->assertEquals(1.564, $floatFilter->execute('1.564')); 187 89 } … … 195 97 { 196 98 stubRegistry::removeConfig(stubFloatFilter::DECIMALS_REGISTRY_KEY); 197 $floatFilter = new stubFloatFilter( $this->mockRequestValueErrorFactory);99 $floatFilter = new stubFloatFilter(); 198 100 $this->assertEquals(1.564, $floatFilter->execute('1.564')); 199 101 } trunk/src/test/php/net/stubbles/ipo/request/filters/stubIntegerFilterTestCase.php
r1323 r1329 17 17 { 18 18 /** 19 * a mock to be used for the rveFactory20 *21 * @var PHPUnit_Framework_MockObject_MockObject22 */23 protected $mockRequestValueErrorFactory;24 /**25 * a mock to be used for the minimum validator26 *27 * @var PHPUnit_Framework_MockObject_MockObject28 */29 protected $mockValidatorMin;30 /**31 * a mock to be used for the maximum validator32 *33 * @var PHPUnit_Framework_MockObject_MockObject34 */35 protected $mockValidatorMax;36 37 /**38 * create test environment39 *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 /**49 19 * assure that values are returned the expected way 50 20 * … … 53 23 public function value() 54 24 { 55 $integerFilter = new stubIntegerFilter( $this->mockRequestValueErrorFactory);25 $integerFilter = new stubIntegerFilter(); 56 26 $this->assertEquals(8, $integerFilter->execute(8)); 57 27 } 58 28 59 29 /** 60 * assure that 0 or 1is returned when value not set or empty when no value30 * assure that 0 is returned when value not set or empty when no value 61 31 * is required 62 32 * 63 33 * @test 64 34 */ 65 public function unset WhenNotRequired()35 public function unsetOrOtherValues() 66 36 { 67 $integerFilter = new stubIntegerFilter( $this->mockRequestValueErrorFactory);37 $integerFilter = new stubIntegerFilter(); 68 38 $this->assertEquals(0, $integerFilter->execute(null)); 69 39 $this->assertEquals(0, $integerFilter->execute('')); 70 40 $this->assertEquals(1, $integerFilter->execute(true)); 71 41 $this->assertEquals(0, $integerFilter->execute(false)); 72 }73 74 /**75 * assure that an FilterException is thrown when value smaller then $min76 *77 * @test78 */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 $min92 *93 * @test94 * @expectedException stubFilterException95 */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 $max114 *115 * @test116 */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 $max130 *131 * @test132 * @expectedException stubFilterException133 */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);148 42 } 149 43 … … 155 49 public function float() 156 50 { 157 $integerFilter = new stubIntegerFilter( $this->mockRequestValueErrorFactory);51 $integerFilter = new stubIntegerFilter(); 158 52 $this->assertEquals(1, $integerFilter->execute(1.564)); 159 53 } trunk/src/test/php/net/stubbles/ipo/request/filters/stubRangeFilterDecoratorTestCase.php
r1323 r1329 1 1 <?php 2 2 /** 3 * Tests for net::stubbles::ipo::request::filters::stub NumberFilter.3 * Tests for net::stubbles::ipo::request::filters::stubRangeFilterDecorator. 4 4 * 5 5 * @author Frank Kleine <mikey@stubbles.net> … … 7 7 * @subpackage ipo_request_filters_test 8 8 */ 9 stubClassLoader::load('net::stubbles::ipo::request::filters::stub NumberFilter');9 stubClassLoader::load('net::stubbles::ipo::request::filters::stubRangeFilterDecorator'); 10 10 /** 11 * Tests for net::stubbles::ipo::request::filters::stubNumberFilter.11 * Helper class for the test. 12 12 * 13 13 * @package stubbles 14 14 * @subpackage ipo_request_filters_test 15 15 */ 16 class stubNumberFilterTestCase extends PHPUnit_Framework_TestCase 16 class 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 */ 35 class stubRangeFilterDecoratorTestCase extends PHPUnit_Framework_TestCase 17 36 { 18 37 /** … … 34 53 */ 35 54 protected $mockValidatorMax; 55 /** 56 * instance to test 57 * 58 * @var TeststubRangeFilterDecorator 59 */ 60 protected $rangeFilterDecorator; 36 61 37 62 /** … … 43 68 $this->mockValidatorMin = $this->getMock('stubValidator'); 44 69 $this->mockValidatorMax = $this->getMock('stubValidator'); 70 $this->rangeFilterDecorator = new TeststubRangeFilterDecorator($this->getMock('stubFilter'), $this->mockRequestValueErrorFactory); 45 71 } 46 72 47 73 /** 48 * assure that values are returned the expected way74 * no validator does not do any harm 49 75 * 50 76 * @test 51 77 */ 52 public function value()78 public function noValidatorReturnsValueAsIs() 53 79 { 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)); 80 85 } 81 86 … … 92 97 $this->mockValidatorMin->expects($this->never()) 93 98 ->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)); 97 102 } 98 103 … … 111 116 ->method('getCriteria') 112 117 ->will($this->returnValue(array())); 113 $ numberFilter = new stubNumberFilter($this->mockRequestValueErrorFactory,$this->mockValidatorMin);118 $this->rangeFilterDecorator->setMinValidator($this->mockValidatorMin); 114 119 $this->mockRequestValueErrorFactory->expects($this->once()) 115 120 ->method('create') 116 121 ->with($this->equalTo('VALUE_TOO_SMALL')) 117 122 ->will($this->returnValue(new stubRequestValueError('foo', array('en_EN' => 'Something wrent wrong.')))); 118 $ numberFilter->execute(-11);123 $this->rangeFilterDecorator->callDoExecute(-11); 119 124 } 120 125 … … 131 136 $this->mockValidatorMax->expects($this->never()) 132 137 ->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)); 136 141 } 137 142 … … 150 155 ->method('getCriteria') 151 156 ->will($this->returnValue(array())); 152 $ numberFilter = new stubNumberFilter($this->mockRequestValueErrorFactory, null,$this->mockValidatorMax);157 $this->rangeFilterDecorator->setMaxValidator($this->mockValidatorMax); 153 158 $this->mockRequestValueErrorFactory->expects($this->once()) 154 159 ->method('create') 155 160 ->with($this->equalTo('VALUE_TOO_GREAT')) 156 161 ->will($this->returnValue(new stubRequestValueError('foo', array('en_EN' => 'Something wrent wrong.')))); 157 $ numberFilter->execute(11);162 $this->rangeFilterDecorator->callDoExecute(11); 158 163 } 159 164 }
