Преглед изворни кода

Refactored various protected helper methods into explicit API methods of the generator

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@19569 44c647ce-9c0f-0410-b52a-842ac1e357ba
lars пре 16 година
родитељ
комит
fbfa388dbd

+ 1 - 2
library/Zend/XmlRpc/Fault.php

@@ -219,8 +219,7 @@ class Zend_XmlRpc_Fault
         }
 
         $structXml = $xml->fault->value->asXML();
-        $structXml = preg_replace('/<\?xml version=.*?\?>/i', '', $structXml);
-        $struct    = Zend_XmlRpc_Value::getXmlRpcValue(trim($structXml), Zend_XmlRpc_Value::XML_STRING);
+        $struct    = Zend_XmlRpc_Value::getXmlRpcValue($structXml, Zend_XmlRpc_Value::XML_STRING);
         $struct    = $struct->getValue();
 
         if (isset($struct['faultCode'])) {

+ 24 - 2
library/Zend/XmlRpc/Generator/Abstract.php

@@ -98,7 +98,7 @@ abstract class Zend_XmlRpc_Generator_Abstract
      */
     public function __toString()
     {
-        return $this->_stripXmlDeclaration($this->saveXml());
+        return $this->stripDeclaration($this->saveXml());
     }
 
     /**
@@ -107,8 +107,30 @@ abstract class Zend_XmlRpc_Generator_Abstract
      * @param string $xml
      * @return string
      */
-    protected function _stripXmlDeclaration($xml)
+    public function stripDeclaration($xml)
     {
         return preg_replace('/<\?xml version="1.0"( encoding="[^\"]*")?\?>\n/u', '', $xml);
     }
+
+    /**
+     * Make sure a string will be safe for XML, convert risky characters to entities
+     *
+     * @param string $str
+     * @return string
+     */
+    public function escapeEntities($str)
+    {
+        return htmlspecialchars($str, ENT_QUOTES, 'UTF-8');
+    }
+
+    /**
+     * Convert XML entities into string values
+     *
+     * @param string $str
+     * @return string
+     */
+    public function decodeEntities($str)
+    {
+        return html_entity_decode($str, ENT_QUOTES, 'UTF-8');
+    }
 }

+ 1 - 2
library/Zend/XmlRpc/Response.php

@@ -205,8 +205,7 @@ class Zend_XmlRpc_Response
                 throw new Zend_XmlRpc_Value_Exception('Missing XML-RPC value in XML');
             }
             $valueXml = $xml->params->param->value->asXML();
-            $valueXml = preg_replace('/<\?xml version=.*?\?>/i', '', $valueXml);
-            $value = Zend_XmlRpc_Value::getXmlRpcValue(trim($valueXml), Zend_XmlRpc_Value::XML_STRING);
+            $value = Zend_XmlRpc_Value::getXmlRpcValue($valueXml, Zend_XmlRpc_Value::XML_STRING);
         } catch (Zend_XmlRpc_Value_Exception $e) {
             $this->_fault = new Zend_XmlRpc_Fault(653);
             $this->_fault->setEncoding($this->getEncoding());

+ 6 - 36
library/Zend/XmlRpc/Value.php

@@ -266,6 +266,11 @@ abstract class Zend_XmlRpc_Value
                     return $value;
                 }
 
+                if ($value instanceof Zend_Crypt_Math_BigInteger) {
+                    require_once 'Zend/XmlRpc/Value/BigInteger.php';
+                    return new Zend_XmlRpc_Value_BigInteger($value);
+                }
+
                 // Otherwise, we convert the object into a struct
                 $value = get_object_vars($value);
                 // Break intentionally omitted
@@ -285,10 +290,6 @@ abstract class Zend_XmlRpc_Value
                 require_once 'Zend/XmlRpc/Value/Integer.php';
                 return new Zend_XmlRpc_Value_Integer($value);
 
-            case 'i8':
-                require_once 'Zend/XmlRpc/Value/BigInteger.php';
-                return new Zend_XmlRpc_Value_BigInteger($value);
-
             case 'double':
                 require_once 'Zend/XmlRpc/Value/Double.php';
                 return new Zend_XmlRpc_Value_Double($value);
