diff --git a/examples/symfony.php b/examples/symfony.php index f7a2df4..03b6cfb 100644 --- a/examples/symfony.php +++ b/examples/symfony.php @@ -29,13 +29,6 @@ public function __construct(\Symfony\Component\HttpFoundation\Request $request) $this->initRequestParser($request); } - public function inRange() - { - $name = $this->queryParameter('name')->int()->inRange(1, 5)->required(); - - return "Hello $name"; - } - public function hello() { $name = $this->queryParameter('name')->string()->required(); diff --git a/tests/ParserSpecTest.php b/tests/ParserSpecTest.php index 0c5036d..8f5a76b 100644 --- a/tests/ParserSpecTest.php +++ b/tests/ParserSpecTest.php @@ -57,7 +57,7 @@ public function specWithInvalidValueAndDefaultValue() { return [ [new IntParser(new Config(), 'id', 'string instead of an int'), 1], - [new FloatParser(new Config(), 'ration', 'string instead of an float'), 0.91], + [new FloatParser(new Config(), 'ration', 'string instead of a float'), 0.91], [new YesNoBooleanParser(new Config(), 'isAwesome', 'invalid'), false], [new BooleanParser(new Config(), 'isAwesome', 'invalid'), false], [new EmailParser(new Config(), 'emailAddress', 'invalid_email'), 'john@doe.com'], @@ -146,4 +146,35 @@ public function testStringSpecific() $parser = new StringParser(new Config(), 'name', 'test'); $this->assertEquals('test', $parser->defaultsToIfEmpty('default')); } + + public function testInRangeValidatorWithValidValues() + { + $parser = new IntParser(new Config(), 'groupId', 1); + $parser->inRange(1, 6); + $this->assertEquals(1, $parser->required()); + $parser = new IntParser(new Config(), 'groupId', 6); + $parser->inRange(1, 6); + $this->assertEquals(6, $parser->required()); + + $parser = new FloatParser(new Config(), 'precipitation', 60.99); + $parser->inRange(60.99, 101.12); + $this->assertEquals(60.99, $parser->required()); + $parser = new FloatParser(new Config(), 'precipitation', 101.12); + $parser->inRange(60.99, 101.12); + $this->assertEquals(101.12, $parser->required()); + } + + public function testIntInRangeValidatorWithValuesOutOfRange() + { + $this->setExpectedException(InvalidValueException::class, "Invalid value for parameter \"groupId\". Expected an integer value between 1 and 6, but got \"7\""); + $parser = new IntParser(new Config(), 'groupId', 7); + $groupId = $parser->inRange(1, 6)->required(); + } + + public function testFloatInRangeValidatorWithValuesOutOfRange() + { + $this->setExpectedException(InvalidValueException::class, "Invalid value for parameter \"precipitation\". Expected a float value between 60.99 and 101.12, but got \"101.13\""); + $parser = new FloatParser(new Config(), 'precipitation', 101.13); + $precipitation = $parser->inRange(60.99, 101.12)->required(); + } }