Skip to content

Commit 2c816b4

Browse files
committed
Merge branch 'master' into 3.0-merge
2 parents 5114a36 + 6337737 commit 2c816b4

File tree

5 files changed

+103
-37
lines changed

5 files changed

+103
-37
lines changed

src/Client.php

Lines changed: 45 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public function file(string $uri, array $data = [], array $headers = [])
134134
public function request(string $method, string $path, array $options = [])
135135
{
136136
return wait(function () use ($method, $path, $options) {
137-
return $this->execute($this->init($method, $path, $options));
137+
return $this->execute($this->initRequest($method, $path, $options));
138138
}, $this->waitTimeout);
139139
}
140140

@@ -145,6 +145,50 @@ public function sendRequest(ServerRequestInterface $psr7Request): ResponseInterf
145145
}, $this->waitTimeout);
146146
}
147147

148+
public function initRequest(string $method, string $path, array $options = []): ServerRequestInterface
149+
{
150+
$query = $options['query'] ?? [];
151+
$params = $options['form_params'] ?? [];
152+
$json = $options['json'] ?? [];
153+
$headers = $options['headers'] ?? [];
154+
$multipart = $options['multipart'] ?? [];
155+
156+
$parsePath = parse_url($path);
157+
$path = $parsePath['path'];
158+
$uriPathQuery = $parsePath['query'] ?? [];
159+
if (! empty($uriPathQuery)) {
160+
parse_str($uriPathQuery, $pathQuery);
161+
$query = array_merge($pathQuery, $query);
162+
}
163+
164+
$data = $params;
165+
166+
// Initialize PSR-7 Request and Response objects.
167+
$uri = (new Uri($this->baseUri . ltrim($path, '/')))->withQuery(http_build_query($query));
168+
169+
$content = http_build_query($params);
170+
if ($method == 'POST' && data_get($headers, 'Content-Type') == 'application/json') {
171+
$content = json_encode($json, JSON_UNESCAPED_UNICODE);
172+
$data = $json;
173+
}
174+
175+
$body = new SwooleStream($content);
176+
177+
$request = new Psr7Request($method, $uri, $headers, $body);
178+
179+
return $request->withQueryParams($query)
180+
->withParsedBody($data)
181+
->withUploadedFiles($this->normalizeFiles($multipart));
182+
}
183+
184+
/**
185+
* @deprecated It will be removed in v3.0
186+
*/
187+
protected function init(string $method, string $path, array $options = []): ServerRequestInterface
188+
{
189+
return $this->initRequest($method, $path, $options);
190+
}
191+
148192
protected function execute(ServerRequestInterface $psr7Request): ResponseInterface
149193
{
150194
$this->persistToContext($psr7Request, new Psr7Response());
@@ -188,41 +232,6 @@ protected function initBaseUri(string $server): void
188232
}
189233
}
190234

191-
protected function init(string $method, string $path, array $options = []): ServerRequestInterface
192-
{
193-
$query = $options['query'] ?? [];
194-
$params = $options['form_params'] ?? [];
195-
$json = $options['json'] ?? [];
196-
$headers = $options['headers'] ?? [];
197-
$multipart = $options['multipart'] ?? [];
198-
199-
$parsePath = parse_url($path);
200-
$path = $parsePath['path'];
201-
$uriPathQuery = $parsePath['query'] ?? [];
202-
if (! empty($uriPathQuery)) {
203-
parse_str($uriPathQuery, $pathQuery);
204-
$query = array_merge($pathQuery, $query);
205-
}
206-
207-
$data = $params;
208-
209-
// Initialize PSR-7 Request and Response objects.
210-
$uri = (new Uri($this->baseUri . ltrim($path, '/')))->withQuery(http_build_query($query));
211-
212-
$content = http_build_query($params);
213-
if ($method == 'POST' && data_get($headers, 'Content-Type') == 'application/json') {
214-
$content = json_encode($json, JSON_UNESCAPED_UNICODE);
215-
$data = $json;
216-
}
217-
218-
$body = new SwooleStream($content);
219-
220-
$request = new Psr7Request($method, $uri, $headers, $body);
221-
return $request->withQueryParams($query)
222-
->withParsedBody($data)
223-
->withUploadedFiles($this->normalizeFiles($multipart));
224-
}
225-
226235
protected function normalizeFiles(array $multipart): array
227236
{
228237
$files = [];

src/Debug.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313

1414
class Debug
1515
{
16-
public static function getRefCount($object): string
16+
/**
17+
* Get object's ref count.
18+
*/
19+
public static function getRefCount(object $object): string
1720
{
1821
ob_start();
1922
debug_zval_dump($object);

tests/ClientTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use Hyperf\Server\Server;
2929
use Hyperf\Testing\Client;
3030
use Hyperf\Utils\ApplicationContext;
31+
use Hyperf\Utils\Codec\Json;
3132
use Hyperf\Utils\Coroutine;
3233
use Hyperf\Utils\Filesystem\Filesystem;
3334
use Hyperf\Utils\Serializer\SimpleNormalizer;
@@ -55,6 +56,21 @@ public function testClientRequest()
5556
$this->assertSame('Hello Hyperf!', $data['data']);
5657
}
5758

59+
public function testSendCookies()
60+
{
61+
$container = $this->getContainer();
62+
63+
$client = new Client($container);
64+
65+
$response = $client->sendRequest($client->initRequest('POST', '/request')->withCookieParams([
66+
'X-CODE' => $id = uniqid(),
67+
]));
68+
69+
$data = Json::decode((string) $response->getBody());
70+
71+
$this->assertSame($id, $data['cookies']['X-CODE']);
72+
}
73+
5874
public function testClientReturnCoroutineId()
5975
{
6076
$container = $this->getContainer();

tests/DebugTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of Hyperf.
6+
*
7+
* @link https://www.hyperf.io
8+
* @document https://hyperf.wiki
9+
* @contact [email protected]
10+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+
*/
12+
namespace HyperfTest\Testing;
13+
14+
use Hyperf\Testing\Debug;
15+
use PHPUnit\Framework\TestCase;
16+
17+
/**
18+
* @internal
19+
* @coversNothing
20+
*/
21+
class DebugTest extends TestCase
22+
{
23+
public function testGetRefCount()
24+
{
25+
$this->assertSame('1', Debug::getRefCount(new \stdClass()));
26+
$obj = new \stdClass();
27+
$this->assertSame('2', Debug::getRefCount($obj));
28+
$obj2 = new \stdClass();
29+
$obj2->obj = $obj;
30+
$this->assertSame('2', Debug::getRefCount($obj2));
31+
$this->assertSame('3', Debug::getRefCount($obj));
32+
$fun = static function () {};
33+
$this->assertSame('2', Debug::getRefCount($fun));
34+
$this->assertSame('1', Debug::getRefCount(function () {}));
35+
}
36+
}

tests/Stub/FooController.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public function id()
3434

3535
public function request()
3636
{
37+
/** @var ServerRequestInterface $request */
3738
$request = Context::get(ServerRequestInterface::class);
3839
$uri = $request->getUri();
3940
return [
@@ -45,6 +46,7 @@ public function request()
4546
'query' => $uri->getQuery(),
4647
],
4748
'params' => $request->getQueryParams(),
49+
'cookies' => $request->getCookieParams(),
4850
];
4951
}
5052
}

0 commit comments

Comments
 (0)