|
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 include FIXTURE_PATH . '/headers/accept-preferred.php'; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* @covers Fracture\Http\Headers\Accept::__construct |
|
233
|
|
|
* @covers Fracture\Http\Headers\Accept::prepare |
|
234
|
|
|
* @covers Fracture\Http\Headers\Accept::getPreferred |
|
235
|
|
|
*/ |
|
236
|
|
|
public function testPreferredTypeComputionForEmptyHeaderValue() |
|
237
|
|
|
{ |
|
238
|
|
|
$instance = new Accept(''); |
|
239
|
|
|
$instance->prepare(); |
|
240
|
|
|
|
|
241
|
|
|
$this->assertNull($instance->getPreferred('application/json;version=2, application/json;version=3')); |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* @covers Fracture\Http\Headers\Accept::__construct |
|
246
|
|
|
* @covers Fracture\Http\Headers\Accept::getFormatedEntry |
|
247
|
|
|
* |
|
248
|
|
|
* @dataProvider provideEntriesForFormating |
|
249
|
|
|
*/ |
|
250
|
|
|
public function testFormatingofEntries($entry, $result) |
|
251
|
|
|
{ |
|
252
|
|
|
$instance = new Accept; |
|
253
|
|
|
$this->assertEquals($result, $instance->getFormatedEntry($entry)); |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
|
|
257
|
|
|
public function provideEntriesForFormating() |
|
258
|
|
|
{ |
|
259
|
|
|
return [ |
|
260
|
|
|
[ |
|
261
|
|
|
'entry' => ['value' => 'text/html'], |
|
262
|
|
|
'result' => 'text/html', |
|
263
|
|
|
], |
|
264
|
|
|
[ |
|
265
|
|
|
'entry' => ['value' => 'text/html', 'version' => '2'], |
|
266
|
|
|
'result' => 'text/html;version=2', |
|
267
|
|
|
], |
|
268
|
|
|
]; |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
|
|
272
|
|
|
/** |
|
273
|
|
|
* @covers Fracture\Http\Headers\Accept::__construct |
|
274
|
|
|
* @covers Fracture\Http\Headers\Accept::getName |
|
275
|
|
|
*/ |
|
276
|
|
|
public function testGivenName() |
|
277
|
|
|
{ |
|
278
|
|
|
$instance = new Accept; |
|
279
|
|
|
$this->assertSame('Accept', $instance->getName()); |
|
280
|
|
|
} |
|
281
|
|
|
} |
|
282
|
|
|
|