Procházet zdrojové kódy

Better code coverage, a few bug fixes

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@17762 44c647ce-9c0f-0410-b52a-842ac1e357ba
lars před 16 roky
rodič
revize
e1f7f24e90

+ 1 - 1
library/Zend/XmlRpc/Request.php

@@ -326,7 +326,7 @@ class Zend_XmlRpc_Request
             $types = array();
             $argv  = array();
             foreach ($xml->params->children() as $param) {
-                if (! $param->value instanceof SimpleXMLElement) {
+                if (!isset($param->value)) {
                     $this->_fault = new Zend_XmlRpc_Fault(633);
                     $this->_fault->setEncoding($this->getEncoding());
                     return false;

+ 2 - 2
library/Zend/XmlRpc/Server.php

@@ -268,9 +268,9 @@ class Zend_XmlRpc_Server extends Zend_Server_Abstract
         }
 
         $argv = null;
-        if (3 < func_num_args()) {
+        if (2 < func_num_args()) {
             $argv = func_get_args();
-            $argv = array_slice($argv, 3);
+            $argv = array_slice($argv, 2);
         }
 
         $dispatchable = Zend_Server_Reflection::reflectClass($class, $argv, $namespace);

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

@@ -257,17 +257,17 @@ abstract class Zend_XmlRpc_Value
     /**
      * Transform an XML string into a XML-RPC native value
      *
-     * @param string|SimpleXMLElement $simple_xml A SimpleXMLElement object represent the XML string
+     * @param string|SimpleXMLElement $xml A SimpleXMLElement object represent the XML string
      *                                            It can be also a valid XML string for convertion
      *
      * @return Zend_XmlRpc_Value
      * @static
      */
-    private static function _xmlStringToNativeXmlRpc($simple_xml)
+    private static function _xmlStringToNativeXmlRpc($xml)
     {
-        if (!$simple_xml instanceof SimpleXMLElement) {
+        if (!$xml instanceof SimpleXMLElement) {
             try {
-                $simple_xml = @new SimpleXMLElement($simple_xml);
+                $xml = @new SimpleXMLElement($xml);
             } catch (Exception $e) {
                 // The given string is not a valid XML
                 require_once 'Zend/XmlRpc/Value/Exception.php';
@@ -276,7 +276,7 @@ abstract class Zend_XmlRpc_Value
         }
 
         // Get the key (tag name) and value from the simple xml object and convert the value to an XML-RPC native value
-        list($type, $value) = each($simple_xml);
+        list($type, $value) = each($xml);
         if (!$type) {    // If no type was specified, the default is string
             $type = self::XMLRPC_TYPE_STRING;
         }
@@ -313,12 +313,6 @@ abstract class Zend_XmlRpc_Value
                 $xmlrpc_val = new Zend_XmlRpc_Value_Nil();
                 break;
             case self::XMLRPC_TYPE_ARRAY:
-                // If the XML is valid, $value must be an SimpleXML element and contain the <data> tag
-                if (!$value instanceof SimpleXMLElement) {
-                    require_once 'Zend/XmlRpc/Value/Exception.php';
-                    throw new Zend_XmlRpc_Value_Exception('XML string is invalid for XML-RPC native '. self::XMLRPC_TYPE_ARRAY .' type');
-                }
-
                 // PHP 5.2.4 introduced a regression in how empty($xml->value)
                 // returns; need to look for the item specifically
                 $data = null;
@@ -368,7 +362,7 @@ abstract class Zend_XmlRpc_Value
                 throw new Zend_XmlRpc_Value_Exception('Value type \''. $type .'\' parsed from the XML string is not a known XML-RPC native type');
                 break;
         }
-        $xmlrpc_val->_setXML($simple_xml->asXML());
+        $xmlrpc_val->_setXML($xml->asXML());
 
         return $xmlrpc_val;
     }

+ 1 - 1
tests/Zend/XmlRpc/AllTests.php

@@ -63,7 +63,7 @@ class Zend_XmlRpc_AllTests
         $suite->addTestSuite('Zend_XmlRpc_ServerTest');
         $suite->addTestSuite('Zend_XmlRpc_Server_CacheTest');
         $suite->addTestSuite('Zend_XmlRpc_Server_FaultTest');
-       
+
         return $suite;
     }
 }

+ 54 - 2
tests/Zend/XmlRpc/FaultTest.php

@@ -61,7 +61,7 @@ class Zend_XmlRpc_FaultTest extends PHPUnit_Framework_TestCase
     /**
      * __construct() test
      */
-    public function test__construct()
+    public function testConstructor()
     {
         $this->assertTrue($this->_fault instanceof Zend_XmlRpc_Fault);
         $this->assertEquals(404, $this->_fault->getCode());
@@ -147,9 +147,54 @@ class Zend_XmlRpc_FaultTest extends PHPUnit_Framework_TestCase
         try {
             $parsed = $this->_fault->loadXml('foo');
             $this->fail('Should not parse invalid XML');
-        } catch (Exception $e) {
+        } catch (Zend_XmlRpc_Exception $e) {
             // do nothing
         }
+
+        $this->assertFalse($this->_fault->loadXml('<wellformedButInvalid/>'));
+
+        try {
+            $this->assertFalse($this->_fault->loadXml('<methodResponse><fault/></methodResponse>'));
+            $this->fail('Should throw an exception. No value element in fault');
+        } catch (Zend_XmlRpc_Exception $e) {
+            $this->assertEquals('Invalid fault structure', $e->getMessage());
+            $this->assertSame(500, $e->getCode());
+        }
+
+        try {
+            $this->_fault->loadXml('<methodResponse><fault/></methodResponse>');
+            $this->fail('Should throw an exception. No struct element in //fault/value');
+        } catch (Zend_XmlRpc_Exception $e) {
+            $this->assertEquals('Invalid fault structure', $e->getMessage());
+            $this->assertSame(500, $e->getCode());
+        }
+
+        try {
+            $this->_fault->loadXml('<methodResponse><fault><value><struct/></value></fault></methodResponse>');
+            $this->fail('Should throw an exception. Empty fault code and string in //fault/value');
+        } catch (Zend_XmlRpc_Exception $e) {
+            $this->assertEquals('Fault code and string required', $e->getMessage());
+        }
+
+        $this->_fault->loadXml('<methodResponse><fault><value><struct>'
+                . '<member><name>faultString</name><value><string>str</string></value></member>'
+                . '</struct></value></fault></methodResponse>');
+        $this->assertSame(404, $this->_fault->getCode(), 'If no fault code is given, use 404 as a default');
+
+        $this->_fault->loadXml('<methodResponse><fault><value><struct>'
+                . '<member><name>faultCode</name><value><int>610</int></value></member>'
+                . '</struct></value></fault></methodResponse>');
+        $this->assertSame(
+            'Invalid method class', $this->_fault->getMessage(), 'If empty fault string is given, resolve the code');
+
+        $this->_fault->loadXml('<methodResponse><fault><value><struct>'
+                . '<member><name>faultCode</name><value><int>1234</int></value></member>'
+                . '</struct></value></fault></methodResponse>');
+        $this->assertSame(
+            'Unknown Error',
+            $this->_fault->getMessage(),
+            'If code resolval failed, use "Unknown Error"'
+        );
     }
 
     /**
@@ -231,6 +276,13 @@ class Zend_XmlRpc_FaultTest extends PHPUnit_Framework_TestCase
         $this->assertEquals('ISO-8859-1', $this->_fault->getEncoding());
     }
 
+    public function testUnknownErrorIsUsedIfUnknownErrorCodeEndEmptyMessageIsPassed()
+    {
+        $fault = new Zend_XmlRpc_Fault(1234);
+        $this->assertSame(1234, $fault->getCode());
+        $this->assertSame('Unknown error', $fault->getMessage());
+    }
+
     public function testFaultStringWithoutStringTypeDeclaration()
     {
         $xml = $this->_createNonStandardXml();

+ 69 - 5
tests/Zend/XmlRpc/RequestTest.php

@@ -104,9 +104,16 @@ class Zend_XmlRpc_RequestTest extends PHPUnit_Framework_TestCase
 
         $this->_request->addParam('string2');
         $params = $this->_request->getParams();
-        $this->assertEquals(2, count($params));
-        $this->assertEquals('string1', $params[0]);
-        $this->assertEquals('string2', $params[1]);
+        $this->assertSame(2, count($params));
+        $this->assertSame('string1', $params[0]);
+        $this->assertSame('string2', $params[1]);
+
+        $this->_request->addParam(new Zend_XmlRpc_Value_String('foo'));
+        $params = $this->_request->getParams();
+        $this->assertSame(3, count($params));
+        $this->assertSame('string1', $params[0]);
+        $this->assertSame('string2', $params[1]);
+        $this->assertSame('foo', $params[2]->getValue());
     }
 
     public function testAddDateParamGeneratesCorrectXml()
@@ -143,6 +150,18 @@ class Zend_XmlRpc_RequestTest extends PHPUnit_Framework_TestCase
         $this->_request->setParams($params);
         $returned = $this->_request->getParams();
         $this->assertSame($params, $returned);
+
+        $params = array(array('value' => 'foobar'));
+        $this->_request->setParams($params);
+        $this->assertSame(array('foobar'), $this->_request->getParams());
+        $this->assertSame(array('string'), $this->_request->getTypes());
+
+        $null = new Zend_XmlRpc_Value_Nil();
+        $this->_request->setParams('foo', 1, $null);
+        $this->assertSame(array('foo', 1, $null), $this->_request->getParams());
+        $this->assertSame(array('string', 'int', 'nil'), $this->_request->getTypes());
+
+        $this->assertNull($this->_request->setParams(), 'Call without argument returns null');
     }
 
     /**
@@ -185,6 +204,51 @@ class Zend_XmlRpc_RequestTest extends PHPUnit_Framework_TestCase
         $this->assertFalse($parsed, 'Parsed non-XML string?');
     }
 
+    public function testPassingInvalidTypeToLoadXml()
+    {
+        $this->assertFalse($this->_request->loadXml(new stdClass()));
+        $this->assertTrue($this->_request->isFault());
+        $this->assertSame(635, $this->_request->getFault()->getCode());
+        $this->assertSame('Invalid XML provided to request', $this->_request->getFault()->getMessage());
+    }
+
+    public function testLoadingXmlWithoutMethodNameElement()
+    {
+        $this->assertFalse($this->_request->loadXml('<empty/>'));
+        $this->assertTrue($this->_request->isFault());
+        $this->assertSame(632, $this->_request->getFault()->getCode());
+        $this->assertSame("Invalid request, no method passed; request must contain a 'methodName' tag",
+            $this->_request->getFault()->getMessage());
+    }
+
+    public function testLoadingXmlWithInvalidParams()
+    {
+        $this->assertFalse($this->_request->loadXml(
+            '<methodCall>'
+          . '<methodName>foo</methodName>'
+          . '<params><param/><param/><param><foo/></param></params>'
+          . '</methodCall>'));
+        $this->assertTrue($this->_request->isFault());
+        $this->assertSame(633, $this->_request->getFault()->getCode());
+        $this->assertSame(
+            'Param must contain a value',
+            $this->_request->getFault()->getMessage());
+    }
+
+    public function testExceptionWhileLoadingXmlParamValueIsHandled()
+    {
+        $this->assertFalse($this->_request->loadXml(
+            '<methodCall>'
+          . '<methodName>foo</methodName>'
+          . '<params><param><value><foo/></value></param></params>'
+          . '</methodCall>'));
+        $this->assertTrue($this->_request->isFault());
+        $this->assertSame(636, $this->_request->getFault()->getCode());
+        $this->assertSame(
+            'Error creating xmlrpc value',
+            $this->_request->getFault()->getMessage());
+    }
+
     /**
      * isFault() test
      */
@@ -209,8 +273,8 @@ class Zend_XmlRpc_RequestTest extends PHPUnit_Framework_TestCase
 
     /**
      * helper for saveXML() and __toString() tests
-     * 
-     * @param string $xml 
+     *
+     * @param string $xml
      * @return void
      */
     protected function _testXmlRequest($xml, $argv)

+ 7 - 0
tests/Zend/XmlRpc/ResponseTest.php

@@ -129,6 +129,13 @@ class Zend_XmlRpc_ResponseTest extends PHPUnit_Framework_TestCase
         $this->assertEquals('Return value', $this->_response->getReturnValue());
     }
 
+    public function testLoadXmlWithInvalidValue()
+    {
+        $this->assertFalse($this->_response->loadXml(new stdClass()));
+        $this->assertTrue($this->_response->isFault());
+        $this->assertSame(650, $this->_response->getFault()->getCode());
+    }
+
     /**
      * @group ZF-5404
      */

+ 47 - 13
tests/Zend/XmlRpc/ServerTest.php

@@ -103,7 +103,7 @@ class Zend_XmlRpc_ServerTest extends PHPUnit_Framework_TestCase
     {
         try {
             $this->_server->addFunction('Zend_XmlRpc_Server_testFunction', 'zsr');
-        } catch (Exception $e) {
+        } catch (Zend_XmlRpc_Exception $e) {
             $this->fail('Attachment should have worked');
         }
 
@@ -113,7 +113,7 @@ class Zend_XmlRpc_ServerTest extends PHPUnit_Framework_TestCase
         try {
             $this->_server->addFunction('nosuchfunction');
             $this->fail('nosuchfunction() should not exist and should throw an exception');
-        } catch (Exception $e) {
+        } catch (Zend_XmlRpc_Exception $e) {
             // do nothing
         }
 
@@ -126,7 +126,7 @@ class Zend_XmlRpc_ServerTest extends PHPUnit_Framework_TestCase
                 ),
                 'zsr'
             );
