Skip to content

Commit

Permalink
Merge pull request #19 from Matts/allow-numeric
Browse files Browse the repository at this point in the history
Allow numeric values for GreaterThan and LessThan
  • Loading branch information
J-Ben87 authored Oct 15, 2020
2 parents 34b6bb9 + 89e1efe commit b669665
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Constraint/Constraints/GreaterThan.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected function configureOptions(OptionsResolver $resolver): void
{
$resolver
->setRequired(['value'])
->setAllowedTypes('value', ['int'])
->setAllowedTypes('value', ['numeric'])
;
}
}
2 changes: 1 addition & 1 deletion Constraint/Constraints/LessThan.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected function configureOptions(OptionsResolver $resolver): void
{
$resolver
->setRequired(['value'])
->setAllowedTypes('value', ['int'])
->setAllowedTypes('value', ['numeric'])
;
}
}
33 changes: 32 additions & 1 deletion Tests/Constraint/Constraints/GreaterThanOrEqualTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ public function testInvalidConfiguration(): void
$this->expectException(InvalidOptionsException::class);

new ParsleyAssert\GreaterThanOrEqual([
'value' => '5',
'value' => 'foo',
]);
}

public function testNormalization(): void
{
// integer
$constraint = new ParsleyAssert\GreaterThanOrEqual([
'value' => 5,
]);
Expand All @@ -34,5 +35,35 @@ public function testNormalization(): void
'data-parsley-gte' => '5',
'data-parsley-gte-message' => 'Invalid.',
], $constraint->normalize($this->normalizer));

// float to int
$constraint = new ParsleyAssert\GreaterThanOrEqual([
'value' => 5.0,
]);

$this->assertSame([
'data-parsley-gte' => '5',
'data-parsley-gte-message' => 'Invalid.',
], $constraint->normalize($this->normalizer));

// floating
$constraint = new ParsleyAssert\GreaterThanOrEqual([
'value' => 5.2,
]);

$this->assertSame([
'data-parsley-gte' => '5.2',
'data-parsley-gte-message' => 'Invalid.',
], $constraint->normalize($this->normalizer));

// string
$constraint = new ParsleyAssert\GreaterThanOrEqual([
'value' => '5',
]);

$this->assertSame([
'data-parsley-gte' => '5',
'data-parsley-gte-message' => 'Invalid.',
], $constraint->normalize($this->normalizer));
}
}
33 changes: 32 additions & 1 deletion Tests/Constraint/Constraints/GreaterThanTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ public function testInvalidConfiguration(): void
$this->expectException(InvalidOptionsException::class);

new ParsleyAssert\GreaterThan([
'value' => '5',
'value' => 'foo',
]);
}

public function testNormalization(): void
{
// integer
$constraint = new ParsleyAssert\GreaterThan([
'value' => 5,
]);
Expand All @@ -34,5 +35,35 @@ public function testNormalization(): void
'data-parsley-gt' => '5',
'data-parsley-gt-message' => 'Invalid.',
], $constraint->normalize($this->normalizer));

// float to int
$constraint = new ParsleyAssert\GreaterThan([
'value' => 5.0,
]);

$this->assertSame([
'data-parsley-gt' => '5',
'data-parsley-gt-message' => 'Invalid.',
], $constraint->normalize($this->normalizer));

// floating
$constraint = new ParsleyAssert\GreaterThan([
'value' => 5.2,
]);

$this->assertSame([
'data-parsley-gt' => '5.2',
'data-parsley-gt-message' => 'Invalid.',
], $constraint->normalize($this->normalizer));

// string
$constraint = new ParsleyAssert\GreaterThan([
'value' => '5',
]);

$this->assertSame([
'data-parsley-gt' => '5',
'data-parsley-gt-message' => 'Invalid.',
], $constraint->normalize($this->normalizer));
}
}
33 changes: 32 additions & 1 deletion Tests/Constraint/Constraints/LessThanOrEqualTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ public function testInvalidConfiguration(): void
$this->expectException(InvalidOptionsException::class);

new ParsleyAssert\LessThanOrEqual([
'value' => '10',
'value' => 'foo',
]);
}

public function testNormalization(): void
{
// integer
$constraint = new ParsleyAssert\LessThanOrEqual([
'value' => 10,
]);
Expand All @@ -34,5 +35,35 @@ public function testNormalization(): void
'data-parsley-lte' => '10',
'data-parsley-lte-message' => 'Invalid.',
], $constraint->normalize($this->normalizer));

// float to int
$constraint = new ParsleyAssert\LessThanOrEqual([
'value' => 10.0,
]);

$this->assertSame([
'data-parsley-lte' => '10',
'data-parsley-lte-message' => 'Invalid.',
], $constraint->normalize($this->normalizer));

// floating
$constraint = new ParsleyAssert\LessThanOrEqual([
'value' => 10.3,
]);

$this->assertSame([
'data-parsley-lte' => '10.3',
'data-parsley-lte-message' => 'Invalid.',
], $constraint->normalize($this->normalizer));

// string
$constraint = new ParsleyAssert\LessThanOrEqual([
'value' => '10',
]);

$this->assertSame([
'data-parsley-lte' => '10',
'data-parsley-lte-message' => 'Invalid.',
], $constraint->normalize($this->normalizer));
}
}
33 changes: 32 additions & 1 deletion Tests/Constraint/Constraints/LessThanTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ public function testInvalidConfiguration(): void
$this->expectException(InvalidOptionsException::class);

new ParsleyAssert\LessThan([
'value' => '10',
'value' => 'foo',
]);
}

public function testNormalization(): void
{
// integer
$constraint = new ParsleyAssert\LessThan([
'value' => 10,
]);
Expand All @@ -34,5 +35,35 @@ public function testNormalization(): void
'data-parsley-lt' => '10',
'data-parsley-lt-message' => 'Invalid.',
], $constraint->normalize($this->normalizer));

// float to int
$constraint = new ParsleyAssert\LessThan([
'value' => 10.0,
]);

$this->assertSame([
'data-parsley-lt' => '10',
'data-parsley-lt-message' => 'Invalid.',
], $constraint->normalize($this->normalizer));

// floating
$constraint = new ParsleyAssert\LessThan([
'value' => 10.5,
]);

$this->assertSame([
'data-parsley-lt' => '10.5',
'data-parsley-lt-message' => 'Invalid.',
], $constraint->normalize($this->normalizer));

// string
$constraint = new ParsleyAssert\LessThan([
'value' => '10',
]);

$this->assertSame([
'data-parsley-lt' => '10',
'data-parsley-lt-message' => 'Invalid.',
], $constraint->normalize($this->normalizer));
}
}

0 comments on commit b669665

Please sign in to comment.