Skip to content

Commit c664d2e

Browse files
authored
Develop (#2)
* Use tap * Laravel 7 & guzzle 7 * Immutable client (#1) * Replace/mutate client * Bug fixes and test * Update changelog
1 parent d4dbe35 commit c664d2e

File tree

4 files changed

+43
-5
lines changed

4 files changed

+43
-5
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip
1616
1717
### Fixed -->
1818

19+
## [1.0.2](https://github.com/jenky/hermes/compare/1.0.2...1.1.0) - 2020-03-17
20+
21+
### Added
22+
- Prepare for Laravel 7 and Guzzle 7
23+
24+
### Changed
25+
- Mutable client by passing `$options` argument
26+
1927
## [1.0.2](https://github.com/jenky/hermes/compare/1.0.1...1.0.2) - 2020-03-13
2028

2129
### Fixed

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
],
2424
"require": {
2525
"php" : "^7.1.3",
26-
"illuminate/container": "^5.7|^6.0",
27-
"illuminate/config": "^5.7|^6.0",
28-
"guzzlehttp/guzzle": "^6.3"
26+
"illuminate/container": "^5.7|^6.0|^7.0",
27+
"illuminate/config": "^5.7|^6.0|^7.0",
28+
"guzzlehttp/guzzle": "^6.3|^7.0"
2929
},
3030
"require-dev": {
3131
"orchestra/testbench": "^3.5|^4.0",

src/GuzzleManager.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,13 @@ public function channel($channel = null, array $options = [])
8080
*/
8181
protected function client($name, array $options = [])
8282
{
83-
return $this->channels[$name] ?? with($this->resolve($name, $options), function ($client) use ($name) {
83+
// Remove the cached channel and create new client
84+
// to mutate the config
85+
if (! empty($options) && ! empty($this->channels[$name])) {
86+
unset($this->channels[$name]);
87+
}
88+
89+
return $this->channels[$name] ?? tap($this->resolve($name, $options), function ($client) use ($name) {
8490
return $this->channels[$name] = $client;
8591
});
8692
}
@@ -106,7 +112,7 @@ protected function configurationFor($name)
106112
*/
107113
protected function resolve($name, array $options = [])
108114
{
109-
$config = array_merge(
115+
$config = array_merge_recursive(
110116
$this->configurationFor($name), $options
111117
);
112118

tests/FeatureTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use GuzzleHttp\HandlerStack;
77
use GuzzleHttp\Middleware;
88
use Illuminate\Support\Facades\Event;
9+
use Illuminate\Support\Str;
910
use Jenky\Hermes\Contracts\Hermes;
1011
use Jenky\Hermes\Contracts\HttpResponseHandler;
1112
use Jenky\Hermes\Events\RequestHandled;
@@ -133,6 +134,29 @@ public function test_driver_not_supported()
133134
guzzle('foo')->get('https://example.com');
134135
}
135136

137+
public function test_mutable_client()
138+
{
139+
$response = $this->httpClient()->get('bearer');
140+
141+
$this->assertTrue($response->isClientError());
142+
$this->assertEquals(401, $response->getStatusCode());
143+
144+
// Mutate the client by creating new client instance
145+
$this->httpClient([
146+
'options' => [
147+
'headers' => [
148+
'Authorization' => 'Bearer '.$token = Str::random(),
149+
],
150+
],
151+
]);
152+
153+
$response = $this->httpClient()->get('bearer');
154+
155+
$this->assertTrue($response->isSuccessful());
156+
$this->assertTrue($response->authenticated);
157+
$this->assertEquals($token, $response->token);
158+
}
159+
136160
public function test_default_channel()
137161
{
138162
guzzle()->setDefaultChannel('jsonplaceholder');

0 commit comments

Comments
 (0)