Skip to content
This repository has been archived by the owner on Jun 16, 2022. It is now read-only.

Commit

Permalink
Replace array response to promise
Browse files Browse the repository at this point in the history
  • Loading branch information
perevoshchikov committed Sep 29, 2019
1 parent f81fa55 commit 4db4ba5
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 99 deletions.
88 changes: 56 additions & 32 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
namespace Anper\Jsonbox;

use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Promise\PromiseInterface;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\RequestOptions;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\UriInterface;

/**
Expand Down Expand Up @@ -38,77 +41,98 @@ public function __construct(ClientInterface $client)
* @param UriInterface $uri
* @param array $values
*
* @return array
* @return PromiseInterface
* @throws Exception
*/
public function create(UriInterface $uri, array $values): array
public function create(UriInterface $uri, array $values): PromiseInterface
{
return $this->send('POST', $uri, $values);
return $this->send(
$this->createRequest('POST', $uri, $values)
);
}

/**
* @param UriInterface $uri
*
* @return array
* @return PromiseInterface
* @throws Exception
*/
public function read(UriInterface $uri): array
public function read(UriInterface $uri): PromiseInterface
{
return $this->send('GET', $uri);
return $this->send(
$this->createRequest('GET', $uri)
);
}

/**
* @param UriInterface $uri
* @param array $values
*
* @return array
* @return PromiseInterface
* @throws Exception
*/
public function update(UriInterface $uri, array $values): array
public function update(UriInterface $uri, array $values): PromiseInterface
{
return $this->send('PUT', $uri, $values);
return $this->send(
$this->createRequest('PUT', $uri, $values)
);
}

/**
* @param UriInterface $uri
*
* @return array
* @return PromiseInterface
* @throws Exception
*/
public function delete(UriInterface $uri): array
public function delete(UriInterface $uri): PromiseInterface
{
return $this->send('DELETE', $uri);
return $this->send(
$this->createRequest('DELETE', $uri)
);
}

/**
* @param RequestInterface $request
*
* @return PromiseInterface
*/
public function send(RequestInterface $request): PromiseInterface
{
return $this->client->sendAsync($request)
->then(static function (ResponseInterface $response) {
return $response->getBody();
})
->then(static function (string $body) {
return (array) \GuzzleHttp\json_decode($body, true);
})
->otherwise(function ($value) {
if ($value instanceof \Throwable) {
throw new Exception($value->getMessage());
}

throw new Exception((string) $value);
});
}

/**
* @param string $method
* @param UriInterface $uri
* @param array $body
*
* @return array
* @return RequestInterface
* @throws Exception
*/
public function send(string $method, UriInterface $uri, array $body = []): array
public function createRequest(string $method, UriInterface $uri, array $body = []): RequestInterface
{
$options = [
RequestOptions::JSON => $body,
RequestOptions::HEADERS => [
'Accept' => 'application/json',
],
];

$options = \array_filter($options);

try {
$response = $this->client->request($method, $uri, $options);
} catch (\Exception|GuzzleException $exception) {
throw new Exception($exception->getMessage(), 0, $exception);
}

try {
return (array) \GuzzleHttp\json_decode($response->getBody(), true);
$data = $body ? \GuzzleHttp\json_encode($body): null;
} catch (\Exception $exception) {
throw new Exception($exception->getMessage(), 0, $exception);
throw new Exception($exception->getMessage());
}

return new Request($method, $uri, [
'Content-Type' => 'application/json',
'Accept' => 'application/json',
], $data);
}
}
8 changes: 6 additions & 2 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ public function __construct(Client $client, UriInterface $uri)
*/
public function create(array $values): array
{
return $this->client->create($this->uri, $values);
return $this->client
->create($this->uri, $values)
->wait();
}

/**
Expand All @@ -49,7 +51,9 @@ public function create(array $values): array
*/
public function read(Filter $filter = null): array
{
return $this->client->read($this->resolveUri($filter));
return $this->client
->read($this->resolveUri($filter))
->wait();
}

/**
Expand Down
10 changes: 6 additions & 4 deletions src/Jsonbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ public function collection(string $collection): Collection
*/
public function delete(Filter $filter): array
{
return $this->client->delete(
$this->resolveUri($filter)
);
return $this->client
->delete($this->resolveUri($filter))
->wait();
}

/**
Expand All @@ -83,7 +83,9 @@ public function delete(Filter $filter): array
*/
protected function withPath(string $path): UriInterface
{
$uri = $this->uri->getPath() . '/' . \trim($path. '/');
$uri = \trim($this->uri->getPath(), '/')
. '/'
. \trim($path. '/');

return $this->uri->withPath($uri);
}
Expand Down
12 changes: 9 additions & 3 deletions src/Record.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ public function __construct(Client $client, UriInterface $uri)
*/
public function read(): array
{
return $this->client->read($this->uri);
return $this->client
->read($this->uri)
->wait();
}

