FloatTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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_Float
  24. */
  25. require_once 'Zend/Validate/Float.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_FloatTest extends PHPUnit_Framework_TestCase
  35. {
  36. /**
  37. * Zend_Validate_Float object
  38. *
  39. * @var Zend_Validate_Float
  40. */
  41. protected $_validator;
  42. /**
  43. * Creates a new Zend_Validate_Float object for each test method
  44. *
  45. * @return void
  46. */
  47. public function setUp()
  48. {
  49. $this->_locale = setlocale(LC_ALL, 0); //backup locale
  50. require_once 'Zend/Registry.php';
  51. if (Zend_Registry::isRegistered('Zend_Locale')) {
  52. Zend_Registry::getInstance()->offsetUnset('Zend_Locale');
  53. }
  54. $this->_validator = new Zend_Validate_Float();
  55. }
  56. public function tearDown()
  57. {
  58. //restore locale
  59. if (is_string($this->_locale) && strpos($this->_locale, ';')) {
  60. $locales = array();
  61. foreach (explode(';', $this->_locale) as $l) {
  62. $tmp = explode('=', $l);
  63. $locales[$tmp[0]] = $tmp[1];
  64. }
  65. setlocale(LC_ALL, $locales);
  66. return;
  67. }
  68. setlocale(LC_ALL, $this->_locale);
  69. }
  70. /**
  71. * Ensures that the validator follows expected behavior
  72. *
  73. * @return void
  74. */
  75. public function testBasic()
  76. {
  77. $valuesExpected = array(
  78. array(1.00, true),
  79. array(0.01, true),
  80. array(-0.1, true),
  81. array(1, true),
  82. array('not a float', false),
  83. );
  84. foreach ($valuesExpected as $element) {
  85. $this->assertEquals($element[1], $this->_validator->isValid($element[0]));
  86. }
  87. }
  88. /**
  89. * Ensures that getMessages() returns expected default value
  90. *
  91. * @return void
  92. */
  93. public function testGetMessages()
  94. {
  95. $this->assertEquals(array(), $this->_validator->getMessages());
  96. }
  97. /**
  98. * Ensures that set/getLocale() works
  99. */
  100. public function testSettingLocales()
  101. {
  102. $this->_validator->setLocale('de');
  103. $this->assertEquals('de', $this->_validator->getLocale());
  104. $this->assertEquals(true, $this->_validator->isValid('10,5'));
  105. }
  106. /**
  107. * @ZF-4352
  108. */
  109. public function testNonStringValidation()
  110. {
  111. $this->assertFalse($this->_validator->isValid(array(1 => 1)));
  112. }
  113. /**
  114. * @ZF-7489
  115. */
  116. public function testUsingApplicationLocale()
  117. {
  118. Zend_Registry::set('Zend_Locale', new Zend_Locale('de'));
  119. $valid = new Zend_Validate_Float();
  120. $this->assertTrue($valid->isValid('123,456'));
  121. }
  122. /**
  123. * @ZF-7987
  124. */
  125. public function testNoZendLocaleButPhpLocale()
  126. {
  127. setlocale(LC_ALL, 'de');
  128. $valid = new Zend_Validate_Float();
  129. $this->assertTrue($valid->isValid(123,456));
  130. $this->assertTrue($valid->isValid('123,456'));
  131. }
  132. /**
  133. * @ZF-7987
  134. */
  135. public function testLocaleDeFloatType()
  136. {
  137. $this->_validator->setLocale('de');
  138. $this->assertEquals('de', $this->_validator->getLocale());
  139. $this->assertEquals(true, $this->_validator->isValid(10.5));
  140. }
  141. /**
  142. * @ZF-7987
  143. */
  144. public function testPhpLocaleDeFloatType()
  145. {
  146. setlocale(LC_ALL, 'de');
  147. $valid = new Zend_Validate_Float();
  148. $this->assertTrue($valid->isValid(10.5));
  149. }
  150. /**
  151. * @ZF-7987
  152. */
  153. public function testPhpLocaleFrFloatType()
  154. {
  155. setlocale(LC_ALL, 'fr');
  156. $valid = new Zend_Validate_Float();
  157. $this->assertTrue($valid->isValid(10.5));
  158. }
  159. /**
  160. * @ZF-8919
  161. */
  162. public function testPhpLocaleDeStringType()
  163. {
  164. setlocale(LC_ALL, 'de_AT');
  165. setlocale(LC_NUMERIC, 'de_AT');
  166. $valid = new Zend_Validate_Float('de_AT');
  167. $this->assertTrue($valid->isValid('1,3'));
  168. $this->assertTrue($valid->isValid('1000,3'));
  169. $this->assertTrue($valid->isValid('1.000,3'));
  170. $this->assertFalse($valid->isValid('1.3'));
  171. $this->assertFalse($valid->isValid('1000.3'));
  172. $this->assertFalse($valid->isValid('1,000.3'));
  173. }
  174. /**
  175. * @ZF-8919
  176. */
  177. public function testPhpLocaleFrStringType()
  178. {
  179. $valid = new Zend_Validate_Float('fr_FR');
  180. $this->assertTrue($valid->isValid('1,3'));
  181. $this->assertTrue($valid->isValid('1000,3'));
  182. $this->assertTrue($valid->isValid('1 000,3'));
  183. $this->assertFalse($valid->isValid('1.3'));
  184. $this->assertFalse($valid->isValid('1000.3'));
  185. $this->assertFalse($valid->isValid('1,000.3'));
  186. }
  187. /**
  188. * @ZF-8919
  189. */
  190. public function testPhpLocaleEnStringType()
  191. {
  192. $valid = new Zend_Validate_Float('en_US');
  193. $this->assertTrue($valid->isValid('1.3'));
  194. $this->assertTrue($valid->isValid('1000.3'));
  195. $this->assertTrue($valid->isValid('1,000.3'));
  196. $this->assertFalse($valid->isValid('1,3'));
  197. $this->assertFalse($valid->isValid('1000,3'));
  198. $this->assertFalse($valid->isValid('1.000,3'));
  199. }
  200. }