AlnumTest.php 5.3 KB

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