Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: set inputs() default value #835

Merged
merged 4 commits into from Nov 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,10 @@

- [#832](https://github.com/hyperf/hyperf/pull/832) Added `Hyperf\Utils\Codec\Json`.

## 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']));
}
}