Example #1
0
 /**
  * @covers      \fpoirotte\XRL\Server::call
  * @covers      \fpoirotte\XRL\CapableServer::multicall
  */
 public function testMulticall()
 {
     $i = 0;
     // Basic server.
     $server = new \fpoirotte\XRL\Server();
     $server->bar = function ($throw) {
         if ($throw) {
             throw new \Exception('Oops');
         }
         return 42;
     };
     // Enable advanced capabilities.
     \fpoirotte\XRL\CapableServer::enable($server);
     $requests = array(null, array(), array('params' => array()), array('methodName' => 'bar'), array('methodName' => '', 'params' => array()), array('methodName' => 42, 'params' => array()), array('methodName' => '', 'params' => 42), array('methodName' => 'system.multicall', 'params' => array()), array('methodName' => 'bar', 'params' => array(true)), array('methodName' => 'bar', 'params' => array(false)));
     $res = $server->call('system.multicall', array($requests));
     $this->assertCount(count($requests), $res);
     // 0: Invalid call: garbage input.
     $this->assertInstanceOf('\\BadFunctionCallException', $res[$i]);
     $this->assertSame('Expected struct', $res[$i++]->getMessage());
     // 1: Invalid call: missing call information.
     $this->assertInstanceOf('\\BadFunctionCallException', $res[$i]);
     $this->assertSame('Missing methodName', $res[$i++]->getMessage());
     // 2: Invalid call: missing procedure name.
     $this->assertInstanceOf('\\BadFunctionCallException', $res[$i]);
     $this->assertSame('Missing methodName', $res[$i++]->getMessage());
     // 3: Invalid call: missing parameters.
     $this->assertInstanceOf('\\BadFunctionCallException', $res[$i]);
     $this->assertSame('Missing params', $res[$i++]->getMessage());
     // 4: Invalid call: non-existent method.
     $this->assertInstanceOf('\\fpoirotte\\XRL\\Exception', $res[$i]);
     $this->assertSame('server error. requested method not found', $res[$i++]->getMessage());
     // 5: Invalid call: invalid type for "methodName".
     $this->assertInstanceOf('\\BadFunctionCallException', $res[$i]);
     $this->assertSame('Expected a string', $res[$i++]->getMessage());
     // 6: Invalid call: invalid type for "params".
     $this->assertInstanceOf('\\BadFunctionCallException', $res[$i]);
     $this->assertSame('Invalid params', $res[$i++]->getMessage());
     // 7: Invalid call: recursive call.
     $this->assertInstanceOf('\\BadFunctionCallException', $res[$i]);
     $this->assertSame('Recursive call', $res[$i++]->getMessage());
     // 8: Valid call throwing an exception.
     $this->assertInstanceOf('\\Exception', $res[$i]);
     $this->assertSame('Oops', $res[$i++]->getMessage());
     // 9: Valid call returning a result.
     $this->assertInternalType('array', $res[$i]);
     $this->assertSame(array(42), $res[$i++]);
 }
Example #2
0
    {
        return $a - $b;
    }
    public static function multiply($a, $b)
    {
        return $a * $b;
    }
    public static function divide($a, $b)
    {
        return $a / $b;
    }
}
// Create a new server.
$server = new \fpoirotte\XRL\Server();
// Add capabilities (system.*) to that server.
\fpoirotte\XRL\CapableServer::enable($server);
// Now, let's register some procedures!
// - inline closure
$server->hello = function ($s) {
    return "Hello {$s}";
};
// - global function
$server->qux = 'foo';
// - object method
$bar = new Bar(42);
$server->bar = array($bar, 'getValue');
// Expose the methods of the Maths class
// under the "maths." prefix (eg. "maths.add").
$server->expose('Maths', 'maths');
// Let the server handle the current request:
// - A full XML-RPC request may be passed using the "request"