Skip to content

Commit

Permalink
Added ability to use multi-wallet calls.
Browse files Browse the repository at this point in the history
closes #12
  • Loading branch information
denpamusic committed Sep 7, 2018
1 parent fe02e0f commit cb9858a
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 3 deletions.
25 changes: 23 additions & 2 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ class Client
*/
protected $client = null;

/**
* URL path.
*
* @var string
*/
protected $path = '/';

/**
* JSON-RPC Id.
*
Expand Down Expand Up @@ -97,6 +104,20 @@ public function setClient(ClientInterface $client)
return $this;
}

/**
* Sets wallet for multi-wallet rpc request.
*
* @param string $name
*
* @return static
*/
public function wallet($name)
{
$this->path = "/wallet/$name";

return $this;
}

/**
* Makes request to Bitcoin Core.
*
Expand All @@ -105,7 +126,7 @@ public function setClient(ClientInterface $client)
*
* @return array
*/
public function request($method, $params = [])
public function request($method, ...$params)
{
try {
$json = [
Expand All @@ -114,7 +135,7 @@ public function request($method, $params = [])
'id' => $this->rpcId++,
];

$response = $this->client->request('POST', '/', ['json' => $json]);
$response = $this->client->request('POST', $this->path, ['json' => $json]);

if ($response->hasError()) {
// throw exception on error
Expand Down
19 changes: 19 additions & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,25 @@ public function testRequest()
$this->assertEquals(self::$getBlockResponse, $response->get());
}

public function testMultiWalletRequest()
{
$wallet = 'testwallet.dat';
$history = [];

$guzzle = $this->mockGuzzle([
$this->getBalanceResponse(),
], $history);

$response = $this->bitcoind
->setClient($guzzle)
->wallet($wallet)
->request('getbalance');

$request = $history[0]['request'];
$this->assertEquals(self::$balanceResponse, $response->get());
$this->assertEquals($request->getUri()->getPath(), "/wallet/$wallet");
}

/**
* Test async request.
*
Expand Down
29 changes: 28 additions & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase
'message' => 'No information available about transaction',
];

/**
* Balance response.
*
* @var float
*/
protected static $balanceResponse = 0.1;

/**
* Get error 500 message.
*
Expand Down Expand Up @@ -94,11 +101,13 @@ protected function mockCallable(array $with = [])
*
* @return \GuzzleHttp\Client
*/
protected function mockGuzzle(array $queue = [])
protected function mockGuzzle(array $queue = [], &$container = [])
{
$handler = $this->bitcoind->getConfig('handler');

if ($handler) {
$history = \GuzzleHttp\Middleware::history($container);
$handler->push($history);
$handler->setHandler(new MockHandler($queue));
}

Expand All @@ -125,6 +134,24 @@ protected function getBlockResponse($code = 200)
return new Response($code, [], $json);
}

/**
* Get getbalance command response.
*
* @param int $code
*
* @return \Psr\Http\Message\ResponseInterface
*/
protected function getBalanceResponse($code = 200)
{
$json = json_encode([
'result' => self::$balanceResponse,
'error' => null,
'id' => 0,
]);

return new Response($code, [], $json);
}

/**
* Make raw transaction error response.
*
Expand Down

0 comments on commit cb9858a

Please sign in to comment.