Skip to content
This repository was archived by the owner on Jan 31, 2020. It is now read-only.

Commit 8f08111

Browse files
committed
Merge pull request #174 from codisart/issue-167
Fix Between validator behavior
2 parents ce7ab72 + 4b61f11 commit 8f08111

File tree

2 files changed

+90
-27
lines changed

2 files changed

+90
-27
lines changed

src/Between.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ class Between extends AbstractValidator
1717
const NOT_BETWEEN = 'notBetween';
1818
const NOT_BETWEEN_STRICT = 'notBetweenStrict';
1919

20+
/**
21+
* Retain if min and max are numeric values. Allow to not compare string and numeric types
22+
*
23+
* @var boolean
24+
*/
25+
private $numeric;
26+
2027
/**
2128
* Validation failure message template definitions
2229
*
@@ -81,7 +88,17 @@ public function __construct($options = null)
8188
if (count($options) !== 2
8289
&& (! array_key_exists('min', $options) || ! array_key_exists('max', $options))
8390
) {
84-
throw new Exception\InvalidArgumentException("Missing option. 'min' and 'max' have to be given");
91+
throw new Exception\InvalidArgumentException("Missing option : 'min' and 'max' have to be given");
92+
}
93+
94+
if (is_numeric($options['min']) && is_numeric($options['max'])) {
95+
$this->numeric = true;
96+
} elseif (is_string($options['min']) && is_string($options['max'])) {
97+
$this->numeric = false;
98+
} else {
99+
throw new Exception\InvalidArgumentException(
100+
"Invalid options : 'min' and 'max' should be of the same scalar type"
101+
);
85102
}
86103

87104
parent::__construct($options);
@@ -162,6 +179,13 @@ public function setInclusive($inclusive)
162179
*/
163180
public function isValid($value)
164181
{
182+
if ($this->numeric && ! is_numeric($value)) {
183+
return false;
184+
}
185+
if (! $this->numeric && ! is_string($value)) {
186+
return false;
187+
}
188+
165189
$this->setValue($value);
166190

167191
if ($this->getInclusive()) {

test/BetweenTest.php

Lines changed: 65 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,37 +18,76 @@
1818
*/
1919
class BetweenTest extends TestCase
2020
{
21+
public function providerBasic()
22+
{
23+
return [
24+
[
25+
'min' => 1,
26+
'max' => 100,
27+
'inclusive' => true,
28+
'expected' => true,
29+
'values' => [1, 10, 100],
30+
],
31+
[
32+
'min' => 1,
33+
'max' => 100,
34+
'inclusive' => true,
35+
'expected' => false,
36+
'values' => [0, 0.99, 100.01, 101],
37+
],
38+
[
39+
'min' => 1,
40+
'max' => 100,
41+
'inclusive' => false,
42+
'expected' => false,
43+
'values' => [0, 1, 100, 101],
44+
],
45+
[
46+
'min' => 'a',
47+
'max' => 'z',
48+
'inclusive' => true,
49+
'expected' => true,
50+
'values' => ['a', 'b', 'y', 'z'],
51+
],
52+
[
53+
'min' => 'a',
54+
'max' => 'z',
55+
'inclusive' => false,
56+
'expected' => false,
57+
'values' => ['!', 'a', 'z'],
58+
],
59+
[
60+
'min' => 0,
61+
'max' => 99999999,
62+
'inclusive' => true,
63+
'expected' => false,
64+
'values' => ['asdasd', 'q'],
65+
],
66+
[
67+
'min' => 'a',
68+
'max' => 'zzzzz',
69+
'inclusive' => true,
70+
'expected' => false,
71+
'values' => [0, 10, 548],
72+
],
73+
];
74+
}
2175
/**
2276
* Ensures that the validator follows expected behavior
2377
*
78+
* @dataProvider providerBasic
2479
* @return void
2580
*/
26-
public function testBasic()
81+
public function testBasic($min, $max, $inclusive, $expected, $values)
2782
{
28-
/**
29-
* The elements of each array are, in order:
30-
* - minimum
31-
* - maximum
32-
* - inclusive
33-
* - expected validation result
34-
* - array of test input values
35-
*/
36-
$valuesExpected = [
37-
[1, 100, true, true, [1, 10, 100]],
38-
[1, 100, true, false, [0, 0.99, 100.01, 101]],
39-
[1, 100, false, false, [0, 1, 100, 101]],
40-
['a', 'z', true, true, ['a', 'b', 'y', 'z']],
41-
['a', 'z', false, false, ['!', 'a', 'z']]
42-
];
43-
foreach ($valuesExpected as $element) {
44-
$validator = new Between(['min' => $element[0], 'max' => $element[1], 'inclusive' => $element[2]]);
45-
foreach ($element[4] as $input) {
46-
$this->assertEquals(
47-
$element[3],
48-
$validator->isValid($input),
49-
'Failed values: ' . $input . ":" . implode("\n", $validator->getMessages())
50-
);
51-
}
83+
$validator = new Between(['min' => $min, 'max' => $max, 'inclusive' => $inclusive]);
84+
85+
foreach ($values as $input) {
86+
$this->assertEquals(
87+
$expected,
88+
$validator->isValid($input),
89+
'Failed values: ' . $input . ":" . implode("\n", $validator->getMessages())
90+
);
5291
}
5392
}
5493

@@ -117,7 +156,7 @@ public function testEqualsMessageVariables()
117156
public function testMissingMinOrMax(array $args)
118157
{
119158
$this->expectException(InvalidArgumentException::class);
120-
$this->expectExceptionMessage("Missing option. 'min' and 'max' have to be given");
159+
$this->expectExceptionMessage("Missing option : 'min' and 'max' have to be given");
121160

122161
new Between($args);
123162
}

0 commit comments

Comments
 (0)