Changeset 1274
- Timestamp:
- 01/22/08 12:04:12 (1 year ago)
- Files:
-
- trunk/src/main/php/net/stubbles/ipo/request/filters/stubFloatFilter.php (modified) (2 diffs)
- trunk/src/main/php/net/stubbles/ipo/request/filters/stubRegexFilterDecorator.php (modified) (8 diffs)
- trunk/src/test/php/net/stubbles/ipo/IPOTestSuite.php (modified) (1 diff)
- trunk/src/test/php/net/stubbles/ipo/request/filters/stubFloatFilterTestCase.php (modified) (8 diffs)
- trunk/src/test/php/net/stubbles/ipo/request/filters/stubHTTPURLFilterTestCase.php (modified) (8 diffs)
- trunk/src/test/php/net/stubbles/ipo/request/filters/stubIntegerFilterTestCase.php (modified) (8 diffs)
- trunk/src/test/php/net/stubbles/ipo/request/filters/stubMD5FilterTestCase.php (modified) (2 diffs)
- trunk/src/test/php/net/stubbles/ipo/request/filters/stubMailFilterTestCase.php (modified) (4 diffs)
- trunk/src/test/php/net/stubbles/ipo/request/filters/stubPassThruFilterTestCase.php (modified) (5 diffs)
- trunk/src/test/php/net/stubbles/ipo/request/filters/stubPasswordFilterTestCase.php (modified) (9 diffs)
- trunk/src/test/php/net/stubbles/ipo/request/filters/stubRegexFilterDecoratorTestCase.php (modified) (5 diffs)
- trunk/src/test/php/net/stubbles/ipo/request/filters/stubStringFilterTestCase.php (modified) (9 diffs)
- trunk/src/test/php/net/stubbles/ipo/request/filters/stubTextFilterTestCase.php (modified) (8 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/main/php/net/stubbles/ipo/request/filters/stubFloatFilter.php
r1223 r1274 25 25 { 26 26 /** 27 * registry key under which the amount of decimals is stored 28 */ 29 const DECIMALS_REGISTRY_KEY = 'net.stubbles.number.decimals'; 30 31 /** 27 32 * constructor 28 33 * … … 49 54 } 50 55 51 $value = parent::doExecute($value); 52 return (int) ($value * pow(10, stubRegistry::getConfig('net.stubbles.number.decimals'))); 56 $value = parent::doExecute($value); 57 $decimals = stubRegistry::getConfig(self::DECIMALS_REGISTRY_KEY); 58 if (null == $decimals) { 59 return $value; 60 } 61 62 return (int) ($value * pow(10, $decimals)); 53 63 } 54 64 } trunk/src/main/php/net/stubbles/ipo/request/filters/stubRegexFilterDecorator.php
r1223 r1274 8 8 * @subpackage ipo_request_filters 9 9 */ 10 stubClassLoader::load('net::stubbles::ipo::request::filters::stubAbstractFilterDecorator'); 10 stubClassLoader::load('net::stubbles::ipo::request::filters::stubAbstractFilterDecorator', 11 'net::stubbles::lang::exceptions::stubIllegalArgumentException', 12 'net::stubbles::lang::exceptions::stubIllegalStateException' 13 ); 11 14 /** 12 15 * Decorator which adds regex filtering (input value may be changed due to regex) … … 26 29 */ 27 30 const STRATEGY_BEFORE = -1; 28 29 31 /** 30 32 * indicates the decorator behavior: … … 35 37 */ 36 38 const STRATEGY_AFTER = 1; 37 38 39 /** 39 40 * Decorator Strategy: … … 46 47 */ 47 48 protected $strategy; 48 49 49 /** 50 50 * regex … … 57 57 * constructor 58 58 * 59 * @param stubFilter $filter decorated filter 60 * @param string $regex regex to apply 61 * @param int $strategy decrorator strategy 59 * @param stubFilter $filter decorated filter 60 * @param string $regex regex to apply 61 * @param int $strategy decrorator strategy 62 * @throws stubIllegalArgumentException 62 63 */ 63 64 function __construct(stubFilter $filter, $regex, $strategy) 64 65 { 66 if (in_array($strategy, array(self::STRATEGY_BEFORE, self::STRATEGY_AFTER)) === false) { 67 throw new stubIllegalArgumentException('Invalid strategy type ' . $strategy); 68 } 69 65 70 $this->setDecoratedFilter($filter); 66 71 $this->strategy = $strategy; … … 73 78 * @param string $value 74 79 * @throws stubRuntimeException 75 * @throws stubIllegal ArgumentException80 * @throws stubIllegalStateException 76 81 */ 77 82 public function execute($value) 78 83 { 79 if ($value != null) { 84 if ($value != null) { 85 $result = null; 80 86 switch ($this->strategy) { 81 87 case self::STRATEGY_BEFORE: 82 $matchAmount = @preg_match($this->regex, $value, $matches); 83 if ($matchAmount === false) { 88 if (@preg_match($this->regex, $value, $matches) === false) { 84 89 throw new stubRuntimeException('Invalid regular expression ' . $this->regex); 85 } else {86 $result = ($matchAmount !== 0)? $this->getDecoratedFilter()->execute($matches[0]): null;90 } elseif (isset($matches[0]) === true) { 91 $result = $this->getDecoratedFilter()->execute($matches[0]); 87 92 } 88 93 break; … … 90 95 case self::STRATEGY_AFTER: 91 96 $filtered = $this->getDecoratedFilter()->execute($value); 92 $matchAmount = @preg_match($this->regex, $filtered, $matches); 93 if ($matchAmount === false) { 97 if (@preg_match($this->regex, $filtered, $matches) === false) { 94 98 throw new stubRuntimeException('Invalid regular expression ' . $this->regex); 95 } else {99 } elseif (isset($matches[0]) === true) { 96 100 $result = $matches[0]; 97 101 } … … 99 103 100 104 default: 101 throw new stubIllegalArgumentException('Invalid strategy type' . $this->strategy); 102 break; 105 throw new stubIllegalStateException('Invalid strategy type ' . $this->strategy); 103 106 } 107 104 108 return $result; 105 109 } 110 106 111 return $value; 107 112 } trunk/src/test/php/net/stubbles/ipo/IPOTestSuite.php
r1273 r1274 41 41 #$suite->addTestFile($dir . '/request/broker/annotations/stubTextFilterAnnotationTestCase.php'); 42 42 43 #$suite->addTestFile($dir . '/request/filters/stubFloatFilterTestCase.php');44 #$suite->addTestFile($dir . '/request/filters/stubHTTPURLFilterTestCase.php');45 #$suite->addTestFile($dir . '/request/filters/stubIntegerFilterTestCase.php');46 #$suite->addTestFile($dir . '/request/filters/stubMailFilterTestCase.php');47 #$suite->addTestFile($dir . '/request/filters/stubMD5FilterTestCase.php');48 #$suite->addTestFile($dir . '/request/filters/stubPassThruFilterTestCase.php');49 #$suite->addTestFile($dir . '/request/filters/stubPasswordFilterTestCase.php');50 #$suite->addTestFile($dir . '/request/filters/stubRegexFilterDecoratorTestCase.php');51 #$suite->addTestFile($dir . '/request/filters/stubStringFilterTestCase.php');52 #$suite->addTestFile($dir . '/request/filters/stubTextFilterTestCase.php');43 $suite->addTestFile($dir . '/request/filters/stubFloatFilterTestCase.php'); 44 $suite->addTestFile($dir . '/request/filters/stubHTTPURLFilterTestCase.php'); 45 $suite->addTestFile($dir . '/request/filters/stubIntegerFilterTestCase.php'); 46 $suite->addTestFile($dir . '/request/filters/stubMailFilterTestCase.php'); 47 $suite->addTestFile($dir . '/request/filters/stubMD5FilterTestCase.php'); 48 $suite->addTestFile($dir . '/request/filters/stubPassThruFilterTestCase.php'); 49 $suite->addTestFile($dir . '/request/filters/stubPasswordFilterTestCase.php'); 50 $suite->addTestFile($dir . '/request/filters/stubRegexFilterDecoratorTestCase.php'); 51 $suite->addTestFile($dir . '/request/filters/stubStringFilterTestCase.php'); 52 $suite->addTestFile($dir . '/request/filters/stubTextFilterTestCase.php'); 53 53 54 54 $suite->addTestFile($dir . '/response/stubCookieTestCase.php'); trunk/src/test/php/net/stubbles/ipo/request/filters/stubFloatFilterTestCase.php
r1223 r1274 1 1 <?php 2 2 /** 3 * Tests for net::stubbles::ipo::request::filters::stub DoubleFilter.3 * Tests for net::stubbles::ipo::request::filters::stubFloatFilter. 4 4 * 5 * @author Frank Kleine < frank@kl-s.com>5 * @author Frank Kleine <mikey@stubbles.net> 6 6 * @package stubbles 7 7 * @subpackage ipo_request_filters_test 8 8 */ 9 9 stubClassLoader::load('net::stubbles::ipo::request::filters::stubFloatFilter'); 10 Mock::generate('stubRequestValueErrorFactory');11 Mock::generate('stubValidator');12 10 /** 13 * Tests for net::stubbles::ipo::request::filters::stub DoubleFilter.11 * Tests for net::stubbles::ipo::request::filters::stubFloatFilter. 14 12 * 15 13 * @package stubbles 16 14 * @subpackage ipo_request_filters_test 17 15 */ 18 class stubFloatFilterTestCase extends UnitTestCase16 class stubFloatFilterTestCase extends PHPUnit_Framework_TestCase 19 17 { 20 18 /** 21 19 * a mock to be used for the rveFactory 22 20 * 23 * @var stubRequestValueErrorFactory24 */ 25 protected $mock StubRequestValueErrorFactory;21 * @var PHPUnit_Framework_MockObject_MockObject 22 */ 23 protected $mockRequestValueErrorFactory; 26 24 /** 27 25 * a mock to be used for the minimum validator 28 26 * 29 * @var stubValidator30 */ 31 protected $mock StubValidatorMin;27 * @var PHPUnit_Framework_MockObject_MockObject 28 */ 29 protected $mockValidatorMin; 32 30 /** 33 31 * a mock to be used for the maximum validator 34 32 * 35 * @var stubValidator36 */ 37 protected $mock StubValidatorMax;33 * @var PHPUnit_Framework_MockObject_MockObject 34 */ 35 protected $mockValidatorMax; 38 36 39 37 /** … … 42 40 public function setUp() 43 41 { 44 stubRegistry::setConfig('net.stubbles.number.decimals', 3); 45 $this->mockStubRequestValueErrorFactory = new MockStubRequestValueErrorFactory(); 46 $this->mockStubValidatorMin = new MockStubValidator(); 47 $this->mockStubValidatorMax = new MockStubValidator(); 48 $this->mockStubRequestValueErrorFactory->setReturnValue('create', new stubRequestValueError('foo', array('en_EN' => 'Something wrent wrong.'))); 42 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'); 49 46 } 50 47 … … 54 51 public function tearDown() 55 52 { 56 stubRegistry::removeConfig( 'net.stubbles.number.decimals');53 stubRegistry::removeConfig(stubFloatFilter::DECIMALS_REGISTRY_KEY); 57 54 } 58 55 59 56 /** 60 57 * assure that values are returned the expected way 61 */ 62 public function testValue() 63 { 64 $floatFilter = new stubFloatFilter($this->mockStubRequestValueErrorFactory); 65 $this->assertEqual($floatFilter->execute('8.4533'), 8453); 66 $this->assertEqual($floatFilter->execute('8.4538'), 8453); 67 $this->assertEqual($floatFilter->execute('8.45'), 8450); 68 $this->assertEqual($floatFilter->execute('8'), 8000); 69 $this->assertEqual($floatFilter->execute(8.4533), 8453); 70 $this->assertEqual($floatFilter->execute(8.4538), 8453); 71 $this->assertEqual($floatFilter->execute(8.45), 8450); 72 $this->assertEqual($floatFilter->execute(8), 8000); 58 * 59 * @test 60 */ 61 public function value() 62 { 63 $floatFilter = new stubFloatFilter($this->mockRequestValueErrorFactory); 64 $this->assertEquals(8453, $floatFilter->execute('8.4533')); 65 $this->assertEquals(8453, $floatFilter->execute('8.4538')); 66 $this->assertEquals(8450, $floatFilter->execute('8.45')); 67 $this->assertEquals(8000, $floatFilter->execute('8')); 68 $this->assertEquals(8453, $floatFilter->execute(8.4533)); 69 $this->assertEquals(8453, $floatFilter->execute(8.4538)); 70 $this->assertEquals(8450, $floatFilter->execute(8.45)); 71 $this->assertEquals(8000, $floatFilter->execute(8)); 73 72 } 74 73 … … 76 75 * assure that an exceptiom is thrown when a value is 77 76 * required but not passed 78 */ 79 public function testWithUnsetWhenRequired() 80 { 81 $this->mockStubRequestValueErrorFactory->expect('create', array('FIELD_EMPTY')); 82 $floatFilter = new stubFloatFilter($this->mockStubRequestValueErrorFactory); 77 * 78 * @test 79 * @expectedException stubFilterException 80 */ 81 public function withUnsetWhenRequired() 82 { 83 $floatFilter = new stubFloatFilter($this->mockRequestValueErrorFactory); 83 84 $floatFilter->setRequired(true); 84 $this->assertEqual($floatFilter->execute(true), 1000); 85 $this->assertEqual($floatFilter->execute(false), 0); 86 $this->expectException('stubFilterException'); 85 $this->assertEquals(1000, $floatFilter->execute(true)); 86 $this->assertEquals(0, $floatFilter->execute(false)); 87 $this->mockRequestValueErrorFactory->expects($this->once()) 88 ->method('create') 89 ->with($this->equalTo('FIELD_EMPTY')) 90 ->will($this->returnValue(new stubRequestValueError('foo', array('en_EN' => 'Something wrent wrong.')))); 87 91 $floatFilter->execute(null); 88 92 } … … 91 95 * assure that an exceptiom is thrown when a value is 92 96 * required but not passed 93 */ 94 public function testWithUnsetEmptyStringWhenRequired() 95 { 96 $this->mockStubRequestValueErrorFactory->expect('create', array('FIELD_EMPTY')); 97 $floatFilter = new stubFloatFilter($this->mockStubRequestValueErrorFactory); 97 * 98 * @test 99 * @expectedException stubFilterException 100 */ 101 public function withUnsetEmptyStringWhenRequired() 102 { 103 $floatFilter = new stubFloatFilter($this->mockRequestValueErrorFactory); 98 104 $floatFilter->setRequired(true); 99 $this->expectException('stubFilterException'); 105 $this->mockRequestValueErrorFactory->expects($this->once()) 106 ->method('create') 107 ->with($this->equalTo('FIELD_EMPTY')) 108 ->will($this->returnValue(new stubRequestValueError('foo', array('en_EN' => 'Something wrent wrong.')))); 100 109 $floatFilter->execute(''); 101 110 } … … 104 113 * assure that 0 is returned when value not set or empty when no value 105 114 * is required 106 */ 107 public function testUnsetWhenNotRequired() 108 { 109 $floatFilter = new stubFloatFilter($this->mockStubRequestValueErrorFactory); 115 * 116 * @test 117 */ 118 public function unsetWhenNotRequired() 119 { 120 $floatFilter = new stubFloatFilter($this->mockRequestValueErrorFactory); 110 121 $floatFilter->setRequired(false); 111 $this->assertEqual ($floatFilter->execute(null), 0);112 $this->assertEqual ($floatFilter->execute(''), 0);113 $this->assertEqual ($floatFilter->execute(true), 1000);114 $this->assertEqual ($floatFilter->execute(false), 0);122 $this->assertEquals(0, $floatFilter->execute(null)); 123 $this->assertEquals(0, $floatFilter->execute('')); 124 $this->assertEquals(1000, $floatFilter->execute(true)); 125 $this->assertEquals(0, $floatFilter->execute(false)); 115 126 } 116 127 117 128 /** 118 129 * assure that an FilterException is thrown when value smaller then $min 119 */ 120 public function testWithMinValidator() 121 { 122 $this->mockStubValidatorMin->setReturnValueAt(0, 'validate', true); 123 $this->mockStubValidatorMin->setReturnValueAt(1, 'validate', false); 124 $this->mockStubValidatorMin->setReturnValue('getCriteria', array()); 125 $this->mockStubRequestValueErrorFactory->expect('create', array('VALUE_TOO_SMALL')); 126 127 $floatFilter = new stubFloatFilter($this->mockStubRequestValueErrorFactory, $this->mockStubValidatorMin); 128 $this->assertEqual($floatFilter->execute(-10), -10000); 129 $this->expectException('stubFilterException'); 130 * 131 * @test 132 */ 133 public function withMinValidator() 134 { 135 $this->mockValidatorMin->expects($this->once()) 136 ->method('validate') 137 ->will($this->returnValue(true)); 138 $this->mockValidatorMin->expects($this->never()) 139 ->method('getCriteria'); 140 $floatFilter = new stubFloatFilter($this->mockRequestValueErrorFactory, $this->mockValidatorMin); 141 $this->assertEquals(-10000, $floatFilter->execute(-10)); 142 } 143 144 /** 145 * assure that an FilterException is thrown when value smaller then $min 146 * 147 * @test 148 * @expectedException stubFilterException 149 */ 150 public function withMinValidatorFails() 151 { 152 $this->mockValidatorMin->expects($this->once()) 153 ->method('validate') 154 ->will($this->returnValue(false)); 155 $this->mockValidatorMin->expects($this->once()) 156 ->method('getCriteria') 157 ->will($this->returnValue(array())); 158 $floatFilter = new stubFloatFilter($this->mockRequestValueErrorFactory, $this->mockValidatorMin); 159 $this->mockRequestValueErrorFactory->expects($this->once()) 160 ->method('create') 161 ->with($this->equalTo('VALUE_TOO_SMALL')) 162 ->will($this->returnValue(new stubRequestValueError('foo', array('en_EN' => 'Something wrent wrong.')))); 130 163 $floatFilter->execute(-11); 131 164 } … … 133 166 /** 134 167 * assure that an FilterException is thrown when value greater then $max 135 */ 136 public function testWithMaxValidator() 137 { 138 $this->mockStubValidatorMax->setReturnValueAt(0, 'validate', true); 139 $this->mockStubValidatorMax->setReturnValueAt(1, 'validate', false); 140 $this->mockStubValidatorMax->setReturnValue('getCriteria', array()); 141 $this->mockStubRequestValueErrorFactory->expect('create', array('VALUE_TOO_GREAT')); 142 143 $floatFilter = new stubFloatFilter($this->mockStubRequestValueErrorFactory, null, $this->mockStubValidatorMax); 144 $this->assertEqual($floatFilter->execute(10), 10000); 145 $this->expectException('stubFilterException'); 168 * 169 * @test 170 */ 171 public function withMaxValidator() 172 { 173 $this->mockValidatorMax->expects($this->once()) 174 ->method('validate') 175 ->will($this->returnValue(true)); 176 $this->mockValidatorMax->expects($this->never()) 177 ->method('getCriteria'); 178 $floatFilter = new stubFloatFilter($this->mockRequestValueErrorFactory, null, $this->mockValidatorMax); 179 $this->assertEquals($floatFilter->execute(10), 10000); 180 } 181 182 /** 183 * assure that an FilterException is thrown when value greater then $max 184 * 185 * @test 186 * @expectedException stubFilterException 187 */ 188 public function withMaxValidatorFails() 189 { 190 $this->mockValidatorMax->expects($this->once()) 191 ->method('validate') 192 ->will($this->returnValue(false)); 193 $this->mockValidatorMax->expects($this->once()) 194 ->method('getCriteria') 195 ->will($this->returnValue(array())); 196 $floatFilter = new stubFloatFilter($this->mockRequestValueErrorFactory, null, $this->mockValidatorMax); 197 $this->mockRequestValueErrorFactory->expects($this->once()) 198 ->method('create') 199 ->with($this->equalTo('VALUE_TOO_GREAT')) 200 ->will($this->returnValue(new stubRequestValueError('foo', array('en_EN' => 'Something wrent wrong.')))); 146 201 $floatFilter->execute(11); 147 202 } … … 149 204 /** 150 205 * assure that the correct value depending on $decimal_places is returned 151 */ 152 public function testFloat() 153 { 154 stubRegistry::setConfig('net.stubbles.number.decimals', 2); 155 $floatFilter = new stubFloatFilter($this->mockStubRequestValueErrorFactory); 156 $this->assertEqual($floatFilter->execute('1.564'), 156); 206 * 207 * @test 208 */ 209 public function float() 210 { 211 stubRegistry::setConfig(stubFloatFilter::DECIMALS_REGISTRY_KEY, 2); 212 $floatFilter = new stubFloatFilter($this->mockRequestValueErrorFactory); 213 $this->assertEquals(156, $floatFilter->execute('1.564')); 214 } 215 216 /** 217 * assure that the correct value depending on $decimal_places is returned 218 * 219 * @test 220 */ 221 public function decimals0() 222 { 223 stubRegistry::setConfig(stubFloatFilter::DECIMALS_REGISTRY_KEY, 0); 224 $floatFilter = new stubFloatFilter($this->mockRequestValueErrorFactory); 225 $this->assertEquals(1.564, $floatFilter->execute('1.564')); 226 } 227 228 /** 229 * assure that the correct value depending on $decimal_places is returned 230 * 231 * @test 232 */ 233 public function registryKeyMissing() 234 { 235 stubRegistry::removeConfig(stubFloatFilter::DECIMALS_REGISTRY_KEY); 236 $floatFilter = new stubFloatFilter($this->mockRequestValueErrorFactory); 237 $this->assertEquals(1.564, $floatFilter->execute('1.564')); 157 238 } 158 239 } trunk/src/test/php/net/stubbles/ipo/request/filters/stubHTTPURLFilterTestCase.php
r1223 r1274 8 8 */ 9 9 stubClassLoader::load('net::stubbles::ipo::request::filters::stubHTTPURLFilter'); 10 Mock::generate('stubRequestValueErrorFactory');11 10 /** 12 11 * Tests for net::stubbles::ipo::request::filters::stubHTTPURLFilter. … … 15 14 * @subpackage ipo_request_filters_test 16 15 */ 17 class stubHTTPURLFilterTestCase extends UnitTestCase16 class stubHTTPURLFilterTestCase extends PHPUnit_Framework_TestCase 18 17 { 19 18 /** … … 26 25 * a mock to be used for the rveFactory 27 26 * 28 * @var stubRequestValueErrorFactory27 * @var PHPUnit_Framework_MockObject_MockObject 29 28 */ 30 protected $mock StubRequestValueErrorFactory;29 protected $mockRequestValueErrorFactory; 31 30 32 31 /** … … 36 35 public function setUp() 37 36 { 38 $this->mockStubRequestValueErrorFactory = new MockStubRequestValueErrorFactory(); 39 $this->mockStubRequestValueErrorFactory->setReturnValue('create', new stubRequestValueError('foo', array('en_EN' => 'Something wrent wrong.'))); 40 $this->httpURLFilter = new stubHTTPURLFilter($this->mockStubRequestValueErrorFactory); 37 $this->mockRequestValueErrorFactory = $this->getMock('stubRequestValueErrorFactory'); 38 $this->httpURLFilter = new stubHTTPURLFilter($this->mockRequestValueErrorFactory); 41 39 } 42 40 43 41 /** 44 42 * assure that values are returned the expected way 43 * 44 * @test 45 45 */ 46 public function testValue()46 public function value() 47 47 { 48 $this->assertEqual ($this->httpURLFilter->execute('http://example.org'), 'http://example.org/');49 $this->assertEqual ($this->httpURLFilter->execute('http://example.org:45'), 'http://example.org:45/');48 $this->assertEquals('http://example.org/', $this->httpURLFilter->execute('http://example.org')); 49 $this->assertEquals('http://example.org:45/', $this->httpURLFilter->execute('http://example.org:45')); 50 50 } 51 51 52 52 /** 53 53 * assure correct behaviour when a null value is passed 54 * 55 * @test 54 56 */ 55 public function testNullValue()57 public function nullValue() 56 58 { 57 59 $this->httpURLFilter->setRequired(false); 58 60 $this->assertNull($this->httpURLFilter->execute(null)); 59 $this->httpURLFilter->setRequired(true);60 $this->mockStubRequestValueErrorFactory->expect('create', array('URL_INCORRECT'));61 $this->expectException('stubFilterException');62 $this->httpURLFilter->execute(null);63 61 } 64 62 65 63 /** 66 64 * assure correct behaviour when a null value is passed 65 * 66 * @test 67 * @expectedException stubFilterException 67 68 */ 68 public function testNullValueWithDifferentErrorId()69 public function nullValueFails() 69 70 { 70 71 $this->httpURLFilter->setRequired(true); 71 $this->mockStubRequestValueErrorFactory->expect('create', array('URL_INCORRECT')); 72 $this->expectException('stubFilterException'); 72 $this->mockRequestValueErrorFactory->expects($this->once()) 73 ->method('create') 74 ->with($this->equalTo('URL_INCORRECT')) 75 ->will($this->returnValue(new stubRequestValueError('foo', array('en_EN' => 'Something wrent wrong.')))); 73 76 $this->httpURLFilter->execute(null); 74 77 } … … 76 79 /** 77 80 * assure correct behaviour when an empty value is passed 81 * 82 * @test 78 83 */ 79 public function testEmptyValue()84 public function emptyValue() 80 85 { 81 86 $this->httpURLFilter->setRequired(false); 82 87 $this->assertNull($this->httpURLFilter->execute('')); 88 } 89 90 /** 91 * assure correct behaviour when an empty value is passed 92 * 93 * @test 94 * @expectedException stubFilterException 95 */ 96 public function emptyValueFails() 97 { 83 98 $this->httpURLFilter->setRequired(true); 84 $this->mockStubRequestValueErrorFactory->expect('create', array('URL_INCORRECT')); 85 $this->expectException('stubFilterException'); 99 $this->mockRequestValueErrorFactory->expects($this->once()) 100 ->method('create') 101 ->with($this->equalTo('URL_INCORRECT')) 102 ->will($this->returnValue(new stubRequestValueError('foo', array('en_EN' => 'Something wrent wrong.')))); 86 103 $this->httpURLFilter->execute(''); 87 104 } … … 89 106 /** 90 107 * assure that an exception is thrown when a wrong scheme is passed 108 * 109 * @test 110 * @expectedException stubFilterException 91 111 */ 92 public function testWrongScheme()112 public function wrongScheme() 93 113 { 94 $this->mockStubRequestValueErrorFactory->expect('create', array('URL_INCORRECT')); 95 $this->expectException('stubFilterException'); 114 $this->mockRequestValueErrorFactory->expects($this->once()) 115 ->method('create') 116 ->with($this->equalTo('URL_INCORRECT')) 117 ->will($this->returnValue(new stubRequestValueError('foo', array('en_EN' => 'Something wrent wrong.')))); 96 118 $this->httpURLFilter->execute('ftp://foobar.de/'); 97 119 } … … 99 121 /** 100 122 * assure that an exception is thrown when a wrong value is passed 123 * 124 * @test 125 * @expectedException stubFilterException 101 126 */ 102 public function testWrongValue()127 public function wrongValue() 103 128 { 104 $this->mockStubRequestValueErrorFactory->expect('create', array('URL_INCORRECT')); 105 $this->expectException('stubFilterException'); 129 $this->mockRequestValueErrorFactory->expects($this->once()) 130 ->method('create') 131 ->with($this->equalTo('URL_INCORRECT')) 132 ->will($this->returnValue(new stubRequestValueError('foo', array('en_EN' => 'Something wrent wrong.')))); 106 133 $this->httpURLFilter->execute('http://wrong example!'); 107 134 } … … 109 136 /** 110 137 * assert that a non-existing but correct URL is treated correct 138 * 139 * @test 111 140 */ 112 public function testURLNotAvailable()141 public function urlCheckDisabled() 113 142 { 114 143 $this->assertFalse($this->httpURLFilter->isDNSCheckEnabled()); 115 $this->assertEqual($this->httpURLFilter->execute('http://doesnotexist.1und1.de/'), 'http://doesnotexist.1und1.de/'); 144 $this->assertEquals('http://doesnotexist.1und1.de/', $this->httpURLFilter->execute('http://doesnotexist.1und1.de/')); 145 } 146 147 /** 148 * assert that a non-existing but correct URL is treated correct 149 * 150 * @test 151 */ 152 public function urlNotAvailable() 153 { 116 154 $this->httpURLFilter->setCheckDNS(true); 117 155 $this->assertTrue($this->httpURLFilter->isDNSCheckEnabled()); 118 156 if (DIRECTORY_SEPARATOR === '\\') { 119 157 // Windows does not support dns checks, filter will always return ok 120 $this->assertEqual ($this->httpURLFilter->execute('http://doesnotexist.1und1.de/'), 'http://doesnotexist.1und1.de/');158 $this->assertEquals('http://doesnotexist.1und1.de/', $this->httpURLFilter->execute('http://doesnotexist.1und1.de/')); 121 159 } else { 122 $this->mockStubRequestValueErrorFactory->expect('create', array('URL_NOT_AVAILABLE')); 123 $this->expectException('stubFilterException'); 160 $this->setExpectedException('stubFilterException'); 161 $this->mockRequestValueErrorFactory->expects($this->once()) 162 ->method('create') 163 ->with($this->equalTo('URL_NOT_AVAILABLE')) 164 ->will($this->returnValue(new stubRequestValueError('foo', array('en_EN' => 'Something wrent wrong.')))); 124 165 $this->httpURLFilter->execute('http://doesnotexist.1und1.de/'); 125 166 } trunk/src/test/php/net/stubbles/ipo/request/filters/stubIntegerFilterTestCase.php
r1223 r1274 8 8 */ 9 9 stubClassLoader::load('net::stubbles::ipo::request::filters::stubIntegerFilter'); 10 Mock::generate('stubRequestValueErrorFactory');11 Mock::generate('stubValidator');12 10 /** 13 11 * Tests for net::stubbles::ipo::request::filters::stubIntegerFilter. … … 16 14 * @subpackage ipo_test 17 15 */ 18 class stubIntegerFilterTestCase extends UnitTestCase16 class stubIntegerFilterTestCase extends PHPUnit_Framework_TestCase 19 17 { 20 18 /** 21 19 * a mock to be used for the rveFactory 22 20 * 23 * @var stubRequestValueErrorFactory24 */ 25 protected $mock StubRequestValueErrorFactory;21 * @var PHPUnit_Framework_MockObject_MockObject 22 */ 23 protected $mockRequestValueErrorFactory; 26 24 /** 27 25 * a mock to be used for the minimum validator 28 26 * 29 * @var stubValidator30 */ 31 protected $mock StubValidatorMin;27 * @var PHPUnit_Framework_MockObject_MockObject 28 */ 29 protected $mockValidatorMin; 32 30 /** 33 31 * a mock to be used for the maximum validator 34 32 * 35 * @var stubValidator36 */ 37 protected $mock StubValidatorMax;33 * @var PHPUnit_Framework_MockObject_MockObject 34 */ 35 protected $mockValidatorMax; 38 36 39 37 /** … … 43 41 public function setUp() 44 42 { 45 $this->mockStubRequestValueErrorFactory = new MockStubRequestValueErrorFactory(); 46 $this->mockStubValidatorMin = new MockStubValidator(); 47 $this->mockStubValidatorMax = new MockStubValidator(); 48 $this->mockStubRequestValueErrorFactory->setReturnValue('create', new stubRequestValueError('foo', array('en_EN' => 'Something wrent wrong.'))); 43 $this->mockRequestValueErrorFactory = $this->getMock('stubRequestValueErrorFactory'); 44 $this->mockValidatorMin = $this->getMock('stubValidator'); 45 $this->mockValidatorMax = $this->getMock('stubValidator'); 49 46 } 50 47 51 48 /** 52 49 * assure that values are returned the expected way 53 */ 54 public function testValue() 55 { 56 $integerFilter = new stubIntegerFilter($this->mockStubRequestValueErrorFactory); 57 $this->assertEqual($integerFilter->execute(8), 8); 50 * 51 * @test 52 */ 53 public function value() 54 { 55 $integerFilter = new stubIntegerFilter($this->mockRequestValueErrorFactory); 56 $this->assertEquals(8, $integerFilter->execute(8)); 58 57 } 59 58 … … 61 60 * assure that an exceptiom is thrown when a value is 62 61 * required but not passed 63 */ 64 public function testWithUnsetWhenRequired() 65 { 66 $this->mockStubRequestValueErrorFactory->expect('create', array('FIELD_EMPTY')); 67 $integerFilter = new stubIntegerFilter($this->mockStubRequestValueErrorFactory); 62 * 63 * @test 64 * @expectedException stubFilterException 65 */ 66 public function withUnsetWhenRequired() 67 { 68 $integerFilter = new stubIntegerFilter($this->mockRequestValueErrorFactory); 68 69 $integerFilter->setRequired(true); 69 $this->assertEqual($integerFilter->execute(true), 1); 70 $this->assertEqual($integerFilter->execute(false), 0); 71 $this->expectException('stubFilterException'); 70 $this->assertEquals(1, $integerFilter->execute(true)); 71 $this->assertEquals(0, $integerFilter->execute(false)); 72 $this->mockRequestValueErrorFactory->expects($this->once()) 73 ->method('create') 74 ->with($this->equalTo('FIELD_EMPTY')) 75 ->will($this->returnValue(new stubRequestValueError('foo', array('en_EN' => 'Something wrent wrong.')))); 72 76 $value = $integerFilter->execute(null); 73 77 } … … 76 80 * assure that an exceptiom is thrown when a value is 77 81 * required but not passed 78 */ 79 public function testWithUnsetEmptyStringWhenRequired() 80 { 81 $this->mockStubRequestValueErrorFactory->expect('create', array('FIELD_EMPTY')); 82 $integerFilter = new stubIntegerFilter($this->mockStubRequestValueErrorFactory); 82 * 83 * @test 84 * @expectedException stubFilterException 85 */ 86 public function withUnsetEmptyStringWhenRequired() 87 { 88 $integerFilter = new stubIntegerFilter($this->mockRequestValueErrorFactory); 83 89 $integerFilter->setRequired(true); 84 $this->expectException('stubFilterException'); 90 $this->mockRequestValueErrorFactory->expects($this->once()) 91 ->method('create') 92 ->with($this->equalTo('FIELD_EMPTY')) 93 ->will($this->returnValue(new stubRequestValueError('foo', array('en_EN' => 'Something wrent wrong.')))); 85 94 $integerFilter->execute(''); 86 95 } … … 89 98 * assure that 0 or 1 is returned when value not set or empty when no value 90 99 * is required 91 */ 92 public function testUnsetWhenNotRequired() 93 { 94 $integerFilter = new stubIntegerFilter($this->mockStubRequestValueErrorFactory); 100 * 101 * @test 102 */ 103 public function unsetWhenNotRequired() 104 { 105 $integerFilter = new stubIntegerFilter($this->mockRequestValueErrorFactory); 95 106 $integerFilter->setRequired(false); 96 $this->assertEqual ($integerFilter->execute(null), 0);97 $this->assertEqual ($integerFilter->execute(''), 0);98 $this->assertEqual ($integerFilter->execute(true), 1);99 $this->assertEqual ($integerFilter->execute(false), 0);107 $this->assertEquals(0, $integerFilter->execute(null)); 108 $this->assertEquals(0, $integerFilter->execute('')); 109 $this->assertEquals(1, $integerFilter->execute(true)); 110 $this->assertEquals(0, $integerFilter->execute(false)); 100 111 } 101 112 102 113 /** 103 114 * assure that an FilterException is thrown when value smaller then $min 104 */ 105 public function testWithMinValidator() 106 { 107 $this->mockStubValidatorMin->setReturnValueAt(0, 'validate', true); 108 $this->mockStubValidatorMin->setReturnValueAt(1, 'validate', false); 109 $this->mockStubValidatorMin->setReturnValue('getCriteria', array()); 110 $this->mockStubRequestValueErrorFactory->expect('create', array('VALUE_TOO_SMALL')); 111 112 $integerFilter = new stubIntegerFilter($this->mockStubRequestValueErrorFactory, $this->mockStubValidatorMin); 113 $this->assertEqual($integerFilter->execute(-10), -10); 114 $this->expectException('stubFilterException'); 115 * 116 * @test 117 */ 118 public function withMinValidator() 119 { 120 $this->mockValidatorMin->expects($this->once()) 121 ->method('validate') 122 ->will($this->returnValue(true)); 123 $this->mockValidatorMin->expects($this->never()) 124 ->method('getCriteria'); 125 $integerFilter = new stubIntegerFilter($this->mockRequestValueErrorFactory, $this->mockValidatorMin); 126 $this->assertEquals(-10, $integerFilter->execute(-10)); 127 } 128 129 /** 130 * assure that an FilterException is thrown when value smaller then $min 131 * 132 * @test 133 * @expectedException stubFilterException 134 */ 135 public function withMinValidatorFails() 136 { 137 $this->mockValidatorMin->expects($this->once()) 138 ->method('validate') 139 ->will($this->returnValue(false)); 140 $this->mockValidatorMin->expects($this->once()) 141 ->method('getCriteria') 142 ->will($this->returnValue(array())); 143 $integerFilter = new stubIntegerFilter($this->mockRequestValueErrorFactory, $this->mockValidatorMin); 144 $this->mockRequestValueErrorFactory->expects($this->once(
