2
0

IntTest.php 3.6 KB

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