1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
<?php include 'helpers/config.php';
class BookNumericality extends ActiveRecord\Model { static $table_name = 'books';
static $validates_numericality_of = array( array('name') ); }
class ValidatesNumericalityOfTest extends DatabaseTest { static $NULL = array(null); static $BLANK = array("", " ", " \t \r \n"); static $FLOAT_STRINGS = array('0.0','+0.0','-0.0','10.0','10.5','-10.5','-0.0001','-090.1'); static $INTEGER_STRINGS = array('0', '+0', '-0', '10', '+10', '-10', '0090', '-090'); static $FLOATS = array(0.0, 10.0, 10.5, -10.5, -0.0001); static $INTEGERS = array(0, 10, -10); static $JUNK = array("not a number", "42 not a number", "00-1", "--3", "+-3", "+3-1", "-+019.0", "12.12.13.12", "123\nnot a number");
public function set_up($connection_name=null) { parent::set_up($connection_name); BookNumericality::$validates_numericality_of = array( array('numeric_test') ); }
private function assert_validity($value, $boolean, $msg=null) { $book = new BookNumericality; $book->numeric_test = $value;
if ($boolean == 'valid') { $this->assert_true($book->save()); $this->assert_false($book->errors->is_invalid('numeric_test')); } else { $this->assert_false($book->save()); $this->assert_true($book->errors->is_invalid('numeric_test'));
if (!is_null($msg)) $this->assert_same($msg, $book->errors->on('numeric_test')); } }
private function assert_invalid($values, $msg=null) { foreach ($values as $value) $this->assert_validity($value, 'invalid', $msg); }
private function assert_valid($values, $msg=null) { foreach ($values as $value) $this->assert_validity($value, 'valid', $msg); }
public function test_numericality() { //$this->assert_invalid(array("0xdeadbeef"));
$this->assert_valid(array_merge(self::$FLOATS, self::$INTEGERS)); $this->assert_invalid(array_merge(self::$NULL, self::$BLANK, self::$JUNK)); }
public function test_not_anumber() { $this->assert_invalid(array('blah'), 'is not a number'); }
public function test_invalid_null() { $this->assert_invalid(array(null)); }
public function test_invalid_blank() { $this->assert_invalid(array(' ', ' '), 'is not a number'); }
public function test_invalid_whitespace() { $this->assert_invalid(array('')); }
public function test_valid_null() { BookNumericality::$validates_numericality_of[0]['allow_null'] = true; $this->assert_valid(array(null)); }
public function test_only_integer() { BookNumericality::$validates_numericality_of[0]['only_integer'] = true;
$this->assert_valid(array(1, '1')); $this->assert_invalid(array(1.5, '1.5')); }
public function test_only_integer_matching_does_not_ignore_other_options() { BookNumericality::$validates_numericality_of[0]['only_integer'] = true; BookNumericality::$validates_numericality_of[0]['greater_than'] = 0;
$this->assert_invalid(array(-1,'-1')); }
public function test_greater_than() { BookNumericality::$validates_numericality_of[0]['greater_than'] = 5;
$this->assert_valid(array(6, '7')); $this->assert_invalid(array(5, '5'), 'must be greater than 5'); }
public function test_greater_than_or_equal_to() { BookNumericality::$validates_numericality_of[0]['greater_than_or_equal_to'] = 5;
$this->assert_valid(array(5, 5.1, '5.1')); $this->assert_invalid(array(-50, 4.9, '4.9','-5.1')); }
public function test_less_than() { BookNumericality::$validates_numericality_of[0]['less_than'] = 5;
$this->assert_valid(array(4.9, -1, 0, '-5')); $this->assert_invalid(array(5, '5'), 'must be less than 5'); }
public function test_less_than_or_equal_to() { BookNumericality::$validates_numericality_of[0]['less_than_or_equal_to'] = 5;
$this->assert_valid(array(5, -1, 0, 4.9, '-5')); $this->assert_invalid(array('8', 5.1), 'must be less than or equal to 5'); }
public function test_greater_than_less_than_and_even() { BookNumericality::$validates_numericality_of[0] = array('numeric_test', 'greater_than' => 1, 'less_than' => 4, 'even' => true);
$this->assert_valid(array(2)); $this->assert_invalid(array(1,3,4)); }
public function test_custom_message() { BookNumericality::$validates_numericality_of = array( array('numeric_test', 'message' => 'Hello') ); $book = new BookNumericality(array('numeric_test' => 'NaN')); $book->is_valid(); $this->assert_equals(array('Numeric test Hello'),$book->errors->full_messages()); } };
array_merge(ValidatesNumericalityOfTest::$INTEGERS, ValidatesNumericalityOfTest::$INTEGER_STRINGS); array_merge(ValidatesNumericalityOfTest::$FLOATS, ValidatesNumericalityOfTest::$FLOAT_STRINGS); ?>
|