2
0

StringLengthTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  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.
  14. *
  15. * @category Zend
  16. * @package Zend_Validate
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * Test helper
  24. */
  25. require_once dirname(__FILE__) . '/../../TestHelper.php';
  26. /**
  27. * @see Zend_Validate_StringLength
  28. */
  29. require_once 'Zend/Validate/StringLength.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Validate
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. * @group Zend_Validate
  37. */
  38. class Zend_Validate_StringLengthTest extends PHPUnit_Framework_TestCase
  39. {
  40. /**
  41. * Default instance created for all test methods
  42. *
  43. * @var Zend_Validate_StringLength
  44. */
  45. protected $_validator;
  46. /**
  47. * Creates a new Zend_Validate_StringLength object for each test method
  48. *
  49. * @return void
  50. */
  51. public function setUp()
  52. {
  53. $this->_validator = new Zend_Validate_StringLength();
  54. }
  55. /**
  56. * Ensures that the validator follows expected behavior
  57. *
  58. * @return void
  59. */
  60. public function testBasic()
  61. {
  62. iconv_set_encoding('internal_encoding', 'UTF-8');
  63. /**
  64. * The elements of each array are, in order:
  65. * - minimum length
  66. * - maximum length
  67. * - expected validation result
  68. * - array of test input values
  69. */
  70. $valuesExpected = array(
  71. array(0, null, true, array('', 'a', 'ab')),
  72. array(-1, null, true, array('')),
  73. array(2, 2, true, array('ab', ' ')),
  74. array(2, 2, false, array('a', 'abc')),
  75. array(1, null, false, array('')),
  76. array(2, 3, true, array('ab', 'abc')),
  77. array(2, 3, false, array('a', 'abcd')),
  78. array(3, 3, true, array('äöü')),
  79. array(6, 6, true, array('Müller'))
  80. );
  81. foreach ($valuesExpected as $element) {
  82. $validator = new Zend_Validate_StringLength($element[0], $element[1]);
  83. foreach ($element[3] as $input) {
  84. $this->assertEquals($element[2], $validator->isValid($input));
  85. }
  86. }
  87. }
  88. /**
  89. * Ensures that getMessages() returns expected default value
  90. *
  91. * @return void
  92. */
  93. public function testGetMessages()
  94. {
  95. $this->assertEquals(array(), $this->_validator->getMessages());
  96. }
  97. /**
  98. * Ensures that getMin() returns expected default value
  99. *
  100. * @return void
  101. */
  102. public function testGetMin()
  103. {
  104. $this->assertEquals(0, $this->_validator->getMin());
  105. }
  106. /**
  107. * Ensures that getMax() returns expected default value
  108. *
  109. * @return void
  110. */
  111. public function testGetMax()
  112. {
  113. $this->assertEquals(null, $this->_validator->getMax());
  114. }
  115. /**
  116. * Ensures that setMin() throws an exception when given a value greater than the maximum
  117. *
  118. * @return void
  119. */
  120. public function testSetMinExceptionGreaterThanMax()
  121. {
  122. $max = 1;
  123. $min = 2;
  124. try {
  125. $this->_validator->setMax($max)->setMin($min);
  126. $this->fail('Expected Zend_Validate_Exception not thrown');
  127. } catch (Zend_Validate_Exception $e) {
  128. $this->assertEquals(
  129. "The minimum must be less than or equal to the maximum length, but $min > $max",
  130. $e->getMessage()
  131. );
  132. }
  133. }
  134. /**
  135. * Ensures that setMax() throws an exception when given a value less than the minimum
  136. *
  137. * @return void
  138. */
  139. public function testSetMaxExceptionLessThanMin()
  140. {
  141. $max = 1;
  142. $min = 2;
  143. try {
  144. $this->_validator->setMin($min)->setMax($max);
  145. $this->fail('Expected Zend_Validate_Exception not thrown');
  146. } catch (Zend_Validate_Exception $e) {
  147. $this->assertEquals(
  148. "The maximum must be greater than or equal to the minimum length, but $max < $min",
  149. $e->getMessage()
  150. );
  151. }
  152. }
  153. /**
  154. * @return void
  155. */
  156. public function testDifferentEncodingWithValidator()
  157. {
  158. iconv_set_encoding('internal_encoding', 'UTF-8');
  159. $validator = new Zend_Validate_StringLength(2, 2, 'UTF-8');
  160. $this->assertEquals(true, $validator->isValid('ab'));
  161. $this->assertEquals('UTF-8', $validator->getEncoding());
  162. $validator->setEncoding('ISO-8859-1');
  163. $this->assertEquals('ISO-8859-1', $validator->getEncoding());
  164. }
  165. /**
  166. * @ZF-4352
  167. */
  168. public function testNonStringValidation()
  169. {
  170. $this->assertFalse($this->_validator->isValid(array(1 => 1)));
  171. }
  172. }