1818 */
1919class BetweenTest extends TestCase
2020{
21+ public function providerBasic ()
22+ {
23+ return [
24+ 'inclusive-int-valid-floor ' => [
25+ 'min ' => 1 ,
26+ 'max ' => 100 ,
27+ 'inclusive ' => true ,
28+ 'expected ' => true ,
29+ 'value ' => 1 ,
30+ ],
31+ 'inclusive-int-valid-between ' => [
32+ 'min ' => 1 ,
33+ 'max ' => 100 ,
34+ 'inclusive ' => true ,
35+ 'expected ' => true ,
36+ 'value ' => 10 ,
37+ ],
38+ 'inclusive-int-valid-ceiling ' => [
39+ 'min ' => 1 ,
40+ 'max ' => 100 ,
41+ 'inclusive ' => true ,
42+ 'expected ' => true ,
43+ 'value ' => 100 ,
44+ ],
45+ 'inclusive-int-invaild-below ' => [
46+ 'min ' => 1 ,
47+ 'max ' => 100 ,
48+ 'inclusive ' => true ,
49+ 'expected ' => false ,
50+ 'value ' => 0 ,
51+ ],
52+ 'inclusive-int-invalid-below-fractional ' => [
53+ 'min ' => 1 ,
54+ 'max ' => 100 ,
55+ 'inclusive ' => true ,
56+ 'expected ' => false ,
57+ 'value ' => 0.99 ,
58+ ],
59+ 'inclusive-int-invalid-above-fractional ' => [
60+ 'min ' => 1 ,
61+ 'max ' => 100 ,
62+ 'inclusive ' => true ,
63+ 'expected ' => false ,
64+ 'value ' => 100.01 ,
65+ ],
66+ 'inclusive-int-invalid-above ' => [
67+ 'min ' => 1 ,
68+ 'max ' => 100 ,
69+ 'inclusive ' => true ,
70+ 'expected ' => false ,
71+ 'value ' => 101 ,
72+ ],
73+ 'exclusive-int-invalid-below ' => [
74+ 'min ' => 1 ,
75+ 'max ' => 100 ,
76+ 'inclusive ' => false ,
77+ 'expected ' => false ,
78+ 'value ' => 0 ,
79+ ],
80+ 'exclusive-int-invalid-floor ' => [
81+ 'min ' => 1 ,
82+ 'max ' => 100 ,
83+ 'inclusive ' => false ,
84+ 'expected ' => false ,
85+ 'value ' => 1 ,
86+ ],
87+ 'exclusive-int-invalid-ceiling ' => [
88+ 'min ' => 1 ,
89+ 'max ' => 100 ,
90+ 'inclusive ' => false ,
91+ 'expected ' => false ,
92+ 'value ' => 100 ,
93+ ],
94+ 'exclusive-int-invalid-above ' => [
95+ 'min ' => 1 ,
96+ 'max ' => 100 ,
97+ 'inclusive ' => false ,
98+ 'expected ' => false ,
99+ 'value ' => 101 ,
100+ ],
101+ 'inclusive-string-valid-floor ' => [
102+ 'min ' => 'a ' ,
103+ 'max ' => 'z ' ,
104+ 'inclusive ' => true ,
105+ 'expected ' => true ,
106+ 'value ' => 'a ' ,
107+ ],
108+ 'inclusive-string-valid-between ' => [
109+ 'min ' => 'a ' ,
110+ 'max ' => 'z ' ,
111+ 'inclusive ' => true ,
112+ 'expected ' => true ,
113+ 'value ' => 'm ' ,
114+ ],
115+ 'inclusive-string-valid-ceiling ' => [
116+ 'min ' => 'a ' ,
117+ 'max ' => 'z ' ,
118+ 'inclusive ' => true ,
119+ 'expected ' => true ,
120+ 'value ' => 'z ' ,
121+ ],
122+ 'exclusive-string-invalid-out-of-range ' => [
123+ 'min ' => 'a ' ,
124+ 'max ' => 'z ' ,
125+ 'inclusive ' => false ,
126+ 'expected ' => false ,
127+ 'value ' => '! ' ,
128+ ],
129+ 'exclusive-string-invalid-floor ' => [
130+ 'min ' => 'a ' ,
131+ 'max ' => 'z ' ,
132+ 'inclusive ' => false ,
133+ 'expected ' => false ,
134+ 'value ' => 'a ' ,
135+ ],
136+ 'exclusive-string-invalid-ceiling ' => [
137+ 'min ' => 'a ' ,
138+ 'max ' => 'z ' ,
139+ 'inclusive ' => false ,
140+ 'expected ' => false ,
141+ 'value ' => 'z ' ,
142+ ],
143+ 'inclusive-int-invalid-string ' => [
144+ 'min ' => 0 ,
145+ 'max ' => 99999999 ,
146+ 'inclusive ' => true ,
147+ 'expected ' => false ,
148+ 'value ' => 'asdasd ' ,
149+ ],
150+ 'inclusive-int-invalid-char ' => [
151+ 'min ' => 0 ,
152+ 'max ' => 99999999 ,
153+ 'inclusive ' => true ,
154+ 'expected ' => false ,
155+ 'value ' => 'q ' ,
156+ ],
157+ 'inclusive-string-invalid-zero ' => [
158+ 'min ' => 'a ' ,
159+ 'max ' => 'zzzzz ' ,
160+ 'inclusive ' => true ,
161+ 'expected ' => false ,
162+ 'value ' => 0 ,
163+ ],
164+ 'inclusive-string-invalid-non-zero ' => [
165+ 'min ' => 'a ' ,
166+ 'max ' => 'zzzzz ' ,
167+ 'inclusive ' => true ,
168+ 'expected ' => false ,
169+ 'value ' => 10 ,
170+ ],
171+ ];
172+ }
21173 /**
22174 * Ensures that the validator follows expected behavior
23175 *
176+ * @dataProvider providerBasic
177+ * @param int|float|string $min
178+ * @param int|float|string $max
179+ * @param bool $inclusive
180+ * @param bool $expected
181+ * @param mixed $value
24182 * @return void
25183 */
26- public function testBasic ()
184+ public function testBasic ($ min , $ max , $ inclusive , $ expected , $ value )
27185 {
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- }
52- }
186+ $ validator = new Between (['min ' => $ min , 'max ' => $ max , 'inclusive ' => $ inclusive ]);
187+
188+ $ this ->assertSame (
189+ $ expected ,
190+ $ validator ->isValid ($ value ),
191+ 'Failed value: ' . $ value . ': ' . implode ("\n" , $ validator ->getMessages ())
192+ );
53193 }
54194
55195 /**
@@ -117,7 +257,7 @@ public function testEqualsMessageVariables()
117257 public function testMissingMinOrMax (array $ args )
118258 {
119259 $ this ->expectException (InvalidArgumentException::class);
120- $ this ->expectExceptionMessage ("Missing option. 'min' and 'max' have to be given " );
260+ $ this ->expectExceptionMessage ("Missing option: 'min' and 'max' have to be given " );
121261
122262 new Between ($ args );
123263 }
@@ -140,12 +280,34 @@ public function testConstructorCanAcceptInclusiveParameter()
140280 $ this ->assertFalse ($ validator ->getInclusive ());
141281 }
142282
143- public function testConstructWithTravesableOptions ()
283+ public function testConstructWithTraversableOptions ()
144284 {
145285 $ options = new \ArrayObject (['min ' => 1 , 'max ' => 10 , 'inclusive ' => false ]);
146286 $ validator = new Between ($ options );
147287
148288 $ this ->assertTrue ($ validator ->isValid (5 ));
149289 $ this ->assertFalse ($ validator ->isValid (10 ));
150290 }
291+
292+ public function testStringValidatedAgainstNumericMinAndMaxIsInvalidAndReturnsAFailureMessage ()
293+ {
294+ $ validator = new Between (['min ' => 1 , 'max ' => 10 ]);
295+ $ this ->assertFalse ($ validator ->isValid ('a ' ));
296+ $ messages = $ validator ->getMessages ();
297+ $ this ->assertContains (
298+ 'The min ( \'1 \') and max ( \'10 \') values are numeric, but the input is not ' ,
299+ $ messages
300+ );
301+ }
302+
303+ public function testNumericValidatedAgainstStringMinAndMaxIsInvalidAndReturnsAFailureMessage ()
304+ {
305+ $ validator = new Between (['min ' => 'a ' , 'max ' => 'z ' ]);
306+ $ this ->assertFalse ($ validator ->isValid (10 ));
307+ $ messages = $ validator ->getMessages ();
308+ $ this ->assertContains (
309+ 'The min ( \'a \') and max ( \'z \') values are non-numeric strings, but the input is not a string ' ,
310+ $ messages
311+ );
312+ }
151313}
0 commit comments