forked from slimphp/Slim-Psr7
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ResponseTest.php
181 lines (141 loc) · 5.25 KB
/
ResponseTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
/**
* Slim Framework (https://slimframework.com)
*
* @license https://github.com/slimphp/Slim-Psr7/blob/master/LICENSE.md (MIT License)
*/
declare(strict_types=1);
namespace Slim\Tests\Psr7;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use ReflectionProperty;
use Slim\Psr7\Environment;
use Slim\Psr7\Headers;
use Slim\Psr7\Response;
use Slim\Psr7\Stream;
use function fopen;
use function property_exists;
class ResponseTest extends TestCase
{
public function testConstructorWithDefaultArgs()
{
$response = new Response();
$headersReflection = new ReflectionProperty($response, 'headers');
$headersReflection->setAccessible(true);
$bodyReflection = new ReflectionProperty($response, 'body');
$bodyReflection->setAccessible(true);
$this->assertEquals(200, $response->getStatusCode());
$this->assertInstanceOf(Headers::class, $headersReflection->getValue($response));
$this->assertInstanceOf(Stream::class, $bodyReflection->getValue($response));
}
public function testConstructorWithCustomArgs()
{
$headers = new Headers();
$body = new Stream(fopen('php://temp', 'r+'));
$response = new Response(404, $headers, $body);
$headersReflection = new ReflectionProperty($response, 'headers');
$headersReflection->setAccessible(true);
$bodyReflection = new ReflectionProperty($response, 'body');
$bodyReflection->setAccessible(true);
$this->assertEquals(404, $response->getStatusCode());
$this->assertSame($headers, $headersReflection->getValue($response));
$this->assertSame($body, $bodyReflection->getValue($response));
}
public function testDeepCopyClone()
{
$headers = new Headers();
$body = new Stream(fopen('php://temp', 'r+'));
$response = new Response(404, $headers, $body);
$clone = clone $response;
$headersReflection = new ReflectionProperty($response, 'headers');
$headersReflection->setAccessible(true);
$this->assertEquals(404, $clone->getStatusCode());
$this->assertEquals('1.1', $clone->getProtocolVersion());
$this->assertNotSame($headers, $headersReflection->getValue($clone));
}
public function testDisableSetter()
{
$response = new Response();
$response->foo = 'bar';
$this->assertFalse(property_exists($response, 'foo'));
}
public function testGetStatusCode()
{
$response = new Response();
$responseStatus = new ReflectionProperty($response, 'status');
$responseStatus->setAccessible(true);
$responseStatus->setValue($response, 404);
$this->assertEquals(404, $response->getStatusCode());
}
public function testWithStatus()
{
$response = new Response();
$clone = $response->withStatus(302);
$this->assertEquals(302, $clone->getStatusCode());
}
/**
*/
public function testWithStatusInvalidStatusCodeThrowsException()
{
$this->expectException(InvalidArgumentException::class);
$response = new Response();
$response->withStatus(800);
}
/**
*/
public function testWithStatusInvalidReasonPhraseThrowsException()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Response reason phrase must be a string');
$response = new Response();
$response->withStatus(200, null);
}
public function testWithStatusEmptyReasonPhrase()
{
$responseWithNoMessage = new Response(310);
$this->assertEquals('', $responseWithNoMessage->getReasonPhrase());
}
public function testWithStatusValidReasonPhraseObject()
{
$mock = $this->getMockBuilder('ResponseTestReasonPhrase')->setMethods(['__toString'])->getMock();
$mock->expects($this->once())
->method('__toString')
->will($this->returnValue('Slim OK'));
$response = new Response();
$response = $response->withStatus(200, $mock);
$this->assertEquals('Slim OK', $response->getReasonPhrase());
}
public function testGetReasonPhrase()
{
$response = new Response(404);
$this->assertEquals('Not Found', $response->getReasonPhrase());
}
public function testEmptyReasonPhraseForUnrecognisedCode()
{
$response = new Response();
$response = $response->withStatus(199);
$this->assertSame('', $response->getReasonPhrase());
}
public function testSetReasonPhraseForUnrecognisedCode()
{
$response = new Response();
$response = $response->withStatus(199, 'Random Message');
$this->assertEquals('Random Message', $response->getReasonPhrase());
}
public function testGetCustomReasonPhrase()
{
$response = new Response();
$clone = $response->withStatus(200, 'Custom Phrase');
$this->assertEquals('Custom Phrase', $clone->getReasonPhrase());
}
public function testResponseHeadersDoNotContainAuthorizationHeader()
{
$_SERVER = Environment::mock(
[
'PHP_AUTH_USER' => 'foo'
]
);
$response = new Response();
$this->assertEmpty($response->getHeaders());
}
}