/**
Expand All @@ -47,7 +49,9 @@ public function read(): array
*/
public function update(array $values): array
{
return $this->client->update($this->uri, $values);
return $this->client
->update($this->uri, $values)
->wait();
}

/**
Expand All @@ -56,6 +60,8 @@ public function update(array $values): array
*/
public function delete(): array
{
return $this->client->delete($this->uri);
return $this->client
->delete($this->uri)
->wait();
}
}
121 changes: 79 additions & 42 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Psr7\Uri;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;

/**
* Class ClientTest
Expand All @@ -18,79 +17,117 @@ class ClientTest extends TestCase
public function testInvalidGuzzleClient(): void
{
$this->expectException(Exception::class);

$guzzle = $this->createMock(ClientInterface::class);

$client = new Client($guzzle);
new Client($guzzle);
}

public function testSend(): void
{
$uri = new Uri('/');
$body = ['foo' => 'bar'];
$data = ['name' => 123];
$method = 'GET';

$response = $this->createMock(ResponseInterface::class);
$response->expects($this->once())
->method('getBody')
->willReturn(\json_encode($data));
$client = new Client(
$this->getGuzzleClient()
);

$guzzle = $this->getGuzzleClient();
$guzzle->expects($this->once())
->method('request')
->with($method, $uri, $this->contains($body))
->willReturn($response);
$request = $client->createRequest('GET', new Uri('/json'));

$client = new Client($guzzle);

$this->assertEquals($data, $client->send($method, $uri, $body));
$this->assertIsArray($client->send($request)->wait());
}

public function testInvalidRequest(): void
{
$message = 'exception message';

$this->expectException(Exception::class);
$this->expectExceptionMessage($message);

$guzzle = $this->getGuzzleClient();
$guzzle->expects($this->once())
->method('request')
->willThrowException(new \Exception($message));
$client = new Client(
$this->getGuzzleClient()
);

$client = new Client($guzzle);
$request = $client->createRequest('GET', new Uri('/status/400'));

$client->send('GET', new Uri('/'));
$client->send($request)->wait();
}

public function testInvalidJson(): void
{
$this->expectException(Exception::class);

$response = $this->createMock(ResponseInterface::class);;
$response->expects($this->once())
->method('getBody')
->willReturn('');
$client = new Client(
$this->getGuzzleClient()
);

$request = $client->createRequest('GET', new Uri('/xml'));

$client->send($request)->wait();
}

public function testCreateRequest(): void
{
$client = new Client(
$this->getGuzzleClient()
);

$uri = new Uri('/foo');
$body = ['name' => 'John'];

$request = $client->createRequest('GET', $uri, $body);

$this->assertEquals('GET', $request->getMethod());
$this->assertEquals($uri, $request->getUri());
$this->assertEquals(\json_encode($body), (string) $request->getBody());
}

public function testCreate(): void
{
$client = new Client(
$this->getGuzzleClient()
);

$body = ['name' => 'John'];

$result = $client->create(new Uri('/post'), $body)->wait();

$this->assertEquals($result['json'] ?? [], $body);
}

public function testRead(): void
{
$client = new Client(
$this->getGuzzleClient()
);

$guzzle = $this->getGuzzleClient();
$guzzle->expects($this->once())
->method('request')
->willReturn($response);
$this->assertIsArray($client->read(new Uri('/get'))->wait());
}

$client = new Client($guzzle);
public function testDelete(): void
{
$client = new Client(
$this->getGuzzleClient()
);

$client->send('GET', new Uri('/'));
$this->assertIsArray($client->delete(new Uri('/delete'))->wait());
}

public function testUpdate(): void
{
$client = new Client(
$this->getGuzzleClient()
);

$body = ['name' => 'John'];

$result = $client->update(new Uri('/put'), $body)->wait();

$this->assertEquals($result['json'] ?? [], $body);
}

/**
* @return ClientInterface|\PHPUnit\Framework\MockObject\MockObject
*/
protected function getGuzzleClient()
{
$guzzle = $this->createMock(ClientInterface::class);
$guzzle->expects($this->once())
->method('getConfig')
->willReturn(true);
$guzzle = new \GuzzleHttp\Client([
'base_uri' => 'https://httpbin.org',
]);

return $guzzle;
}
Expand Down
Loading

0 comments on commit 4db4ba5

Please sign in to comment.