StringLengthTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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-2012 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-2012 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. iconv_set_encoding('internal_encoding', 'UTF-8');
  59. /**
  60. * The elements of each array are, in order:
  61. * - minimum length
  62. * - maximum length
  63. * - expected validation result
  64. * - array of test input values
  65. */
  66. $valuesExpected = array(
  67. array(0, null, true, array('', 'a', 'ab')),
  68. array(-1, null, true, array('')),
  69. array(2, 2, true, array('ab', ' ')),
  70. array(2, 2, false, array('a', 'abc')),
  71. array(1, null, false, array('')),
  72. array(2, 3, true, array('ab', 'abc')),
  73. array(2, 3, false, array('a', 'abcd')),
  74. array(3, 3, true, array('äöü')),
  75. array(6, 6, true, array('Müller'))
  76. );
  77. foreach ($valuesExpected as $element) {
  78. $validator = new Zend_Validate_StringLength($element[0], $element[1]);
  79. foreach ($element[3] as $input) {
  80. $this->assertEquals($element[2], $validator->isValid($input));
  81. }
  82. }
  83. }
  84. /**
  85. * Ensures that getMessages() returns expected default value
  86. *
  87. * @return void
  88. */
  89. public function testGetMessages()
  90. {
  91. $this->assertEquals(array(), $this->_validator->getMessages());
  92. }
  93. /**
  94. * Ensures that getMin() returns expected default value
  95. *
  96. * @return void
  97. */
  98. public function testGetMin()
  99. {
  100. $this->assertEquals(0, $this->_validator->getMin());
  101. }
  102. /**
  103. * Ensures that getMax() returns expected default value
  104. *
  105. * @return void
  106. */
  107. public function testGetMax()
  108. {
  109. $this->assertEquals(null, $this->_validator->getMax());
  110. }
  111. /**
  112. * Ensures that setMin() throws an exception when given a value greater than the maximum
  113. *
  114. * @return void
  115. */
  116. public function testSetMinExceptionGreaterThanMax()
  117. {
  118. $max = 1;
  119. $min = 2;
  120. try {
  121. $this->_validator->setMax($max)->setMin($min);
  122. $this->fail('Expected Zend_Validate_Exception not thrown');
  123. } catch (Zend_Validate_Exception $e) {
  124. $this->assertEquals(
  125. "The minimum must be less than or equal to the maximum length, but $min > $max",
  126. $e->getMessage()
  127. );
  128. }
  129. }
  130. /**
  131. * Ensures that setMax() throws an exception when given a value less than the minimum
  132. *
  133. * @return void
  134. */
  135. public function testSetMaxExceptionLessThanMin()
  136. {
  137. $max = 1;
  138. $min = 2;
  139. try {
  140. $this->_validator->setMin($min)->setMax($max);
  141. $this->fail('Expected Zend_Validate_Exception not thrown');
  142. } catch (Zend_Validate_Exception $e) {
  143. $this->assertEquals(
  144. "The maximum must be greater than or equal to the minimum length, but $max < $min",
  145. $e->getMessage()
  146. );
  147. }
  148. }
  149. /**
  150. * @return void
  151. */
  152. public function testDifferentEncodingWithValidator()
  153. {
  154. iconv_set_encoding('internal_encoding', 'UTF-8');
  155. $validator = new Zend_Validate_StringLength(2, 2, 'UTF-8');
  156. $this->assertEquals(true, $validator->isValid('ab'));
  157. $this->assertEquals('UTF-8', $validator->getEncoding());
  158. $validator->setEncoding('ISO-8859-1');
  159. $this->assertEquals('ISO-8859-1', $validator->getEncoding());
  160. }
  161. /**
  162. * @ZF-4352
  163. */
  164. public function testNonStringValidation()
  165. {
  166. $this->assertFalse($this->_validator->isValid(array(1 => 1)));
  167. }
  168. }