Skip to content

Commit fa9d8c1

Browse files
authored
Support modify the context of sub coroutine when using test components. (#5962)
1 parent e660ba6 commit fa9d8c1

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

src/Client.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,16 +145,18 @@ public function file(string $uri, array $data = [], array $headers = [])
145145
return $this->packer->unpack((string) $response->getBody());
146146
}
147147

148-
public function request(string $method, string $path, array $options = [])
148+
public function request(string $method, string $path, array $options = [], ?callable $callable = null)
149149
{
150-
return wait(function () use ($method, $path, $options) {
150+
return wait(function () use ($method, $path, $options, $callable) {
151+
$callable && $callable();
151152
return $this->execute($this->initRequest($method, $path, $options));
152153
}, $this->waitTimeout);
153154
}
154155

155-
public function sendRequest(ServerRequestInterface $psr7Request): ResponseInterface
156+
public function sendRequest(ServerRequestInterface $psr7Request, ?callable $callable = null): ResponseInterface
156157
{
157-
return wait(function () use ($psr7Request) {
158+
return wait(function () use ($psr7Request, $callable) {
159+
$callable && $callable();
158160
return $this->execute($psr7Request);
159161
}, $this->waitTimeout);
160162
}

tests/ClientTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Hyperf\Codec\Json;
1515
use Hyperf\Config\Config;
1616
use Hyperf\Context\ApplicationContext;
17+
use Hyperf\Context\Context;
1718
use Hyperf\Contract\ConfigInterface;
1819
use Hyperf\Contract\NormalizerInterface;
1920
use Hyperf\Coroutine\Coroutine;
@@ -119,6 +120,19 @@ public function testClientGetUri()
119120
$this->assertSame($id, $data['params']['id']);
120121
}
121122

123+
public function testClientCallable()
124+
{
125+
$container = $this->getContainer();
126+
127+
$client = new Client($container);
128+
129+
$id = uniqid();
130+
131+
$res = $client->request('GET', '/context', callable: fn () => Context::set('request_id', $id));
132+
133+
$this->assertSame(['request_id' => $id], Json::decode((string) $res->getBody()));
134+
}
135+
122136
public function getContainer()
123137
{
124138
$container = Mockery::mock(Container::class);
@@ -175,6 +189,7 @@ public function getContainer()
175189
Router::get('/', [FooController::class, 'index']);
176190
Router::get('/exception', [FooController::class, 'exception']);
177191
Router::get('/id', [FooController::class, 'id']);
192+
Router::get('/context', [FooController::class, 'context']);
178193
Router::addRoute(['GET', 'POST'], '/request', [FooController::class, 'request']);
179194

180195
return $container;

tests/Stub/FooController.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ public function id()
3333
return ['code' => 0, 'data' => Coroutine::id()];
3434
}
3535

36+
public function context()
37+
{
38+
return [
39+
'request_id' => Context::getOrSet('request_id', uniqid()),
40+
];
41+
}
42+
3643
public function request()
3744
{
3845
/** @var ServerRequestInterface $request */

0 commit comments

Comments
 (0)