Merge "rdbms: avoid LoadBalancer::getConnection waste when using $groups"
[lhc/web/wiklou.git] / tests / phpunit / includes / config / ServiceOptionsTest.php
1 <?php
2
3 use MediaWiki\Config\ServiceOptions;
4
5 /**
6 * @coversDefaultClass \MediaWiki\Config\ServiceOptions
7 */
8 class ServiceOptionsTest extends MediaWikiTestCase {
9 public static $testObj;
10
11 public static function setUpBeforeClass() {
12 parent::setUpBeforeClass();
13
14 self::$testObj = new stdclass();
15 }
16
17 /**
18 * @dataProvider provideConstructor
19 * @covers ::__construct
20 * @covers ::assertRequiredOptions
21 * @covers ::get
22 */
23 public function testConstructor( $expected, $keys, ...$sources ) {
24 $options = new ServiceOptions( $keys, ...$sources );
25
26 foreach ( $expected as $key => $val ) {
27 $this->assertSame( $val, $options->get( $key ) );
28 }
29
30 // This is lumped in the same test because there's no support for depending on a test that
31 // has a data provider.
32 $options->assertRequiredOptions( array_keys( $expected ) );
33
34 // Suppress warning if no assertions were run. This is expected for empty arguments.
35 $this->assertTrue( true );
36 }
37
38 public function provideConstructor() {
39 return [
40 'No keys' => [ [], [], [ 'a' => 'aval' ] ],
41 'Simple array source' => [
42 [ 'a' => 'aval', 'b' => 'bval' ],
43 [ 'a', 'b' ],
44 [ 'a' => 'aval', 'b' => 'bval', 'c' => 'cval' ],
45 ],
46 'Simple HashConfig source' => [
47 [ 'a' => 'aval', 'b' => 'bval' ],
48 [ 'a', 'b' ],
49 new HashConfig( [ 'a' => 'aval', 'b' => 'bval', 'c' => 'cval' ] ),
50 ],
51 'Three different sources' => [
52 [ 'a' => 'aval', 'b' => 'bval' ],
53 [ 'a', 'b' ],
54 [ 'z' => 'zval' ],
55 new HashConfig( [ 'a' => 'aval', 'c' => 'cval' ] ),
56 [ 'b' => 'bval', 'd' => 'dval' ],
57 ],
58 'null key' => [
59 [ 'a' => null ],
60 [ 'a' ],
61 [ 'a' => null ],
62 ],
63 'Numeric option name' => [
64 [ '0' => 'nothing' ],
65 [ '0' ],
66 [ '0' => 'nothing' ],
67 ],
68 'Multiple sources for one key' => [
69 [ 'a' => 'winner' ],
70 [ 'a' ],
71 [ 'a' => 'winner' ],
72 [ 'a' => 'second place' ],
73 ],
74 'Object value is passed by reference' => [
75 [ 'a' => self::$testObj ],
76 [ 'a' ],
77 [ 'a' => self::$testObj ],
78 ],
79 ];
80 }
81
82 /**
83 * @covers ::__construct
84 */
85 public function testKeyNotFound() {
86 $this->setExpectedException( InvalidArgumentException::class,
87 'Key "a" not found in input sources' );
88
89 new ServiceOptions( [ 'a' ], [ 'b' => 'bval' ], [ 'c' => 'cval' ] );
90 }
91
92 /**
93 * @covers ::__construct
94 * @covers ::assertRequiredOptions
95 */
96 public function testOutOfOrderAssertRequiredOptions() {
97 $options = new ServiceOptions( [ 'a', 'b' ], [ 'a' => '', 'b' => '' ] );
98 $options->assertRequiredOptions( [ 'b', 'a' ] );
99 $this->assertTrue( true, 'No exception thrown' );
100 }
101
102 /**
103 * @covers ::__construct
104 * @covers ::get
105 */
106 public function testGetUnrecognized() {
107 $this->setExpectedException( InvalidArgumentException::class,
108 'Unrecognized option "b"' );
109
110 $options = new ServiceOptions( [ 'a' ], [ 'a' => '' ] );
111 $options->get( 'b' );
112 }
113
114 /**
115 * @covers ::__construct
116 * @covers ::assertRequiredOptions
117 */
118 public function testExtraKeys() {
119 $this->setExpectedException( Wikimedia\Assert\PreconditionException::class,
120 'Precondition failed: Unsupported options passed: b, c!' );
121
122 $options = new ServiceOptions( [ 'a', 'b', 'c' ], [ 'a' => '', 'b' => '', 'c' => '' ] );
123 $options->assertRequiredOptions( [ 'a' ] );
124 }
125
126 /**
127 * @covers ::__construct
128 * @covers ::assertRequiredOptions
129 */
130 public function testMissingKeys() {
131 $this->setExpectedException( Wikimedia\Assert\PreconditionException::class,
132 'Precondition failed: Required options missing: a, b!' );
133
134 $options = new ServiceOptions( [ 'c' ], [ 'c' => '' ] );
135 $options->assertRequiredOptions( [ 'a', 'b', 'c' ] );
136 }
137
138 /**
139 * @covers ::__construct
140 * @covers ::assertRequiredOptions
141 */
142 public function testExtraAndMissingKeys() {
143 $this->setExpectedException( Wikimedia\Assert\PreconditionException::class,
144 'Precondition failed: Unsupported options passed: b! Required options missing: c!' );
145
146 $options = new ServiceOptions( [ 'a', 'b' ], [ 'a' => '', 'b' => '' ] );
147 $options->assertRequiredOptions( [ 'a', 'c' ] );
148 }
149 }