DigitsTest.php 4.2 KB

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