StringLengthTest.php 5.1 KB

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