Skip to content

Commit 67c2c08

Browse files
committed
Added test cases for response proxy trait.
1 parent 533b4c0 commit 67c2c08

File tree

4 files changed

+177
-5
lines changed

4 files changed

+177
-5
lines changed

src/Server/ResponseProxyTrait.php

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
*/
1212
namespace Hyperf\HttpMessage\Server;
1313

14+
use Hyperf\HttpMessage\Cookie\Cookie;
1415
use Psr\Http\Message\ResponseInterface;
1516
use Psr\Http\Message\StreamInterface;
1617
use RuntimeException;
@@ -27,6 +28,14 @@ public function setResponse(ResponseInterface $response)
2728
$this->response = $response;
2829
}
2930

31+
public function getResponse(): ResponseInterface
32+
{
33+
if (! $this->response instanceof ResponseInterface) {
34+
throw new RuntimeException('response is invalid.');
35+
}
36+
return $this->response;
37+
}
38+
3039
public function getProtocolVersion()
3140
{
3241
return $this->getResponse()->getProtocolVersion();
@@ -103,11 +112,46 @@ public function getReasonPhrase()
103112
return $this->getResponse()->getReasonPhrase();
104113
}
105114

106-
public function getResponse(): ResponseInterface
115+
/**
116+
* Returns an instance with specified cookies.
117+
*/
118+
public function withCookie(Cookie $cookie): self
107119
{
108-
if (! $this->response instanceof ResponseInterface) {
109-
throw new RuntimeException('response is invalid.');
110-
}
111-
return $this->response;
120+
$this->setResponse($this->getResponse()->withCookie($cookie));
121+
return $this;
122+
}
123+
124+
/**
125+
* Retrieves all cookies.
126+
*/
127+
public function getCookies(): array
128+
{
129+
return $this->getResponse()->getCookies();
130+
}
131+
132+
/**
133+
* Returns an instance with specified trailer.
134+
* @param string $value
135+
*/
136+
public function withTrailer(string $key, $value): self
137+
{
138+
$this->setResponse($this->getResponse()->withTrailer($key, $value));
139+
return $this;
140+
}
141+
142+
/**
143+
* Retrieves a specified trailer value, returns null if the value does not exists.
144+
*/
145+
public function getTrailer(string $key)
146+
{
147+
return $this->getResponse()->getTrailer($key);
148+
}
149+
150+
/**
151+
* Retrieves all trailers values.
152+
*/
153+
public function getTrailers(): array
154+
{
155+
return $this->getResponse()->getTrailers();
112156
}
113157
}

tests/ResponseProxyTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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\HttpMessage;
13+
14+
use Hyperf\HttpMessage\Server\Response;
15+
use HyperfTest\HttpMessage\Stub\Server\ResponseStub;
16+
17+
/**
18+
* @internal
19+
* @coversNothing
20+
*/
21+
class ResponseProxyTest extends ResponseTest
22+
{
23+
protected function tearDown()
24+
{
25+
parent::tearDown(); // TODO: Change the autogenerated stub
26+
}
27+
28+
public function testStatusCode()
29+
{
30+
parent::testStatusCode(); // TODO: Change the autogenerated stub
31+
}
32+
33+
public function testHeaders()
34+
{
35+
parent::testHeaders(); // TODO: Change the autogenerated stub
36+
}
37+
38+
public function testCookies()
39+
{
40+
parent::testCookies(); // TODO: Change the autogenerated stub
41+
}
42+
43+
protected function newResponse()
44+
{
45+
$response = new ResponseStub();
46+
$response->setResponse(new Response());
47+
return $response;
48+
}
49+
}

tests/ResponseTest.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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\HttpMessage;
13+
14+
use Hyperf\HttpMessage\Cookie\Cookie;
15+
use Hyperf\HttpMessage\Server\Response;
16+
use PHPUnit\Framework\TestCase;
17+
18+
/**
19+
* @internal
20+
* @coversNothing
21+
*/
22+
class ResponseTest extends TestCase
23+
{
24+
protected function tearDown()
25+
{
26+
\Mockery::close();
27+
}
28+
29+
public function testStatusCode()
30+
{
31+
$response = $this->newResponse();
32+
$this->assertSame(200, $response->getStatusCode());
33+
$this->assertSame(201, $response->withStatus(201)->getStatusCode());
34+
}
35+
36+
public function testHeaders()
37+
{
38+
$response = $this->newResponse();
39+
$this->assertSame([], $response->getHeaders());
40+
$response = $response->withHeader('Server', 'Hyperf');
41+
$this->assertSame(['Server' => ['Hyperf']], $response->getHeaders());
42+
$this->assertSame(['Hyperf'], $response->getHeader('Server'));
43+
$this->assertSame('Hyperf', $response->getHeaderLine('Server'));
44+
}
45+
46+
public function testCookies()
47+
{
48+
$cookie = new Cookie('test', uniqid(), 3600, '/', 'hyperf.io');
49+
$response = $this->newResponse();
50+
$this->assertSame([], $response->getCookies());
51+
$response = $response->withCookie($cookie);
52+
$this->assertSame(['hyperf.io' => ['/' => ['test' => $cookie]]], $response->getCookies());
53+
}
54+
55+
protected function newResponse()
56+
{
57+
return new Response();
58+
}
59+
}

tests/Stub/Server/ResponseStub.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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\HttpMessage\Stub\Server;
13+
14+
use Hyperf\HttpMessage\Server\ResponseProxyTrait;
15+
use Psr\Http\Message\ResponseInterface;
16+
17+
class ResponseStub implements ResponseInterface
18+
{
19+
use ResponseProxyTrait;
20+
}

0 commit comments

Comments
 (0)