FloatTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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('10.1', true),
  86. array(1, true),
  87. array('not a float', false),
  88. );
  89. foreach ($valuesExpected as $element) {
  90. $this->assertEquals($element[1], $this->_validator->isValid($element[0]));
  91. }
  92. }
  93. /**
  94. * Ensures that getMessages() returns expected default value
  95. *
  96. * @return void
  97. */
  98. public function testGetMessages()
  99. {
  100. $this->assertEquals(array(), $this->_validator->getMessages());
  101. }
  102. /**
  103. * Ensures that set/getLocale() works
  104. */
  105. public function testSettingLocales()
  106. {
  107. $this->_validator->setLocale('de');
  108. $this->assertEquals('de', $this->_validator->getLocale());
  109. $this->assertEquals(true, $this->_validator->isValid('10,5'));
  110. }
  111. /**
  112. * @ZF-4352
  113. */
  114. public function testNonStringValidation()
  115. {
  116. $this->assertFalse($this->_validator->isValid(array(1 => 1)));
  117. }
  118. /**
  119. * @ZF-7489
  120. */
  121. public function testUsingApplicationLocale()
  122. {
  123. Zend_Registry::set('Zend_Locale', new Zend_Locale('de'));
  124. $valid = new Zend_Validate_Float();
  125. $this->assertTrue($valid->isValid('123,456'));
  126. }
  127. /**
  128. * @ZF-7987
  129. */
  130. public function testNoZendLocaleButPhpLocale()
  131. {
  132. setlocale(LC_ALL, 'de');
  133. $valid = new Zend_Validate_Float();
  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. }