-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathServerRequestInterfaceTestsTrait.php
199 lines (156 loc) · 6.08 KB
/
ServerRequestInterfaceTestsTrait.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
namespace Maks3w\Psr7Assertions\PhpUnit;
use PHPUnit_Framework_Assert as Assert;
use Psr\Http\Message\ServerRequestInterface;
/**
* Provide PHPUnit test methods for Psr\Http\Message\ServerRequestInterface constraints.
*/
trait ServerRequestInterfaceTestsTrait
{
use RequestInterfaceTestsTrait;
// Test methods for default/empty instances
public function testServerRequestImplementsInterface()
{
Assert::assertInstanceOf('Psr\Http\Message\ServerRequestInterface', $this->createDefaultServerRequest());
}
public function testValidDefaultServerParams()
{
$message = $this->createDefaultServerRequest();
$serverParams = $message->getServerParams();
Assert::assertInternalType('array', $serverParams, 'getServerParams must return an array');
}
public function testValidDefaultCookieParams()
{
$message = $this->createDefaultServerRequest();
$cookieParams = $message->getCookieParams();
Assert::assertInternalType('array', $cookieParams, 'getCookieParams must return an array');
}
public function testValidDefaultQueryParams()
{
$message = $this->createDefaultServerRequest();
$queryParams = $message->getQueryParams();
Assert::assertInternalType('array', $queryParams, 'getQueryParams must return an array');
}
public function testValidDefaultUploadedFiles()
{
$message = $this->createDefaultServerRequest();
$uploadedFiles = $message->getUploadedFiles();
Assert::assertInternalType('array', $uploadedFiles, 'getUploadedFiles must return an array');
Assert::assertEmpty($uploadedFiles, 'an empty array MUST be returned if no data is present');
}
public function testValidDefaultParsedBody()
{
$message = $this->createDefaultServerRequest();
$parsedBody = $message->getParsedBody();
Assert::assertNull($parsedBody);
}
public function testValidNotExistsAttribute()
{
$message = $this->createDefaultServerRequest();
$attribute = $message->getAttribute('not exists');
Assert::assertNull($attribute, 'MUST return the argument $default if the attribute does not exist');
}
public function testValidNotExistsAttributeCustomDefault()
{
$message = $this->createDefaultServerRequest();
$default = 'custom';
$attribute = $message->getAttribute('not exists', $default);
Assert::assertEquals($default, $attribute, 'MUST return the argument $default if the attribute does not exist');
}
// Test methods for change instances status
public function testWithCookieParams()
{
$message = $this->createDefaultServerRequest();
$messageClone = clone $message;
$cookies = ['foo' => 'bar'];
$newMessage = $message->withCookieParams($cookies);
$this->assertImmutable($messageClone, $message, $newMessage);
Assert::assertEquals(
$cookies,
$newMessage->getCookieParams(),
'getCookieParams does not match body set in withCookieParams'
);
}
public function testWithQueryParams()
{
$message = $this->createDefaultServerRequest();
$messageClone = clone $message;
$query = ['foo' => 'bar'];
$newMessage = $message->withQueryParams($query);
$this->assertImmutable($messageClone, $message, $newMessage);
Assert::assertEquals(
$query,
$newMessage->getQueryParams(),
'getQueryParams does not match body set in withQueryParams'
);
}
/**
* @dataProvider validAttributeProvider
*
* @param string $attributeName
* @param string|string[] $attributeValue
*
* @return void
*/
public function testValidWithAttribute($attributeName, $attributeValue)
{
$message = $this->createDefaultServerRequest();
$messageClone = clone $message;
$newMessage = $message->withAttribute($attributeName, $attributeValue);
$this->assertImmutable($messageClone, $message, $newMessage);
Assert::assertEquals(
$attributeValue,
$newMessage->getAttribute($attributeName),
'getAttribute does not match attribute set in withAttribute'
);
}
/**
* @dataProvider validAttributeProvider
*
* @param string $attributeName
* @param string|string[] $attributeValue
* @param string[] $expectedAttributeValue
*
* @return void
*/
public function testGetAttributes($attributeName, $attributeValue, $expectedAttributeValue)
{
$message = $this->createDefaultServerRequest();
$newMessage = $message->withAttribute($attributeName, $attributeValue);
Assert::assertEquals($expectedAttributeValue, $newMessage->getAttributes());
}
/**
* @dataProvider validAttributeProvider
*
* @param string $attributeName
* @param string|string[] $attributeValue
*
* @return void
*/
public function testWithoutAttribute($attributeName, $attributeValue)
{
$message = $this->createDefaultServerRequest();
$messageWithAttribute = $message->withAttribute($attributeName, $attributeValue);
$messageClone = clone $messageWithAttribute;
$newMessage = $messageWithAttribute->withoutAttribute($attributeName);
$this->assertImmutable($messageClone, $messageWithAttribute, $newMessage);
Assert::assertEquals($message, $newMessage);
}
public function validAttributeProvider()
{
return [
// Description => [attribute name, attribute value, getAttributes()],
'Basic: value' => ['Basic', 'value', ['Basic' => 'value']],
'array value' => ['Basic', ['value'], ['Basic' => ['value']]],
'two value' => ['Basic', ['value1', 'value2'], ['Basic' => ['value1', 'value2']]],
];
}
/**
* @return ServerRequestInterface
*/
abstract protected function createDefaultServerRequest();
protected function createDefaultRequest()
{
return $this->createDefaultServerRequest();
}
}