Update methods of withXxx return not only psr response.

This commit is contained in:
李铭昕 2019-09-02 09:56:51 +08:00
parent d5fff8af1e
commit 9398486157
3 changed files with 30 additions and 29 deletions

View File

@ -57,12 +57,5 @@ interface ResponseInterface
/**
* Override a response with a cookie.
*/
public function cookie(Cookie $cookie): ResponseInterface;
/**
* Override a response with a header.
*
* @param string|string[] $value
*/
public function header(string $name, $value): ResponseInterface;
public function withCookie(Cookie $cookie): ResponseInterface;
}

View File

@ -164,7 +164,7 @@ class Response implements PsrResponseInterface, ResponseInterface
->withBody(new SwooleFileStream($file));
}
public function cookie(Cookie $cookie): ResponseInterface
public function withCookie(Cookie $cookie): ResponseInterface
{
Context::override(PsrResponseInterface::class, function ($response) use ($cookie) {
if (! $response instanceof ServerResponse) {
@ -176,18 +176,6 @@ class Response implements PsrResponseInterface, ResponseInterface
return $this;
}
public function header(string $name, $value): ResponseInterface
{
Context::override(PsrResponseInterface::class, function ($response) use ($name, $value) {
if (! $response instanceof PsrResponseInterface) {
throw new InvalidResponseException('The response is not instanceof ' . PsrResponseInterface::class);
}
return $response->withHeader($name, $value);
});
return $this;
}
/**
* Retrieves the HTTP protocol version as a string.
* The string MUST contain only the HTTP version number (e.g., "1.1", "1.0").
@ -212,7 +200,7 @@ class Response implements PsrResponseInterface, ResponseInterface
*/
public function withProtocolVersion($version)
{
return $this->getResponse()->withProtocolVersion($version);
return $this->call(__FUNCTION__, func_get_args());
}
/**
@ -307,7 +295,7 @@ class Response implements PsrResponseInterface, ResponseInterface
*/
public function withHeader($name, $value)
{
return $this->getResponse()->withHeader($name, $value);
return $this->call(__FUNCTION__, func_get_args());
}
/**
@ -326,7 +314,7 @@ class Response implements PsrResponseInterface, ResponseInterface
*/
public function withAddedHeader($name, $value)
{
return $this->getResponse()->withAddedHeader($name, $value);
return $this->call(__FUNCTION__, func_get_args());
}
/**
@ -341,7 +329,7 @@ class Response implements PsrResponseInterface, ResponseInterface
*/
public function withoutHeader($name)
{
return $this->getResponse()->withoutHeader($name);
return $this->call(__FUNCTION__, func_get_args());
}
/**
@ -367,7 +355,7 @@ class Response implements PsrResponseInterface, ResponseInterface
*/
public function withBody(StreamInterface $body)
{
return $this->getResponse()->withBody($body);
return $this->call(__FUNCTION__, func_get_args());
}
/**
@ -402,7 +390,7 @@ class Response implements PsrResponseInterface, ResponseInterface
*/
public function withStatus($code, $reasonPhrase = '')
{
return $this->getResponse()->withStatus($code, $reasonPhrase);
return $this->call(__FUNCTION__, func_get_args());
}
/**
@ -422,6 +410,23 @@ class Response implements PsrResponseInterface, ResponseInterface
return $this->getResponse()->getReasonPhrase();
}
protected function call($name, $arguments)
{
Context::override(PsrResponseInterface::class, function ($response) use ($name, $arguments) {
if (! $response instanceof PsrResponseInterface) {
throw new InvalidResponseException('The response is not instanceof ' . PsrResponseInterface::class);
}
if (! method_exists($response, $name)) {
throw new BadMethodCallException(sprintf('Call to undefined method %s::%s()', get_class($this), $name));
}
return $response->{$name}(...$arguments);
});
return $this;
}
/**
* Get ETag header according to the checksum of the file.
*/

View File

@ -175,6 +175,7 @@ class ResponseTest extends TestCase
$response = $response->withBody(new SwooleStream('xxx'));
$this->assertInstanceOf(PsrResponseInterface::class, $response);
$this->assertInstanceOf(ResponseInterface::class, $response);
}
public function testCookiesAndHeaders()
@ -186,7 +187,9 @@ class ResponseTest extends TestCase
$id = uniqid();
$cookie1 = new Cookie('Name', 'Hyperf');
$cookie2 = new Cookie('Request-Id', $id);
$swooleResponse->shouldReceive('status')->with(Mockery::any())->andReturn(200);
$swooleResponse->shouldReceive('status')->with(Mockery::any())->andReturnUsing(function ($code) {
$this->assertSame($code, 200);
});
$swooleResponse->shouldReceive('header')->withAnyArgs()->twice()->andReturnUsing(function ($name, $value) {
if ($name == 'X-Token') {
$this->assertSame($value, 'xxx');
@ -203,7 +206,7 @@ class ResponseTest extends TestCase
Context::set(PsrResponseInterface::class, $psrResponse = new \Hyperf\HttpMessage\Server\Response($swooleResponse));
$response = new Response();
$response = $response->cookie($cookie1)->cookie($cookie2)->header('X-Token', 'xxx');
$response = $response->withCookie($cookie1)->withCookie($cookie2)->withHeader('X-Token', 'xxx')->withStatus(200);
$this->assertInstanceOf(Response::class, $response);
$this->assertInstanceOf(ResponseInterface::class, $response);