Skip to content

Commit dadffe6

Browse files
authored
Optimized code for testing which mock request in an alone coroutine. (#3338)
1 parent 1eb8d08 commit dadffe6

File tree

3 files changed

+50
-43
lines changed

3 files changed

+50
-43
lines changed

src/Client.php

Lines changed: 26 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
use Hyperf\Contract\PackerInterface;
1515
use Hyperf\Dispatcher\HttpDispatcher;
16-
use Hyperf\Engine\Coroutine;
1716
use Hyperf\ExceptionHandler\ExceptionHandlerDispatcher;
1817
use Hyperf\HttpMessage\Server\Request as Psr7Request;
1918
use Hyperf\HttpMessage\Server\Response as Psr7Response;
@@ -28,25 +27,21 @@
2827
use Hyperf\Utils\Context;
2928
use Hyperf\Utils\Filesystem\Filesystem;
3029
use Hyperf\Utils\Packer\JsonPacker;
31-
use Hyperf\Utils\Str;
3230
use Psr\Container\ContainerInterface;
3331
use Psr\Http\Message\ResponseInterface;
3432
use Psr\Http\Message\ServerRequestInterface;
3533

3634
class Client extends Server
3735
{
3836
/**
39-
* @var array
37+
* @var PackerInterface
4038
*/
41-
public $ignoreContextPrefix = [
42-
'database.connection',
43-
'redis.connection',
44-
];
39+
protected $packer;
4540

4641
/**
47-
* @var PackerInterface
42+
* @var float
4843
*/
49-
protected $packer;
44+
protected $waitTimeout = 10.0;
5045

5146
public function __construct(ContainerInterface $container, PackerInterface $packer = null, $server = 'http')
5247
{
@@ -134,34 +129,34 @@ public function file($uri, $data = [], $headers = [])
134129

135130
public function request(string $method, string $path, array $options = [])
136131
{
137-
/*
138-
* @var Psr7Request
139-
*/
140-
[$psr7Request, $psr7Response] = $this->init($method, $path, $options);
141-
142-
$psr7Request = $this->coreMiddleware->dispatch($psr7Request);
143-
/** @var Dispatched $dispatched */
144-
$dispatched = $psr7Request->getAttribute(Dispatched::class);
145-
$middlewares = $this->middlewares;
146-
if ($dispatched->isFound()) {
147-
$registeredMiddlewares = MiddlewareManager::get($this->serverName, $dispatched->handler->route, $psr7Request->getMethod());
148-
$middlewares = array_merge($middlewares, $registeredMiddlewares);
149-
}
132+
return wait(function () use ($method, $path, $options) {
133+
/*
134+
* @var Psr7Request
135+
*/
136+
[$psr7Request, $psr7Response] = $this->init($method, $path, $options);
137+
138+
$psr7Request = $this->coreMiddleware->dispatch($psr7Request);
139+
/** @var Dispatched $dispatched */
140+
$dispatched = $psr7Request->getAttribute(Dispatched::class);
141+
$middlewares = $this->middlewares;
142+
if ($dispatched->isFound()) {
143+
$registeredMiddlewares = MiddlewareManager::get($this->serverName, $dispatched->handler->route, $psr7Request->getMethod());
144+
$middlewares = array_merge($middlewares, $registeredMiddlewares);
145+
}
150146

151-
try {
152-
$psr7Response = $this->dispatcher->dispatch($psr7Request, $middlewares, $this->coreMiddleware);
153-
} catch (\Throwable $throwable) {
154-
// Delegate the exception to exception handler.
155-
$psr7Response = $this->exceptionHandlerDispatcher->dispatch($throwable, $this->exceptionHandlers);
156-
}
147+
try {
148+
$psr7Response = $this->dispatcher->dispatch($psr7Request, $middlewares, $this->coreMiddleware);
149+
} catch (\Throwable $throwable) {
150+
// Delegate the exception to exception handler.
151+
$psr7Response = $this->exceptionHandlerDispatcher->dispatch($throwable, $this->exceptionHandlers);
152+
}
157153

158-
return $psr7Response;
154+
return $psr7Response;
155+
}, $this->waitTimeout);
159156
}
160157

161158
protected function init(string $method, string $path, array $options = []): array
162159
{
163-
$this->flushContext();
164-
165160
$query = $options['query'] ?? [];
166161
$params = $options['form_params'] ?? [];
167162
$json = $options['json'] ?? [];
@@ -192,18 +187,6 @@ protected function init(string $method, string $path, array $options = []): arra
192187
return [$psr7Request, $psr7Response];
193188
}
194189

195-
protected function flushContext()
196-
{
197-
$context = Coroutine::getContextFor() ?? [];
198-
199-
foreach ($context as $key => $value) {
200-
if (Str::startsWith($key, $this->ignoreContextPrefix)) {
201-
continue;
202-
}
203-
$context[$key] = null;
204-
}
205-
}
206-
207190
protected function normalizeFiles(array $multipart): array
208191
{
209192
$files = [];

tests/ClientTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@
2626
use Hyperf\HttpServer\Router\Router;
2727
use Hyperf\Testing\Client;
2828
use Hyperf\Utils\ApplicationContext;
29+
use Hyperf\Utils\Coroutine;
2930
use Hyperf\Utils\Filesystem\Filesystem;
3031
use Hyperf\Utils\Serializer\SimpleNormalizer;
32+
use Hyperf\Utils\Waiter;
3133
use HyperfTest\Testing\Stub\Exception\Handler\FooExceptionHandler;
3234
use HyperfTest\Testing\Stub\FooController;
3335
use Mockery;
@@ -51,6 +53,19 @@ public function testClientRequest()
5153
$this->assertSame('Hello Hyperf!', $data['data']);
5254
}
5355

56+
public function testClientReturnCoroutineId()
57+
{
58+
$container = $this->getContainer();
59+
60+
$client = new Client($container);
61+
62+
$id = Coroutine::id();
63+
$data = $client->get('/id');
64+
65+
$this->assertSame(0, $data['code']);
66+
$this->assertNotEquals($id, $data['data']);
67+
}
68+
5469
public function testClientException()
5570
{
5671
$container = $this->getContainer();
@@ -90,11 +105,13 @@ public function getContainer()
90105
$container->shouldReceive('make')->with(CoreMiddleware::class, Mockery::any())->andReturnUsing(function ($class, $args) {
91106
return new CoreMiddleware(...array_values($args));
92107
});
108+
$container->shouldReceive('get')->with(Waiter::class)->andReturn(new Waiter());
93109
ApplicationContext::setContainer($container);
94110

95111
Router::init($factory);
96112
Router::get('/', [FooController::class, 'index']);
97113
Router::get('/exception', [FooController::class, 'exception']);
114+
Router::get('/id', [FooController::class, 'id']);
98115

99116
return $container;
100117
}

tests/Stub/FooController.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
*/
1212
namespace HyperfTest\Testing\Stub;
1313

14+
use Hyperf\Utils\Coroutine;
15+
1416
class FooController
1517
{
1618
public function index()
@@ -22,4 +24,9 @@ public function exception()
2224
{
2325
throw new \RuntimeException('Server Error', 500);
2426
}
27+
28+
public function id()
29+
{
30+
return ['code' => 0, 'data' => Coroutine::id()];
31+
}
2532
}

0 commit comments

Comments
 (0)