CRequestBasicTest::testGet()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 8
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 11
rs 9.4285
1
<?php
2
3
namespace Anax\Request;
4
5
/**
6
 * Storing information from the request and calculating related essentials.
7
 *
8
 */
9
class CRequestBasicTest extends \PHPUnit_Framework_TestCase
10
{
11
    /**
12
     * Properties
13
     */
14
    private $request;
15
16
17
18
    /**
19
     * Set up a request object
20
     *
21
     * @return void
22
     */
23
    public function setUp()
24
    {
25
        $this->request = new \Anax\Request\CRequestBasic();
26
        $this->request->setGlobals(
27
            [
28
                'server' => [
29
                    'REQUEST_SCHEME' => "http",
30
                    'HTTPS'       => null, //"on",
31
                    'SERVER_NAME' => "dbwebb.se",
32
                    'SERVER_PORT' => "80",
33
                    'REQUEST_URI' => "/anax-mvc/webroot/app.php",
34
                    'SCRIPT_NAME' => "/anax-mvc/webroot/app.php",
35
                ]
36
            ]
37
        );
38
    }
39
40
41
42
    /**
43
     * Test 
44
     *
45
     * @return void
46
     *
47
     */
48
    public function testGet()
49
    {
50
        $get = $this->request->getGet("nothing");
51
        $this->assertEmpty($get, "Nothing is NOT empty.");
52
53
        $key = "somekey";
54
        $value = "somevalue";
55
        $this->request->setGet($key, $value);
56
        $get = $this->request->getGet($key);
57
        $this->assertEquals($get, $value, "Missmatch between " . $get . " and " . $value);
58
    }
59
60
61
62
    /**
63
     * Provider for routes
64
     *
65
     * @return array
66
     */
67
    public function providerRoute()
68
    {
69
        return [
70
            [""],
71
            ["controller"],
72
            ["controller/action"],
73
            ["controller/action/arg1"],
74
            ["controller/action/arg1/arg2"],
75
            ["controller/action/arg1/arg2/arg3"],
76
        ];
77
    }
78
79
80
81
    /**
82
     * Test 
83
     *
84
     * @param string $route the route part
85
     *
86
     * @return void
87
     *
88
     * @dataProvider providerRoute
89
     *
90
     */
91
    public function testGetRoute($route)
92
    {
93
        $uri = $this->request->getServer('REQUEST_URI');
94
        //$this->assertEmpty($uri, "REQUEST_URI is empty.");
0 ignored issues
show
Unused Code Comprehensibility introduced by
80% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
95
96
        $this->request->setServer('REQUEST_URI', $uri . '/' . $route);
97
        $this->assertEquals($route, $this->request->extractRoute(), "Failed extractRoute: " . $route);
98
        $this->assertEquals($route, $this->request->getRoute(), "Failed getRoute: " . $route);
99
    }
100
101
102
103
    /**
104
     * Provider for $_SERVER
105
     *
106
     * @return array
107
     */
108
    public function providerGetCurrentUrl()
109
    {
110
        return [
111
            [
112
                [
113
                    'REQUEST_SCHEME' => "http",
114
                    'HTTPS'       => null, //"on",
115
                    'SERVER_NAME' => "dbwebb.se",
116
                    'SERVER_PORT' => "80",
117
                    'REQUEST_URI' => "/",
118
                    'url'         => "http://dbwebb.se",
119
                ]
120
            ],
121
            [
122
                [
123
                    'REQUEST_SCHEME' => "http",
124
                    'HTTPS'       => null, //"on",
125
                    'SERVER_NAME' => "dbwebb.se",
126
                    'SERVER_PORT' => "80",
127
                    'REQUEST_URI' => "/img",
128
                    'url'         => "http://dbwebb.se/img",
129
                ]
130
            ],
131
            [
132
                [
133
                    'REQUEST_SCHEME' => "http",
134
                    'HTTPS'       => null, //"on",
135
                    'SERVER_NAME' => "dbwebb.se",
136
                    'SERVER_PORT' => "80",
137
                    'REQUEST_URI' => "/img/",
138
                    'url'         => "http://dbwebb.se/img",
139
                ]
140
            ],
141
            [
142
                [
143
                    'REQUEST_SCHEME' => "http",
144
                    'HTTPS'       => null, //"on",
145
                    'SERVER_NAME' => "dbwebb.se",
146
                    'SERVER_PORT' => "80",
147
                    'REQUEST_URI' => "/anax-mvc/webroot/app.php",
148
                    'url'         => "http://dbwebb.se/anax-mvc/webroot/app.php",
149
                ]
150
            ],
151
            [
152
                [
153
                    'REQUEST_SCHEME' => "http",
154
                    'HTTPS'       => null, //"on",
155
                    'SERVER_NAME' => "dbwebb.se",
156
                    'SERVER_PORT' => "8080",
157
                    'REQUEST_URI' => "/anax-mvc/webroot/app.php",
158
                    'url'         => "http://dbwebb.se:8080/anax-mvc/webroot/app.php",
159
                ]
160
            ],
161
            [
162
                [
163
                    'REQUEST_SCHEME' => "http",
164
                    'HTTPS'       => "on", //"on",
165
                    'SERVER_NAME' => "dbwebb.se",
166
                    'SERVER_PORT' => "443",
167
                    'REQUEST_URI' => "/anax-mvc/webroot/app.php",
168
                    'url'         => "https://dbwebb.se/anax-mvc/webroot/app.php",
169
                ]
170
            ],
171
            [
172
                [
173
                    'REQUEST_SCHEME' => "http",
174
                    'HTTPS'       => "on", //"on",
175
                    'SERVER_NAME' => "dbwebb.se",
176
                    'SERVER_PORT' => "8080",
177
                    'REQUEST_URI' => "/anax-mvc/webroot/app.php",
178
                    'url'         => "https://dbwebb.se:8080/anax-mvc/webroot/app.php",
179
                ]
180
            ],
181
        ];
182
    }
183
184
185
186
    /**
187
     * Test 
188
     *
189
     * @param string $server the $_SERVER part
190
     *
191
     * @return void
192
     *
193
     * @dataProvider providerGetCurrentUrl
194
     *
195
     */
196
    public function testGetCurrentUrl($server) 
197
    {
198
        $this->request->setServer('REQUEST_SCHEME', $server['REQUEST_SCHEME']);
199
        $this->request->setServer('HTTPS', $server['HTTPS']);
200
        $this->request->setServer('SERVER_NAME', $server['SERVER_NAME']);
201
        $this->request->setServer('SERVER_PORT', $server['SERVER_PORT']);
202
        $this->request->setServer('REQUEST_URI', $server['REQUEST_URI']);
203
204
        $url = $server['url'];
205
206
        $res = $this->request->getCurrentUrl();
207
208
        $this->assertEquals($url, $res, "Failed url: " . $url);
209
    }
210
211
212
213
    /**
214
     * Provider for $_SERVER
215
     *
216
     * @return array
217
     */
218
    public function providerInit()
219
    {
220
        return [
221
            [
222
                [
223
                    'REQUEST_SCHEME' => "http",
224
                    'HTTPS'       => null, //"on",
225
                    'SERVER_NAME' => "dbwebb.se",
226
                    'SERVER_PORT' => "80",
227
                    'REQUEST_URI' => "/anax-mvc/webroot/app.php",
228
                    'SCRIPT_NAME' => "/anax-mvc/webroot/app.php",
229
                    'siteUrl'     => "http://dbwebb.se",
230
                    'baseUrl'     => "http://dbwebb.se/anax-mvc/webroot",
231
                ]
232
            ],
233
            [
234
                [
235
                    'REQUEST_SCHEME' => "http",
236
                    'HTTPS'       => null, //"on",
237
                    'SERVER_NAME' => "dbwebb.se",
238
                    'SERVER_PORT' => "8080",
239
                    'REQUEST_URI' => "/anax-mvc/webroot/app.php",
240
                    'SCRIPT_NAME' => "/anax-mvc/webroot/app.php",
241
                    'siteUrl'     => "http://dbwebb.se:8080",
242
                    'baseUrl'     => "http://dbwebb.se:8080/anax-mvc/webroot",
243
                ]
244
            ],
245
            [
246
                [
247
                    'REQUEST_SCHEME' => "http",
248
                    'HTTPS'       => "on",
249
                    'SERVER_NAME' => "dbwebb.se",
250
                    'SERVER_PORT' => "8080",
251
                    'REQUEST_URI' => "/anax-mvc/webroot/app.php",
252
                    'SCRIPT_NAME' => "/anax-mvc/webroot/app.php",
253
                    'siteUrl'     => "https://dbwebb.se:8080",
254
                    'baseUrl'     => "https://dbwebb.se:8080/anax-mvc/webroot",
255
                ]
256
            ],
257
            [
258
                [
259
                    'REQUEST_SCHEME' => "http",
260
                    'HTTPS'       => "on",
261
                    'SERVER_NAME' => "dbwebb.se",
262
                    'SERVER_PORT' => "443",
263
                    'REQUEST_URI' => "/anax-mvc/webroot/app.php",
264
                    'SCRIPT_NAME' => "/anax-mvc/webroot/app.php",
265
                    'siteUrl'     => "https://dbwebb.se",
266
                    'baseUrl'     => "https://dbwebb.se/anax-mvc/webroot",
267
                ]
268
            ]            
269
        ];
270
    }
271
272
273
274
    /**
275
     * Test 
276
     *
277
     * @param string $server the route part
278
     *
279
     * @return void
280
     *
281
     * @dataProvider providerInit
282
     *
283
     */
284
    public function testInit($server) 
285
    {
286
        $this->request->setServer('REQUEST_SCHEME', $server['REQUEST_SCHEME']);
287
        $this->request->setServer('HTTPS', $server['HTTPS']);
288
        $this->request->setServer('SERVER_NAME', $server['SERVER_NAME']);
289
        $this->request->setServer('SERVER_PORT', $server['SERVER_PORT']);
290
        $this->request->setServer('REQUEST_URI', $server['REQUEST_URI']);
291
        $this->request->setServer('SCRIPT_NAME', $server['SCRIPT_NAME']);
292
293
        $siteUrl = $server['siteUrl'];
294
        $baseUrl = $server['baseUrl'];
295
296
        $res = $this->request->init();
297
        $this->assertInstanceOf(get_class($this->request), $res, "Init did not return this.");
298
299
        $this->assertEquals($siteUrl, $this->request->getSiteUrl(), "Failed siteurl: " . $siteUrl);
300
        $this->assertEquals($baseUrl, $this->request->getBaseUrl(), "Failed baseurl: " . $baseUrl);
301
    }
302
}
303
304