7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
16 * @package Zend_Validate
17 * @subpackage UnitTests
18 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
26 require_once dirname(__FILE__
) . '/../../TestHelper.php';
29 * @see Zend_Validate_StringLength
31 require_once 'Zend/Validate/StringLength.php';
35 * @package Zend_Validate
36 * @subpackage UnitTests
37 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
38 * @license http://framework.zend.com/license/new-bsd New BSD License
39 * @group Zend_Validate
41 class Zend_Validate_StringLengthTest
extends PHPUnit_Framework_TestCase
44 * Default instance created for all test methods
46 * @var Zend_Validate_StringLength
48 protected $_validator;
51 * Creates a new Zend_Validate_StringLength object for each test method
55 public function setUp()
57 $this->_validator
= new Zend_Validate_StringLength();
61 * Ensures that the validator follows expected behavior
65 public function testBasic()
67 iconv_set_encoding('internal_encoding', 'UTF-8');
69 * The elements of each array are, in order:
72 * - expected validation result
73 * - array of test input values
75 $valuesExpected = array(
76 array(0, null, true, array('', 'a', 'ab')),
77 array(-1, null, true, array('')),
78 array(2, 2, true, array('ab', ' ')),
79 array(2, 2, false, array('a', 'abc')),
80 array(1, null, false, array('')),
81 array(2, 3, true, array('ab', 'abc')),
82 array(2, 3, false, array('a', 'abcd')),
83 array(3, 3, true, array('äöü')),
84 array(6, 6, true, array('Müller'))
86 foreach ($valuesExpected as $element) {
87 $validator = new Zend_Validate_StringLength($element[0], $element[1]);
88 foreach ($element[3] as $input) {
89 $this->assertEquals($element[2], $validator->isValid($input));
95 * Ensures that getMessages() returns expected default value
99 public function testGetMessages()
101 $this->assertEquals(array(), $this->_validator
->getMessages());
105 * Ensures that getMin() returns expected default value
109 public function testGetMin()
111 $this->assertEquals(0, $this->_validator
->getMin());
115 * Ensures that getMax() returns expected default value
119 public function testGetMax()
121 $this->assertEquals(null, $this->_validator
->getMax());
125 * Ensures that setMin() throws an exception when given a value greater than the maximum
129 public function testSetMinExceptionGreaterThanMax()
134 $this->_validator
->setMax($max)->setMin($min);
135 $this->fail('Expected Zend_Validate_Exception not thrown');
136 } catch (Zend_Validate_Exception
$e) {
138 "The minimum must be less than or equal to the maximum length, but $min > $max",
145 * Ensures that setMax() throws an exception when given a value less than the minimum
149 public function testSetMaxExceptionLessThanMin()
154 $this->_validator
->setMin($min)->setMax($max);
155 $this->fail('Expected Zend_Validate_Exception not thrown');
156 } catch (Zend_Validate_Exception
$e) {
158 "The maximum must be greater than or equal to the minimum length, but $max < $min",
167 public function testDifferentEncodingWithValidator()
169 iconv_set_encoding('internal_encoding', 'UTF-8');
170 $validator = new Zend_Validate_StringLength(2, 2, 'UTF-8');
171 $this->assertEquals(true, $validator->isValid('ab'));
173 $this->assertEquals('UTF-8', $validator->getEncoding());
174 $validator->setEncoding('ISO-8859-1');
175 $this->assertEquals('ISO-8859-1', $validator->getEncoding());
181 public function testNonStringValidation()
183 $this->assertFalse($this->_validator
->isValid(array(1 => 1)));