Skip to content

Commit

Permalink
Support modify the context of sub coroutine when using test component…
Browse files Browse the repository at this point in the history
…s. (#5962)
  • Loading branch information
limingxinleo authored Jul 21, 2023
1 parent e660ba6 commit fa9d8c1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,16 +145,18 @@ public function file(string $uri, array $data = [], array $headers = [])
return $this->packer->unpack((string) $response->getBody());
}

public function request(string $method, string $path, array $options = [])
public function request(string $method, string $path, array $options = [], ?callable $callable = null)
{
return wait(function () use ($method, $path, $options) {
return wait(function () use ($method, $path, $options, $callable) {
$callable && $callable();
return $this->execute($this->initRequest($method, $path, $options));
}, $this->waitTimeout);
}

public function sendRequest(ServerRequestInterface $psr7Request): ResponseInterface
public function sendRequest(ServerRequestInterface $psr7Request, ?callable $callable = null): ResponseInterface
{
return wait(function () use ($psr7Request) {
return wait(function () use ($psr7Request, $callable) {
$callable && $callable();
return $this->execute($psr7Request);
}, $this->waitTimeout);
}
Expand Down
15 changes: 15 additions & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Hyperf\Codec\Json;
use Hyperf\Config\Config;
use Hyperf\Context\ApplicationContext;
use Hyperf\Context\Context;
use Hyperf\Contract\ConfigInterface;
use Hyperf\Contract\NormalizerInterface;
use Hyperf\Coroutine\Coroutine;
Expand Down Expand Up @@ -119,6 +120,19 @@ public function testClientGetUri()
$this->assertSame($id, $data['params']['id']);
}

public function testClientCallable()
{
$container = $this->getContainer();

$client = new Client($container);

$id = uniqid();

$res = $client->request('GET', '/context', callable: fn () => Context::set('request_id', $id));

$this->assertSame(['request_id' => $id], Json::decode((string) $res->getBody()));
}

public function getContainer()
{
$container = Mockery::mock(Container::class);
Expand Down Expand Up @@ -175,6 +189,7 @@ public function getContainer()
Router::get('/', [FooController::class, 'index']);
Router::get('/exception', [FooController::class, 'exception']);
Router::get('/id', [FooController::class, 'id']);
Router::get('/context', [FooController::class, 'context']);
Router::addRoute(['GET', 'POST'], '/request', [FooController::class, 'request']);

return $container;
Expand Down
7 changes: 7 additions & 0 deletions tests/Stub/FooController.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ public function id()
return ['code' => 0, 'data' => Coroutine::id()];
}

public function context()
{
return [
'request_id' => Context::getOrSet('request_id', uniqid()),
];
}

public function request()
{
/** @var ServerRequestInterface $request */
Expand Down

0 comments on commit fa9d8c1

Please sign in to comment.