@@ -469,37 +470,6 @@ abstract class Zend_XmlRpc_Value
      */
     protected function _setXML($xml)
     {
-        $this->_xml = $this->_stripXmlDeclaration($xml);
-    }
-
-    /**
-     * @param DOMDocument $dom
-     * @return mixed
-     */
-    protected function _stripXmlDeclaration($xml)
-    {
-        return preg_replace('/<\?xml version="1.0"( encoding="[^\"]*")?\?>\n/u', '', $xml);
-    }
-
-    /**
-     * Make sure a string will be safe for XML, convert risky characters to entities
-     *
-     * @param string $str
-     * @return string
-     */
-    protected function _escapeXmlEntities($str)
-    {
-        return htmlspecialchars($str, ENT_QUOTES, 'UTF-8');
-    }
-
-    /**
-     * Convert XML entities into string values
-     *
-     * @param string $str
-     * @return string
-     */
-    protected function _decodeXmlEntities($str)
-    {
-        return html_entity_decode($str, ENT_QUOTES, 'UTF-8');
+        $this->_xml = $this->getGenerator()->stripDeclaration($xml);
     }
 }

+ 2 - 2
library/Zend/XmlRpc/Value/String.php

@@ -45,7 +45,7 @@ class Zend_XmlRpc_Value_String extends Zend_XmlRpc_Value_Scalar
         $this->_type = self::XMLRPC_TYPE_STRING;
 
         // Make sure this value is string and all XML characters are encoded
-        $this->_value = $this->_escapeXmlEntities($value);
+        $this->_value = $this->getGenerator()->escapeEntities($value);
     }
 
     /**
@@ -56,7 +56,7 @@ class Zend_XmlRpc_Value_String extends Zend_XmlRpc_Value_Scalar
      */
     public function getValue()
     {
-        return $this->_decodeXmlEntities($this->_value);
+        return $this->getGenerator()->decodeEntities($this->_value);
     }
 }
 

+ 0 - 17
tests/Zend/XmlRpc/ClientTest.php

@@ -19,12 +19,6 @@
  * @license    http://framework.zend.com/license/new-bsd     New BSD License
  * @version $Id$
  */
-
-// Call Zend_XmlRpc_ClientTest::main() if this source file is executed directly.
-if (!defined("PHPUnit_MAIN_METHOD")) {
-    define("PHPUnit_MAIN_METHOD", "Zend_XmlRpc_ClientTest::main");
-}
-
 require_once dirname(__FILE__) . '/../../TestHelper.php';
 
 require_once 'Zend/XmlRpc/Client.php';
@@ -60,17 +54,6 @@ class Zend_XmlRpc_ClientTest extends PHPUnit_Framework_TestCase
      */
     protected $xmlrpcClient;
 
