From a491348d442ab8e6cd2fa403d4365cdad78e52ce Mon Sep 17 00:00:00 2001 From: Frédéric Guillot Date: Thu, 21 Jun 2018 14:13:41 -0700 Subject: Vendoring deprecated composer libs --- libs/jsonrpc/tests/ClientTest.php | 103 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 libs/jsonrpc/tests/ClientTest.php (limited to 'libs/jsonrpc/tests/ClientTest.php') 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 @@ +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); + } +} -- cgit v1.2.3