Passed
Push — master ( 32827b...783ac0 )
by Eliseev
01:15
created

TestRequest::testGetRequestUri()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
cc 2
eloc 9
c 2
b 0
f 2
nc 2
nop 4
dl 0
loc 15
rs 9.9666
1
<?php
2
3
use cse\helpers\Request;
4
use PHPUnit\Framework\TestCase;
5
6
class TestRequest extends TestCase
7
{
8
    /**
9
     * @param string $key
10
     * @param null|string $default
11
     * @param string $value
12
     * @param bool $set
13
     * @param $expected
14
     *
15
     * @dataProvider providerRequest
16
     *
17
     * @runInSeparateProcess
18
     */
19
    public function testPost(string $key, ?string $default, ?string $value, bool $set, $expected): void
20
    {
21
        if ($set) {
22
            $_POST[$key] = $value;
23
        } else {
24
            unset($_POST[$key]);
25
        }
26
27
        $this->assertEquals($expected, Request::post($key, $default));
0 ignored issues
show
Bug introduced by
It seems like $default can also be of type string; however, parameter $default of cse\helpers\Request::post() does only seem to accept null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

27
        $this->assertEquals($expected, Request::post($key, /** @scrutinizer ignore-type */ $default));
Loading history...
28
    }
29
30
    /**
31
     * @param string $key
32
     * @param null|string $default
33
     * @param null|string $value
34
     * @param bool $set
35
     * @param $expected
36
     *
37
     * @dataProvider providerRequest
38
     *
39
     * @runInSeparateProcess
40
     */
41
    public function testGet(string $key, ?string $default, ?string $value, bool $set, $expected): void
42
    {
43
        if ($set) {
44
            $_GET[$key] = $value;
45
        } else {
46
            unset($_GET[$key]);
47
        }
48
        $this->assertEquals($expected, Request::get($key, $default));
0 ignored issues
show
Bug introduced by
It seems like $default can also be of type string; however, parameter $default of cse\helpers\Request::get() does only seem to accept null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

48
        $this->assertEquals($expected, Request::get($key, /** @scrutinizer ignore-type */ $default));
Loading history...
49
    }
50
51
    /**
52
     * @param string $key
53
     * @param $default
54
     * @param $value
55
     * @param bool $set
56
     * @param $expected
57
     *
58
     * @dataProvider providerRequest
59
     *
60
     * @runInSeparateProcess
61
     */
62
    public function testRequests(string $key, $default, $value, bool $set, $expected): void
63
    {
64
        if ($set) {
65
            $_REQUEST[$key] = $value;
66
        } else {
67
            unset($_REQUEST[$key]);
68
        }
69
        $this->assertEquals($expected, Request::request($key, $default));
70
    }
71
72
    /**
73
     * @return array
74
     */
75
    public function providerRequest(): array
76
    {
77
        return [
78
            [
79
                'test',
80
                null,
81
                '12345',
82
                true,
83
                '12345',
84
            ],
85
            [
86
                'test2',
87
                '12345',
88
                null,
89
                true,
90
                '12345',
91
            ],
92
        ];
93
    }
94
95
    /**
96
     * @param bool $is_ajax
97
     * @param bool $expected
98
     *
99
     * @dataProvider providerIsAjax
100
     *
101
     * @runInSeparateProcess
102
     */
103
    public function testIsAjax(bool $is_ajax, bool $expected)
104
    {
105
        if ($is_ajax) {
106
            $_SERVER['HTTP_X_REQUESTED_WITH'] = 'xmlhttprequest';
107
        } else {
108
            unset($_SERVER['HTTP_X_REQUESTED_WITH']);
109
        }
110
        $this->assertEquals($expected, Request::isAjax());
111
    }
112
113
    /**
114
     * @return array
115
     */
116
    public function providerIsAjax(): array
117
    {
118
        return [
119
            [
120
                true,
121
                true,
122
            ],
123
            [
124
                false,
125
                false,
126
            ],
127
        ];
128
    }
129
130
    /**
131
     * @param string $method
132
     * @param bool $expected
133
     *
134
     * @dataProvider providerIsPost
135
     *
136
     * @runInSeparateProcess
137
     */
138
    public function testIsPost(string $method, bool $expected)
139
    {
140
        $_SERVER['REQUEST_METHOD'] = $method;
141
        $this->assertEquals($expected, Request::isPost());
142
    }
143
144
    /**
145
     * @return array
146
     */
147
    public function providerIsPost(): array
148
    {
149
        return [
150
            [
151
                Request::METHOD_GET,
152
                false,
153
            ],
154
            [
155
                Request::METHOD_POST,
156
                true,
157
            ],
158
            [
159
                Request::METHOD_DELETE,
160
                false,
161
            ],
162
        ];
163
    }
164
165
    /**
166
     * @param string $method
167
     * @param bool $expected
168
     *
169
     * @dataProvider providerIsGet
170
     *
171
     * @runInSeparateProcess
172
     */
173
    public function testIsGet(string $method, bool $expected): void
174
    {
175
        $_SERVER['REQUEST_METHOD'] = $method;
176
        $this->assertEquals($expected, Request::isGet());
177
    }
178
179
    /**
180
     * @return array
181
     */
182
    public function providerIsGet(): array
183
    {
184
        return [
185
            [
186
                Request::METHOD_GET,
187
                true,
188
            ],
189
            [
190
                Request::METHOD_POST,
191
                false,
192
            ],
193
            [
194
                Request::METHOD_DELETE,
195
                false,
196
            ],
197
        ];
198
    }
199
200
    /**
201
     * @param null|string $data
202
     * @param bool $is_ajax
203
     * @param null|string $default
204
     * @param string $expected
205
     *
206
     * @dataProvider providerGetRequestUri
207
     *
208
     * @runInSeparateProcess
209
     */
210
    public function testGetRequestUri(?string $data, bool $is_ajax, ?string $default, string $expected)
211
    {
212
        if ($is_ajax) {
213
            unset($_SERVER['REQUEST_URI']);
214
215
            $_SERVER['HTTP_X_REQUESTED_WITH'] = 'xmlhttprequest';
216
            $_SERVER['HTTP_REFERER'] = $data;
217
        } else {
218
            unset($_SERVER['HTTP_X_REQUESTED_WITH']);
219
            unset($_SERVER['HTTP_REFERER']);
220
221
            $_SERVER['REQUEST_URI'] = $data;
222
        }
223
224
        $this->assertEquals($expected, Request::getRequestUri($default));
0 ignored issues
show
Bug introduced by
It seems like $default can also be of type string; however, parameter $default of cse\helpers\Request::getRequestUri() does only seem to accept null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

224
        $this->assertEquals($expected, Request::getRequestUri(/** @scrutinizer ignore-type */ $default));
Loading history...
225
    }
226
227
    /**
228
     * @return array
229
     */
230
    public function providerGetRequestUri(): array
231
    {
232
        return [
233
            [
234
                '/link/test',
235
                true,
236
                null,
237
                '/link/test',
238
            ],
239
            [
240
                '/link/test',
241
                false,
242
                null,
243
                '/link/test',
244
            ],
245
            [
246
                null,
247
                false,
248
                '/link/home',
249
                '/link/home',
250
            ],
251
        ];
252
    }
253
254
    /**
255
     * @param string $url
256
     * @param bool $expected
257
     *
258
     * @dataProvider providerIsRedirectedToHttps
259
     *
260
     * @runInSeparateProcess
261
     */
262
    public function testIsRedirectedToHttps(string $url, bool $expected): void
263
    {
264
        $this->assertEquals($expected, Request::isRedirectedToHttps($url));
265
    }
266
267
    /**
268
     * @return array
269
     */
270
    public function providerIsRedirectedToHttps(): array
271
    {
272
        return [
273
            [
274
                'http://packagist.org',
275
                true,
276
            ]
277
        ];
278
    }
279
}