Completed
Push — master ( 2d7110...752264 )
by Mārtiņš
8s
created

AcceptTest::testAcceptPrecedence()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
1
<?php
2
3
4
namespace Fracture\Http\Headers;
5
6
use Exception;
7
use ReflectionClass;
8
use PHPUnit_Framework_TestCase;
9
10
class AcceptTest extends PHPUnit_Framework_TestCase
11
{
12
13
    /**
14
     * @covers Fracture\Http\Headers\Accept::__construct
15
     * @covers Fracture\Http\Headers\Accept::prepare
16
     * @covers Fracture\Http\Headers\Accept::getParsedData
17
     */
18
    public function testEmptyInstance()
19
    {
20
        $instance = new Accept;
21
        $instance->prepare();
22
23
        $this->assertEquals(null, $instance->getParsedData());
24
    }
25
26
27
    /**
28
     * @covers Fracture\Http\Headers\Accept::__construct
29
     * @covers Fracture\Http\Headers\Accept::prepare
30
     * @covers Fracture\Http\Headers\Accept::getParsedData
31
     * @covers Fracture\Http\Headers\Accept::extractData
32
     *
33
     * @covers Fracture\Http\Headers\Accept::obtainGroupedElements
34
     * @covers Fracture\Http\Headers\Accept::obtainSortedQualityList
35
     * @covers Fracture\Http\Headers\Accept::obtainSortedElements
36
     * @covers Fracture\Http\Headers\Accept::obtainAssessedItem
37
     *
38
     * @dataProvider provideSimpleAccepts
39
     */
40
    public function testSimpleAccepts($input, $expected)
41
    {
42
        $instance = new Accept($input);
43
        $instance->prepare();
44
45
        $this->assertEquals($expected, $instance->getParsedData());
46
    }
47
48
49
    public function provideSimpleAccepts()
50
    {
51
        return [
52
            [
53
                'input' => 'text/html',
54
                'expected' => [['value' => 'text/html']],
55
            ],
56
            [
57
                'input' => 'text/html;version=2',
58
                'expected' => [['value' => 'text/html', 'version' => '2']],
59
            ],
60
            [
61
                'input' => 'text/html;foo=bar; q=0.6',
62
                'expected' => [['value' => 'text/html', 'foo' => 'bar']],
63
            ],
64
            [
65
                'input' => 'application/json;version=1, application/json, */*;q=0.5',
66
                'expected' => [
67
                    ['value' => 'application/json', 'version' => '1'],
68
                    ['value' => 'application/json'], ['value' => '*/*']
69
                ],
70
            ],
71
            [
72
                'input' => 'application/json;version=1, test/test;q=0.8, application/json, */*;q=0.5',
73
                'expected' => [
74
                    ['value' => 'application/json', 'version' => '1'],
75
                    ['value' => 'application/json'],
76
                    ['value' => 'test/test'],
77
                    ['value' => '*/*']
78
                ],
79
            ],
80
        ];
81
    }
82
83
84
    /**
85
     * @covers Fracture\Http\Headers\Accept::__construct
86
     * @covers Fracture\Http\Headers\Accept::prepare
87
     * @covers Fracture\Http\Headers\Accept::getParsedData
88
     * @covers Fracture\Http\Headers\Accept::extractData
89
     *
90
     * @covers Fracture\Http\Headers\Accept::obtainGroupedElements
91
     * @covers Fracture\Http\Headers\Accept::obtainSortedQualityList
92
     * @covers Fracture\Http\Headers\Accept::obtainSortedElements
93
     * @covers Fracture\Http\Headers\Accept::obtainAssessedItem
94
     *
95
     * @dataProvider provideAcceptPrecedence
96
     */
97
    public function testAcceptPrecedence($input, $expected)
98
    {
99
        $instance = new Accept($input);
100
        $instance->prepare();
101
102
        $this->assertEquals($expected, $instance->getParsedData());
103
    }
104
105
106
    public function provideAcceptPrecedence()
107
    {
108
        return [
109
            [
110
                'input' => 'type/subtype, type/subtype;param=1',
111
                'expected' => [
112
                    ['value' => 'type/subtype', 'param' => '1'],
113
                    ['value' => 'type/subtype'],
114
                ],
115
            ],
116
            [
117
                'input' => 'type/subtype;param=1, type/subtype',
118
                'expected' => [
119
                    ['value' => 'type/subtype', 'param' => '1'],
120
                    ['value' => 'type/subtype'],
121
                ],
122
            ],
123
            [
124
                'input' => 'text/*, text/html, text/html;level=1, */*',
125
                'expected' => [
126
                    ['value' => 'text/html', 'level' => '1'],
127
                    ['value' => 'text/html'],
128
                    ['value' => 'text/*'],
129
                    ['value' => '*/*'],
130
                ],
131
            ],
132
            [
133
                'input' => 'application/*, application/json;type=1, application/json; level=1; type=2, */*',
134
                'expected' => [
135
                    ['value' => 'application/json', 'level' => '1', 'type' => '2'],
136
                    ['value' => 'application/json', 'type' => '1'],
137
                    ['value' => 'application/*'],
138
                    ['value' => '*/*'],
139
                ],
140
            ],
141
        ];
142
    }
143
144
145
    /**
146
     * @covers Fracture\Http\Headers\Accept::__construct
147
     * @covers Fracture\Http\Headers\Accept::prepare
148
     * @covers Fracture\Http\Headers\Accept::getParsedData
149
     * @covers Fracture\Http\Headers\Accept::extractData
150
     *
151
     * @covers Fracture\Http\Headers\Accept::obtainGroupedElements
152
     * @covers Fracture\Http\Headers\Accept::obtainSortedQualityList
153
     * @covers Fracture\Http\Headers\Accept::obtainSortedElements
154
     * @covers Fracture\Http\Headers\Accept::obtainAssessedItem
155
     */
156
    public function testUseOfAlternativeValue()
157
    {
158
        $instance = new Accept('text/plain');
159
        $instance->prepare();
160
161
        $this->assertEquals([['value' => 'text/plain']], $instance->getParsedData());
162
163
        $instance->setValue('text/html');
164
        $instance->prepare();
165
166
        $this->assertEquals([['value' => 'text/html']], $instance->getParsedData());
167
    }
168
169
170
    /**
171
     * @covers Fracture\Http\Headers\Accept::__construct
172
     * @covers Fracture\Http\Headers\Accept::prepare
173
     * @covers Fracture\Http\Headers\Accept::contains
174
     *
175
     * @covers Fracture\Http\Headers\Accept::obtainAssessedItem
176
     * @covers Fracture\Http\Headers\Accept::matchFound
177
     * @covers Fracture\Http\Headers\Accept::isMatch
178
     * @covers Fracture\Http\Headers\Accept::replaceStars
179
     */
180
    public function testWhetherContainFindsExistingType()
181
    {
182
        $instance = new Accept('application/json;version=1;param=value, application/json');
183
        $instance->prepare();
184
185
        $this->assertTrue($instance->contains('application/json;param=value;version=1'));
186
        $this->assertFalse($instance->contains('application/json;version=value;param=1'));
187
    }
188
189
190
    /**
191
     * @covers Fracture\Http\Headers\Accept::__construct
192
     * @covers Fracture\Http\Headers\Accept::prepare
193
     * @covers Fracture\Http\Headers\Accept::contains
194
     */
195
    public function testContainsForEmptyValue()
196
    {
197
        $instance = new Accept('');
198
        $instance->prepare();
199
200
        $this->assertFalse($instance->contains('application/json'));
201
    }
202
203
204
    /**
205
     * @covers Fracture\Http\Headers\Accept::__construct
206
     * @covers Fracture\Http\Headers\Accept::prepare
207
     * @covers Fracture\Http\Headers\Accept::getPreferred
208
     *
209
     * @covers Fracture\Http\Headers\Accept::findFormatedEntry
210
     * @covers Fracture\Http\Headers\Accept::obtainEntryFromList
211
     * @covers Fracture\Http\Headers\Accept::getFormatedEntry
212
     * @covers Fracture\Http\Headers\Accept::replaceStars
213
     *
214
     * @dataProvider provideTypesForComputation
215
     */
216
    public function testPreferredTypeCompution($header, $available, $expected)
217
    {
218
        $instance = new Accept($header);
219
        $instance->prepare();
220
221
        $this->assertEquals($expected, $instance->getPreferred($available));
222
    }
223
224
225
    public function provideTypesForComputation()
226
    {
227
        return [
228
            [
229
                'header'    => 'application/json',
230
                'available' => 'application/json',
231
                'expected'  => 'application/json',
232
            ],
233
            [
234
                'header'    => '*/*',
235
                'available' => 'application/json',
236
                'expected'  => 'application/json',
237
            ],
238
            [
239
                'header'    => 'application/json;version=2',
240
                'available' => 'application/json;version=1',
241
                'expected'  => null,
242
            ],
243
            [
244
                'header'    => 'application/json',
245
                'available' => 'text/html, application/json',
246
                'expected'  => 'application/json',
247
            ],
248
            [
249
                'header'    => 'text/html;q=0.1, application/json',
250
                'available' => 'application/json',
251
                'expected'  => 'application/json',
252
            ],
253
            [
254
                'header'    => 'text/html, application/json',
255
                'available' => 'application/json',
256
                'expected'  => 'application/json',
257
            ],
258
            [
259
                'header'    => 'text/html;q=0.1, application/json;q=0.4',
260
                'available' => 'application/json',
261
                'expected'  => 'application/json',
262
            ],
263
            [
264
                'header'    => 'text/html, application/json, text/*',
265
                'available' => 'text/plain',
266
                'expected'  => 'text/plain',
267
            ],
268
            [
269
                'header'    => 'text/html, application/json',
270
                'available' => 'application/json, text/html',
271
                'expected'  => 'text/html',
272
            ],
273
            [
274
                'header'    => 'application/json',
275
                'available' => 'application/json;version=2',
276
                'expected'  => null,
277
            ],
278
            [
279
                'header'    => 'application/json;version=3, application/json',
280
                'available' => 'application/json;version=2, application/json',
281
                'expected'  => 'application/json',
282
            ],
283
            [
284
                'header'    => 'application/json;version=3, application/json',
285
                'available' => 'application/json;version=2, application/json;version=3',
286
                'expected'  => 'application/json;version=3',
287
            ],
288
        ];
289
    }
290
291
292
    /**
293
     * @covers Fracture\Http\Headers\Accept::__construct
294
     * @covers Fracture\Http\Headers\Accept::prepare
295
     * @covers Fracture\Http\Headers\Accept::getPreferred
296
     */
297
    public function testPreferredTypeComputionForEmptyHeaderValue()
298
    {
299
        $instance = new Accept('');
300
        $instance->prepare();
301
302
        $this->assertNull($instance->getPreferred('application/json;version=2, application/json;version=3'));
303
    }
304
305
    /**
306
     * @covers Fracture\Http\Headers\Accept::__construct
307
     * @covers Fracture\Http\Headers\Accept::getFormatedEntry
308
     *
309
     * @dataProvider provideEntriesForFormating
310
     */
311
    public function testFormatingofEntries($entry, $result)
312
    {
313
        $instance = new Accept;
314
        $this->assertEquals($result, $instance->getFormatedEntry($entry));
315
    }
316
317
318
    public function provideEntriesForFormating()
319
    {
320
        return [
321
            [
322
                'entry' => ['value' => 'text/html'],
323
                'result' => 'text/html',
324
            ],
325
            [
326
                'entry' => ['value' => 'text/html', 'version' => '2'],
327
                'result' => 'text/html;version=2',
328
            ],
329
        ];
330
    }
331
332
333
    /**
334
     * @covers Fracture\Http\Headers\Accept::__construct
335
     * @covers Fracture\Http\Headers\Accept::getName
336
     */
337
    public function testGivenName()
338
    {
339
        $instance = new Accept;
340
        $this->assertSame('Accept', $instance->getName());
341
    }
342
}
343