From 444fcb1d6f93c1ecf242b65e796fdc1c4ff88b61 Mon Sep 17 00:00:00 2001 From: Matthew Smeets Date: Tue, 13 Oct 2020 18:11:05 +0200 Subject: [PATCH 1/2] Allow numeric values for GreaterThan and LessThan --- Constraint/Constraints/GreaterThan.php | 2 +- Constraint/Constraints/LessThan.php | 2 +- .../Constraints/GreaterThanOrEqualTest.php | 33 ++++++++++++++++++- .../Constraints/GreaterThanTest.php | 33 ++++++++++++++++++- .../Constraints/LessThanOrEqualTest.php | 33 ++++++++++++++++++- Tests/Constraint/Constraints/LessThanTest.php | 33 ++++++++++++++++++- 6 files changed, 130 insertions(+), 6 deletions(-) diff --git a/Constraint/Constraints/GreaterThan.php b/Constraint/Constraints/GreaterThan.php index 21d57e6..c93be3c 100644 --- a/Constraint/Constraints/GreaterThan.php +++ b/Constraint/Constraints/GreaterThan.php @@ -45,7 +45,7 @@ protected function configureOptions(OptionsResolver $resolver): void { $resolver ->setRequired(['value']) - ->setAllowedTypes('value', ['int']) + ->setAllowedTypes('value', ['numeric', 'string']) ; } } diff --git a/Constraint/Constraints/LessThan.php b/Constraint/Constraints/LessThan.php index 367b0a6..75c5865 100644 --- a/Constraint/Constraints/LessThan.php +++ b/Constraint/Constraints/LessThan.php @@ -45,7 +45,7 @@ protected function configureOptions(OptionsResolver $resolver): void { $resolver ->setRequired(['value']) - ->setAllowedTypes('value', ['int']) + ->setAllowedTypes('value', ['numeric', 'string']) ; } } diff --git a/Tests/Constraint/Constraints/GreaterThanOrEqualTest.php b/Tests/Constraint/Constraints/GreaterThanOrEqualTest.php index 890da8e..4aa86b0 100644 --- a/Tests/Constraint/Constraints/GreaterThanOrEqualTest.php +++ b/Tests/Constraint/Constraints/GreaterThanOrEqualTest.php @@ -20,12 +20,13 @@ public function testInvalidConfiguration(): void $this->expectException(InvalidOptionsException::class); new ParsleyAssert\GreaterThanOrEqual([ - 'value' => '5', + 'value' => [], ]); } public function testNormalization(): void { + // integer $constraint = new ParsleyAssert\GreaterThanOrEqual([ 'value' => 5, ]); @@ -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)); } } diff --git a/Tests/Constraint/Constraints/GreaterThanTest.php b/Tests/Constraint/Constraints/GreaterThanTest.php index ac144e2..ff02b43 100644 --- a/Tests/Constraint/Constraints/GreaterThanTest.php +++ b/Tests/Constraint/Constraints/GreaterThanTest.php @@ -20,12 +20,13 @@ public function testInvalidConfiguration(): void $this->expectException(InvalidOptionsException::class); new ParsleyAssert\GreaterThan([ - 'value' => '5', + 'value' => [], ]); } public function testNormalization(): void { + // integer $constraint = new ParsleyAssert\GreaterThan([ 'value' => 5, ]); @@ -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)); } } diff --git a/Tests/Constraint/Constraints/LessThanOrEqualTest.php b/Tests/Constraint/Constraints/LessThanOrEqualTest.php index 4811099..e4d8df9 100644 --- a/Tests/Constraint/Constraints/LessThanOrEqualTest.php +++ b/Tests/Constraint/Constraints/LessThanOrEqualTest.php @@ -20,12 +20,43 @@ public function testInvalidConfiguration(): void $this->expectException(InvalidOptionsException::class); new ParsleyAssert\LessThanOrEqual([ - 'value' => '10', + 'value' => [], ]); } public function testNormalization(): void { + // integer + $constraint = new ParsleyAssert\LessThanOrEqual([ + 'value' => 10, + ]); + + $this->assertSame([ + '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, ]); diff --git a/Tests/Constraint/Constraints/LessThanTest.php b/Tests/Constraint/Constraints/LessThanTest.php index ee983ec..48f069c 100644 --- a/Tests/Constraint/Constraints/LessThanTest.php +++ b/Tests/Constraint/Constraints/LessThanTest.php @@ -20,12 +20,13 @@ public function testInvalidConfiguration(): void $this->expectException(InvalidOptionsException::class); new ParsleyAssert\LessThan([ - 'value' => '10', + 'value' => [], ]); } public function testNormalization(): void { + // integer $constraint = new ParsleyAssert\LessThan([ 'value' => 10, ]); @@ -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)); } } From 89e1efea3d3bc6e78c7e8bd524cda723625157a3 Mon Sep 17 00:00:00 2001 From: Matthew Smeets Date: Thu, 15 Oct 2020 20:00:18 +0200 Subject: [PATCH 2/2] disallow string type in GreaterThan and LessThan --- Constraint/Constraints/GreaterThan.php | 2 +- Constraint/Constraints/LessThan.php | 2 +- Tests/Constraint/Constraints/GreaterThanOrEqualTest.php | 2 +- Tests/Constraint/Constraints/GreaterThanTest.php | 2 +- Tests/Constraint/Constraints/LessThanOrEqualTest.php | 4 ++-- Tests/Constraint/Constraints/LessThanTest.php | 4 ++-- 6 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Constraint/Constraints/GreaterThan.php b/Constraint/Constraints/GreaterThan.php index c93be3c..86b44f6 100644 --- a/Constraint/Constraints/GreaterThan.php +++ b/Constraint/Constraints/GreaterThan.php @@ -45,7 +45,7 @@ protected function configureOptions(OptionsResolver $resolver): void { $resolver ->setRequired(['value']) - ->setAllowedTypes('value', ['numeric', 'string']) + ->setAllowedTypes('value', ['numeric']) ; } } diff --git a/Constraint/Constraints/LessThan.php b/Constraint/Constraints/LessThan.php index 75c5865..980cdd5 100644 --- a/Constraint/Constraints/LessThan.php +++ b/Constraint/Constraints/LessThan.php @@ -45,7 +45,7 @@ protected function configureOptions(OptionsResolver $resolver): void { $resolver ->setRequired(['value']) - ->setAllowedTypes('value', ['numeric', 'string']) + ->setAllowedTypes('value', ['numeric']) ; } } diff --git a/Tests/Constraint/Constraints/GreaterThanOrEqualTest.php b/Tests/Constraint/Constraints/GreaterThanOrEqualTest.php index 4aa86b0..54c1f13 100644 --- a/Tests/Constraint/Constraints/GreaterThanOrEqualTest.php +++ b/Tests/Constraint/Constraints/GreaterThanOrEqualTest.php @@ -20,7 +20,7 @@ public function testInvalidConfiguration(): void $this->expectException(InvalidOptionsException::class); new ParsleyAssert\GreaterThanOrEqual([ - 'value' => [], + 'value' => 'foo', ]); } diff --git a/Tests/Constraint/Constraints/GreaterThanTest.php b/Tests/Constraint/Constraints/GreaterThanTest.php index ff02b43..a8d0d06 100644 --- a/Tests/Constraint/Constraints/GreaterThanTest.php +++ b/Tests/Constraint/Constraints/GreaterThanTest.php @@ -20,7 +20,7 @@ public function testInvalidConfiguration(): void $this->expectException(InvalidOptionsException::class); new ParsleyAssert\GreaterThan([ - 'value' => [], + 'value' => 'foo', ]); } diff --git a/Tests/Constraint/Constraints/LessThanOrEqualTest.php b/Tests/Constraint/Constraints/LessThanOrEqualTest.php index e4d8df9..8b5fe49 100644 --- a/Tests/Constraint/Constraints/LessThanOrEqualTest.php +++ b/Tests/Constraint/Constraints/LessThanOrEqualTest.php @@ -20,7 +20,7 @@ public function testInvalidConfiguration(): void $this->expectException(InvalidOptionsException::class); new ParsleyAssert\LessThanOrEqual([ - 'value' => [], + 'value' => 'foo', ]); } @@ -58,7 +58,7 @@ public function testNormalization(): void // string $constraint = new ParsleyAssert\LessThanOrEqual([ - 'value' => 10, + 'value' => '10', ]); $this->assertSame([ diff --git a/Tests/Constraint/Constraints/LessThanTest.php b/Tests/Constraint/Constraints/LessThanTest.php index 48f069c..2e080dc 100644 --- a/Tests/Constraint/Constraints/LessThanTest.php +++ b/Tests/Constraint/Constraints/LessThanTest.php @@ -20,7 +20,7 @@ public function testInvalidConfiguration(): void $this->expectException(InvalidOptionsException::class); new ParsleyAssert\LessThan([ - 'value' => [], + 'value' => 'foo', ]); } @@ -58,7 +58,7 @@ public function testNormalization(): void // string $constraint = new ParsleyAssert\LessThan([ - 'value' => "10", + 'value' => '10', ]); $this->assertSame([