Explorar el Código

[ZF-7987] Zend_Validate:

- performance increasement for native floats
- added some unittests (ZF-7987)

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@20515 44c647ce-9c0f-0410-b52a-842ac1e357ba
thomas hace 16 años
padre
commit
b7dd1e1123
Se han modificado 2 ficheros con 56 adiciones y 0 borrados
  1. 4 0
      library/Zend/Validate/Float.php
  2. 52 0
      tests/Zend/Validate/FloatTest.php

+ 4 - 0
library/Zend/Validate/Float.php

@@ -112,6 +112,10 @@ class Zend_Validate_Float extends Zend_Validate_Abstract
             return false;
         }
 
+        if (is_float($value)) {
+            return true;
+        }
+
         $this->_setValue($value);
         if ($this->_locale === null) {
             $locale        = localeconv();

+ 52 - 0
tests/Zend/Validate/FloatTest.php

@@ -54,9 +54,31 @@ class Zend_Validate_FloatTest extends PHPUnit_Framework_TestCase
      */
     public function setUp()
     {
+        $this->_locale = setlocale(LC_ALL, 0); //backup locale
+
+        require_once 'Zend/Registry.php';
+        if (Zend_Registry::isRegistered('Zend_Locale')) {
+            Zend_Registry::getInstance()->offsetUnset('Zend_Locale');
+        }
+
         $this->_validator = new Zend_Validate_Float();
     }
 
+    public function tearDown()
+    {
+        //restore locale
+        if (is_string($this->_locale) && strpos($this->_locale, ';')) {
+            $locales = array();
+            foreach (explode(';', $this->_locale) as $l) {
+                $tmp = explode('=', $l);
+                $locales[$tmp[0]] = $tmp[1];
+            }
+            setlocale(LC_ALL, $locales);
+            return;
+        }
+        setlocale(LC_ALL, $this->_locale);
+    }
+
     /**
      * Ensures that the validator follows expected behavior
      *
@@ -114,4 +136,34 @@ class Zend_Validate_FloatTest extends PHPUnit_Framework_TestCase
         $valid = new Zend_Validate_Float();
         $this->assertTrue($valid->isValid('123,456'));
     }
+
+    /**
+     * @ZF-7987
+     */
+    public function testNoZendLocaleButPhpLocale()
+    {
+        setlocale(LC_ALL, 'de');
+        $valid = new Zend_Validate_Float();
+        $this->assertTrue($valid->isValid(123,456));
+    }
+
+    /**
+     * @ZF-7987
+     */
+    public function testLocaleDeFloatType()
+    {
+        $this->_validator->setLocale('de');
+        $this->assertEquals('de', $this->_validator->getLocale());
+        $this->assertEquals(true, $this->_validator->isValid(10.5));
+    }
+
+    /**
+     * @ZF-7987
+     */
+    public function testPhpLocaleDeFloatType()
+    {
+        setlocale(LC_ALL, 'de');
+        $valid = new Zend_Validate_Float();
+        $this->assertTrue($valid->isValid(10.5));
+    }
 }