Development

/branches/1.1/test/unit/validator/sfValidatorStringTest.php

You must first sign up to be able to contribute.

root/branches/1.1/test/unit/validator/sfValidatorStringTest.php

Revision 6945, 2.3 kB (checked in by fabien, 5 years ago)

added code tests for all validators

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1 <?php
2
3 /*
4  * This file is part of the symfony package.
5  * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 require_once(dirname(__FILE__).'/../../bootstrap/unit.php');
12
13 $t = new lime_test(12, new lime_output_color());
14
15 $v = new sfValidatorString();
16
17 // ->clean()
18 $t->diag('->clean()');
19 $t->is($v->clean('foo'), 'foo', '->clean() returns the string unmodified');
20
21 $v->setOption('required', false);
22 $t->ok($v->clean(null) === '', '->clean() converts the value to a string');
23 $t->ok($v->clean(1) === '1', '->clean() converts the value to a string');
24
25 $v->setOption('max_length', 2);
26 $t->is($v->clean('fo'), 'fo', '->clean() checks the maximum length allowed');
27 try
28 {
29   $v->clean('foo');
30   $t->fail('"max_length" option set the maximum length of the string');
31   $t->skip('', 1);
32 }
33 catch (sfValidatorError $e)
34 {
35   $t->pass('"max_length" option set the maximum length of the string');
36   $t->is($e->getCode(), 'max_length', '->clean() throws a sfValidatorError');
37 }
38
39 $v->setMessage('max_length', 'Too long');
40 try
41 {
42   $v->clean('foo');
43   $t->fail('"max_length" error message customization');
44 }
45 catch (sfValidatorError $e)
46 {
47   $t->is($e->getMessage(), 'Too long', '"max_length" error message customization');
48 }
49
50 $v->setOption('max_length', null);
51
52 $v->setOption('min_length', 3);
53 $t->is($v->clean('foo'), 'foo', '->clean() checks the minimum length allowed');
54 try
55 {
56   $v->clean('fo');
57   $t->fail('"min_length" option set the minimum length of the string');
58   $t->skip('', 1);
59 }
60 catch (sfValidatorError $e)
61 {
62   $t->pass('"min_length" option set the minimum length of the string');
63   $t->is($e->getCode(), 'min_length', '->clean() throws a sfValidatorError');
64 }
65
66 $v->setMessage('min_length', 'Too short');
67 try
68 {
69   $v->clean('fo');
70   $t->fail('"min_length" error message customization');
71 }
72 catch (sfValidatorError $e)
73 {
74   $t->is($e->getMessage(), 'Too short', '"min_length" error message customization');
75 }
76
77 $v->setOption('min_length', null);
78
79 $t->diag('UTF-8 support');
80 if (!function_exists('mb_strlen'))
81 {
82   $t->skip('UTF-8 support needs mb_strlen');
83 }
84 else
85 {
86   $v->setOption('max_length', 4);
87   $t->is($v->clean('été'), 'été', '"sfValidatorString" supports UTF-8');
88 }
89
Note: See TracBrowser for help on using the browser.