Merge "SiteStats::jobs fix when there is a single job"
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / http / HttpAcceptNegotiatorTest.php
1 <?php
2
3 use Wikimedia\Http\HttpAcceptNegotiator;
4
5 /**
6 * @covers Wikimedia\Http\HttpAcceptNegotiator
7 *
8 * @license GPL-2.0+
9 * @author Daniel Kinzler
10 */
11 class HttpAcceptNegotiatorTest extends \PHPUnit_Framework_TestCase {
12
13 public function provideGetFirstSupportedValue() {
14 return [
15 [ // #0: empty
16 [], // supported
17 [], // accepted
18 null, // default
19 null, // expected
20 ],
21 [ // #1: simple
22 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
23 [ 'text/xzy', 'text/bar' ], // accepted
24 null, // default
25 'text/BAR', // expected
26 ],
27 [ // #2: default
28 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
29 [ 'text/xzy', 'text/xoo' ], // accepted
30 'X', // default
31 'X', // expected
32 ],
33 [ // #3: preference
34 [ 'text/foo', 'text/bar', 'application/zuul' ], // supported
35 [ 'text/xoo', 'text/BAR', 'text/foo' ], // accepted
36 null, // default
37 'text/bar', // expected
38 ],
39 [ // #4: * wildcard
40 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
41 [ 'text/xoo', '*' ], // accepted
42 null, // default
43 'text/foo', // expected
44 ],
45 [ // #5: */* wildcard
46 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
47 [ 'text/xoo', '*/*' ], // accepted
48 null, // default
49 'text/foo', // expected
50 ],
51 [ // #6: text/* wildcard
52 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
53 [ 'application/*', 'text/foo' ], // accepted
54 null, // default
55 'application/zuul', // expected
56 ],
57 ];
58 }
59
60 /**
61 * @dataProvider provideGetFirstSupportedValue
62 */
63 public function testGetFirstSupportedValue( $supported, $accepted, $default, $expected ) {
64 $negotiator = new HttpAcceptNegotiator( $supported );
65 $actual = $negotiator->getFirstSupportedValue( $accepted, $default );
66
67 $this->assertEquals( $expected, $actual );
68 }
69
70 public function provideGetBestSupportedKey() {
71 return [
72 [ // #0: empty
73 [], // supported
74 [], // accepted
75 null, // default
76 null, // expected
77 ],
78 [ // #1: simple
79 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
80 [ 'text/xzy' => 1, 'text/bar' => 0.5 ], // accepted
81 null, // default
82 'text/BAR', // expected
83 ],
84 [ // #2: default
85 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
86 [ 'text/xzy' => 1, 'text/xoo' => 0.5 ], // accepted
87 'X', // default
88 'X', // expected
89 ],
90 [ // #3: weighted
91 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
92 [ 'text/foo' => 0.3, 'text/BAR' => 0.8, 'application/zuul' => 0.5 ], // accepted
93 null, // default
94 'text/BAR', // expected
95 ],
96 [ // #4: zero weight
97 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
98 [ 'text/foo' => 0, 'text/xoo' => 1 ], // accepted
99 null, // default
100 null, // expected
101 ],
102 [ // #5: * wildcard
103 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
104 [ 'text/xoo' => 0.5, '*' => 0.1 ], // accepted
105 null, // default
106 'text/foo', // expected
107 ],
108 [ // #6: */* wildcard
109 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
110 [ 'text/xoo' => 0.5, '*/*' => 0.1 ], // accepted
111 null, // default
112 'text/foo', // expected
113 ],
114 [ // #7: text/* wildcard
115 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
116 [ 'text/foo' => 0.3, 'application/*' => 0.8 ], // accepted
117 null, // default
118 'application/zuul', // expected
119 ],
120 [ // #8: Test specific format preferred over wildcard (T133314)
121 [ 'application/rdf+xml', 'text/json', 'text/html' ], // supported
122 [ '*/*' => 1, 'text/html' => 1 ], // accepted
123 null, // default
124 'text/html', // expected
125 ],
126 [ // #9: Test specific format preferred over range (T133314)
127 [ 'application/rdf+xml', 'text/json', 'text/html' ], // supported
128 [ 'text/*' => 1, 'text/html' => 1 ], // accepted
129 null, // default
130 'text/html', // expected
131 ],
132 [ // #10: Test range preferred over wildcard (T133314)
133 [ 'application/rdf+xml', 'text/html' ], // supported
134 [ '*/*' => 1, 'text/*' => 1 ], // accepted
135 null, // default
136 'text/html', // expected
137 ],
138 ];
139 }
140
141 /**
142 * @dataProvider provideGetBestSupportedKey
143 */
144 public function testGetBestSupportedKey( $supported, $accepted, $default, $expected ) {
145 $negotiator = new HttpAcceptNegotiator( $supported );
146 $actual = $negotiator->getBestSupportedKey( $accepted, $default );
147
148 $this->assertEquals( $expected, $actual );
149 }
150
151 }