File size: 3,096 Bytes
e4f4821 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
<?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->expectException('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);
}
}
|