Skip to content

Commit

Permalink
added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuvi committed Aug 11, 2016
1 parent e963f61 commit 108a87e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
7 changes: 0 additions & 7 deletions examples/symfony.php
Expand Up @@ -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();
Expand Down
33 changes: 32 additions & 1 deletion tests/ParserSpecTest.php
Expand Up @@ -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'],
Expand Down Expand Up @@ -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();
}
}

0 comments on commit 108a87e

Please sign in to comment.