-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
Copy pathRequestTest.php
273 lines (227 loc) · 8.11 KB
/
RequestTest.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\HTTP\Test\Unit\PhpEnvironment;
use \Magento\Framework\HTTP\PhpEnvironment\Request;
use Zend\Stdlib\Parameters;
class RequestTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Request
*/
protected $model;
/**
* @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager | \PHPUnit_Framework_MockObject_MockObject
*/
protected $objectManager;
/**
* @var \Magento\Framework\Stdlib\Cookie\CookieReaderInterface | \PHPUnit_Framework_MockObject_MockObject
*/
private $cookieReader;
/**
* @var \Magento\Framework\Stdlib\StringUtils | \PHPUnit_Framework_MockObject_MockObject
*/
private $converter;
/**
* @var array
*/
private $serverArray;
protected function setUp()
{
$this->objectManager = $this->getMock('Magento\Framework\ObjectManagerInterface');
$this->cookieReader = $this->getMock('Magento\Framework\Stdlib\Cookie\CookieReaderInterface');
$this->converter = $this->getMock('Magento\Framework\Stdlib\StringUtils');
// Stash the $_SERVER array to protect it from modification in test
$this->serverArray = $_SERVER;
}
public function tearDown()
{
$_SERVER = $this->serverArray;
}
private function getModel($uri = null)
{
return new Request($this->cookieReader, $this->converter, $uri);
}
public function testSetPathInfoWithNullValue()
{
$this->model = $this->getModel();
$actual = $this->model->setPathInfo();
$this->assertEquals($this->model, $actual);
}
public function testSetPathInfoWithValue()
{
$this->model = $this->getModel();
$expected = 'testPathInfo';
$this->model->setPathInfo($expected);
$this->assertEquals($expected, $this->model->getPathInfo());
}
public function testSetPathInfoWithQueryPart()
{
$uri = 'http://test.com/node?queryValue';
$this->model = $this->getModel($uri);
$this->model->setPathInfo();
$this->assertEquals('/node', $this->model->getPathInfo());
}
/**
* @param string $name
* @param string $default
* @param string $result
* @dataProvider getServerProvider
*/
public function testGetServer($name, $default, $result)
{
$this->model = $this->getModel();
$this->model->setServer(new Parameters([
'HTTPS' => 'off',
'DOCUMENT_ROOT' => '/test',
'HTTP_ACCEPT' => '',
'HTTP_CONNECTION' => 'http-connection',
'HTTP_REFERER' => 'http-referer',
'HTTP_X_FORWARDED_FOR' => 'x-forwarded',
'HTTP_USER_AGENT' => 'user-agent',
'PATH_INFO' => 'path-info',
'QUERY_STRING' => '',
'REMOTE_HOST' => 'remote-host',
'REQUEST_METHOD' => 'GET',
'REQUEST_URI' => 'request-uri',
'SERVER_NAME' => 'server-name',
]));
$this->assertEquals($result, $this->model->getServer($name, $default));
}
/**
* @return array
*/
public function getServerProvider()
{
return [
['HTTPS', '', 'off'],
['DOCUMENT_ROOT', '', '/test'],
['ORIG_PATH_INFO', 'orig-path-info', 'orig-path-info'],
['PATH_INFO', '', 'path-info'],
['QUERY_STRING', '', ''],
['REMOTE_HOST', 'test', 'remote-host'],
['REQUEST_METHOD', '', 'GET'],
['REQUEST_URI', 'test', 'request-uri'],
['SERVER_NAME', 'test', 'server-name'],
['HTTP_ACCEPT', 'http-accept', ''],
['HTTP_CONNECTION', '', 'http-connection'],
['HTTP_HOST', 'http-host', 'http-host'],
['HTTP_REFERER', '', 'http-referer'],
['HTTP_USER_AGENT', '', 'user-agent'],
['HTTP_X_FORWARDED_FOR', '', 'x-forwarded'],
['Accept', 'accept', 'accept'],
['Connection', '', ''],
['Host', 'http-host', 'http-host'],
['Referer', 'referer', 'referer'],
['User-Agent', '', ''],
['X-Forwarded-For', 'test', 'test'],
];
}
public function testGetAliasWhenAliasExists()
{
$this->model = $this->getModel();
$this->model->setAlias('AliasName', 'AliasTarget');
$this->assertEquals('AliasTarget', $this->model->getAlias('AliasName'));
}
public function testGetAliasWhenAliasesIsNull()
{
$this->model = $this->getModel();
$this->assertNull($this->model->getAlias('someValue'));
}
public function testSetPostValue()
{
$this->model = $this->getModel();
$post = ['one' => '111', 'two' => '222'];
$this->model->setPostValue($post);
$this->assertEquals($post, $this->model->getPost()->toArray());
$this->model->setPost(new Parameters([]));
$this->assertEmpty($this->model->getPost()->toArray());
$post = ['post_var' => 'post_value'];
$this->model->setPostValue($post);
$this->model->setPostValue('post_var 2', 'post_value 2');
$this->assertEquals(
['post_var' => 'post_value', 'post_var 2' => 'post_value 2'],
$this->model->getPost()->toArray()
);
}
public function testGetFiles()
{
$this->model = $this->getModel();
$files = ['one' => '111', 'two' => '222'];
$this->model->setFiles(new Parameters($files));
$this->assertEquals($files, $this->model->getFiles()->toArray());
foreach ($files as $key => $value) {
$this->assertEquals($value, $this->model->getFiles($key));
}
$this->assertNull($this->model->getFiles('no_such_file'));
$this->assertEquals('default', $this->model->getFiles('no_such_file', 'default'));
}
public function testGetBaseUrlWithUrl()
{
$this->model = $this->getModel();
$this->model->setBaseUrl('http:\/test.com\one/two');
$this->assertEquals('http://test.com/one/two', $this->model->getBaseUrl());
}
public function testGetBaseUrlWithEmptyUrl()
{
$this->model = $this->getModel();
$this->assertEmpty($this->model->getBaseUrl());
}
public function testGetAliasWhenAliasSet()
{
$this->model = $this->getModel();
$this->model->setAlias('AliasName', 'AliasTarget');
$this->assertEquals('AliasTarget', $this->model->getAlias('AliasName'));
}
public function testGetAliasWhenAliasAreEmpty()
{
$this->model = $this->getModel();
$this->assertNull($this->model->getAlias(''));
}
public function testGetCookie()
{
$key = "cookieName";
$default = "defaultValue";
$this->cookieReader
->expects($this->once())
->method('getCookie')
->with($key, $default);
$this->getModel()->getCookie($key, $default);
}
public function testGetCookieDefault()
{
$key = "cookieName";
$default = "defaultValue";
$this->cookieReader
->expects($this->once())
->method('getCookie')
->with($key, $default)
->will($this->returnValue($default));
$this->assertEquals($default, $this->getModel()->getCookie($key, $default));
}
public function testGetCookieNameExists()
{
$key = "cookieName";
$default = "defaultValue";
$value = "cookieValue";
$this->cookieReader
->expects($this->once())
->method('getCookie')
->with($key, $default)
->will($this->returnValue($value));
$this->assertEquals($value, $this->getModel()->getCookie($key, $default));
}
public function testGetCookieNullName()
{
$nullKey = null;
$default = "defaultValue";
$this->cookieReader
->expects($this->once())
->method('getCookie')
->with($nullKey, $default)
->will($this->returnValue($default));
$this->assertEquals($default, $this->getModel()->getCookie($nullKey, $default));
}
}