-    /**
-     * Runs the test methods of this class.
-     *
-     * @return void
-     */
-    public static function main()
-    {
-        $suite  = new PHPUnit_Framework_TestSuite("Zend_XmlRpc_ClientTest");
-        $result = PHPUnit_TextUI_TestRunner::run($suite);
-    }
-
     public function setUp()
     {
         $this->httpAdapter = new Zend_Http_Client_Adapter_Test();

+ 0 - 6
tests/Zend/XmlRpc/GeneratorTest.php

@@ -20,12 +20,6 @@
  * @version $Id: ValueTest.php 18442 2009-09-30 13:17:48Z lars $
  */
 
-// Call Zend_XmlRpc_ValueTest::main() if this source file is executed directly.
-if (!defined("PHPUnit_MAIN_METHOD")) {
-    require_once dirname(__FILE__) . '/../../TestHelper.php';
-    define("PHPUnit_MAIN_METHOD", "Zend_XmlRpc_ValueTest::main");
-}
-
 require_once "PHPUnit/Framework/TestCase.php";
 require_once "PHPUnit/Framework/TestSuite.php";
 require_once 'Zend/XmlRpc/Generator/DOMDocument.php';

+ 0 - 17
tests/Zend/XmlRpc/ServerTest.php

@@ -19,12 +19,6 @@
  * @license    http://framework.zend.com/license/new-bsd     New BSD License
  * @version $Id$
  */
-
-// Call Zend_XmlRpc_ServerTest::main() if this source file is executed directly.
-if (!defined('PHPUnit_MAIN_METHOD')) {
-    define('PHPUnit_MAIN_METHOD', 'Zend_XmlRpc_ServerTest::main');
-}
-
 require_once dirname(__FILE__) . '/../../TestHelper.php';
 
 require_once 'Zend/XmlRpc/Server.php';
@@ -50,17 +44,6 @@ class Zend_XmlRpc_ServerTest extends PHPUnit_Framework_TestCase
     protected $_server;
 
     /**
-     * Runs the test methods of this class.
-     *
-     * @return void
-     */
-    public static function main()
-    {
-        $suite  = new PHPUnit_Framework_TestSuite("Zend_XmlRpc_ServerTest");
-        $result = PHPUnit_TextUI_TestRunner::run($suite);
-    }
-
-    /**
      * Setup environment
      */
     public function setUp()

+ 11 - 21
tests/Zend/XmlRpc/ValueTest.php

@@ -19,13 +19,6 @@
  * @license    http://framework.zend.com/license/new-bsd     New BSD License
  * @version $Id$
  */
-
-// Call Zend_XmlRpc_ValueTest::main() if this source file is executed directly.
-if (!defined("PHPUnit_MAIN_METHOD")) {
-    require_once dirname(__FILE__) . '/../../TestHelper.php';
-    define("PHPUnit_MAIN_METHOD", "Zend_XmlRpc_ValueTest::main");
-}
-
 require_once "PHPUnit/Framework/TestCase.php";
 require_once "PHPUnit/Framework/TestSuite.php";
 
@@ -59,19 +52,6 @@ require_once 'Zend/XmlRpc/TestProvider.php';
  */
 class Zend_XmlRpc_ValueTest extends PHPUnit_Framework_TestCase
 {
-    /**
-     * Runs the test methods of this class.
-     *
-     * @return void
-     */
-    public static function main()
-    {
-        require_once "PHPUnit/TextUI/TestRunner.php";
-
-        $suite  = new PHPUnit_Framework_TestSuite("Zend_XmlRpc_ValueTest");
-        $result = PHPUnit_TextUI_TestRunner::run($suite);
-    }
-
     // Boolean
 
     public function testFactoryAutodetectsBoolean()
@@ -199,7 +179,7 @@ class Zend_XmlRpc_ValueTest extends PHPUnit_Framework_TestCase
     {
         $native = (string)(PHP_INT_MAX + 1);
         $types = array(Zend_XmlRpc_Value::XMLRPC_TYPE_APACHEI8,
-                        Zend_XmlRpc_Value::XMLRPC_TYPE_I8);
+                       Zend_XmlRpc_Value::XMLRPC_TYPE_I8);
 
         $bigInt = new Zend_Crypt_Math_BigInteger();
         $bigInt->init($native);
@@ -209,6 +189,10 @@ class Zend_XmlRpc_ValueTest extends PHPUnit_Framework_TestCase
             $this->assertSame('i8', $value->getType());
             $this->assertEquals($bigInt, $value->getValue());
         }
+
+        $value = Zend_XmlRpc_Value::getXmlRpcValue($bigInt);
+        $this->assertSame('i8', $value->getType());
+        $this->assertEquals($bigInt, $value->getValue());
     }
 
     // Double
@@ -758,6 +742,12 @@ class Zend_XmlRpc_ValueTest extends PHPUnit_Framework_TestCase
         }
     }
 
+    public function testPassingXmlRpcObjectReturnsTheSameObject()
+    {
+        $xmlRpcValue = new Zend_XmlRpc_Value_String('foo');
+        $this->assertSame($xmlRpcValue, Zend_XmlRpc_Value::getXmlRpcValue($xmlRpcValue));
+    }
+
     // Custom Assertions and Helper Methods
 
     public function assertXmlRpcType($type, $object)