-        } catch (Exception $e) {
+        } catch (Zend_XmlRpc_Exception $e) {
             $this->fail('Error attaching array of functions: ' . $e->getMessage());
         }
         $methods = $server->listMethods();
@@ -147,7 +147,7 @@ class Zend_XmlRpc_ServerTest extends PHPUnit_Framework_TestCase
                 ),
                 'zsr'
             );
-        } catch (Exception $e) {
+        } catch (Zend_XmlRpc_Exception $e) {
             $this->fail('Error attaching functions: ' . $e->getMessage());
         }
 
@@ -174,6 +174,20 @@ class Zend_XmlRpc_ServerTest extends PHPUnit_Framework_TestCase
         $this->assertFalse(in_array('test.__construct', $methods));
     }
 
+    public function testSettingClassWithArguments()
+    {
+        $this->_server->setClass('Zend_XmlRpc_Server_testClass', 'test', 'argv-argument');
+        $request = new Zend_XmlRpc_Request();
+        $request->setMethod('test.test4');
+        $response = $this->_server->handle($request);
+        $this->assertNotType('Zend_XmlRpc_Fault', $response);
+        $this->assertSame(
+            array('test1' => 'argv-argument',
+                'test2' => null,
+                'arg' => array('argv-argument')),
+            $response->getReturnValue());
+    }
+
     /**
      * fault() test
      */
