Explorar el Código

More code coverage. Everything testable is tested now

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@17770 44c647ce-9c0f-0410-b52a-842ac1e357ba
lars hace 16 años
padre
commit
27bd0d2a75

+ 11 - 14
library/Zend/XmlRpc/Server.php

@@ -136,7 +136,7 @@ class Zend_XmlRpc_Server extends Zend_Server_Abstract
 
     /**
      * Dispatch table of name => method pairs
-     * @var Zend_XmlRpc_Server_ServerDefinition
+     * @var Zend_Server_Definition
      */
     protected $_table;
 
@@ -261,10 +261,8 @@ class Zend_XmlRpc_Server extends Zend_Server_Abstract
     public function setClass($class, $namespace = '', $argv = null)
     {
         if (is_string($class) && !class_exists($class)) {
-            if (!class_exists($class)) {
-                require_once 'Zend/XmlRpc/Server/Exception.php';
-                throw new Zend_XmlRpc_Server_Exception('Invalid method class', 610);
-            }
+            require_once 'Zend/XmlRpc/Server/Exception.php';
+            throw new Zend_XmlRpc_Server_Exception('Invalid method class', 610);
         }
 
         $argv = null;
@@ -291,7 +289,7 @@ class Zend_XmlRpc_Server extends Zend_Server_Abstract
         if (!$fault instanceof Exception) {
             $fault = (string) $fault;
             if (empty($fault)) {
-                $fault = 'Unknown error';
+                $fault = 'Unknown Error';
             }
             require_once 'Zend/XmlRpc/Server/Exception.php';
             $fault = new Zend_XmlRpc_Server_Exception($fault, $code);
@@ -447,15 +445,14 @@ class Zend_XmlRpc_Server extends Zend_Server_Abstract
      */
     public function setResponseClass($class)
     {
-        if (class_exists($class)) {
-            $reflection = new ReflectionClass($class);
-            if ($reflection->isSubclassOf(new ReflectionClass('Zend_XmlRpc_Response'))) {
-                $this->_responseClass = $class;
-                return true;
-            }
-        }
+        if (!class_exists($class) or
+            ($c = new ReflectionClass($class) and !$c->isSubclassOf('Zend_XmlRpc_Response'))) {
 
-        return false;
+            require_once 'Zend/XmlRpc/Server/Exception.php';
+            throw new Zend_XmlRpc_Server_Exception('Invalid response class');
+        }
+        $this->_responseClass = $class;
+        return true;
     }
 
     /**

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

@@ -70,7 +70,8 @@ class Zend_XmlRpc_Server_System
     {
         $table = $this->_server->getDispatchTable();
         if (!$table->hasMethod($method)) {
-            throw new Zend_Server_Exception('Method "' . $method . '"does not exist', 640);
+            require_once 'Zend/XmlRpc/Server/Exception.php';
+            throw new Zend_XmlRpc_Server_Exception('Method "' . $method . '" does not exist', 640);
         }
 
         return $table->getMethod($method)->getMethodHelp();
@@ -86,7 +87,8 @@ class Zend_XmlRpc_Server_System
     {
         $table = $this->_server->getDispatchTable();
         if (!$table->hasMethod($method)) {
-            throw new Zend_Server_Exception('Method "' . $method . '"does not exist', 640);
+            require_once 'Zend/XmlRpc/Server/Exception.php';
+            throw new Zend_XmlRpc_Server_Exception('Method "' . $method . '" does not exist', 640);
         }
         $method = $table->getMethod($method)->toArray();
         return $method['prototypes'];

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

@@ -337,18 +337,13 @@ abstract class Zend_XmlRpc_Value
                 $xmlrpc_val = new Zend_XmlRpc_Value_Array($values);
                 break;
             case self::XMLRPC_TYPE_STRUCT:
-                // If the XML is valid, $value must be an SimpleXML
-                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_STRUCT .' type');
-                }
                 $values = array();
                 // Parse all the memebers of the struct from the XML string
                 // (simple xml element) to Zend_XmlRpc_Value objects
                 foreach ($value->member as $member) {
                     // @todo? If a member doesn't have a <value> tag, we don't add it to the struct
                     // Maybe we want to throw an exception here ?
-                    if ((!$member->value instanceof SimpleXMLElement)) {
+                    if (!isset($member->value) or !isset($member->name)) {
                         continue;
                         //throw new Zend_XmlRpc_Value_Exception('Member of the '. self::XMLRPC_TYPE_STRUCT .' XML-RPC native type must contain a VALUE tag');
                     }

+ 0 - 6
library/Zend/XmlRpc/Value/Collection.php

@@ -66,14 +66,8 @@ abstract class Zend_XmlRpc_Value_Collection extends Zend_XmlRpc_Value
         $values = (array)$this->_value;
         foreach ($values as $key => $value) {
             /* @var $value Zend_XmlRpc_Value */
-
-            if (!$value instanceof parent) {
-                throw new Zend_XmlRpc_Value_Exception('Values of '. get_class($this) .' type must be Zend_XmlRpc_Value objects');
-            }
             $values[$key] = $value->getValue();
         }
         return $values;
     }
