Skip to content

Commit 96af5a9

Browse files
committed
fix unit test
1 parent d53e0fa commit 96af5a9

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

src/Libraries/Client.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,14 @@ class Client extends BaseClient
6969
*/
7070
public function __construct(array $config = [])
7171
{
72+
parent::__construct($config);
7273
$context = app(Context::class);
7374
$metadata = $context->get(Metadata::class);
74-
foreach ((array) $metadata as $key => $value) {
75-
$this->requestHeaders[$key] = $value;
75+
if (!is_null($metadata)) {
76+
foreach ((array) $metadata as $key => $value) {
77+
$this->requestHeaders[$key] = $value;
78+
}
7679
}
77-
parent::__construct($config);
7880
}
7981

8082
/**

tests/Libraries/ClientTest.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,15 @@ public function createApplication()
2222

2323
public function testCallY(): void
2424
{
25-
25+
$mock = new MockHandler([
26+
new Response(200, ['Content-Type' => 'application/json'], json_encode(['status' => 'ok', 'message' => 'well done'])),
27+
]);
28+
$handlerStack = new HandlerStack($mock);
2629
$request = new Request(
2730
'GET',
2831
'https://dummyjson.com/test',
2932
);
30-
$client = new Client();
33+
$client = new Client(['handler' => $handlerStack]);
3134
$response = $client->call($request);
3235
$contents = $response->getBody()->getContents();
3336
$contents_arr = json_decode($contents, true, 512);
@@ -36,11 +39,15 @@ public function testCallY(): void
3639

3740
public function testCallX(): void
3841
{
42+
$mock = new MockHandler([
43+
new Response(200, ['Content-Type' => 'application/json'], json_encode(['status' => 'ok', 'message' => 'well done'])),
44+
]);
45+
$handlerStack = new HandlerStack($mock);
3946
$request = new Request(
4047
'GET',
4148
'https://dummyjson.com/test',
4249
);
43-
$client = new Client();
50+
$client = new Client(['handler' => $handlerStack]);
4451
$response = $client->call($request);
4552
$contents = $response->getBody()->getContents();
4653
$contents_arr = json_decode($contents, true, 512);
@@ -75,11 +82,15 @@ public function testCallZ(): void
7582

7683
public function testCallEksternal(): void
7784
{
85+
$mock = new MockHandler([
86+
new Response(200, ['Content-Type' => 'application/json'], json_encode(['status' => 'ok', 'message' => 'well done'])),
87+
]);
88+
$handlerStack = new HandlerStack($mock);
7889
$request = new Request(
7990
'GET',
8091
'https://dummyjson.com/test',
8192
);
82-
$client = new Client();
93+
$client = new Client(['handler' => $handlerStack]);
8394
$response = $client
8495
->call($request);
8596
$contents = $response->getBody()->getContents();
@@ -114,6 +125,10 @@ public function testCallMultipartSuccess(): void
114125

115126
public function testCallMultipartSuccess2(): void
116127
{
128+
$mock = new MockHandler([
129+
new Response(200, ['Content-Type' => 'application/json'], json_encode(['id' => '101', 'status' => 'OK', 'message' => 'well done'])),
130+
]);
131+
$handlerStack = new HandlerStack($mock);
117132
$f = fopen('public/docs/hello.txt', 'w');
118133
fwrite($f, 'hello world');
119134
fclose($f);
@@ -128,7 +143,7 @@ public function testCallMultipartSuccess2(): void
128143
]
129144
])
130145
);
131-
$client = new Client();
146+
$client = new Client(['handler' => $handlerStack]);
132147
$resp = $client
133148
->call($request);
134149
$r = json_decode($resp->getBody()->getContents());

0 commit comments

Comments
 (0)