Skip to content

Commit

Permalink
Fixed Request::inputs default value does not works. (#835)
Browse files Browse the repository at this point in the history
  • Loading branch information
dayday authored and limingxinleo committed Nov 1, 2019
1 parent afcb50a commit 809a418
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,10 @@
- [#832](https://github.com/hyperf/hyperf/pull/832) Added `Hyperf\Utils\Codec\Json`.
- [#833](https://github.com/hyperf/hyperf/pull/833) Added `Hyperf\Utils\Backoff`.

## Fixed

- [#835](https://github.com/hyperf/hyperf/pull/835) Fixed `Request::inputs` default value does not works.

## Optimized

- [#832](https://github.com/hyperf/hyperf/pull/832) Optimized that response will throw a exception when json format failed.
Expand Down
3 changes: 1 addition & 2 deletions src/http-server/src/Request.php
Expand Up @@ -93,10 +93,9 @@ public function input(string $key, $default = null)
public function inputs(array $keys, $default = null): array
{
$data = $this->getInputData();
$result = $default ?? [];

foreach ($keys as $key) {
$result[$key] = data_get($data, $key);
$result[$key] = data_get($data, $key, $default[$key] ?? null);
}

return $result;
Expand Down
35 changes: 35 additions & 0 deletions src/http-server/tests/RequestTest.php
Expand Up @@ -29,6 +29,7 @@ protected function tearDown()
{
Mockery::close();
Context::set(ServerRequestInterface::class, null);
Context::set('http.request.parsedData', null);
}

public function testRequestHasFile()
Expand Down Expand Up @@ -60,4 +61,38 @@ public function testRequestHeaderDefaultValue()
$res = $psrRequest->header('Hyperf-Version', 'v0');
$this->assertSame('v1.0', $res);
}

public function testRequestInput()
{
$psrRequest = Mockery::mock(ServerRequestInterface::class);
$psrRequest->shouldReceive('getParsedBody')->andReturn(['id' => 1]);
$psrRequest->shouldReceive('getQueryParams')->andReturn([]);
Context::set(ServerRequestInterface::class, $psrRequest);

$psrRequest = new Request();
$this->assertSame(1, $psrRequest->input('id'));
$this->assertSame('Hyperf', $psrRequest->input('name', 'Hyperf'));
}

public function testRequestAll()
{
$psrRequest = Mockery::mock(ServerRequestInterface::class);
$psrRequest->shouldReceive('getParsedBody')->andReturn(['id' => 1]);
$psrRequest->shouldReceive('getQueryParams')->andReturn(['name' => 'Hyperf']);
Context::set(ServerRequestInterface::class, $psrRequest);

$psrRequest = new Request();
$this->assertSame(['id' => 1, 'name' => 'Hyperf'], $psrRequest->all());
}

public function testRequestInputs()
{
$psrRequest = Mockery::mock(ServerRequestInterface::class);
$psrRequest->shouldReceive('getParsedBody')->andReturn(['id' => 1]);
$psrRequest->shouldReceive('getQueryParams')->andReturn([]);
Context::set(ServerRequestInterface::class, $psrRequest);

$psrRequest = new Request();
$this->assertSame(['id' => 1, 'name' => 'Hyperf'], $psrRequest->inputs(['id', 'name'], ['name' => 'Hyperf']));
}
}

0 comments on commit 809a418

Please sign in to comment.