DigitsTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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_Digits
  28. */
  29. require_once 'Zend/Validate/Digits.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_DigitsTest extends PHPUnit_Framework_TestCase
  38. {
  39. /**
  40. * Zend_Validate_Digits object
  41. *
  42. * @var Zend_Validate_Digits
  43. */
  44. protected $_validator;
  45. /**
  46. * Creates a new Zend_Validate_Digits object for each test method
  47. *
  48. * @return void
  49. */
  50. public function setUp()
  51. {
  52. $this->_validator = new Zend_Validate_Digits();
  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' => false,
  63. 'abc 123' => false,
  64. 'abcxyz' => false,
  65. 'AZ@#4.3' => false,
  66. '1.23' => false,
  67. '0x9f' => false,
  68. '123' => true,
  69. '09' => true,
  70. '' => false
  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. * @return void
  87. */
  88. public function testEmptyStringValueResultsInProperValidationFailureMessages()
  89. {
  90. $this->assertFalse($this->_validator->isValid(''));
  91. $messages = $this->_validator->getMessages();
  92. $arrayExpected = array(
  93. Zend_Validate_Digits::STRING_EMPTY => '\'\' is an empty string'
  94. );
  95. $this->assertThat($messages, $this->identicalTo($arrayExpected));
  96. }
  97. /**
  98. * @return void
  99. * @deprecated Since 1.5.0
  100. */
  101. public function testEmptyStringValueResultsInProperValidationFailureErrors()
  102. {
  103. $this->assertFalse($this->_validator->isValid(''));
  104. $errors = $this->_validator->getErrors();
  105. $arrayExpected = array(
  106. Zend_Validate_Digits::STRING_EMPTY
  107. );
  108. $this->assertThat($errors, $this->identicalTo($arrayExpected));
  109. }
  110. /**
  111. * @return void
  112. */
  113. public function testInvalidValueResultsInProperValidationFailureMessages()
  114. {
  115. $this->assertFalse($this->_validator->isValid('#'));
  116. $messages = $this->_validator->getMessages();
  117. $arrayExpected = array(
  118. Zend_Validate_Digits::NOT_DIGITS => '\'#\' contains not only digit characters'
  119. );
  120. $this->assertThat($messages, $this->identicalTo($arrayExpected));
  121. }
  122. /**
  123. * @return void
  124. * @deprecated Since 1.5.0
  125. */
  126. public function testInvalidValueResultsInProperValidationFailureErrors()
  127. {
  128. $this->assertFalse($this->_validator->isValid('#'));
  129. $errors = $this->_validator->getErrors();
  130. $arrayExpected = array(
  131. Zend_Validate_Digits::NOT_DIGITS
  132. );
  133. $this->assertThat($errors, $this->identicalTo($arrayExpected));
  134. }
  135. /**
  136. * @ZF-4352
  137. */
  138. public function testNonStringValidation()
  139. {
  140. $this->assertFalse($this->_validator->isValid(array(1 => 1)));
  141. }
  142. }