1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace SmrTest\lib\DefaultGame; |
4
|
|
|
|
5
|
|
|
use Request; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class RequestTest |
9
|
|
|
* @package SmrTest\lib\DefaultGame |
10
|
|
|
* @covers Request |
11
|
|
|
*/ |
12
|
|
|
class RequestTest extends \PHPUnit\Framework\TestCase { |
13
|
|
|
|
14
|
|
|
public static function setUpBeforeClass() : void { |
15
|
|
|
$_REQUEST = [ |
16
|
|
|
'int' => 2, |
17
|
|
|
'float' => 3.14, |
18
|
|
|
'str' => 'ing', |
19
|
|
|
'array_empty' => [], |
20
|
|
|
'array_str' => ['a', 'b', 'c'], |
21
|
|
|
'array_int' => [1, 2, 3], |
22
|
|
|
]; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
protected function setUp() : void { |
26
|
|
|
// Reset $var for each test function |
27
|
|
|
unset($GLOBALS['var']); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
//------------------------------------------------------------------------ |
31
|
|
|
|
32
|
|
|
public function test_has() { |
33
|
|
|
// An index that exists |
34
|
|
|
$this->assertTrue(Request::has('str')); |
35
|
|
|
// An index that doesn't exist |
36
|
|
|
$this->assertFalse(Request::has('noexist')); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
//------------------------------------------------------------------------ |
40
|
|
|
|
41
|
|
|
public function test_getInt() { |
42
|
|
|
// An index that exists, with default |
43
|
|
|
$this->assertSame(Request::getInt('int', 3), 2); |
44
|
|
|
// An index that exists, no default |
45
|
|
|
$this->assertSame(Request::getInt('int'), 2); |
46
|
|
|
// An index that doesn't exist, with default |
47
|
|
|
$this->assertSame(Request::getInt('noexist', 3), 3); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function test_getInt_exception() { |
51
|
|
|
// An index that doesn't exist, no default |
52
|
|
|
$this->expectException(\Exception::class); |
53
|
|
|
$this->expectExceptionMessage('No request variable "noexist"'); |
54
|
|
|
Request::getInt('noexist'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
//------------------------------------------------------------------------ |
58
|
|
|
|
59
|
|
|
public function test_getFloat() { |
60
|
|
|
// An index that exists, with default |
61
|
|
|
$this->assertSame(Request::getFloat('float', 2.0), 3.14); |
62
|
|
|
// An index that exists, no default |
63
|
|
|
$this->assertSame(Request::getFloat('float'), 3.14); |
64
|
|
|
// An index that doesn't exist, with default |
65
|
|
|
$this->assertSame(Request::getFloat('noexist', 3.14), 3.14); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function test_getFloat_exception() { |
69
|
|
|
// An index that doesn't exist, no default |
70
|
|
|
$this->expectException(\Exception::class); |
71
|
|
|
$this->expectExceptionMessage('No request variable "noexist"'); |
72
|
|
|
Request::getFloat('noexist'); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
//------------------------------------------------------------------------ |
76
|
|
|
|
77
|
|
|
public function test_getArray() { |
78
|
|
|
// An index that exists, with default |
79
|
|
|
$this->assertSame(Request::getArray('array_str', []), ['a', 'b', 'c']); |
80
|
|
|
// An index that exists, no default |
81
|
|
|
$this->assertSame(Request::getArray('array_str'), ['a', 'b', 'c']); |
82
|
|
|
// An index that doesn't exist, with default |
83
|
|
|
$this->assertSame(Request::getArray('noexist', ['a']), ['a']); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function test_getArray_exception() { |
87
|
|
|
// An index that doesn't exist, no default |
88
|
|
|
$this->expectException(\Exception::class); |
89
|
|
|
$this->expectExceptionMessage('No request variable "noexist"'); |
90
|
|
|
Request::getArray('noexist'); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
//------------------------------------------------------------------------ |
94
|
|
|
|
95
|
|
|
public function test_getIntArray() { |
96
|
|
|
// An index that exists, with default |
97
|
|
|
$this->assertSame(Request::getIntArray('array_int', []), [1, 2, 3]); |
98
|
|
|
// An index that exists, no default |
99
|
|
|
$this->assertSame(Request::getIntArray('array_int'), [1, 2, 3]); |
100
|
|
|
// An index that doesn't exist, with default |
101
|
|
|
$this->assertSame(Request::getIntArray('noexist', [1]), [1]); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function test_getIntArray_exception() { |
105
|
|
|
// An index that doesn't exist, no default |
106
|
|
|
$this->expectException(\Exception::class); |
107
|
|
|
$this->expectExceptionMessage('No request variable "noexist"'); |
108
|
|
|
Request::getIntArray('noexist'); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
//------------------------------------------------------------------------ |
112
|
|
|
|
113
|
|
|
public function test_get() { |
114
|
|
|
// An index that exists, with default |
115
|
|
|
$this->assertSame(Request::get('str', 'foo'), 'ing'); |
116
|
|
|
// An index that exists, no default |
117
|
|
|
$this->assertSame(Request::get('str'), 'ing'); |
118
|
|
|
// An index that doesn't exist, with default |
119
|
|
|
$this->assertSame(Request::get('noexist', 'foo'), 'foo'); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function test_get_exception() { |
123
|
|
|
// An index that doesn't exist, no default |
124
|
|
|
$this->expectException(\Exception::class); |
125
|
|
|
$this->expectExceptionMessage('No request variable "noexist"'); |
126
|
|
|
Request::get('noexist'); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
//------------------------------------------------------------------------ |
130
|
|
|
|
131
|
|
|
public function test_getVar() { |
132
|
|
|
global $var; |
133
|
|
|
$var['var:str'] = 'ing'; |
134
|
|
|
// An index that exists in var but not request, no default |
135
|
|
|
$this->assertSame(Request::getVar('var:str'), 'ing'); |
136
|
|
|
// An index that exists in var but not request, with default |
137
|
|
|
$this->assertSame(Request::getVar('var:str', 'foo'), 'ing'); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function test_getVar_no_var() { |
141
|
|
|
// An index that exists in request but not var, no default |
142
|
|
|
$this->assertSame(Request::getVar('str'), 'ing'); |
143
|
|
|
// An index that exists in request but not var, with default |
144
|
|
|
$this->assertSame(Request::getVar('str', 'foo'), 'ing'); |
145
|
|
|
// An index neither in request nor var, with default |
146
|
|
|
$this->assertSame(Request::getVar('noexist', 'foo'), 'foo'); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function test_getVar_exception_no_default() { |
150
|
|
|
// An index that doesn't exist in request or var, no default |
151
|
|
|
$this->expectException(\Exception::class); |
152
|
|
|
$this->expectExceptionMessage('No request variable "noexist"'); |
153
|
|
|
Request::getVar('noexist'); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function test_getVar_exception_index_in_both() { |
157
|
|
|
global $var; |
158
|
|
|
$var['str'] = 'ing:var'; |
159
|
|
|
// An index that exists in both var and request, with different values |
160
|
|
|
$this->expectException(\Exception::class); |
161
|
|
|
$this->expectExceptionMessage('Index "str" inconsistent between $var and $_REQUEST!'); |
162
|
|
|
Request::getVar('str'); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function test_getVar_index_same_in_both() { |
166
|
|
|
global $var; |
167
|
|
|
$var['str'] = 'ing'; |
168
|
|
|
// An index that exists in both var and request, with the same value |
169
|
|
|
$this->assertSame(Request::getVar('str'), 'ing'); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
//------------------------------------------------------------------------ |
173
|
|
|
|
174
|
|
|
public function test_getVarInt() { |
175
|
|
|
global $var; |
176
|
|
|
$var['var:int'] = 2; |
177
|
|
|
// An index that exists in var but not request, no default |
178
|
|
|
$this->assertSame(Request::getVarInt('var:int'), 2); |
179
|
|
|
// An index that exists in var but not request, with default |
180
|
|
|
$this->assertSame(Request::getVarInt('var:int', 3), 2); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
public function test_getVarInt_no_var() { |
184
|
|
|
// An index that exists in request but not var, no default |
185
|
|
|
$this->assertSame(Request::getVarInt('int'), 2); |
186
|
|
|
// An index that exists in request but not var, with default |
187
|
|
|
$this->assertSame(Request::getVarInt('int', 3), 2); |
188
|
|
|
// An index neither in request nor var, with default |
189
|
|
|
$this->assertSame(Request::getVarInt('noexist', 3), 3); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
public function test_getVarInt_exception_no_default() { |
193
|
|
|
// An index that doesn't exist in request or var, no default |
194
|
|
|
$this->expectException(\Exception::class); |
195
|
|
|
$this->expectExceptionMessage('No request variable "noexist"'); |
196
|
|
|
Request::getVarInt('noexist'); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
public function test_getVarInt_exception_index_in_both() { |
200
|
|
|
global $var; |
201
|
|
|
$var['int'] = 3; |
202
|
|
|
// An index that exists in both var and request, with different values |
203
|
|
|
$this->expectException(\Exception::class); |
204
|
|
|
$this->expectExceptionMessage('Index "int" inconsistent between $var and $_REQUEST!'); |
205
|
|
|
Request::getVarInt('int'); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
public function test_getVarInt_index_same_in_both() { |
209
|
|
|
global $var; |
210
|
|
|
$var['int'] = 2; |
211
|
|
|
// An index that exists in both var and request, with the same value |
212
|
|
|
$this->assertSame(Request::getVarInt('int'), 2); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
//------------------------------------------------------------------------ |
216
|
|
|
|
217
|
|
|
public function test_getVarIntArray() { |
218
|
|
|
global $var; |
219
|
|
|
$var['var:array_int'] = [1, 2, 3]; |
220
|
|
|
// An index that exists in var but not request, no default |
221
|
|
|
$this->assertSame(Request::getVarIntArray('var:array_int'), [1, 2, 3]); |
222
|
|
|
// An index that exists in var but not request, with default |
223
|
|
|
$this->assertSame(Request::getVarIntArray('var:array_int', []), [1, 2, 3]); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
public function test_getVarIntArray_no_var() { |
227
|
|
|
// An index that exists in request but not var, no default |
228
|
|
|
$this->assertSame(Request::getVarIntArray('array_int'), [1, 2, 3]); |
229
|
|
|
// An index that exists in request but not var, with default |
230
|
|
|
$this->assertSame(Request::getVarIntArray('array_int', []), [1, 2, 3]); |
231
|
|
|
// An index neither in request nor var, with default |
232
|
|
|
$this->assertSame(Request::getVarIntArray('noexist', [1]), [1]); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
public function test_getVarIntArray_exception_no_default() { |
236
|
|
|
// An index that doesn't exist in request or var, no default |
237
|
|
|
$this->expectException(\Exception::class); |
238
|
|
|
$this->expectExceptionMessage('No request variable "noexist"'); |
239
|
|
|
Request::getVarIntArray('noexist'); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
public function test_getVarIntArray_exception_index_in_both() { |
243
|
|
|
global $var; |
244
|
|
|
$var['array_int'] = []; |
245
|
|
|
// An index that exists in both var and request, with different values |
246
|
|
|
$this->expectException(\Exception::class); |
247
|
|
|
$this->expectExceptionMessage('Index "array_int" inconsistent between $var and $_REQUEST!'); |
248
|
|
|
Request::getVarIntArray('array_int'); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
public function test_getVarIntArray_index_same_in_both() { |
252
|
|
|
global $var; |
253
|
|
|
$var['array_int'] = [1, 2, 3]; |
254
|
|
|
// An index that exists in both var and request, with the same value |
255
|
|
|
$this->assertSame(Request::getVarIntArray('array_int'), [1, 2, 3]); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
} |
259
|
|
|
|