MediaWiki  1.30.0
HttpAcceptNegotiatorTest.php
Go to the documentation of this file.
1 <?php
2 
4 
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 
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 
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 }
HttpAcceptNegotiatorTest\provideGetBestSupportedKey
provideGetBestSupportedKey()
Definition: HttpAcceptNegotiatorTest.php:70
HttpAcceptNegotiatorTest\testGetBestSupportedKey
testGetBestSupportedKey( $supported, $accepted, $default, $expected)
provideGetBestSupportedKey
Definition: HttpAcceptNegotiatorTest.php:144
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
HttpAcceptNegotiatorTest
Wikimedia\Http\HttpAcceptNegotiator.
Definition: HttpAcceptNegotiatorTest.php:11
php
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:35
Wikimedia\Http\HttpAcceptNegotiator
Definition: HttpAcceptNegotiator.php:18
HttpAcceptNegotiatorTest\testGetFirstSupportedValue
testGetFirstSupportedValue( $supported, $accepted, $default, $expected)
provideGetFirstSupportedValue
Definition: HttpAcceptNegotiatorTest.php:63
HttpAcceptNegotiatorTest\provideGetFirstSupportedValue
provideGetFirstSupportedValue()
Definition: HttpAcceptNegotiatorTest.php:13