Skip to content

Commit

Permalink
Updates validation to check strings, not casted
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobemerick committed Apr 10, 2017
1 parent 21cf5cd commit 232e55d
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/Parameter/Format/BooleanCheck.php
Expand Up @@ -12,7 +12,7 @@ class BooleanCheck
*/
public function check(array $param)
{
if (!is_bool($param['value'])) {
if ($param['value'] !== 'true' && $param['value'] !== 'false') {
throw new ValidationException('Value is not a boolean');
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Parameter/Format/IntegerCheck.php
Expand Up @@ -12,7 +12,7 @@ class IntegerCheck
*/
public function check(array $param)
{
if (!is_int($param['value'])) {
if (!filter_var($param['value'], FILTER_VALIDATE_INT)) {
throw new ValidationException('Value is not an integer');
}
if (!isset($param['format'])) {
Expand Down
2 changes: 1 addition & 1 deletion src/Parameter/Format/NumberCheck.php
Expand Up @@ -12,7 +12,7 @@ class NumberCheck
*/
public function check(array $param)
{
if (!is_int($param['value']) && !is_float($param['value'])) {
if (!filter_var($param['value'], FILTER_VALIDATE_FLOAT)) {
throw new ValidationException('Value is not a number');
}

Expand Down
2 changes: 1 addition & 1 deletion tests/unit/src/Parameter/Format/BooleanCheckTest.php
Expand Up @@ -24,7 +24,7 @@ public function testCheckThrowsExceptionIfNotABoolean()
public function testCheckContinuesNormallyIfOkay()
{
$mockParam = [
'value' => false,
'value' => 'false',
];

$booleanCheck = new BooleanCheck;
Expand Down
10 changes: 5 additions & 5 deletions tests/unit/src/Parameter/Format/IntegerCheckTest.php
Expand Up @@ -24,7 +24,7 @@ public function testCheckThrowsExceptionIfNotAnInteger()
public function testCheckIgnoresFormatIfNotDefined()
{
$mockParam = [
'value' => 2147483647 + 1,
'value' => (string) 2147483647 + 1,
];

$integerCheck = new IntegerCheck;
Expand All @@ -38,7 +38,7 @@ public function testCheckIgnoresFormatIfNotDefined()
public function testCheckThrowsExceptionIfExceedsInt32Bounds()
{
$mockParam = [
'value' => 2147483647 + 1,
'value' => (string) 2147483647 + 1,
'format' => 'int32',
];

Expand All @@ -49,7 +49,7 @@ public function testCheckThrowsExceptionIfExceedsInt32Bounds()
public function testCheckPassesIfWithinInt32Bounds()
{
$mockParam = [
'value' => -2147483647 + 1,
'value' => (string) -2147483647 + 1,
'format' => 'int32',
];

Expand All @@ -64,7 +64,7 @@ public function testCheckPassesIfWithinInt32Bounds()
public function testCheckThrowsExceptionIfExceedsInt64Bounds()
{
$mockParam = [
'value' => -9223372036854775807 - 1,
'value' => (string) -9223372036854775807 - 1,
'format' => 'int64',
];

Expand All @@ -75,7 +75,7 @@ public function testCheckThrowsExceptionIfExceedsInt64Bounds()
public function testCheckPassesIfWithinInt64Bounds()
{
$mockParam = [
'value' => 9223372036854775807 - 1,
'value' => (string) 9223372036854775807 - 1,
];

$integerCheck = new IntegerCheck;
Expand Down
12 changes: 6 additions & 6 deletions tests/unit/src/Parameter/Format/NumberCheckTest.php
Expand Up @@ -28,7 +28,7 @@ public function testCheckThrowsExceptionIfNotANumber()
public function testCheckCallsToRangeCheckIfOkay()
{
$mockParam = [
'value' => 512.123,
'value' => (string) 512.123,
];

$numberCheck = $this->getMockBuilder(NumberCheck::class)
Expand All @@ -51,7 +51,7 @@ public function testCheckRangeFailsIfValueIsExceedsMaximum()
$mockParam = [
'maximum' => 5,
'minimum' => 2,
'value' => 6,
'value' => (string) 6,
];

$reflectedNumberCheck = new ReflectionClass(NumberCheck::class);
Expand All @@ -72,7 +72,7 @@ public function testCheckRangeFailsIfValueIsExceedsExclusiveMaximum()
{
$mockParam = [
'exclusiveMaximum' => 5,
'value' => 5,
'value' => (string) 5,
];

$reflectedNumberCheck = new ReflectionClass(NumberCheck::class);
Expand All @@ -94,7 +94,7 @@ public function testCheckRangeFailsIfValueIsExceedsMinimum()
$mockParam = [
'minimum' => 5,
'minimum' => 2,
'value' => 1,
'value' => (string) 1,
];

$reflectedNumberCheck = new ReflectionClass(NumberCheck::class);
Expand All @@ -115,7 +115,7 @@ public function testCheckRangeFailsIfValueIsExceedsExclusiveMinimum()
{
$mockParam = [
'exclusiveMinimum' => 2,
'value' => 2,
'value' => (string) 2,
];

$reflectedNumberCheck = new ReflectionClass(NumberCheck::class);
Expand All @@ -133,7 +133,7 @@ public function testCheckRangePassesIfWithinRange()
$mockParam = [
'maximum' => 5,
'minimum' => 2,
'value' => 4,
'value' => (string) 4,
];

$reflectedNumberCheck = new ReflectionClass(NumberCheck::class);
Expand Down

0 comments on commit 232e55d

Please sign in to comment.