Skip to content

Commit

Permalink
Improved project structure.
Browse files Browse the repository at this point in the history
  • Loading branch information
denpamusic committed Oct 4, 2018
1 parent 5c783a9 commit c09cd82
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 73 deletions.
14 changes: 13 additions & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,16 @@ protected function getDsn()
$this->config['port'];
}

/**
* Gets response handler class name.
*
* @return string
*/
protected function getResponseHandler()
{
return 'Denpa\\Bitcoin\\Responses\\BitcoindResponse';
}

/**
* Gets Guzzle handler stack.
*
Expand All @@ -287,7 +297,9 @@ protected function getHandler()

$stack->push(
Middleware::mapResponse(function (ResponseInterface $response) {
return BitcoindResponse::createFrom($response);
$handler = $this->getResponseHandler();

return new $handler($response);
}),
'json_response'
);
Expand Down
16 changes: 16 additions & 0 deletions src/Responses/BitcoindResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Denpa\Bitcoin\Responses;

use Denpa\Bitcoin\Traits\Collection;
use Denpa\Bitcoin\Traits\ReadOnlyArray;
use Denpa\Bitcoin\Traits\SerializableContainer;

class BitcoindResponse extends Response implements
\ArrayAccess,
\Countable,
\Serializable,
\JsonSerializable
{
use Collection, ReadOnlyArray, SerializableContainer;
}
34 changes: 4 additions & 30 deletions src/BitcoindResponse.php → src/Responses/Response.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
<?php

namespace Denpa\Bitcoin;
namespace Denpa\Bitcoin\Responses;

use Denpa\Bitcoin\Traits\Message;
use Psr\Http\Message\ResponseInterface;

class BitcoindResponse implements
ResponseInterface,
\ArrayAccess,
\Countable,
\Serializable,
\JsonSerializable
abstract class Response implements ResponseInterface
{
use MessageTrait,
ResponseArrayTrait,
ReadOnlyArrayTrait,
SerializableContainerTrait;
use Message;

/**
* Response instance.
Expand All @@ -30,13 +23,6 @@ class BitcoindResponse implements
*/
protected $container = [];

/**
* Current key.
*
* @var string
*/
protected $current;

/**
* Constructs new json response.
*
Expand Down Expand Up @@ -154,16 +140,4 @@ public function getReasonPhrase()
{
return $this->response->getReasonPhrase();
}

/**
* Creates new json response from response interface object.
*
* @param \Psr\Http\Message\ResponseInterface $response
*
* @return \Denpa\Bitcoin\BitcoindResponse
*/
public static function createFrom(ResponseInterface $response)
{
return new self($response);
}
}
11 changes: 9 additions & 2 deletions src/ResponseArrayTrait.php → src/Traits/Collection.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
<?php

namespace Denpa\Bitcoin;
namespace Denpa\Bitcoin\Traits;

use InvalidArgumentException;

