[ZF-10060] Unittests:
[zend/radio.git] / tests / Zend / Validate / StringLengthTest.php
blobde0bd9d6b78806605472b4ff4f1a6b670b13e227
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
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.
15 * @category Zend
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
20 * @version $Id$
23 /**
24 * Test helper
26 require_once dirname(__FILE__) . '/../../TestHelper.php';
28 /**
29 * @see Zend_Validate_StringLength
31 require_once 'Zend/Validate/StringLength.php';
33 /**
34 * @category Zend
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
43 /**
44 * Default instance created for all test methods
46 * @var Zend_Validate_StringLength
48 protected $_validator;
50 /**
51 * Creates a new Zend_Validate_StringLength object for each test method
53 * @return void
55 public function setUp()
57 $this->_validator = new Zend_Validate_StringLength();
60 /**
61 * Ensures that the validator follows expected behavior
63 * @return void
65 public function testBasic()
67 iconv_set_encoding('internal_encoding', 'UTF-8');
68 /**
69 * The elements of each array are, in order:
70 * - minimum length
71 * - maximum length
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));
94 /**
95 * Ensures that getMessages() returns expected default value
97 * @return void
99 public function testGetMessages()
101 $this->assertEquals(array(), $this->_validator->getMessages());
105 * Ensures that getMin() returns expected default value
107 * @return void
109 public function testGetMin()
111 $this->assertEquals(0, $this->_validator->getMin());
115 * Ensures that getMax() returns expected default value
117 * @return void
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
127 * @return void
129 public function testSetMinExceptionGreaterThanMax()
131 $max = 1;
132 $min = 2;
133 try {
134 $this->_validator->setMax($max)->setMin($min);
135 $this->fail('Expected Zend_Validate_Exception not thrown');
136 } catch (Zend_Validate_Exception $e) {
137 $this->assertEquals(
138 "The minimum must be less than or equal to the maximum length, but $min > $max",
139 $e->getMessage()
145 * Ensures that setMax() throws an exception when given a value less than the minimum
147 * @return void
149 public function testSetMaxExceptionLessThanMin()
151 $max = 1;
152 $min = 2;
153 try {
154 $this->_validator->setMin($min)->setMax($max);
155 $this->fail('Expected Zend_Validate_Exception not thrown');
156 } catch (Zend_Validate_Exception $e) {
157 $this->assertEquals(
158 "The maximum must be greater than or equal to the minimum length, but $max < $min",
159 $e->getMessage()
165 * @return void
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());
179 * @ZF-4352
181 public function testNonStringValidation()
183 $this->assertFalse($this->_validator->isValid(array(1 => 1)));