|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace KochTest\Http; |
|
4
|
|
|
|
|
5
|
|
|
use Koch\Http\HttpRequest; |
|
6
|
|
|
|
|
7
|
|
|
class HttpRequestTest extends \PHPUnit_Framework_TestCase |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var HttpRequest |
|
11
|
|
|
*/ |
|
12
|
|
|
protected $request; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Sets up the fixture, for example, opens a network connection. |
|
16
|
|
|
* This method is called before a test is executed. |
|
17
|
|
|
*/ |
|
18
|
|
|
public function setUp() |
|
19
|
|
|
{ |
|
20
|
|
|
$this->request = new HttpRequest(); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Tears down the fixture, for example, closes a network connection. |
|
25
|
|
|
* This method is called after a test is executed. |
|
26
|
|
|
*/ |
|
27
|
|
|
public function tearDown() |
|
28
|
|
|
{ |
|
29
|
|
|
unset($this->request); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function testMethodcontructor_unsetsGlobalVars() |
|
|
|
|
|
|
33
|
|
|
{ |
|
34
|
|
|
$this->assertFalse(isset($_REQUEST)); |
|
35
|
|
|
|
|
36
|
|
|
// Undefined variable: GLOBALS |
|
37
|
|
|
// it's not possible to unset globals - phpunit side effect? |
|
38
|
|
|
//$this->assertFalse(isset($GLOBALS)); |
|
|
|
|
|
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function testMethodgetRequestMethod() |
|
|
|
|
|
|
42
|
|
|
{ |
|
43
|
|
|
$_SERVER['REQUEST_METHOD'] = 'someMethod'; |
|
44
|
|
|
$_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'] = 'OverrideMethodName'; |
|
45
|
|
|
$this->assertEquals('OverrideMethodName', HttpRequest::getRequestMethod()); |
|
46
|
|
|
|
|
47
|
|
|
unset($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE']); |
|
48
|
|
|
$_SERVER['REQUEST_METHOD'] = 'HEAD'; |
|
49
|
|
|
$this->assertEquals('GET', HttpRequest::getRequestMethod()); |
|
50
|
|
|
|
|
51
|
|
|
$this->request->setRequestMethod('BEAVIS'); |
|
52
|
|
|
$this->assertEquals('BEAVIS', HttpRequest::getRequestMethod()); |
|
53
|
|
|
|
|
54
|
|
|
unset($_SERVER['REQUEST_METHOD']); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function testMethodsetRequestMethod() |
|
58
|
|
|
{ |
|
59
|
|
|
$this->request->setRequestMethod('BUTTHEAD'); |
|
60
|
|
|
$this->assertEquals('BUTTHEAD', HttpRequest::getRequestMethod()); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function testMethodisGET() |
|
64
|
|
|
{ |
|
65
|
|
|
$this->request->setRequestMethod('GET'); |
|
66
|
|
|
$this->assertTrue($this->request->isGet()); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function testMethodisPOST() |
|
70
|
|
|
{ |
|
71
|
|
|
$this->request->setRequestMethod('POST'); |
|
72
|
|
|
$this->assertTrue($this->request->isPost()); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function testMethodisPUT() |
|
76
|
|
|
{ |
|
77
|
|
|
$this->request->setRequestMethod('PUT'); |
|
78
|
|
|
$this->assertTrue($this->request->isPut()); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function testMethodisDELETE() |
|
82
|
|
|
{ |
|
83
|
|
|
$this->request->setRequestMethod('DELETE'); |
|
84
|
|
|
$this->assertTrue($this->request->isDelete()); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function testMethodisAjax() |
|
|
|
|
|
|
88
|
|
|
{ |
|
89
|
|
|
$isAjax = $this->request->isAjax(); |
|
90
|
|
|
$this->assertFalse($isAjax); |
|
91
|
|
|
|
|
92
|
|
|
$_SERVER['X-Requested-With'] = 'XMLHttpRequest'; |
|
93
|
|
|
$isAjax = $this->request->isAjax(); |
|
94
|
|
|
$this->assertTrue($isAjax); |
|
95
|
|
|
|
|
96
|
|
|
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest'; |
|
97
|
|
|
$isAjax = $this->request->isAjax(); |
|
98
|
|
|
$this->assertTrue($isAjax); |
|
99
|
|
|
|
|
100
|
|
|
unset($_SERVER['X-Requested-With']); |
|
101
|
|
|
unset($_SERVER['HTTP_X_REQUESTED_WITH']); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/*public function testMethodgetPost() |
|
|
|
|
|
|
105
|
|
|
{ |
|
106
|
|
|
$_POST['POST-ABC'] = '123'; |
|
107
|
|
|
$result = $this->request->getPost(); |
|
108
|
|
|
$this->assertArrayHasKey('POST-ABC', $result); |
|
109
|
|
|
|
|
110
|
|
|
// ArrayAccess via offsetExists |
|
111
|
|
|
$this->assertEquals($this->request['POST-ABC'], '123'); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public function testMethodgetGet() |
|
115
|
|
|
{ |
|
116
|
|
|
$_GET['GET-ABC'] = '123'; |
|
117
|
|
|
$result = $this->request->getGET(); |
|
118
|
|
|
$this->assertArrayHasKey('GETABC', $result); |
|
119
|
|
|
}*/ |
|
120
|
|
|
|
|
121
|
|
|
public function testMethodgetServerProtocol() |
|
|
|
|
|
|
122
|
|
|
{ |
|
123
|
|
|
$this->assertEquals($this->request->getServerProtocol(), 'http://'); |
|
124
|
|
|
|
|
125
|
|
|
$_SERVER['HTTPS'] = 'on'; |
|
126
|
|
|
$this->assertEquals($this->request->getServerProtocol(), 'https://'); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
public function testMethodIsSecure() |
|
|
|
|
|
|
130
|
|
|
{ |
|
131
|
|
|
$_SERVER['HTTPS'] = 'NO'; |
|
132
|
|
|
$this->assertFalse($this->request->isSecure()); |
|
133
|
|
|
|
|
134
|
|
|
$_SERVER['HTTPS'] = '1'; |
|
135
|
|
|
$this->assertTrue($this->request->isSecure()); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
public function testMethodgetBaseURL() |
|
|
|
|
|
|
139
|
|
|
{ |
|
140
|
|
|
$_SERVER['HTTPS'] = 'off'; |
|
141
|
|
|
$_SERVER['SERVER_NAME'] = 'localhost'; |
|
142
|
|
|
$_SERVER['SERVER_PORT'] = 80; |
|
143
|
|
|
$this->assertEquals($this->request->getBaseURL(), 'http://localhost'); |
|
144
|
|
|
|
|
145
|
|
|
/*$_SERVER['HTTPS'] = 'on'; |
|
|
|
|
|
|
146
|
|
|
$_SERVER['SERVER_NAME'] = 'localhost'; |
|
147
|
|
|
$_SERVER['SERVER_PORT'] = 123; |
|
148
|
|
|
$this->assertEquals($this->request->getBaseURL(), 'https://localhost:123'); |
|
149
|
|
|
|
|
150
|
|
|
$_SERVER['HTTPS'] = 'on'; |
|
151
|
|
|
$_SERVER['SERVER_NAME'] = 'localhost'; |
|
152
|
|
|
$_SERVER['SERVER_PORT'] = 443; |
|
153
|
|
|
$this->assertEquals($this->request->getBaseURL(), 'https://localhost');*/ |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
public function testMethodgetServerName() |
|
|
|
|
|
|
157
|
|
|
{ |
|
158
|
|
|
$name = 'ServerName'; |
|
159
|
|
|
$_SERVER['SERVER_NAME'] = $name; |
|
160
|
|
|
$this->assertEquals($this->request->getServerName(), $name); |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|
This check examines a number of code elements and verifies that they conform to the given naming conventions.
You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.