AlnumTest.php 5.2 KB

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