-
 }
-

+ 8 - 10
library/Zend/XmlRpc/Value/DateTime.php

@@ -36,7 +36,6 @@ require_once 'Zend/XmlRpc/Value/Scalar.php';
  */
 class Zend_XmlRpc_Value_DateTime extends Zend_XmlRpc_Value_Scalar
 {
-
     /**
      * Set the value of a dateTime.iso8601 native type
      *
@@ -51,20 +50,21 @@ class Zend_XmlRpc_Value_DateTime extends Zend_XmlRpc_Value_Scalar
 
         // If the value is not numeric, we try to convert it to a timestamp (using the strtotime function)
         if (is_numeric($value)) {   // The value is numeric, we make sure it is an integer
-            $value = (int)$value;
+            $timestamp = (int)$value;
         } else {
-            $value = strtotime($value);
-            if ($value === false || $value == -1) { // cannot convert the value to a timestamp
+            $timestamp = strtotime($value);
+            if ($timestamp === false || $timestamp == -1) { // cannot convert the value to a timestamp
                 throw new Zend_XmlRpc_Value_Exception('Cannot convert given value \''. $value .'\' to a timestamp');
             }
         }
-        $value = date('c', $value); // Convert the timestamp to iso8601 format
+
+        $date = date('c', $timestamp); // Convert the timestamp to iso8601 format
 
         // Strip out TZ information and dashes
-        $value = preg_replace('/(\+|-)\d{2}:\d{2}$/', '', $value);
-        $value = str_replace('-', '', $value);
+        $date = preg_replace('/(\+|-)\d{2}:\d{2}$/', '', $date);
+        $date = str_replace('-', '', $date);
 
-        $this->_value = $value;
+        $this->_value = $date;
     }
 
     /**
@@ -76,6 +76,4 @@ class Zend_XmlRpc_Value_DateTime extends Zend_XmlRpc_Value_Scalar
     {
         return $this->_value;
     }
-
 }
-

+ 0 - 19
library/Zend/XmlRpc/Value/Nil.php

@@ -56,24 +56,5 @@ class Zend_XmlRpc_Value_Nil extends Zend_XmlRpc_Value_Scalar
     {
         return null;
     }
-
-    /**
-     * Return the XML code representing the nil
-     *
-     * @return string
-     */
-    public function saveXML()
-    {
-        if (! $this->_as_xml) {   // The XML was not generated yet
-            $dom   = new DOMDocument('1.0', 'UTF-8');
-            $value = $dom->appendChild($dom->createElement('value'));
-            $type  = $value->appendChild($dom->createElement($this->_type));
-
-            $this->_as_dom = $value;
-            $this->_as_xml = $this->_stripXmlDeclaration($dom);
-        }
-
-        return $this->_as_xml;
-    }
 }
 

+ 15 - 0
tests/Zend/XmlRpc/Server/FaultTest.php

@@ -164,6 +164,8 @@ class Zend_XmlRpc_Server_FaultTest extends PHPUnit_Framework_TestCase
         $this->assertTrue($f instanceof Zend_XmlRpc_Server_Fault);
         $this->assertEquals('Checking observers', $f->getMessage());
         $this->assertEquals(411, $f->getCode());
