2
0

FloatTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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-2010 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_Float
  28. */
  29. require_once 'Zend/Validate/Float.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Validate
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2010 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_FloatTest extends PHPUnit_Framework_TestCase
  39. {
  40. /**
  41. * Zend_Validate_Float object
  42. *
  43. * @var Zend_Validate_Float
  44. */
  45. protected $_validator;
  46. /**
  47. * Creates a new Zend_Validate_Float object for each test method
  48. *
  49. * @return void
  50. */
  51. public function setUp()
  52. {
  53. $this->_locale = setlocale(LC_ALL, 0); //backup locale
  54. require_once 'Zend/Registry.php';
  55. if (Zend_Registry::isRegistered('Zend_Locale')) {
  56. Zend_Registry::getInstance()->offsetUnset('Zend_Locale');
  57. }
  58. $this->_validator = new Zend_Validate_Float();
  59. }
  60. public function tearDown()
  61. {
  62. //restore locale
  63. if (is_string($this->_locale) && strpos($this->_locale, ';')) {
  64. $locales = array();
  65. foreach (explode(';', $this->_locale) as $l) {
  66. $tmp = explode('=', $l);
  67. $locales[$tmp[0]] = $tmp[1];
  68. }
  69. setlocale(LC_ALL, $locales);
  70. return;
  71. }
  72. setlocale(LC_ALL, $this->_locale);
  73. }
  74. /**
  75. * Ensures that the validator follows expected behavior
  76. *
  77. * @return void
  78. */
  79. public function testBasic()
  80. {
  81. $valuesExpected = array(
  82. array(1.00, true),
  83. array(0.01, true),
  84. array(-0.1, true),
  85. array(1, true),
  86. array('not a float', false),
  87. );
  88. foreach ($valuesExpected as $element) {
  89. $this->assertEquals($element[1], $this->_validator->isValid($element[0]));
  90. }
  91. }
  92. /**
  93. * Ensures that getMessages() returns expected default value
  94. *
  95. * @return void
  96. */
  97. public function testGetMessages()
  98. {
  99. $this->assertEquals(array(), $this->_validator->getMessages());
  100. }
  101. /**
  102. * Ensures that set/getLocale() works
  103. */
  104. public function testSettingLocales()
  105. {
  106. $this->_validator->setLocale('de');
  107. $this->assertEquals('de', $this->_validator->getLocale());
  108. $this->assertEquals(true, $this->_validator->isValid('10,5'));
  109. }
  110. /**
  111. * @ZF-4352
  112. */
  113. public function testNonStringValidation()
  114. {
  115. $this->assertFalse($this->_validator->isValid(array(1 => 1)));
  116. }
  117. /**
  118. * @ZF-7489
  119. */
  120. public function testUsingApplicationLocale()
  121. {
  122. Zend_Registry::set('Zend_Locale', new Zend_Locale('de'));
  123. $valid = new Zend_Validate_Float();
  124. $this->assertTrue($valid->isValid('123,456'));
  125. }
  126. /**
  127. * @ZF-7987
  128. */
  129. public function testNoZendLocaleButPhpLocale()
  130. {
  131. setlocale(LC_ALL, 'de');
  132. $valid = new Zend_Validate_Float();
  133. $this->assertTrue($valid->isValid(123,456));
  134. $this->assertTrue($valid->isValid('123,456'));
  135. }
  136. /**
  137. * @ZF-7987
  138. */
  139. public function testLocaleDeFloatType()
  140. {
  141. $this->_validator->setLocale('de');
  142. $this->assertEquals('de', $this->_validator->getLocale());
  143. $this->assertEquals(true, $this->_validator->isValid(10.5));
  144. }
  145. /**
  146. * @ZF-7987
  147. */
  148. public function testPhpLocaleDeFloatType()
  149. {
  150. setlocale(LC_ALL, 'de');
  151. $valid = new Zend_Validate_Float();
  152. $this->assertTrue($valid->isValid(10.5));
  153. }
  154. /**
  155. * @ZF-8919
  156. */
  157. public function testPhpLocaleDeStringType()
  158. {
  159. setlocale(LC_NUMERIC, 'de_AT');
  160. $valid = new Zend_Validate_Float();
  161. $this->assertTrue($valid->isValid('1,3'));
  162. }
  163. }