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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
|
<?php include 'helpers/config.php';
class BookLength extends ActiveRecord\Model { static $table = 'books'; static $validates_length_of = array(); }
class BookSize extends ActiveRecord\Model { static $table = 'books'; static $validates_size_of = array(); }
class ValidatesLengthOfTest extends DatabaseTest { public function set_up($connection_name=null) { parent::set_up($connection_name); BookLength::$validates_length_of[0] = array('name', 'allow_blank' => false, 'allow_null' => false); } public function test_within() { BookLength::$validates_length_of[0]['within'] = array(1, 5); $book = new BookLength; $book->name = '12345'; $book->save(); $this->assert_false($book->errors->is_invalid('name')); }
public function test_within_error_message() { BookLength::$validates_length_of[0]['within'] = array(2,5); $book = new BookLength(); $book->name = '1'; $book->is_valid(); $this->assert_equals(array('Name is too short (minimum is 2 characters)'),$book->errors->full_messages());
$book->name = '123456'; $book->is_valid(); $this->assert_equals(array('Name is too long (maximum is 5 characters)'),$book->errors->full_messages()); }
public function test_within_custom_error_message() { BookLength::$validates_length_of[0]['within'] = array(2,5); BookLength::$validates_length_of[0]['too_short'] = 'is too short'; BookLength::$validates_length_of[0]['message'] = 'is not between 2 and 5 characters'; $book = new BookLength(); $book->name = '1'; $book->is_valid(); $this->assert_equals(array('Name is not between 2 and 5 characters'),$book->errors->full_messages());
$book->name = '123456'; $book->is_valid(); $this->assert_equals(array('Name is not between 2 and 5 characters'),$book->errors->full_messages()); } public function test_valid_in() { BookLength::$validates_length_of[0]['in'] = array(1, 5); $book = new BookLength; $book->name = '12345'; $book->save(); $this->assert_false($book->errors->is_invalid('name')); }
public function test_aliased_size_of() { BookSize::$validates_size_of = BookLength::$validates_length_of; BookSize::$validates_size_of[0]['within'] = array(1, 5); $book = new BookSize; $book->name = '12345'; $book->save(); $this->assert_false($book->errors->is_invalid('name')); }
public function test_invalid_within_and_in() { BookLength::$validates_length_of[0]['within'] = array(1, 3); $book = new BookLength; $book->name = 'four'; $book->save(); $this->assert_true($book->errors->is_invalid('name'));
$this->set_up(); BookLength::$validates_length_of[0]['in'] = array(1, 3); $book = new BookLength; $book->name = 'four'; $book->save(); $this->assert_true($book->errors->is_invalid('name')); }
public function test_valid_null() { BookLength::$validates_length_of[0]['within'] = array(1, 3); BookLength::$validates_length_of[0]['allow_null'] = true;
$book = new BookLength; $book->name = null; $book->save(); $this->assert_false($book->errors->is_invalid('name')); }
public function test_valid_blank() { BookLength::$validates_length_of[0]['within'] = array(1, 3); BookLength::$validates_length_of[0]['allow_blank'] = true;
$book = new BookLength; $book->name = ''; $book->save(); $this->assert_false($book->errors->is_invalid('name')); }
public function test_invalid_blank() { BookLength::$validates_length_of[0]['within'] = array(1, 3);
$book = new BookLength; $book->name = ''; $book->save(); $this->assert_true($book->errors->is_invalid('name')); $this->assert_equals('is too short (minimum is 1 characters)', $book->errors->on('name')); }
public function test_invalid_null_within() { BookLength::$validates_length_of[0]['within'] = array(1, 3);
$book = new BookLength; $book->name = null; $book->save(); $this->assert_true($book->errors->is_invalid('name')); $this->assert_equals('is too short (minimum is 1 characters)', $book->errors->on('name')); } public function test_invalid_null_minimum() { BookLength::$validates_length_of[0]['minimum'] = 1;
$book = new BookLength; $book->name = null; $book->save(); $this->assert_true($book->errors->is_invalid('name')); $this->assert_equals('is too short (minimum is 1 characters)', $book->errors->on('name')); } public function test_valid_null_maximum() { BookLength::$validates_length_of[0]['maximum'] = 1;
$book = new BookLength; $book->name = null; $book->save(); $this->assert_false($book->errors->is_invalid('name')); }
public function test_float_as_impossible_range_option() { BookLength::$validates_length_of[0]['within'] = array(1, 3.6); $book = new BookLength; $book->name = '123'; try { $book->save(); } catch (ActiveRecord\ValidationsArgumentError $e) { $this->assert_equals('maximum value cannot use a float for length.', $e->getMessage()); }
$this->set_up(); BookLength::$validates_length_of[0]['is'] = 1.8; $book = new BookLength; $book->name = '123'; try { $book->save(); } catch (ActiveRecord\ValidationsArgumentError $e) { $this->assert_equals('is value cannot use a float for length.', $e->getMessage()); return; }
$this->fail('An expected exception has not be raised.'); }
public function test_signed_integer_as_impossible_within_option() { BookLength::$validates_length_of[0]['within'] = array(-1, 3);
$book = new BookLength; $book->name = '123'; try { $book->save(); } catch (ActiveRecord\ValidationsArgumentError $e) { $this->assert_equals('minimum value cannot use a signed integer.', $e->getMessage()); return; }
$this->fail('An expected exception has not be raised.'); }
public function test_signed_integer_as_impossible_is_option() { BookLength::$validates_length_of[0]['is'] = -8;
$book = new BookLength; $book->name = '123'; try { $book->save(); } catch (ActiveRecord\ValidationsArgumentError $e) { $this->assert_equals('is value cannot use a signed integer.', $e->getMessage()); return; }
$this->fail('An expected exception has not be raised.'); }
public function test_lack_of_option() { try { $book = new BookLength; $book->name = null; $book->save(); } catch (ActiveRecord\ValidationsArgumentError $e) { $this->assert_equals('Range unspecified. Specify the [within], [maximum], or [is] option.', $e->getMessage()); return; }
$this->fail('An expected exception has not be raised.'); }
public function test_too_many_options() { BookLength::$validates_length_of[0]['within'] = array(1, 3); BookLength::$validates_length_of[0]['in'] = array(1, 3);
try { $book = new BookLength; $book->name = null; $book->save(); } catch (ActiveRecord\ValidationsArgumentError $e) { $this->assert_equals('Too many range options specified. Choose only one.', $e->getMessage()); return; }
$this->fail('An expected exception has not be raised.'); }
public function test_too_many_options_with_different_option_types() { BookLength::$validates_length_of[0]['within'] = array(1, 3); BookLength::$validates_length_of[0]['is'] = 3;
try { $book = new BookLength; $book->name = null; $book->save(); } catch (ActiveRecord\ValidationsArgumentError $e) { $this->assert_equals('Too many range options specified. Choose only one.', $e->getMessage()); return; }
$this->fail('An expected exception has not be raised.'); }
/** * @expectedException ActiveRecord\ValidationsArgumentError */ public function test_with_option_as_non_numeric() { BookLength::$validates_length_of[0]['with'] = array('test');
$book = new BookLength; $book->name = null; $book->save(); }
/** * @expectedException ActiveRecord\ValidationsArgumentError */ public function test_with_option_as_non_numeric_non_array() { BookLength::$validates_length_of[0]['with'] = 'test';
$book = new BookLength; $book->name = null; $book->save(); }
public function test_validates_length_of_maximum() { BookLength::$validates_length_of[0] = array('name', 'maximum' => 10); $book = new BookLength(array('name' => '12345678901')); $book->is_valid(); $this->assert_equals(array("Name is too long (maximum is 10 characters)"),$book->errors->full_messages()); }
public function test_validates_length_of_minimum() { BookLength::$validates_length_of[0] = array('name', 'minimum' => 2); $book = new BookLength(array('name' => '1')); $book->is_valid(); $this->assert_equals(array("Name is too short (minimum is 2 characters)"),$book->errors->full_messages()); } public function test_validates_length_of_min_max_custom_message() { BookLength::$validates_length_of[0] = array('name', 'maximum' => 10, 'message' => 'is far too long'); $book = new BookLength(array('name' => '12345678901')); $book->is_valid(); $this->assert_equals(array("Name is far too long"),$book->errors->full_messages());
BookLength::$validates_length_of[0] = array('name', 'minimum' => 10, 'message' => 'is far too short'); $book = new BookLength(array('name' => '123456789')); $book->is_valid(); $this->assert_equals(array("Name is far too short"),$book->errors->full_messages()); } public function test_validates_length_of_min_max_custom_message_overridden() { BookLength::$validates_length_of[0] = array('name', 'minimum' => 10, 'too_short' => 'is too short', 'message' => 'is custom message'); $book = new BookLength(array('name' => '123456789')); $book->is_valid(); $this->assert_equals(array("Name is custom message"),$book->errors->full_messages()); }
public function test_validates_length_of_is() { BookLength::$validates_length_of[0] = array('name', 'is' => 2); $book = new BookLength(array('name' => '123')); $book->is_valid(); $this->assert_equals(array("Name is the wrong length (should be 2 characters)"),$book->errors->full_messages()); } }; ?>
|