+
+        $this->assertFalse(Zend_XmlRpc_Server_Fault::attachObserver('foo'));
     }
 
     /**
@@ -181,6 +183,8 @@ class Zend_XmlRpc_Server_FaultTest extends PHPUnit_Framework_TestCase
         $fault = Zend_XmlRpc_Server_Fault::getInstance($e);
         $observed = zxrs_fault_observer::getObserved();
         $this->assertTrue(empty($observed));
+
+        $this->assertFalse(Zend_XmlRpc_Server_Fault::detachObserver('foo'));
     }
 
     /**
@@ -195,6 +199,17 @@ class Zend_XmlRpc_Server_FaultTest extends PHPUnit_Framework_TestCase
     }
 
     /**
+     * getException() test
+     */
+    public function testGetException()
+    {
+        $e = new Zend_XmlRpc_Server_Exception('Testing fault', 411);
+        $fault = Zend_XmlRpc_Server_Fault::getInstance($e);
+
+        $this->assertSame($e, $fault->getException());
+    }
+
+    /**
      * getMessage() test
      */
     public function testGetMessage()

+ 63 - 3
tests/Zend/XmlRpc/ServerTest.php

@@ -239,6 +239,17 @@ class Zend_XmlRpc_ServerTest extends PHPUnit_Framework_TestCase
         $this->assertEquals(623, $response->getCode());
     }
 
