Support modify the context of sub coroutine when using test components. (#5962)

This commit is contained in:
李铭昕 2023-07-21 11:15:25 +08:00 committed by GitHub
parent 0cb5adacfb
commit db7dfe5b04
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 4 deletions

View File

@ -13,6 +13,7 @@
- [#5951](https://github.com/hyperf/hyperf/pull/5951) Added `SameSite` support to session cookies.
- [#5955](https://github.com/hyperf/hyperf/pull/5955) Support `access_key` and `access_secret` for nacos service governance.
- [#5957](https://github.com/hyperf/hyperf/pull/5957) Added `Hyperf\Codec\Packer\IgbinarySerializerPacker`.
- [#5962](https://github.com/hyperf/hyperf/pull/5962) Support modify the context of sub coroutine when using test components.
# v3.0.29 - 2023-07-14

View File

@ -145,16 +145,18 @@ class Client extends Server
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);
}

View File

@ -14,6 +14,7 @@ namespace HyperfTest\Testing;
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;
@ -119,6 +120,19 @@ class ClientTest extends TestCase
$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);
@ -175,6 +189,7 @@ class ClientTest extends TestCase
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;

View File

@ -33,6 +33,13 @@ class FooController
return ['code' => 0, 'data' => Coroutine::id()];
}
public function context()
{
return [
'request_id' => Context::getOrSet('request_id', uniqid()),
];
}
public function request()
{
/** @var ServerRequestInterface $request */