@@ -379,7 +393,7 @@ class Zend_XmlRpc_ServerTest extends PHPUnit_Framework_TestCase
         try {
             $this->_server->addFunction($o);
             $this->fail('addFunction() should not accept objects');
-        } catch (Exception $e) {
+        } catch (Zend_XmlRpc_Exception $e) {
             // success
         }
     }
@@ -390,7 +404,7 @@ class Zend_XmlRpc_ServerTest extends PHPUnit_Framework_TestCase
         try {
             $this->_server->loadFunctions($o);
             $this->fail('loadFunctions() should not accept objects');
-        } catch (Exception $e) {
+        } catch (Zend_XmlRpc_Exception $e) {
             // success
         }
 
@@ -398,8 +412,8 @@ class Zend_XmlRpc_ServerTest extends PHPUnit_Framework_TestCase
         try {
             $this->_server->loadFunctions($o);
             $this->fail('loadFunctions() should not allow non-reflection objects in an array');
-        } catch (Exception $e) {
-            // success
+        } catch (Zend_Server_Exception $e) {
+            $this->assertSame('Invalid method provided', $e->getMessage());
         }
     }
 
@@ -408,8 +422,7 @@ class Zend_XmlRpc_ServerTest extends PHPUnit_Framework_TestCase
         try {
             $this->_server->setClass('mybogusclass');
             $this->fail('setClass() should not allow invalid classes');
-        } catch (Exception $e) {
-            // success
+        } catch (Zend_XmlRpc_Exception $e) {
         }
     }
 