trait ResponseArrayTrait
trait Collection
{
/**
* Current key.
*
* @var string
*/
protected $current;

/**
* Gets data by using key with dotted notation.
*
Expand Down
4 changes: 2 additions & 2 deletions src/MessageTrait.php → src/Traits/Message.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

namespace Denpa\Bitcoin;
namespace Denpa\Bitcoin\Traits;

use Psr\Http\Message\StreamInterface;

trait MessageTrait
trait Message
{
/**
* Retrieves the HTTP protocol version as a string.
Expand Down
6 changes: 4 additions & 2 deletions src/ReadOnlyArrayTrait.php → src/Traits/ReadOnlyArray.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?php

namespace Denpa\Bitcoin;
namespace Denpa\Bitcoin\Traits;

trait ReadOnlyArrayTrait
use Denpa\Bitcoin\Exceptions;

trait ReadOnlyArray
{
/**
* Assigns a value to the specified offset.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace Denpa\Bitcoin;
namespace Denpa\Bitcoin\Traits;

trait SerializableContainerTrait
trait SerializableContainer
{
/**
* Gets container.
Expand Down
47 changes: 20 additions & 27 deletions tests/BitcoindResponseTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php

use Denpa\Bitcoin;
use Denpa\Bitcoin\Exceptions;
use Denpa\Bitcoin\Responses\BitcoindResponse;
use Psr\Http\Message\ResponseInterface;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\BufferStream;

class BitcoindResponseTest extends TestCase
Expand All @@ -16,7 +18,7 @@ public function setUp()
parent::setUp();

$this->guzzleResponse = $this->getBlockResponse();
$this->response = Bitcoin\BitcoindResponse::createFrom($this->guzzleResponse);
$this->response = new BitcoindResponse($this->guzzleResponse);
$this->response = $this->response->withHeader('X-Test', 'test');
}

Expand Down Expand Up @@ -61,13 +63,22 @@ public function testResult()
*/
public function testNoResult()
{
$response = Bitcoin\BitcoindResponse::createFrom(
$this->rawTransactionError()
);

$response = new BitcoindResponse($this->rawTransactionError());
$this->assertFalse($response->hasResult());
}

/**
* Test raw response getter.
*
* @return void
*/
public function testRawResponse()
{
$response = $this->response->response();
$this->assertInstanceOf(ResponseInterface::class, $response);
$this->assertInstanceOf(Response::class, $response);
}

/**
* Test getter for status code.
*
Expand Down Expand Up @@ -101,31 +112,14 @@ public function testWithStatus()
$this->assertEquals('test', $response->getReasonPhrase());
}

/**
* Test creating BitcoindResponse from Guzzle.
*
* @return void
*/
public function testCreateFrom()
{
$guzzleResponse = $this->getBlockResponse();

$response = Bitcoin\BitcoindResponse::createFrom($guzzleResponse);

$this->assertInstanceOf(Bitcoin\BitcoindResponse::class, $response);
$this->assertEquals($response->response(), $guzzleResponse);
}

/**
* Test error in response.
*
* @return void
*/
public function testError()
{
$response = Bitcoin\BitcoindResponse::createFrom(
$this->rawTransactionError()
);
$response = new BitcoindResponse($this->rawTransactionError());

$this->assertTrue($response->hasError());

Expand Down Expand Up @@ -561,12 +555,11 @@ public function testBody()
public function testSerialize()
{
$serializedContainer = serialize($this->response->getContainer());
$class = Bitcoin\BitcoindResponse::class;

$serialized = sprintf(
'C:%u:"%s":%u:{%s}',
strlen($class),
$class,
strlen(BitcoindResponse::class),
BitcoindResponse::class,
strlen($serializedContainer),
$serializedContainer
);
Expand Down
46 changes: 44 additions & 2 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use Denpa\Bitcoin;
use Denpa\Bitcoin\Exceptions;
use Denpa\Bitcoin\Responses\BitcoindResponse;
use GuzzleHttp\Psr7\Response;

class ClientTest extends TestCase
Expand Down Expand Up @@ -214,7 +215,7 @@ public function testAsyncRequest()
]);

$onFulfilled = $this->mockCallable([
$this->callback(function (Bitcoin\BitcoindResponse $response) {
$this->callback(function (BitcoindResponse $response) {
return $response->get() == self::$getBlockResponse;
}),
]);
Expand Down Expand Up @@ -276,7 +277,7 @@ public function testAsyncMagic()
]);

$onFulfilled = $this->mockCallable([
$this->callback(function (Bitcoin\BitcoindResponse $response) {
$this->callback(function (BitcoindResponse $response) {
return $response->get() == self::$getBlockResponse;
}),
]);
Expand Down Expand Up @@ -536,4 +537,45 @@ function ($exception) use ($rejected) {

$this->bitcoind->__destruct();
}

/**
* Test setting different response handler class.
*
* @return void
*/
public function testSetResponseHandler()
{
$fake = new FakeClient();

$guzzle = $this->mockGuzzle([
$this->getBlockResponse(),
], $fake->getConfig('handler'));

$response = $fake
->setClient($guzzle)
->request(
'getblockheader',
'000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f'
);

$this->assertInstanceOf(FakeResponse::class, $response);
}
}

class FakeClient extends Bitcoin\Client
{
/**
* Gets response handler class name.
*
* @return string
*/
protected function getResponseHandler()
{
return 'FakeResponse';
}
}

class FakeResponse extends Bitcoin\Responses\Response
{
//
}
11 changes: 6 additions & 5 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

use Denpa\Bitcoin;
use Denpa\Bitcoin\Responses\BitcoindResponse;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\Psr7\Response;
Expand Down Expand Up @@ -109,13 +109,14 @@ protected function mockCallable(array $with = [])
/**
* Get Guzzle mock client.
*
* @param array $queue
* @param array $queue
* @param \GuzzleHttp\HandlerStack $handler
*
* @return \GuzzleHttp\Client
*/
protected function mockGuzzle(array $queue = [])
protected function mockGuzzle(array $queue = [], $handler = null)
{
$handler = $this->bitcoind->getConfig('handler');
$handler = $handler ?: $this->bitcoind->getConfig('handler');

if ($handler) {
$middleware = \GuzzleHttp\Middleware::history($this->history);
Expand Down Expand Up @@ -193,7 +194,7 @@ protected function requestExceptionWithResponse()
return new RequestException(
'test',
$request,
Bitcoin\BitcoindResponse::createFrom($this->rawTransactionError())
new BitcoindResponse($this->rawTransactionError())
);
};

Expand Down

0 comments on commit c09cd82

Please sign in to comment.