mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-12-01 19:27:39 +08:00
Support modify the context of sub coroutine when using test components. (#5962)
This commit is contained in:
parent
0cb5adacfb
commit
db7dfe5b04
@ -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
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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 */
|
||||
|
Loading…
Reference in New Issue
Block a user