@@ -425,14 +438,14 @@ class Zend_XmlRpc_ServerTest extends PHPUnit_Framework_TestCase
         try {
             $this->_server->setRequest('Zend_XmlRpc_Server_testRequest2');
             $this->fail('Invalid request class should throw exception');
-        } catch (Exception $e) {
+        } catch (Zend_XmlRpc_Exception $e) {
             // success
         }
 
         try {
             $this->_server->setRequest($this);
             $this->fail('Invalid request object should throw exception');
-        } catch (Exception $e) {
+        } catch (Zend_XmlRpc_Exception $e) {
             // success
         }
     }
@@ -548,6 +561,13 @@ class Zend_XmlRpc_ServerTest extends PHPUnit_Framework_TestCase
             $this->assertNotEquals('void', $prototype->getReturnType(), var_export($prototype, 1));
         }
     }
+
+    public function testCallingUnregisteredMethod()
+    {
+        $this->setExpectedException('Zend_XmlRpc_Server_Exception',
+            'Unknown instance method called on server: foobarbaz');
+        $this->_server->foobarbaz();
+    }
 }
 
 /**
@@ -579,13 +599,18 @@ function Zend_XmlRpc_Server_testFunction2()
 
 class Zend_XmlRpc_Server_testClass
 {
+    private $_value1;
+    private $_value2;
+
     /**
      * Constructor
      * 
      * @return void
      */
