a tiny mvc framework for php using php-activerecord
1<?php
2include 'helpers/config.php';
3
4class BookLength extends ActiveRecord\Model
5{
6 static $table = 'books';
7 static $validates_length_of = array();
8}
9
10class BookSize extends ActiveRecord\Model
11{
12 static $table = 'books';
13 static $validates_size_of = array();
14}
15
16class ValidatesLengthOfTest extends DatabaseTest
17{
18 public function set_up($connection_name=null)
19 {
20 parent::set_up($connection_name);
21 BookLength::$validates_length_of[0] = array('name', 'allow_blank' => false, 'allow_null' => false);
22 }
23
24 public function test_within()
25 {
26 BookLength::$validates_length_of[0]['within'] = array(1, 5);
27 $book = new BookLength;
28 $book->name = '12345';
29 $book->save();
30 $this->assert_false($book->errors->is_invalid('name'));
31 }
32
33 public function test_within_error_message()
34 {
35 BookLength::$validates_length_of[0]['within'] = array(2,5);
36 $book = new BookLength();
37 $book->name = '1';
38 $book->is_valid();
39 $this->assert_equals(array('Name is too short (minimum is 2 characters)'),$book->errors->full_messages());
40
41 $book->name = '123456';
42 $book->is_valid();
43 $this->assert_equals(array('Name is too long (maximum is 5 characters)'),$book->errors->full_messages());
44 }
45
46 public function test_within_custom_error_message()
47 {
48 BookLength::$validates_length_of[0]['within'] = array(2,5);
49 BookLength::$validates_length_of[0]['too_short'] = 'is too short';
50 BookLength::$validates_length_of[0]['message'] = 'is not between 2 and 5 characters';
51 $book = new BookLength();
52 $book->name = '1';
53 $book->is_valid();
54 $this->assert_equals(array('Name is not between 2 and 5 characters'),$book->errors->full_messages());
55
56 $book->name = '123456';
57 $book->is_valid();
58 $this->assert_equals(array('Name is not between 2 and 5 characters'),$book->errors->full_messages());
59 }
60
61 public function test_valid_in()
62 {
63 BookLength::$validates_length_of[0]['in'] = array(1, 5);
64 $book = new BookLength;
65 $book->name = '12345';
66 $book->save();
67 $this->assert_false($book->errors->is_invalid('name'));
68 }
69
70 public function test_aliased_size_of()
71 {
72 BookSize::$validates_size_of = BookLength::$validates_length_of;
73 BookSize::$validates_size_of[0]['within'] = array(1, 5);
74 $book = new BookSize;
75 $book->name = '12345';
76 $book->save();
77 $this->assert_false($book->errors->is_invalid('name'));
78 }
79
80 public function test_invalid_within_and_in()
81 {
82 BookLength::$validates_length_of[0]['within'] = array(1, 3);
83 $book = new BookLength;
84 $book->name = 'four';
85 $book->save();
86 $this->assert_true($book->errors->is_invalid('name'));
87
88 $this->set_up();
89 BookLength::$validates_length_of[0]['in'] = array(1, 3);
90 $book = new BookLength;
91 $book->name = 'four';
92 $book->save();
93 $this->assert_true($book->errors->is_invalid('name'));
94 }
95
96 public function test_valid_null()
97 {
98 BookLength::$validates_length_of[0]['within'] = array(1, 3);
99 BookLength::$validates_length_of[0]['allow_null'] = true;
100
101 $book = new BookLength;
102 $book->name = null;
103 $book->save();
104 $this->assert_false($book->errors->is_invalid('name'));
105 }
106
107 public function test_valid_blank()
108 {
109 BookLength::$validates_length_of[0]['within'] = array(1, 3);
110 BookLength::$validates_length_of[0]['allow_blank'] = true;
111
112 $book = new BookLength;
113 $book->name = '';
114 $book->save();
115 $this->assert_false($book->errors->is_invalid('name'));
116 }
117
118 public function test_invalid_blank()
119 {
120 BookLength::$validates_length_of[0]['within'] = array(1, 3);
121
122 $book = new BookLength;
123 $book->name = '';
124 $book->save();
125 $this->assert_true($book->errors->is_invalid('name'));
126 $this->assert_equals('is too short (minimum is 1 characters)', $book->errors->on('name'));
127 }
128
129 public function test_invalid_null_within()
130 {
131 BookLength::$validates_length_of[0]['within'] = array(1, 3);
132
133 $book = new BookLength;
134 $book->name = null;
135 $book->save();
136 $this->assert_true($book->errors->is_invalid('name'));
137 $this->assert_equals('is too short (minimum is 1 characters)', $book->errors->on('name'));
138 }
139
140 public function test_invalid_null_minimum()
141 {
142 BookLength::$validates_length_of[0]['minimum'] = 1;
143
144 $book = new BookLength;
145 $book->name = null;
146 $book->save();
147 $this->assert_true($book->errors->is_invalid('name'));
148 $this->assert_equals('is too short (minimum is 1 characters)', $book->errors->on('name'));
149
150 }
151
152 public function test_valid_null_maximum()
153 {
154 BookLength::$validates_length_of[0]['maximum'] = 1;
155
156 $book = new BookLength;
157 $book->name = null;
158 $book->save();
159 $this->assert_false($book->errors->is_invalid('name'));
160 }
161
162 public function test_float_as_impossible_range_option()
163 {
164 BookLength::$validates_length_of[0]['within'] = array(1, 3.6);
165 $book = new BookLength;
166 $book->name = '123';
167 try {
168 $book->save();
169 } catch (ActiveRecord\ValidationsArgumentError $e) {
170 $this->assert_equals('maximum value cannot use a float for length.', $e->getMessage());
171 }
172
173 $this->set_up();
174 BookLength::$validates_length_of[0]['is'] = 1.8;
175 $book = new BookLength;
176 $book->name = '123';
177 try {
178 $book->save();
179 } catch (ActiveRecord\ValidationsArgumentError $e) {
180 $this->assert_equals('is value cannot use a float for length.', $e->getMessage());
181 return;
182 }
183
184 $this->fail('An expected exception has not be raised.');
185 }
186
187 public function test_signed_integer_as_impossible_within_option()
188 {
189 BookLength::$validates_length_of[0]['within'] = array(-1, 3);
190
191 $book = new BookLength;
192 $book->name = '123';
193 try {
194 $book->save();
195 } catch (ActiveRecord\ValidationsArgumentError $e) {
196 $this->assert_equals('minimum value cannot use a signed integer.', $e->getMessage());
197 return;
198 }
199
200 $this->fail('An expected exception has not be raised.');
201 }
202
203 public function test_signed_integer_as_impossible_is_option()
204 {
205 BookLength::$validates_length_of[0]['is'] = -8;
206
207 $book = new BookLength;
208 $book->name = '123';
209 try {
210 $book->save();
211 } catch (ActiveRecord\ValidationsArgumentError $e) {
212 $this->assert_equals('is value cannot use a signed integer.', $e->getMessage());
213 return;
214 }
215
216 $this->fail('An expected exception has not be raised.');
217 }
218
219 public function test_lack_of_option()
220 {
221 try {
222 $book = new BookLength;
223 $book->name = null;
224 $book->save();
225 } catch (ActiveRecord\ValidationsArgumentError $e) {
226 $this->assert_equals('Range unspecified. Specify the [within], [maximum], or [is] option.', $e->getMessage());
227 return;
228 }
229
230 $this->fail('An expected exception has not be raised.');
231 }
232
233 public function test_too_many_options()
234 {
235 BookLength::$validates_length_of[0]['within'] = array(1, 3);
236 BookLength::$validates_length_of[0]['in'] = array(1, 3);
237
238 try {
239 $book = new BookLength;
240 $book->name = null;
241 $book->save();
242 } catch (ActiveRecord\ValidationsArgumentError $e) {
243 $this->assert_equals('Too many range options specified. Choose only one.', $e->getMessage());
244 return;
245 }
246
247 $this->fail('An expected exception has not be raised.');
248 }
249
250 public function test_too_many_options_with_different_option_types()
251 {
252 BookLength::$validates_length_of[0]['within'] = array(1, 3);
253 BookLength::$validates_length_of[0]['is'] = 3;
254
255 try {
256 $book = new BookLength;
257 $book->name = null;
258 $book->save();
259 } catch (ActiveRecord\ValidationsArgumentError $e) {
260 $this->assert_equals('Too many range options specified. Choose only one.', $e->getMessage());
261 return;
262 }
263
264 $this->fail('An expected exception has not be raised.');
265 }
266
267 /**
268 * @expectedException ActiveRecord\ValidationsArgumentError
269 */
270 public function test_with_option_as_non_numeric()
271 {
272 BookLength::$validates_length_of[0]['with'] = array('test');
273
274 $book = new BookLength;
275 $book->name = null;
276 $book->save();
277 }
278
279 /**
280 * @expectedException ActiveRecord\ValidationsArgumentError
281 */
282 public function test_with_option_as_non_numeric_non_array()
283 {
284 BookLength::$validates_length_of[0]['with'] = 'test';
285
286 $book = new BookLength;
287 $book->name = null;
288 $book->save();
289 }
290
291 public function test_validates_length_of_maximum()
292 {
293 BookLength::$validates_length_of[0] = array('name', 'maximum' => 10);
294 $book = new BookLength(array('name' => '12345678901'));
295 $book->is_valid();
296 $this->assert_equals(array("Name is too long (maximum is 10 characters)"),$book->errors->full_messages());
297 }
298
299 public function test_validates_length_of_minimum()
300 {
301 BookLength::$validates_length_of[0] = array('name', 'minimum' => 2);
302 $book = new BookLength(array('name' => '1'));
303 $book->is_valid();
304 $this->assert_equals(array("Name is too short (minimum is 2 characters)"),$book->errors->full_messages());
305 }
306
307 public function test_validates_length_of_min_max_custom_message()
308 {
309 BookLength::$validates_length_of[0] = array('name', 'maximum' => 10, 'message' => 'is far too long');
310 $book = new BookLength(array('name' => '12345678901'));
311 $book->is_valid();
312 $this->assert_equals(array("Name is far too long"),$book->errors->full_messages());
313
314 BookLength::$validates_length_of[0] = array('name', 'minimum' => 10, 'message' => 'is far too short');
315 $book = new BookLength(array('name' => '123456789'));
316 $book->is_valid();
317 $this->assert_equals(array("Name is far too short"),$book->errors->full_messages());
318 }
319
320 public function test_validates_length_of_min_max_custom_message_overridden()
321 {
322 BookLength::$validates_length_of[0] = array('name', 'minimum' => 10, 'too_short' => 'is too short', 'message' => 'is custom message');
323 $book = new BookLength(array('name' => '123456789'));
324 $book->is_valid();
325 $this->assert_equals(array("Name is custom message"),$book->errors->full_messages());
326 }
327
328 public function test_validates_length_of_is()
329 {
330 BookLength::$validates_length_of[0] = array('name', 'is' => 2);
331 $book = new BookLength(array('name' => '123'));
332 $book->is_valid();
333 $this->assert_equals(array("Name is the wrong length (should be 2 characters)"),$book->errors->full_messages());
334 }
335};
336?>