Debian Codegraph @

tests/phpunit/unit/includes/config/ServiceOptionsTest.php

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