2
0

AlnumTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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_Alnum
  28. */
  29. require_once 'Zend/Validate/Alnum.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_AlnumTest extends PHPUnit_Framework_TestCase
  38. {
  39. /**
  40. * Zend_Validate_Alnum object
  41. *
  42. * @var Zend_Validate_Alnum
  43. */
  44. protected $_validator;
  45. /**
  46. * Creates a new Zend_Validate_Alnum object for each test method
  47. *
  48. * @return void
  49. */
  50. public function setUp()
  51. {
  52. $this->_validator = new Zend_Validate_Alnum();
  53. }
  54. /**
  55. * Ensures that the validator follows expected behavior for basic input values
  56. *
  57. * @return void
  58. */
  59. public function testExpectedResultsWithBasicInputValues()
  60. {
  61. $valuesExpected = array(
  62. 'abc123' => true,
  63. 'abc 123' => false,
  64. 'abcxyz' => true,
  65. 'AZ@#4.3' => false,
  66. 'aBc123' => true,
  67. '' => false,
  68. ' ' => false,
  69. "\n" => false,
  70. 'foobar1' => true
  71. );
  72. foreach ($valuesExpected as $input => $result) {
  73. $this->assertEquals($result, $this->_validator->isValid($input));
  74. }
  75. }
  76. /**
  77. * Ensures that getMessages() returns expected initial value
  78. *
  79. * @return void
  80. */
  81. public function testMessagesEmptyInitially()
  82. {
  83. $this->assertEquals(array(), $this->_validator->getMessages());
  84. }
  85. /**
  86. * Ensures that the allowWhiteSpace option works as expected
  87. *
  88. * @return void
  89. */
  90. public function testOptionToAllowWhiteSpaceWithBasicInputValues()
  91. {
  92. $this->_validator->setAllowWhiteSpace(true);
  93. $valuesExpected = array(
  94. 'abc123' => true,
  95. 'abc 123' => true,
  96. 'abcxyz' => true,
  97. 'AZ@#4.3' => false,
  98. 'aBc123' => true,
  99. '' => false,
  100. ' ' => true,
  101. "\n" => true,
  102. " \t " => true,
  103. 'foobar1' => true
  104. );
  105. foreach ($valuesExpected as $input => $result) {
  106. $this->assertEquals(
  107. $result,
  108. $this->_validator->isValid($input),
  109. "Expected '$input' to be considered " . ($result ? '' : 'in') . "valid"
  110. );
  111. }
  112. }
  113. /**
  114. * @return void
  115. */
  116. public function testEmptyStringValueResultsInProperValidationFailureMessages()
  117. {
  118. $this->assertFalse($this->_validator->isValid(''));
  119. $messages = $this->_validator->getMessages();
  120. $arrayExpected = array(
  121. Zend_Validate_Alnum::STRING_EMPTY => '\'\' is an empty string'
  122. );
  123. $this->assertThat($messages, $this->identicalTo($arrayExpected));
  124. }
  125. /**
  126. * @return void
  127. * @deprecated Since 1.5.0
  128. */
  129. public function testEmptyStringValueResultsInProperValidationFailureErrors()
  130. {
  131. $this->assertFalse($this->_validator->isValid(''));
  132. $errors = $this->_validator->getErrors();
  133. $arrayExpected = array(
  134. Zend_Validate_Alnum::STRING_EMPTY
  135. );
  136. $this->assertThat($errors, $this->identicalTo($arrayExpected));
  137. }
  138. /**
  139. * @return void
  140. */
  141. public function testInvalidValueResultsInProperValidationFailureMessages()
  142. {
  143. $this->assertFalse($this->_validator->isValid('#'));
  144. $messages = $this->_validator->getMessages();
  145. $arrayExpected = array(
  146. Zend_Validate_Alnum::NOT_ALNUM => '\'#\' has not only alphabetic and digit characters'
  147. );
  148. $this->assertThat($messages, $this->identicalTo($arrayExpected));
  149. }
  150. /**
  151. * @return void
  152. * @deprecated Since 1.5.0
  153. */
  154. public function testInvalidValueResultsInProperValidationFailureErrors()
  155. {
  156. $this->assertFalse($this->_validator->isValid('#'));
  157. $errors = $this->_validator->getErrors();
  158. $arrayExpected = array(
  159. Zend_Validate_Alnum::NOT_ALNUM
  160. );
  161. $this->assertThat($errors, $this->identicalTo($arrayExpected));
  162. }
  163. /**
  164. * @ZF-4352
  165. */
  166. public function testNonStringValidation()
  167. {
  168. $this->assertFalse($this->_validator->isValid(array(1 => 1)));
  169. }
  170. }