StringLengthTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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-2015 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. * @see Zend_Validate_StringLength
  24. */
  25. require_once 'Zend/Validate/StringLength.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_Validate
  29. * @subpackage UnitTests
  30. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. * @group Zend_Validate
  33. */
  34. class Zend_Validate_StringLengthTest extends PHPUnit_Framework_TestCase
  35. {
  36. /**
  37. * Default instance created for all test methods
  38. *
  39. * @var Zend_Validate_StringLength
  40. */
  41. protected $_validator;
  42. /**
  43. * Creates a new Zend_Validate_StringLength object for each test method
  44. *
  45. * @return void
  46. */
  47. public function setUp()
  48. {
  49. $this->_validator = new Zend_Validate_StringLength();
  50. }
  51. /**
  52. * Ensures that the validator follows expected behavior
  53. *
  54. * @return void
  55. */
  56. public function testBasic()
  57. {
  58. if (PHP_VERSION_ID < 50600) {
  59. iconv_set_encoding('internal_encoding', 'UTF-8');
  60. } else {
  61. ini_set('default_charset', 'UTF-8');
  62. }
  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. if (PHP_VERSION_ID < 50600) {
  159. iconv_set_encoding('internal_encoding', 'UTF-8');
  160. } else {
  161. ini_set('default_charset', 'UTF-8');
  162. }
  163. $validator = new Zend_Validate_StringLength(2, 2, 'UTF-8');
  164. $this->assertEquals(true, $validator->isValid('ab'));
  165. $this->assertEquals('UTF-8', $validator->getEncoding());
  166. $validator->setEncoding('ISO-8859-1');
  167. $this->assertEquals('ISO-8859-1', $validator->getEncoding());
  168. }
  169. /**
  170. * @expectedException Zend_Validate_Exception
  171. * @group GH-634
  172. */
  173. public function testWrongEncoding()
  174. {
  175. $this->_validator->setEncoding('');
  176. }
  177. /**
  178. * @ZF-4352
  179. */
  180. public function testNonStringValidation()
  181. {
  182. $this->assertFalse($this->_validator->isValid(array(1 => 1)));
  183. }
  184. }