Skip to content

Commit 5d65382

Browse files
committed
Fix http test
1 parent 46be480 commit 5d65382

File tree

5 files changed

+39
-12
lines changed

5 files changed

+39
-12
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ composer.lock
55
.php_cs.cache
66
/demo.php
77
.phpunit.result.cache
8+
.php-cs-fixer.cache
89
.php-cs-fixer.php

.php-cs-fixer.cache

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/Client/Client.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@ final class Client implements ClientInterface
2727
* @param HttpClientInterface $httpClient
2828
*/
2929
public function __construct(
30-
HttpClientInterface $httpClient,
3130
string $chatworkToken,
32-
string $apiVersion
31+
string $apiVersion,
32+
?HttpClientInterface $httpClient = null
3333
) {
34-
$httpClient->getConfig('handler')->push(Middleware::mapRequest(fn (RequestInterface $request) => $request->withHeader('X-ChatWorkToken', $chatworkToken)));
34+
if ($httpClient === null) {
35+
$httpClient = ClientFactory::createHttpClient($chatworkToken);
36+
}
3537
$this->apiVersion = $apiVersion;
3638
$this->httpClient = $httpClient;
3739
}
@@ -80,9 +82,11 @@ private function request(string $method, string $path, array $options = []): arr
8082
{
8183
$path = sprintf('/%s/%s', $this->apiVersion, $path);
8284
try {
83-
return json_decode($this->httpClient->request($method, $path, $options)->getBody()->getContents(), true);
85+
return json_decode($this->httpClient->request($method, $path, $options)->getBody()->getContents(), true, 512, JSON_THROW_ON_ERROR);
8486
} catch (GuzzleException $e) {
8587
throw new ClientException(sprintf('request error. method = %s, path = %s', $method, $path), $e->getCode(), $e);
88+
} catch (\JsonException $e) {
89+
throw new ClientException(sprintf('json parse error. method = %s, path = %s', $method, $path), $e->getCode(), $e);
8690
}
8791
}
8892
}

src/Client/ClientFactory.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
namespace Polidog\Chatwork\Client;
66

7+
use GuzzleHttp\HandlerStack;
8+
use GuzzleHttp\Middleware;
9+
use Psr\Http\Message\RequestInterface;
10+
711
final class ClientFactory
812
{
913
/**
@@ -23,10 +27,32 @@ final class ClientFactory
2327
],
2428
];
2529

30+
/**
31+
* @deprecated
32+
*
33+
* @param string $token
34+
* @param string $version
35+
* @param array $httpOptions
36+
* @return ClientInterface
37+
*/
2638
public static function create(string $token, string $version, array $httpOptions = []): ClientInterface
2739
{
2840
$httpOptions = array_merge(self::$httpOptions, $httpOptions);
2941

30-
return new Client(new \GuzzleHttp\Client($httpOptions), $token, $version);
42+
return new Client($token, $version, new \GuzzleHttp\Client($httpOptions));
43+
}
44+
45+
public static function createHttpClient(string $chatworkToken, array $middlewares = []): \GuzzleHttp\Client
46+
{
47+
$stack = new HandlerStack();
48+
$stack->push(Middleware::mapRequest(static fn (RequestInterface $request) => $request->withHeader('X-ChatWorkToken', $chatworkToken)));
49+
foreach ($middlewares as $middleware) {
50+
$stack->push($middleware);
51+
}
52+
53+
$options = array_merge(self::$httpOptions, [
54+
'handler' => $stack,
55+
]);
56+
return new \GuzzleHttp\Client($options);
3157
}
3258
}

tests/Client/ClientTest.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44

55
namespace Polidog\Chatwork\Client;
66

7-
use GuzzleHttp\HandlerStack;
87
use PHPUnit\Framework\TestCase;
98
use Prophecy\PhpUnit\ProphecyTrait;
10-
use Psr\Http\Message\RequestInterface;
9+
use Psr\Http\Message\ResponseInterface;
1110
use Psr\Http\Message\StreamInterface;
1211

1312
class ClientTest extends TestCase
@@ -20,7 +19,7 @@ public function testGet(): void
2019
$stream->getContents()
2120
->willReturn('[]');
2221

23-
$response = $this->prophesize(RequestInterface::class);
22+
$response = $this->prophesize(ResponseInterface::class);
2423
$response->getBody()
2524
->willReturn($stream);
2625

@@ -31,9 +30,7 @@ public function testGet(): void
3130
],
3231
])->willReturn($response);
3332

34-
$httpClient->getConfig('handler')->willReturn(HandlerStack::create());
35-
36-
$client = new Client($httpClient->reveal(), 'test token', 'v2');
33+
$client = new Client('test token', 'v2', $httpClient->reveal());
3734
$client->get('a/b', ['s' => 'test']);
3835

3936
$httpClient->request('get', '/v2/a/b', [

0 commit comments

Comments
 (0)