3 use MediaWiki\Config\ServiceOptions
;
6 * @coversDefaultClass \MediaWiki\Config\ServiceOptions
8 class ServiceOptionsTest
extends MediaWikiTestCase
{
9 public static $testObj;
11 public static function setUpBeforeClass() {
12 parent
::setUpBeforeClass();
14 self
::$testObj = new stdclass();
18 * @dataProvider provideConstructor
19 * @covers ::__construct
20 * @covers ::assertRequiredOptions
23 public function testConstructor( $expected, $keys, ...$sources ) {
24 $options = new ServiceOptions( $keys, ...$sources );
26 foreach ( $expected as $key => $val ) {
27 $this->assertSame( $val, $options->get( $key ) );
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 ) );
34 // Suppress warning if no assertions were run. This is expected for empty arguments.
35 $this->assertTrue( true );
38 public function provideConstructor() {
40 'No keys' => [ [], [], [ 'a' => 'aval' ] ],
41 'Simple array source' => [
42 [ 'a' => 'aval', 'b' => 'bval' ],
44 [ 'a' => 'aval', 'b' => 'bval', 'c' => 'cval' ],
46 'Simple HashConfig source' => [
47 [ 'a' => 'aval', 'b' => 'bval' ],
49 new HashConfig( [ 'a' => 'aval', 'b' => 'bval', 'c' => 'cval' ] ),
51 'Three different sources' => [
52 [ 'a' => 'aval', 'b' => 'bval' ],
55 new HashConfig( [ 'a' => 'aval', 'c' => 'cval' ] ),
56 [ 'b' => 'bval', 'd' => 'dval' ],
63 'Numeric option name' => [
68 'Multiple sources for one key' => [
72 [ 'a' => 'second place' ],
74 'Object value is passed by reference' => [
75 [ 'a' => self
::$testObj ],
77 [ 'a' => self
::$testObj ],
83 * @covers ::__construct
85 public function testKeyNotFound() {
86 $this->setExpectedException( InvalidArgumentException
::class,
87 'Key "a" not found in input sources' );
89 new ServiceOptions( [ 'a' ], [ 'b' => 'bval' ], [ 'c' => 'cval' ] );
93 * @covers ::__construct
94 * @covers ::assertRequiredOptions
96 public function testOutOfOrderAssertRequiredOptions() {
97 $options = new ServiceOptions( [ 'a', 'b' ], [ 'a' => '', 'b' => '' ] );
98 $options->assertRequiredOptions( [ 'b', 'a' ] );
99 $this->assertTrue( true, 'No exception thrown' );
103 * @covers ::__construct
106 public function testGetUnrecognized() {
107 $this->setExpectedException( InvalidArgumentException
::class,
108 'Unrecognized option "b"' );
110 $options = new ServiceOptions( [ 'a' ], [ 'a' => '' ] );
111 $options->get( 'b' );
115 * @covers ::__construct
116 * @covers ::assertRequiredOptions
118 public function testExtraKeys() {
119 $this->setExpectedException( Wikimedia\Assert\PreconditionException
::class,
120 'Precondition failed: Unsupported options passed: b, c!' );
122 $options = new ServiceOptions( [ 'a', 'b', 'c' ], [ 'a' => '', 'b' => '', 'c' => '' ] );
123 $options->assertRequiredOptions( [ 'a' ] );
127 * @covers ::__construct
128 * @covers ::assertRequiredOptions
130 public function testMissingKeys() {
131 $this->setExpectedException( Wikimedia\Assert\PreconditionException
::class,
132 'Precondition failed: Required options missing: a, b!' );
134 $options = new ServiceOptions( [ 'c' ], [ 'c' => '' ] );
135 $options->assertRequiredOptions( [ 'a', 'b', 'c' ] );
139 * @covers ::__construct
140 * @covers ::assertRequiredOptions
142 public function testExtraAndMissingKeys() {
143 $this->setExpectedException( Wikimedia\Assert\PreconditionException
::class,
144 'Precondition failed: Unsupported options passed: b! Required options missing: c!' );
146 $options = new ServiceOptions( [ 'a', 'b' ], [ 'a' => '', 'b' => '' ] );
147 $options->assertRequiredOptions( [ 'a', 'c' ] );