3 namespace Wikimedia\ParamValidator\TypeDef
;
5 use Wikimedia\ParamValidator\ParamValidator
;
6 use Wikimedia\ParamValidator\ValidationException
;
9 * @covers Wikimedia\ParamValidator\TypeDef\IntegerDef
11 class IntegerDefTest
extends TypeDefTestCase
{
13 protected static $testClass = IntegerDef
::class;
16 * @param string $v Representing a positive integer
17 * @return string Representing $v + 1
19 private static function plusOne( $v ) {
20 for ( $i = strlen( $v ) - 1; $i >= 0; $i-- ) {
21 if ( $v[$i] === '9' ) {
31 public function provideValidate() {
32 $badinteger = new ValidationException( 'test', '...', [], 'badinteger', [] );
33 $belowminimum = new ValidationException(
34 'test', '...', [], 'belowminimum', [ 'min' => 0, 'max' => 2, 'max2' => '' ]
36 $abovemaximum = new ValidationException(
37 'test', '...', [], 'abovemaximum', [ 'min' => 0, 'max' => 2, 'max2' => '' ]
39 $abovemaximum2 = new ValidationException(
40 'test', '...', [], 'abovemaximum', [ 'min' => 0, 'max' => 2, 'max2' => 4 ]
42 $abovehighmaximum = new ValidationException(
43 'test', '...', [], 'abovehighmaximum', [ 'min' => 0, 'max' => 2, 'max2' => 4 ]
45 $asWarn = function ( ValidationException
$ex ) {
46 return [ $ex->getFailureCode() ] +
$ex->getFailureData();
50 IntegerDef
::PARAM_MIN
=> 0,
51 IntegerDef
::PARAM_MAX
=> 2,
54 IntegerDef
::PARAM_MIN
=> 0,
55 IntegerDef
::PARAM_MAX
=> 2,
56 IntegerDef
::PARAM_MAX2
=> 4,
59 IntegerDef
::PARAM_IGNORE_RANGE
=> true,
61 $usehigh = [ 'useHighLimits' => true ];
69 [ (string)PHP_INT_MAX
, PHP_INT_MAX
],
70 [ '0000' . PHP_INT_MAX
, PHP_INT_MAX
],
71 [ (string)PHP_INT_MIN
, PHP_INT_MIN
],
72 [ '-0000' . substr( PHP_INT_MIN
, 1 ), PHP_INT_MIN
],
74 'Overflow' => [ self
::plusOne( (string)PHP_INT_MAX
), $badinteger ],
75 'Negative overflow' => [ '-' . self
::plusOne( substr( PHP_INT_MIN
, 1 ) ), $badinteger ],
77 'Float' => [ '1.5', $badinteger ],
78 'Float (e notation)' => [ '1e1', $badinteger ],
79 'Bad sign (space)' => [ ' 1', $badinteger ],
80 'Bad sign (newline)' => [ "\n1", $badinteger ],
81 'Bogus value' => [ 'foo', $badinteger ],
82 'Bogus value (2)' => [ '1foo', $badinteger ],
83 'Hex value' => [ '0x123', $badinteger ],
84 'Newline' => [ "1\n", $badinteger ],
86 'Ok with range' => [ '1', 1, $minmax ],
87 'Below minimum' => [ '-1', $belowminimum, $minmax ],
88 'Below minimum, ignored' => [ '-1', 0, $minmax +
$ignore, [], [ $asWarn( $belowminimum ) ] ],
89 'Above maximum' => [ '3', $abovemaximum, $minmax ],
90 'Above maximum, ignored' => [ '3', 2, $minmax +
$ignore, [], [ $asWarn( $abovemaximum ) ] ],
91 'Not above max2 but can\'t use it' => [ '3', $abovemaximum2, $minmax2, [] ],
92 'Not above max2 but can\'t use it, ignored'
93 => [ '3', 2, $minmax2 +
$ignore, [], [ $asWarn( $abovemaximum2 ) ] ],
94 'Not above max2' => [ '3', 3, $minmax2, $usehigh ],
95 'Above max2' => [ '5', $abovehighmaximum, $minmax2, $usehigh ],
97 => [ '5', 4, $minmax2 +
$ignore, $usehigh, [ $asWarn( $abovehighmaximum ) ] ],
101 public function provideNormalizeSettings() {
105 [ IntegerDef
::PARAM_MAX
=> 2 ],
106 [ IntegerDef
::PARAM_MAX
=> 2 ],
109 [ IntegerDef
::PARAM_MIN
=> 1, IntegerDef
::PARAM_MAX
=> 2, IntegerDef
::PARAM_MAX2
=> 4 ],
110 [ IntegerDef
::PARAM_MIN
=> 1, IntegerDef
::PARAM_MAX
=> 2, IntegerDef
::PARAM_MAX2
=> 4 ],
113 [ IntegerDef
::PARAM_MIN
=> 1, IntegerDef
::PARAM_MAX
=> 4, IntegerDef
::PARAM_MAX2
=> 2 ],
114 [ IntegerDef
::PARAM_MIN
=> 1, IntegerDef
::PARAM_MAX
=> 4, IntegerDef
::PARAM_MAX2
=> 4 ],
117 [ IntegerDef
::PARAM_MAX2
=> 2 ],
123 public function provideDescribeSettings() {
125 'Basic' => [ [], [], [] ],
127 [ ParamValidator
::PARAM_DEFAULT
=> 123 ],
128 [ 'default' => '123' ],
129 [ 'default' => [ 'value' => '123' ] ],
132 [ ParamValidator
::PARAM_DEFAULT
=> 123, IntegerDef
::PARAM_MIN
=> 0 ],
133 [ 'default' => '123', 'min' => 0 ],
134 [ 'default' => [ 'value' => '123' ], 'min' => [ 'min' => 0, 'max' => '', 'max2' => '' ] ],
137 [ IntegerDef
::PARAM_MAX
=> 2 ],
139 [ 'max' => [ 'min' => '', 'max' => 2, 'max2' => '' ] ],
142 [ IntegerDef
::PARAM_MAX
=> 2, IntegerDef
::PARAM_MAX2
=> 4 ],
143 [ 'max' => 2, 'max2' => 4 ],
144 [ 'max2' => [ 'min' => '', 'max' => 2, 'max2' => 4 ] ],
147 [ IntegerDef
::PARAM_MIN
=> 0, IntegerDef
::PARAM_MAX
=> 2 ],
148 [ 'min' => 0, 'max' => 2 ],
149 [ 'minmax' => [ 'min' => 0, 'max' => 2, 'max2' => '' ] ],
152 [ IntegerDef
::PARAM_MIN
=> 0, IntegerDef
::PARAM_MAX
=> 2, IntegerDef
::PARAM_MAX2
=> 4 ],
153 [ 'min' => 0, 'max' => 2, 'max2' => 4 ],
154 [ 'minmax2' => [ 'min' => 0, 'max' => 2, 'max2' => 4 ] ],