MediaWiki REL1_31
HttpAcceptNegotiatorTest.php
Go to the documentation of this file.
1<?php
2
4
10class HttpAcceptNegotiatorTest extends \PHPUnit\Framework\TestCase {
11
13 return [
14 [ // #0: empty
15 [], // supported
16 [], // accepted
17 null, // default
18 null, // expected
19 ],
20 [ // #1: simple
21 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
22 [ 'text/xzy', 'text/bar' ], // accepted
23 null, // default
24 'text/BAR', // expected
25 ],
26 [ // #2: default
27 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
28 [ 'text/xzy', 'text/xoo' ], // accepted
29 'X', // default
30 'X', // expected
31 ],
32 [ // #3: preference
33 [ 'text/foo', 'text/bar', 'application/zuul' ], // supported
34 [ 'text/xoo', 'text/BAR', 'text/foo' ], // accepted
35 null, // default
36 'text/bar', // expected
37 ],
38 [ // #4: * wildcard
39 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
40 [ 'text/xoo', '*' ], // accepted
41 null, // default
42 'text/foo', // expected
43 ],
44 [ // #5: */* wildcard
45 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
46 [ 'text/xoo', '*/*' ], // accepted
47 null, // default
48 'text/foo', // expected
49 ],
50 [ // #6: text/* wildcard
51 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
52 [ 'application/*', 'text/foo' ], // accepted
53 null, // default
54 'application/zuul', // expected
55 ],
56 ];
57 }
58
62 public function testGetFirstSupportedValue( $supported, $accepted, $default, $expected ) {
63 $negotiator = new HttpAcceptNegotiator( $supported );
64 $actual = $negotiator->getFirstSupportedValue( $accepted, $default );
65
66 $this->assertEquals( $expected, $actual );
67 }
68
69 public function provideGetBestSupportedKey() {
70 return [
71 [ // #0: empty
72 [], // supported
73 [], // accepted
74 null, // default
75 null, // expected
76 ],
77 [ // #1: simple
78 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
79 [ 'text/xzy' => 1, 'text/bar' => 0.5 ], // accepted
80 null, // default
81 'text/BAR', // expected
82 ],
83 [ // #2: default
84 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
85 [ 'text/xzy' => 1, 'text/xoo' => 0.5 ], // accepted
86 'X', // default
87 'X', // expected
88 ],
89 [ // #3: weighted
90 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
91 [ 'text/foo' => 0.3, 'text/BAR' => 0.8, 'application/zuul' => 0.5 ], // accepted
92 null, // default
93 'text/BAR', // expected
94 ],
95 [ // #4: zero weight
96 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
97 [ 'text/foo' => 0, 'text/xoo' => 1 ], // accepted
98 null, // default
99 null, // expected
100 ],
101 [ // #5: * wildcard
102 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
103 [ 'text/xoo' => 0.5, '*' => 0.1 ], // accepted
104 null, // default
105 'text/foo', // expected
106 ],
107 [ // #6: */* wildcard
108 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
109 [ 'text/xoo' => 0.5, '*/*' => 0.1 ], // accepted
110 null, // default
111 'text/foo', // expected
112 ],
113 [ // #7: text/* wildcard
114 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
115 [ 'text/foo' => 0.3, 'application/*' => 0.8 ], // accepted
116 null, // default
117 'application/zuul', // expected
118 ],
119 [ // #8: Test specific format preferred over wildcard (T133314)
120 [ 'application/rdf+xml', 'text/json', 'text/html' ], // supported
121 [ '*/*' => 1, 'text/html' => 1 ], // accepted
122 null, // default
123 'text/html', // expected
124 ],
125 [ // #9: Test specific format preferred over range (T133314)
126 [ 'application/rdf+xml', 'text/json', 'text/html' ], // supported
127 [ 'text/*' => 1, 'text/html' => 1 ], // accepted
128 null, // default
129 'text/html', // expected
130 ],
131 [ // #10: Test range preferred over wildcard (T133314)
132 [ 'application/rdf+xml', 'text/html' ], // supported
133 [ '*/*' => 1, 'text/*' => 1 ], // accepted
134 null, // default
135 'text/html', // expected
136 ],
137 ];
138 }
139
143 public function testGetBestSupportedKey( $supported, $accepted, $default, $expected ) {
144 $negotiator = new HttpAcceptNegotiator( $supported );
145 $actual = $negotiator->getBestSupportedKey( $accepted, $default );
146
147 $this->assertEquals( $expected, $actual );
148 }
149
150}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
Wikimedia\Http\HttpAcceptNegotiator.
testGetBestSupportedKey( $supported, $accepted, $default, $expected)
provideGetBestSupportedKey
testGetFirstSupportedValue( $supported, $accepted, $default, $expected)
provideGetFirstSupportedValue
Utility for negotiating a value from a set of supported values using a preference list.
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37