+    public function testCallingInvalidMethod()
+    {
+        $request = new Zend_XmlRpc_Request();
+        $request->setMethod('invalid');
+        $response = $this->_server->handle($request);
+        $this->assertType('Zend_XmlRpc_Fault', $response);
+        $this->assertSame('Method "invalid" does not exist', $response->getMessage());
+        $this->assertSame(620, $response->getCode());
+    }
+
+
     /**
      * setResponseClass() test
      *
@@ -251,7 +262,7 @@ class Zend_XmlRpc_ServerTest extends PHPUnit_Framework_TestCase
      */
     public function testSetResponseClass()
     {
-        $this->_server->setResponseClass('Zend_XmlRpc_Server_testResponse');
+        $this->assertTrue($this->_server->setResponseClass('Zend_XmlRpc_Server_testResponse'));
         $request = new Zend_XmlRpc_Request();
         $request->setMethod('system.listMethods');
         $response = $this->_server->handle($request);
@@ -289,8 +300,11 @@ class Zend_XmlRpc_ServerTest extends PHPUnit_Framework_TestCase
      */
     public function testMethodHelp()
     {
-        $help = $this->_server->methodHelp('system.listMethods');
-        $this->assertContains('all available XMLRPC methods', $help);
+        $help = $this->_server->methodHelp('system.methodHelp', 'system.listMethods');
+        $this->assertContains('Display help message for an XMLRPC method', $help);
+
+        $this->setExpectedException('Zend_XmlRpc_Server_Exception', 'Method "foo" does not exist');
+        $this->_server->methodHelp('foo');
     }
 
     /**
@@ -308,6 +322,9 @@ class Zend_XmlRpc_ServerTest extends PHPUnit_Framework_TestCase
         $sig = $this->_server->methodSignature('system.methodSignature');
         $this->assertTrue(is_array($sig));
         $this->assertEquals(1, count($sig), var_export($sig, 1));
+
+        $this->setExpectedException('Zend_XmlRpc_Server_Exception', 'Method "foo" does not exist');
+        $this->_server->methodSignature('foo');
     }
 
     /**
@@ -408,6 +425,13 @@ class Zend_XmlRpc_ServerTest extends PHPUnit_Framework_TestCase
             // success
         }
 
+        try {
+            $this->_server->loadFunctions('foo');
+            $this->fail('loadFunctions() should not accept primitive values');
+        } catch (Zend_XmlRpc_Server_Exception $e) {
+            // success
+        }
+
         $o = array($o);
         try {
             $this->_server->loadFunctions($o);
@@ -417,6 +441,17 @@ class Zend_XmlRpc_ServerTest extends PHPUnit_Framework_TestCase
         }
     }
 
+    public function testLoadFunctionsReadsMethodsFromServerDefinitionObjects()
+    {
+        $mockedMethod = $this->getMock('Zend_Server_Method_Definition', array(), array(), '', false,
+            false);
+        $mockedDefinition = $this->getMock('Zend_Server_Definition', array(), array(), '', false, false);
+        $mockedDefinition->expects($this->once())
+                         ->method('getMethods')
+                         ->will($this->returnValue(array('bar' => $mockedMethod)));
+        $this->_server->loadFunctions($mockedDefinition);
+    }
+
     public function testSetClassThrowsExceptionWithInvalidClass()
     {
         try {
@@ -568,6 +603,31 @@ class Zend_XmlRpc_ServerTest extends PHPUnit_Framework_TestCase
             'Unknown instance method called on server: foobarbaz');
         $this->_server->foobarbaz();
     }
+
+    public function testSetPersistenceDoesNothing()
+    {
+        $this->assertNull($this->_server->setPersistence('foo'));
+        $this->assertNull($this->_server->setPersistence('whatever'));
+    }
+
+    public function testPassingInvalidRequestClassThrowsException()
+    {
+        $this->setExpectedException('Zend_XmlRpc_Server_Exception', 'Invalid request class');
+        $this->_server->setRequest('stdClass');
+    }
+
+    public function testPassingInvalidResponseClassThrowsException()
+    {
+        $this->setExpectedException('Zend_XmlRpc_Server_Exception', 'Invalid response class');
+        $this->_server->setResponseClass('stdClass');
+    }
+
+    public function testCreatingFaultWithEmptyMessageResultsInUnknownError()
+    {
+        $fault = $this->_server->fault('', 123);
+        $this->assertSame('Unknown Error', $fault->getMessage());
+        $this->assertSame(123, $fault->getCode());
+    }
 }
 
 /**

+ 45 - 1
tests/Zend/XmlRpc/ValueTest.php

@@ -259,7 +259,6 @@ class Zend_XmlRpc_ValueTest extends PHPUnit_Framework_TestCase
         $xml = '<value><nil/></value>';
         $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
                                     Zend_XmlRpc_Value::XML_STRING);
-
         $this->assertXmlRpcType('nil', $val);
         $this->assertEquals('nil', $val->getType());
         $this->assertSame(NULL, $val->getValue());
@@ -385,6 +384,44 @@ class Zend_XmlRpc_ValueTest extends PHPUnit_Framework_TestCase
         $this->assertEquals($this->wrapXml($xml), $val->saveXML());
     }
 
+    public function testMarshallingStructWithMemberWithoutValue()
+    {
+        $native = array('foo' => 0, 'bar' => 1);
+        $xml = '<value><struct>'
+             . '<member><name>foo</name><value><int>0</int></value></member>'
+             . '<member><name>foo</name><bar/></member>'
+             . '<member><name>bar</name><value><int>1</int></value></member>'
+             . '</struct></value>';
+
+        $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
+                                    Zend_XmlRpc_Value::XML_STRING);
+
+        $this->assertXmlRpcType('struct', $val);
+        $this->assertEquals('struct', $val->getType());
+        $this->assertSame($native, $val->getValue());
+        $this->assertType('DomElement', $val->getAsDOM());
+        $this->assertEquals($this->wrapXml($xml), $val->saveXML());
+    }
+
+    public function testMarshallingStructWithMemberWithoutName()
+    {
+        $native = array('foo' => 0, 'bar' => 1);
+        $xml = '<value><struct>'
+             . '<member><name>foo</name><value><int>0</int></value></member>'
+             . '<member><value><string>foo</string></value></member>'
+             . '<member><name>bar</name><value><int>1</int></value></member>'
+             . '</struct></value>';
+
+        $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
+                                    Zend_XmlRpc_Value::XML_STRING);
+
+        $this->assertXmlRpcType('struct', $val);
+        $this->assertEquals('struct', $val->getType());
+        $this->assertSame($native, $val->getValue());
+        $this->assertType('DomElement', $val->getAsDOM());
+        $this->assertEquals($this->wrapXml($xml), $val->saveXML());
+    }
+
     /**
      * @group ZF-7639
      */
@@ -447,6 +484,13 @@ class Zend_XmlRpc_ValueTest extends PHPUnit_Framework_TestCase
         $this->assertEquals($expected, $received);
     }
 
+    public function testMarshalDateTimeFromInvalidString()
+    {
+        $this->setExpectedException('Zend_XmlRpc_Value_Exception',
+            "Cannot convert given value 'foobarbaz' to a timestamp");
+        Zend_XmlRpc_Value::getXmlRpcValue('foobarbaz', Zend_XmlRpc_Value::XMLRPC_TYPE_DATETIME);
+    }
+
     public function testMarshalDateTimeFromNativeInteger()
     {
         $native = strtotime('1997-07-16T19:20+01:00');