IntTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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_Int
  24. */
  25. require_once 'Zend/Validate/Int.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_IntTest extends PHPUnit_Framework_TestCase
  35. {
  36. /**
  37. * Zend_Validate_Int object
  38. *
  39. * @var Zend_Validate_Int
  40. */
  41. protected $_validator;
  42. /**
  43. * Creates a new Zend_Validate_Int object for each test method
  44. *
  45. * @return void
  46. */
  47. public function setUp()
  48. {
  49. $this->_validator = new Zend_Validate_Int();
  50. }
  51. /**
  52. * Ensures that the validator follows expected behavior
  53. *
  54. * @return void
  55. */
  56. public function testBasic()
  57. {
  58. $this->_validator->setLocale('en');
  59. $valuesExpected = array(
  60. array(1.00, true),
  61. array(0.00, true),
  62. array(0.01, false),
  63. array(-0.1, false),
  64. array(-1, true),
  65. array('10', true),
  66. array(1, true),
  67. array('not an int', false),
  68. array(true, false),
  69. array(false, false),
  70. );
  71. foreach ($valuesExpected as $element) {
  72. $this->assertEquals($element[1], $this->_validator->isValid($element[0]),
  73. 'Test failed with ' . var_export($element, 1));
  74. }
  75. }
  76. /**
  77. * Ensures that getMessages() returns expected default value
  78. *
  79. * @return void
  80. */
  81. public function testGetMessages()
  82. {
  83. $this->assertEquals(array(), $this->_validator->getMessages());
  84. }
  85. /**
  86. * Ensures that set/getLocale() works
  87. */
  88. public function testSettingLocales()
  89. {
  90. $this->_validator->setLocale('de');
  91. $this->assertEquals('de', $this->_validator->getLocale());
  92. $this->assertEquals(false, $this->_validator->isValid('10 000'));
  93. $this->assertEquals(true, $this->_validator->isValid('10.000'));
  94. }
  95. /**
  96. * @ZF-4352
  97. */
  98. public function testNonStringValidation()
  99. {
  100. $this->assertFalse($this->_validator->isValid(array(1 => 1)));
  101. }
  102. /**
  103. * @ZF-7489
  104. */
  105. public function testUsingApplicationLocale()
  106. {
  107. Zend_Registry::set('Zend_Locale', new Zend_Locale('de'));
  108. $valid = new Zend_Validate_Int();
  109. $this->assertTrue($valid->isValid('10.000'));
  110. }
  111. /**
  112. * @ZF-7703
  113. */
  114. public function testLocaleDetectsNoEnglishLocaleOnOtherSetLocale()
  115. {
  116. Zend_Registry::set('Zend_Locale', new Zend_Locale('de'));
  117. $valid = new Zend_Validate_Int();
  118. $this->assertTrue($valid->isValid(1200));
  119. $this->assertFalse($valid->isValid('1,200'));
  120. }
  121. }