source: trunk/tests/FormValidatorTest.php @ 169

Last change on this file since 169 was 169, checked in by scdev, 18 years ago

${1}

File size: 10.0 KB
Line 
1<?php
2/**
3 * PHPUnit test case for FormValidator
4 *
5 * The method skeletons below need to be filled in with
6 * real data so that the tests will run correctly. Replace
7 * all EXPECTED_VAL and PARAM strings with real data.
8 *
9 * Created with PHPUnit_Skeleton on 2005-08-09
10 */
11require_once 'PHPUnit.php';
12class FormValidatorTest extends PHPUnit_TestCase {
13
14    var $FormValidator;
15
16    function FormValidatorTest($name)
17    {
18        $this->PHPUnit_TestCase($name);
19    }
20
21    function setUp()
22    {
23        require dirname(__FILE__) . '/_config.inc.php';
24        require_once '../lib/FormValidator.inc.php';
25        $this->FormValidator =& new FormValidator();
26    }
27
28    function tearDown()
29    {
30        unset($this->FormValidator);
31    }
32
33    function test_geterrorlist()
34    {
35        $this->FormValidator->addError('some_field', _("This is an error."), MSG_ERR, __FILE__, __LINE__);
36        $err_list = $this->FormValidator->geterrorlist();
37        $this->assertTrue($err_list[0]['message'] === _("This is an error."));
38    }
39
40    function test_adderror()
41    {
42        $this->FormValidator->addError('some_field', _("This is an error."), MSG_ERR, __FILE__, __LINE__);
43        $this->assertTrue($this->FormValidator->errors[0]['message'] === _("This is an error."));
44    }
45
46    function test_anyerrors()
47    {
48        $result = $this->FormValidator->anyerrors();
49        $this->assertFalse($result);
50        $this->FormValidator->addError('some_field', _("This is an error."), MSG_ERR, __FILE__, __LINE__);
51        $result = $this->FormValidator->anyerrors();
52        $this->assertTrue($result);
53    }
54
55    function test_reseterrorlist()
56    {
57        $this->FormValidator->addError('some_field', _("This is an error."), MSG_ERR, __FILE__, __LINE__);
58        $this->FormValidator->reseterrorlist();
59        $this->assertFalse($this->FormValidator->anyerrors());
60    }
61
62    function test_printerrormessages()
63    {
64        $this->FormValidator->addError('some_field', _("This is an error."), MSG_ERR, __FILE__, __LINE__);
65        ob_start();
66        $this->FormValidator->printerrormessages();
67        $result = ob_get_clean();
68        $this->assertContains('This is an error', $result);
69    }
70
71    function test_err()
72    {
73        $this->FormValidator->addError('some_field', _("This is an error."), MSG_ERR, __FILE__, __LINE__);
74        ob_start();
75        $this->FormValidator->err('some_field', 'printthis');
76        $result = ob_get_clean();
77        $this->assertContains('printthis', $result);
78    }
79
80    function test_notempty()
81    {
82        // This should not generate any error.
83        $_POST['some_field'] = 'a non empty string';
84        $this->FormValidator->isempty('some_field', _("Error message"));
85        $this->assertFalse($this->FormValidator->anyerrors());
86       
87        // This one is an error!
88        $_POST['some_field'] = '';
89        $this->FormValidator->isempty('some_field', _("Error message"));
90        $this->assertTrue($this->FormValidator->anyerrors());
91    }
92
93    function test_isempty()
94    {
95        // This should not generate any error.
96        $_POST['some_field'] = 'a non empty string';
97        $this->FormValidator->isempty('some_field', _("Error message"));
98        $this->assertFalse($this->FormValidator->anyerrors());
99       
100        // This one is an error!
101        $_POST['some_field'] = '';
102        $this->FormValidator->isempty('some_field', _("Error message"));
103        $this->assertTrue($this->FormValidator->anyerrors());
104    }
105
106    function test_isstring()
107    {
108        // This should not generate any error.
109        $_POST['some_field'] = 'this is a string';
110        $this->FormValidator->isstring('some_field', _("Error message"));
111        $this->assertFalse($this->FormValidator->anyerrors());
112       
113        // This one is an error!
114        $_POST['some_field'] = 12.3248; // not a string.
115        $this->FormValidator->isstring('some_field', _("Error message"));
116        $this->assertTrue($this->FormValidator->anyerrors());
117    }
118
119    function test_isnumber()
120    {
121        // This should not generate any error.
122        $_POST['some_field'] = '1234.453';
123        $this->FormValidator->isnumber('some_field', _("Error message"));
124        $this->assertFalse($this->FormValidator->anyerrors());
125       
126        // This one is an error!
127        $_POST['some_field'] = 'not a number';
128        $this->FormValidator->isnumber('some_field', _("Error message"));
129        $this->assertTrue($this->FormValidator->anyerrors());
130    }
131
132    function test_isinteger()
133    {
134        // This should not generate any error.
135        $_POST['some_field'] = '1234';
136        $this->FormValidator->isinteger('some_field', _("Error message"));
137        $this->assertFalse($this->FormValidator->anyerrors());
138       
139        // This one is an error!
140        $_POST['some_field'] = '1234.1';
141        $this->FormValidator->isinteger('some_field', _("Error message"));
142        $this->assertTrue($this->FormValidator->anyerrors());
143    }
144
145    function test_isfloat()
146    {
147        // This should not generate any error.
148        $_POST['some_field'] = '123.1234';
149        $this->FormValidator->isfloat('some_field', _("Error message"));
150        $this->assertFalse($this->FormValidator->anyerrors());
151       
152        // This one is an error!
153        $_POST['some_field'] = 'some falsity';
154        $this->FormValidator->isfloat('some_field', _("Error message"));
155        $this->assertTrue($this->FormValidator->anyerrors());
156    }
157
158    function test_isarray()
159    {
160        // This should not generate any error.
161        $_POST['some_field'] = array('asdf', 123);
162        $this->FormValidator->isarray('some_field', _("Error message"));
163        $this->assertFalse($this->FormValidator->anyerrors());
164       
165        // This one is an error!
166        $_POST['some_field'] = 'some falsity';
167        $this->FormValidator->isarray('some_field', _("Error message"));
168        $this->assertTrue($this->FormValidator->anyerrors());
169    }
170
171    function test_checkregex()
172    {
173        // This should not generate any error.
174        $_POST['some_field'] = '1234abcd';
175        $this->FormValidator->checkregex('some_field', '/\d{4}[a-d]{4}/', true, _("Error message"));
176        $this->assertFalse($this->FormValidator->anyerrors());
177       
178        // This should not generate any error.
179        $_POST['some_field'] = 'no digits here';
180        $this->FormValidator->checkregex('some_field', '/\d/', false, _("Error message"));
181        $this->assertFalse($this->FormValidator->anyerrors());
182       
183        // This one is an error!
184        $_POST['some_field'] = 'oops, a d1git';
185        $this->FormValidator->checkregex('some_field', '/\d/', false, _("Error message"));
186        $this->assertTrue($this->FormValidator->anyerrors());
187    }
188
189    function test_stringlength()
190    {
191        // This should not generate any error.
192        $_POST['some_field'] = 'some truth';
193        $this->FormValidator->stringLength('some_field', 0, 255, _("Error message"));
194        $this->assertFalse($this->FormValidator->anyerrors());
195       
196        // This one is an error!
197        $_POST['some_field'] = 'some falsity';
198        $this->FormValidator->stringLength('some_field', 0, 4, _("Error message"));
199        $this->assertTrue($this->FormValidator->anyerrors());
200    }
201
202    function test_numericrange()
203    {
204        // This should not generate any error.
205        $_POST['some_field'] = '12';
206        $this->FormValidator->numericrange('some_field', 3, 22, _("Error message"));
207        $this->assertFalse($this->FormValidator->anyerrors());
208       
209        // This one is an error!
210        $_POST['some_field'] = '12';
211        $this->FormValidator->numericrange('some_field', 300, 2200, _("Error message"));
212        $this->assertTrue($this->FormValidator->anyerrors());
213    }
214
215    function test_validateemail()
216    {
217        // This should not generate any error.
218        $_POST['some_field'] = 'Quinn the Kook <quinn@strangecode.com>';
219        $this->FormValidator->validateemail('some_field', _("Error message"));
220        $this->assertFalse($this->FormValidator->anyerrors());
221               
222        // This one is an error!
223        $_POST['some_field'] = 'quinn@kook.com.';
224        $this->FormValidator->validateemail('some_field', _("Error message"));
225        $this->assertTrue($this->FormValidator->anyerrors());
226    }
227
228    function test_validatephone()
229    {
230        // This should not generate any error.
231        $_POST['some_field'] = '+1 (530) 555-1212';
232        $this->FormValidator->validatephone('some_field', _("Error message"));
233        $this->assertFalse($this->FormValidator->anyerrors());
234       
235        // This one is an error!
236        $_POST['some_field'] = 'd321/654*9875 ';
237        $this->FormValidator->validatephone('some_field', _("Error message"));
238        $this->assertTrue($this->FormValidator->anyerrors());
239    }
240
241    function test_validatestrdate()
242    {
243        // This should not generate any error.
244        $_POST['some_field'] = 'next tuesday';
245        $this->FormValidator->validatestrdate('some_field', _("Error message"));
246        $this->assertFalse($this->FormValidator->anyerrors());
247       
248        // This one is an error!
249        $_POST['some_field'] = 'in a galaxy far far away';
250        $this->FormValidator->validatestrdate('some_field', _("Error message"));
251        $this->assertTrue($this->FormValidator->anyerrors());
252    }
253
254    function test_validateccnumber()
255    {
256        // This should not generate any error.
257        $_POST['some_field'] = '2323-2005-7766-3554';
258        $this->FormValidator->validateccnumber('some_field', null);
259        $this->assertFalse($this->FormValidator->anyerrors());
260       
261        // This one is an error!
262        $_POST['some_field'] = '1234 1234 1234 1234';
263        $this->FormValidator->validateccnumber('some_field', null);
264        $this->assertTrue($this->FormValidator->anyerrors());
265    }
266
267    function test_fileuploaded()
268    {
269        // This one is an error!
270        $this->FormValidator->fileUploaded('this-file-does-not-exist', _("Error message"));
271        $this->assertTrue($this->FormValidator->anyerrors());
272    }
273
274}
275// Running the test.
276$suite = new PHPUnit_TestSuite('FormValidatorTest');
277$result = PHPUnit::run($suite);
278echo $result->toString();
279?>
Note: See TracBrowser for help on using the repository browser.