-    public function __construct()
+    public function __construct($value1 = null, $value2 = null)
     {
+        $this->_value1 = $value1;
+        $this->_value2 = $value2;
     }
 
     /**
@@ -626,6 +651,15 @@ class Zend_XmlRpc_Server_testClass
     }
 
     /**
+     * @param string $arg
+     * @return struct
+     */
+    public function test4($arg)
+    {
+        return array('test1' => $this->_value1, 'test2' => $this->_value2, 'arg' => func_get_args());
+    }
+
+    /**
      * Test base64 encoding in request and response
      * 
      * @param  base64 $data 

+ 25 - 0
tests/Zend/XmlRpc/ValueTest.php

@@ -222,6 +222,20 @@ class Zend_XmlRpc_ValueTest extends PHPUnit_Framework_TestCase
         $this->assertEquals($this->wrapXml($xml), $val->saveXML());
     }
 
+    public function testMarshalStringFromDefault()
+    {
+        $native = 'foo';
+        $xml = "<string>$native</string>";
+        $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
+                                    Zend_XmlRpc_Value::XML_STRING);
+
+        $this->assertXmlRpcType('string', $val);
+        $this->assertEquals('string', $val->getType());
+        $this->assertSame($native, $val->getValue());
+        $this->assertType('DomElement', $val->getAsDOM());
+        $this->assertEquals($this->wrapXml($xml), $val->saveXML());
+    }
+
     //Nil
 
     public function testFactoryAutodetectsNil()
@@ -305,6 +319,17 @@ class Zend_XmlRpc_ValueTest extends PHPUnit_Framework_TestCase
         $this->assertSame($native, $value->getValue());
     }
 
+    public function testArrayMustContainDataElement()
+    {
+        $native = array();
+        $xml    = '<value><array/></value>';
+
+        $this->setExpectedException('Zend_XmlRpc_Value_Exception',
+            'Invalid XML for XML-RPC native array type: ARRAY tag must contain DATA tag');
+        $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
+                                    Zend_XmlRpc_Value::XML_STRING);
+    }
+
     /**
      * @group ZF-5405
      */