summaryrefslogtreecommitdiff
path: root/libs/jsonrpc/tests/ClientTest.php
diff options
context:
space:
mode:
Diffstat (limited to 'libs/jsonrpc/tests/ClientTest.php')
-rw-r--r--libs/jsonrpc/tests/ClientTest.php103
1 files changed, 103 insertions, 0 deletions
diff --git a/libs/jsonrpc/tests/ClientTest.php b/libs/jsonrpc/tests/ClientTest.php
new file mode 100644
index 00000000..d1f83877
--- /dev/null
+++ b/libs/jsonrpc/tests/ClientTest.php
@@ -0,0 +1,103 @@
+<?php
+
+use JsonRPC\Client;
+
+require_once __DIR__.'/../../../vendor/autoload.php';
+
+class ClientTest extends PHPUnit_Framework_TestCase
+{
+ private $httpClient;
+
+ public function setUp()
+ {
+ $this->httpClient = $this
+ ->getMockBuilder('\JsonRPC\HttpClient')
+ ->setMethods(array('execute'))
+ ->getMock();
+ }
+
+ public function testSendBatch()
+ {
+ $client = new Client('', false, $this->httpClient);
+ $response = array(
+ array(
+ 'jsonrpc' => '2.0',
+ 'result' => 'c',
+ 'id' => 1,
+ ),
+ array(
+ 'jsonrpc' => '2.0',
+ 'result' => 'd',
+ 'id' => 2,
+ )
+ );
+
+ $this->httpClient
+ ->expects($this->once())
+ ->method('execute')
+ ->with($this->stringContains('[{"jsonrpc":"2.0","method":"methodA","id":'))
+ ->will($this->returnValue($response));
+
+
+ $result = $client->batch()
+ ->execute('methodA', array('a' => 'b'))
+ ->execute('methodB', array('a' => 'b'))
+ ->send();
+
+ $this->assertEquals(array('c', 'd'), $result);
+ }
+
+ public function testSendRequest()
+ {
+ $client = new Client('', false, $this->httpClient);
+
+ $this->httpClient
+ ->expects($this->once())
+ ->method('execute')
+ ->with($this->stringContains('{"jsonrpc":"2.0","method":"methodA","id":'))
+ ->will($this->returnValue(array('jsonrpc' => '2.0', 'result' => 'foobar', 'id' => 1)));
+
+ $result = $client->execute('methodA', array('a' => 'b'));
+ $this->assertEquals($result, 'foobar');
+ }
+
+ public function testSendRequestWithError()
+ {
+ $client = new Client('', false, $this->httpClient);
+
+ $this->httpClient
+ ->expects($this->once())
+ ->method('execute')
+ ->with($this->stringContains('{"jsonrpc":"2.0","method":"methodA","id":'))
+ ->will($this->returnValue(array(
+ 'jsonrpc' => '2.0',
+ 'error' => array(
+ 'code' => -32601,
+ 'message' => 'Method not found',
+ ),
+ )));
+
+ $this->setExpectedException('BadFunctionCallException');
+ $client->execute('methodA', array('a' => 'b'));
+ }
+
+ public function testSendRequestWithErrorAndReturnExceptionEnabled()
+ {
+ $client = new Client('', true, $this->httpClient);
+
+ $this->httpClient
+ ->expects($this->once())
+ ->method('execute')
+ ->with($this->stringContains('{"jsonrpc":"2.0","method":"methodA","id":'))
+ ->will($this->returnValue(array(
+ 'jsonrpc' => '2.0',
+ 'error' => array(
+ 'code' => -32601,
+ 'message' => 'Method not found',
+ ),
+ )));
+
+ $result = $client->execute('methodA', array('a' => 'b'));
+ $this->assertInstanceOf('BadFunctionCallException', $result);
+ }
+}