Merge branch 'master' into 3.0-merge

This commit is contained in:
李铭昕 2022-01-08 17:53:48 +08:00
commit c1cbd3e54c
10 changed files with 175 additions and 38 deletions

View File

@ -1,4 +1,6 @@
# v2.2.22 - TBD
# v2.2.23 - TBD
# v2.2.22 - 2022-01-04
## Fixed
@ -13,6 +15,7 @@
## Optimized
- [#4406](https://github.com/hyperf/hyperf/pull/4406) Adapt swoole 5.0 by removing swoole classes with `PSR-0`.
- [#4429](https://github.com/hyperf/hyperf/pull/4429) Added type hint for `Debug::getRefCount()` which only support `object`.
# v2.2.21 - 2021-12-20

View File

@ -1,5 +1,22 @@
# 版本更新记录
# v2.2.22 - 2022-01-04
## Fixed
- [#4399](https://github.com/hyperf/hyperf/pull/4399) 修复使用 `RedisCluster` 时,无法使用 `scan` 方法的问题。
## Added
- [#4409](https://github.com/hyperf/hyperf/pull/4409) 为 `session` 增加数据库支持。
- [#4411](https://github.com/hyperf/hyperf/pull/4411) 为 `tracer` 组件,新增 `Hyperf\Tracer\Aspect\DbAspect`,用于记录 `hyperf/db` 组件产生的 `SQL` 日志。
- [#4420](https://github.com/hyperf/hyperf/pull/4420) 为 `Hyperf\Amqp\IO\SwooleIO` 增加 `SSL` 支持。
## Optimized
- [#4406](https://github.com/hyperf/hyperf/pull/4406) 删除 `Swoole PSR-0` 风格代码,更加友好的支持 `Swoole 5.0` 版本。
- [#4429](https://github.com/hyperf/hyperf/pull/4429) 为 `Debug::getRefCount()` 方法增加类型检测,只能用于输出对象的 `RefCount`
# v2.2.21 - 2021-12-20
## 修复

View File

@ -100,6 +100,23 @@ $result = $client->json('/user/0',[
]);
```
### 使用 Cookies
```php
<?php
use Hyperf\Testing\Client;
use Hyperf\Utils\Codec\Json;
$client = make(Client::class);
$response = $client->sendRequest($client->initRequest('POST', '/request')->withCookieParams([
'X-CODE' => $id = uniqid(),
]));
$data = Json::decode((string) $response->getBody());
```
## 示例
让我们写个小 DEMO 来测试一下。

View File

@ -1,5 +1,22 @@
# 版本更新記錄
# v2.2.22 - 2022-01-04
## Fixed
- [#4399](https://github.com/hyperf/hyperf/pull/4399) 修復使用 `RedisCluster` 時,無法使用 `scan` 方法的問題。
## Added
- [#4409](https://github.com/hyperf/hyperf/pull/4409) 為 `session` 增加數據庫支持。
- [#4411](https://github.com/hyperf/hyperf/pull/4411) 為 `tracer` 組件,新增 `Hyperf\Tracer\Aspect\DbAspect`,用於記錄 `hyperf/db` 組件產生的 `SQL` 日誌。
- [#4420](https://github.com/hyperf/hyperf/pull/4420) 為 `Hyperf\Amqp\IO\SwooleIO` 增加 `SSL` 支持。
## Optimized
- [#4406](https://github.com/hyperf/hyperf/pull/4406) 刪除 `Swoole PSR-0` 風格代碼,更加友好的支持 `Swoole 5.0` 版本。
- [#4429](https://github.com/hyperf/hyperf/pull/4429) 為 `Debug::getRefCount()` 方法增加類型檢測,只能用於輸出對象的 `RefCount`
# v2.2.21 - 2021-12-20
## 修復

View File

@ -1,5 +1,22 @@
# 版本更新記錄
# v2.2.22 - 2022-01-04
## Fixed
- [#4399](https://github.com/hyperf/hyperf/pull/4399) 修復使用 `RedisCluster` 時,無法使用 `scan` 方法的問題。
## Added
- [#4409](https://github.com/hyperf/hyperf/pull/4409) 為 `session` 增加資料庫支援。
- [#4411](https://github.com/hyperf/hyperf/pull/4411) 為 `tracer` 元件,新增 `Hyperf\Tracer\Aspect\DbAspect`,用於記錄 `hyperf/db` 元件產生的 `SQL` 日誌。
- [#4420](https://github.com/hyperf/hyperf/pull/4420) 為 `Hyperf\Amqp\IO\SwooleIO` 增加 `SSL` 支援。
## Optimized
- [#4406](https://github.com/hyperf/hyperf/pull/4406) 刪除 `Swoole PSR-0` 風格程式碼,更加友好的支援 `Swoole 5.0` 版本。
- [#4429](https://github.com/hyperf/hyperf/pull/4429) 為 `Debug::getRefCount()` 方法增加型別檢測,只能用於輸出物件的 `RefCount`
# v2.2.21 - 2021-12-20
## 修復

View File

@ -134,7 +134,7 @@ class Client extends Server
public function request(string $method, string $path, array $options = [])
{
return wait(function () use ($method, $path, $options) {
return $this->execute($this->init($method, $path, $options));
return $this->execute($this->initRequest($method, $path, $options));
}, $this->waitTimeout);
}
@ -145,6 +145,50 @@ class Client extends Server
}, $this->waitTimeout);
}
public function initRequest(string $method, string $path, array $options = []): ServerRequestInterface
{
$query = $options['query'] ?? [];
$params = $options['form_params'] ?? [];
$json = $options['json'] ?? [];
$headers = $options['headers'] ?? [];
$multipart = $options['multipart'] ?? [];
$parsePath = parse_url($path);
$path = $parsePath['path'];
$uriPathQuery = $parsePath['query'] ?? [];
if (! empty($uriPathQuery)) {
parse_str($uriPathQuery, $pathQuery);
$query = array_merge($pathQuery, $query);
}
$data = $params;
// Initialize PSR-7 Request and Response objects.
$uri = (new Uri($this->baseUri . ltrim($path, '/')))->withQuery(http_build_query($query));
$content = http_build_query($params);
if ($method == 'POST' && data_get($headers, 'Content-Type') == 'application/json') {
$content = json_encode($json, JSON_UNESCAPED_UNICODE);
$data = $json;
}
$body = new SwooleStream($content);
$request = new Psr7Request($method, $uri, $headers, $body);
return $request->withQueryParams($query)
->withParsedBody($data)
->withUploadedFiles($this->normalizeFiles($multipart));
}
/**
* @deprecated It will be removed in v3.0
*/
protected function init(string $method, string $path, array $options = []): ServerRequestInterface
{
return $this->initRequest($method, $path, $options);
}
protected function execute(ServerRequestInterface $psr7Request): ResponseInterface
{
$this->persistToContext($psr7Request, new Psr7Response());
@ -188,41 +232,6 @@ class Client extends Server
}
}
protected function init(string $method, string $path, array $options = []): ServerRequestInterface
{
$query = $options['query'] ?? [];
$params = $options['form_params'] ?? [];
$json = $options['json'] ?? [];
$headers = $options['headers'] ?? [];
$multipart = $options['multipart'] ?? [];
$parsePath = parse_url($path);
$path = $parsePath['path'];
$uriPathQuery = $parsePath['query'] ?? [];
if (! empty($uriPathQuery)) {
parse_str($uriPathQuery, $pathQuery);
$query = array_merge($pathQuery, $query);
}
$data = $params;
// Initialize PSR-7 Request and Response objects.
$uri = (new Uri($this->baseUri . ltrim($path, '/')))->withQuery(http_build_query($query));
$content = http_build_query($params);
if ($method == 'POST' && data_get($headers, 'Content-Type') == 'application/json') {
$content = json_encode($json, JSON_UNESCAPED_UNICODE);
$data = $json;
}
$body = new SwooleStream($content);
$request = new Psr7Request($method, $uri, $headers, $body);
return $request->withQueryParams($query)
->withParsedBody($data)
->withUploadedFiles($this->normalizeFiles($multipart));
}
protected function normalizeFiles(array $multipart): array
{
$files = [];

View File

@ -13,7 +13,10 @@ namespace Hyperf\Testing;
class Debug
{
public static function getRefCount($object): string
/**
* Get object's ref count.
*/
public static function getRefCount(object $object): string
{
ob_start();
debug_zval_dump($object);

View File

@ -28,6 +28,7 @@ use Hyperf\Server\Event;
use Hyperf\Server\Server;
use Hyperf\Testing\Client;
use Hyperf\Utils\ApplicationContext;
use Hyperf\Utils\Codec\Json;
use Hyperf\Utils\Coroutine;
use Hyperf\Utils\Filesystem\Filesystem;
use Hyperf\Utils\Serializer\SimpleNormalizer;
@ -55,6 +56,21 @@ class ClientTest extends TestCase
$this->assertSame('Hello Hyperf!', $data['data']);
}
public function testSendCookies()
{
$container = $this->getContainer();
$client = new Client($container);
$response = $client->sendRequest($client->initRequest('POST', '/request')->withCookieParams([
'X-CODE' => $id = uniqid(),
]));
$data = Json::decode((string) $response->getBody());
$this->assertSame($id, $data['cookies']['X-CODE']);
}
public function testClientReturnCoroutineId()
{
$container = $this->getContainer();

View File

@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace HyperfTest\Testing;
use Hyperf\Testing\Debug;
use PHPUnit\Framework\TestCase;
/**
* @internal
* @coversNothing
*/
class DebugTest extends TestCase
{
public function testGetRefCount()
{
$this->assertSame('1', Debug::getRefCount(new \stdClass()));
$obj = new \stdClass();
$this->assertSame('2', Debug::getRefCount($obj));
$obj2 = new \stdClass();
$obj2->obj = $obj;
$this->assertSame('2', Debug::getRefCount($obj2));
$this->assertSame('3', Debug::getRefCount($obj));
$fun = static function () {};
$this->assertSame('2', Debug::getRefCount($fun));
$this->assertSame('1', Debug::getRefCount(function () {}));
}
}

View File

@ -34,6 +34,7 @@ class FooController
public function request()
{
/** @var ServerRequestInterface $request */
$request = Context::get(ServerRequestInterface::class);
$uri = $request->getUri();
return [
@ -45,6 +46,7 @@ class FooController
'query' => $uri->getQuery(),
],
'params' => $request->getQueryParams(),
'cookies' => $request->getCookieParams(),
];
}
}