HttpTest::buildQueryStringMultiValue()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace TraderInteractive\Util;
4
5
use PHPUnit\Framework\TestCase;
6
7
/**
8
 * @coversDefaultClass \TraderInteractive\Util\Http
9
 */
10
final class HttpTest extends TestCase
11
{
12
    /**
13
     * @test
14
     * @group unit
15
     * @covers ::parseHeaders
16
     *
17
     * @return void
18
     */
19
    public function parseHeadersBasicUsage()
20
    {
21
        $headers = 'Content-Type: text/json';
22
        $result = Http::parseHeaders($headers);
23
        $this->assertSame(['Content-Type' => 'text/json'], $result);
24
    }
25
26
    /**
27
     * @test
28
     * @group unit
29
     * @covers ::parseHeaders
30
     *
31
     * @return void
32
     */
33
    public function parseHeadersMalformed()
34
    {
35
        try {
36
            $headers = "&some\r\nbad+headers";
37
            Http::parseHeaders($headers);
38
            $this->fail('No exception thrown');
39
        } catch (\Exception $e) {
40
            $this->assertSame('Unsupported header format: &some', $e->getMessage());
41
        }
42
    }
43
44
    /**
45
     * Verifies parseHeaders retains the functionality of http_parse_headers()
46
     *
47
     * @test
48
     * @group unit
49
     * @covers ::parseHeaders
50
     *
51
     * @return void
52
     */
53
    public function parseHeadersPeclHttpFunctionality()
54
    {
55
        $headers = <<<EOT
56
HTTP/1.1 200 OK\r\n
57
content-type: text/html; charset=UTF-8\r\n
58
Server: Funky/1.0\r\n
59
Set-Cookie: foo=bar\r\n
60
Set-Cookie: baz=quux\r\n
61
Set-Cookie: key=value\r\n
62
EOT;
63
        $expected = [
64
            'Response Code' => 200,
65
            'Response Status' => 'OK',
66
            'Content-Type' => 'text/html; charset=UTF-8',
67
            'Server' => 'Funky/1.0',
68
            'Set-Cookie' => ['foo=bar', 'baz=quux', 'key=value'],
69
        ];
70
        $result = Http::parseHeaders($headers);
71
        $this->assertSame($expected, $result);
72
    }
73
74
    /**
75
     * Verifies Request Method and Request Url are set properly
76
     *
77
     * @test
78
     * @group unit
79
     * @covers ::parseHeaders
80
     *
81
     * @return void
82
     */
83
    public function parseHeadersMethodAndUrlSet()
84
    {
85
        $headers = <<<EOT
86
GET /file.xml HTTP/1.1\r\n
87
Host: www.example.com\r\n
88
Accept: */*\r\n
89
EOT;
90
        $expected = [
91
            'Request Method' => 'GET',
92
            'Request Url' => '/file.xml',
93
            'Host' => 'www.example.com',
94
            'Accept' => '*/*'
95
        ];
96
        $result = Http::parseHeaders($headers);
97
        $this->assertSame($expected, $result);
98
    }
99
100
    /**
101
     * Verifies that the rawHeaders string cannot be whitespace.
102
     *
103
     * @test
104
     * @group unit
105
     * @covers ::parseHeaders
106
     *
107
     * @return void
108
     */
109
    public function parseHeadersWhitespace()
110
    {
111
        $this->expectException(\InvalidArgumentException::class);
112
        $this->expectExceptionMessage('$rawHeaders cannot be whitespace');
113
        Http::parseHeaders('');
114
    }
115
116
    /**
117
     * @test
118
     * @covers ::buildQueryString
119
     *
120
     * @return void
121
     */
122
    public function buildQueryStringBasicUse()
123
    {
124
        $data = [
125
            'foo' => 'bar',
126
            'baz' => 'boom',
127
            'cow' => 'milk',
128
            'php' => 'hypertext processor',
129
            'theFalse' => false,
130
            'theTrue' => true
131
        ];
132
133
        $this->assertSame(
134
            'foo=bar&baz=boom&cow=milk&php=hypertext%20processor&theFalse=false&theTrue=true',
135
            Http::buildQueryString($data)
136
        );
137
    }
138
139
    /**
140
     * @test
141
     * @covers ::buildQueryString
142
     *
143
     * @return void
144
     */
145
    public function buildQueryStringMultiValue()
146
    {
147
        $data = ['param1' => ['value', 'another value'], 'param2' => 'a value'];
148
149
        $this->assertSame('param1=value&param1=another%20value&param2=a%20value', Http::buildQueryString($data));
150
    }
151
152
    /**
153
     * @test
154
     * @covers ::buildQueryString
155
     *
156
     * @return void
157
     */
158
    public function buildQueryStringComplexValues()
159
    {
160
        $this->assertSame(
161
            'a%20b%20c=1%242%283&a%20b%20c=4%295%2A6',
162
            Http::buildQueryString(['a b c' => ['1$2(3', '4)5*6']])
163
        );
164
    }
