Completed
Push — master ( b5ed08...526d1c )
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
     * @covers Fracture\Http\Headers\Accept::sortBySpecificity
214
     * @covers Fracture\Http\Headers\Accept::computeSpecificity
215
     *
216
     * @dataProvider provideTypesForComputation
217
     */
218
    public function testPreferredTypeCompution($header, $available, $expected)
219
    {
220
        $instance = new Accept($header);
221
        $instance->prepare();
222
223
        $this->assertEquals($expected, $instance->getPreferred($available));
224
    }
225
226
227
    public function provideTypesForComputation()
228
    {
229
        return include FIXTURE_PATH . '/headers/accept-preferred.php';
230
    }
231
232
233
    /**
234
     * @covers Fracture\Http\Headers\Accept::__construct
235
     * @covers Fracture\Http\Headers\Accept::prepare
236
     * @covers Fracture\Http\Headers\Accept::getPreferred
237
     *
238
     * @covers Fracture\Http\Headers\Accept::sortBySpecificity
239
     * @covers Fracture\Http\Headers\Accept::computeSpecificity
240
     */
241
    public function testPreferredTypeComputionForEmptyHeaderValue()
242
    {
243
        $instance = new Accept('');
244
        $instance->prepare();
245
246
        $this->assertNull($instance->getPreferred('application/json;version=2, application/json;version=3'));
247
    }
248
249
    /**
250
     * @covers Fracture\Http\Headers\Accept::__construct
251
     * @covers Fracture\Http\Headers\Accept::getFormatedEntry
252
     *
253
     * @dataProvider provideEntriesForFormating
254
     */
255
    public function testFormatingofEntries($entry, $result)
256
    {
257
        $instance = new Accept;
258
        $this->assertEquals($result, $instance->getFormatedEntry($entry));
259
    }
260
261
262
    public function provideEntriesForFormating()
263
    {
264
        return [
265
            [
266
                'entry' => ['value' => 'text/html'],
267
                'result' => 'text/html',
268
            ],
269
            [
270
                'entry' => ['value' => 'text/html', 'version' => '2'],
271
                'result' => 'text/html;version=2',
272
            ],
273
        ];
274
    }
275
276
277
    /**
278
     * @covers Fracture\Http\Headers\Accept::__construct
279
     * @covers Fracture\Http\Headers\Accept::getName
280
     */
281
    public function testGivenName()
282
    {
283
        $instance = new Accept;
284
        $this->assertSame('Accept', $instance->getName());
285
    }
286
}
287