165
166
    /**
167
     * Verifies Multi Parameter Method can handle a normal url
168
     *
169
     * @test
170
     * @group unit
171
     * @covers ::getQueryParams
172
     *
173
     * @return void
174
     */
175
    public function getQueryParamsNormal()
176
    {
177
        $url = 'http://foo.com/bar/?otherStuff=green&stuff=yeah&moreStuff=rock&moreStuff=jazz&otherStuff=blue&'
178
             . 'otherStuff=black';
179
        $expected = [
180
            'otherStuff' => ['green', 'blue', 'black'],
181
            'stuff' => ['yeah'],
182
            'moreStuff' => ['rock', 'jazz'],
183
        ];
184
        $result = Http::getQueryParams($url);
185
        $this->assertSame($expected, $result);
186
    }
187
188
    /**
189
     * Verifies Multi Parameter Method can handle a url with an empty parameter
190
     *
191
     * @test
192
     * @group unit
193
     * @covers ::getQueryParams
194
     *
195
     * @return void
196
     */
197
    public function getQueryParamsEmptyParameter()
198
    {
199
        $url = 'http://foo.com/bar/?stuff=yeah&moreStuff=&moreStuff=jazz&otherStuff';
200
        $expected = [
201
            'stuff' => ['yeah'],
202
            'moreStuff' => ['', 'jazz'],
203
            'otherStuff' => [''],
204
        ];
205
        $result = Http::getQueryParams($url);
206
        $this->assertSame($expected, $result);
207
    }
208
209
    /**
210
     * Verifies multi parameter method with a garbage query string
211
     *
212
     * @test
213
     * @group unit
214
     * @covers ::getQueryParams
215
     *
216
     * @return void
217
     */
218
    public function getQueryParamsGarbage()
219
    {
220
        $this->assertSame([], Http::getQueryParams('GARBAGE'));
221
    }
222
223
    /**
224
     * @test
225
     * @covers ::getQueryParams
226
     *
227
     * @return void
228
     */
229
    public function getQueryParamsWithCollapsed()
230
    {
231
        $result = Http::getQueryParams('http://foo.com/bar/?stuff=yeah&moreStuff=mhmm', ['stuff', 'notThere']);
232
        $this->assertSame(['stuff' => 'yeah', 'moreStuff' => ['mhmm']], $result);
233
    }
234
235
    /**
236
     * @test
237
     * @covers ::getQueryParams
238
     *
239
     * @return void
240
     */
241
    public function getQueryParamsCollapsedMoreThanOneValue()
242
    {
243
        $this->expectException(\Exception::class);
244
        $this->expectExceptionMessage('Parameter \'stuff\' had more than one value but in $collapsedParams');
245
        Http::getQueryParams('http://foo.com/bar/?stuff=yeah&stuff=boy&moreStuff=mhmm', ['stuff']);
246
    }
247
248
    /**
249
     * @test
250
     * @covers ::getQueryParamsCollapsed
251
     *
252
     * @return void
253
     */
254
    public function getQueryParamsCollapsed()
255
    {
256
        $url = 'http://foo.com/bar/?boo=1&foo=bar&boo=2';
257
        $actual = Http::getQueryParamsCollapsed($url, ['boo']);
258
        $this->assertSame(['boo' => ['1', '2'], 'foo' => 'bar'], $actual);
259
    }
260
261
    /**
262
     * @test
263
     * @covers ::getQueryParamsCollapsed
264
     *
265
     * @return void
266
     */
267
    public function getQueryParamsCollapsedUnexpectedArray()
268
    {
269
        $this->expectException(\Exception::class);
270
        $this->expectExceptionMessage('Parameter \'boo\' is not expected to be an array, but array given');
271
        $url = 'http://foo.com/bar/?boo=1&foo=bar&boo=2';
272
        Http::getQueryParamsCollapsed($url);
273
    }
274
275
    /**
276
     * Verifies multi parameter method with a garbage query string
277
     *
278
     * @test
279
     * @covers ::getQueryParamsCollapsed
280
     *
281
     * @return void
282
     */
283
    public function getQueryParamsCollapsedGarbage()
284
    {
285
        $this->assertSame([], Http::getQueryParamsCollapsed('GARBAGE'));
286
    }
287
288
    /**
289
     * Verifies Multi Parameter Method can handle a url with an empty parameter
290
     *
291
     * @test
292
     * @covers ::getQueryParamsCollapsed
293
     *
294
     * @return void
295
     */
296
    public function getQueryParamsCollapsedEmptyParameter()
297
    {
298
        $url = 'http://foo.com/bar/?stuff=yeah&moreStuff=&moreStuff=jazz&otherStuff';
299
        $expected = ['stuff' => 'yeah', 'moreStuff' => ['', 'jazz'], 'otherStuff' => ''];
300
        $this->assertSame($expected, Http::getQueryParamsCollapsed($url, ['moreStuff']));
301
    }
302
}
303