File: ServiceOptionsTest.php

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (124 lines) | stat: -rw-r--r-- 3,872 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php

use MediaWiki\Config\HashConfig;
use MediaWiki\Config\ServiceOptions;

/**
 * @covers \MediaWiki\Config\ServiceOptions
 */
class ServiceOptionsTest extends MediaWikiUnitTestCase {

	/**
	 * @dataProvider provideConstructor
	 */
	public function testConstructor( $expected, $keys, ...$sources ) {
		$options = new ServiceOptions( $keys, ...$sources );

		foreach ( $expected as $key => $val ) {
			$this->assertSame( $val, $options->get( $key ) );
		}

		// This is lumped in the same test because there's no support for depending on a test that
		// has a data provider.
		$options->assertRequiredOptions( array_keys( $expected ) );

		// Suppress warning if no assertions were run. This is expected for empty arguments.
		$this->assertTrue( true );
	}

	public static function provideConstructor() {
		$testObj = (object)[];
		return [
			'No keys' => [ [], [], [ 'a' => 'aval' ] ],
			'Simple array source' => [
				[ 'a' => 'aval', 'b' => 'bval' ],
				[ 'a', 'b' ],
				[ 'a' => 'aval', 'b' => 'bval', 'c' => 'cval' ],
			],
			'Simple HashConfig source' => [
				[ 'a' => 'aval', 'b' => 'bval' ],
				[ 'a', 'b' ],
				new HashConfig( [ 'a' => 'aval', 'b' => 'bval', 'c' => 'cval' ] ),
			],
			'Simple ServiceOptions source' => [
				[ 'a' => 'aval', 'b' => 'bval' ],
				[ 'a', 'b' ],
				new ServiceOptions( [ 'a', 'b', 'c' ], [ 'a' => 'aval', 'b' => 'bval', 'c' => 'cval' ] ),
			],
			'Three different sources' => [
				[ 'a' => 'aval', 'b' => 'bval' ],
				[ 'a', 'b' ],
				[ 'z' => 'zval' ],
				new HashConfig( [ 'a' => 'aval', 'c' => 'cval' ] ),
				new ServiceOptions( [ 'b', 'd' ], [ 'b' => 'bval', 'd' => 'dval' ] ),
			],
			'null key' => [
				[ 'a' => null ],
				[ 'a' ],
				[ 'a' => null ],
			],
			'Numeric option name' => [
				[ '0' => 'nothing' ],
				[ '0' ],
				[ '0' => 'nothing' ],
			],
			'Multiple sources for one key' => [
				[ 'a' => 'winner' ],
				[ 'a' ],
				[ 'a' => 'winner' ],
				[ 'a' => 'second place' ],
			],
			'Object value is passed by reference' => [
				[ 'a' => $testObj ],
				[ 'a' ],
				[ 'a' => $testObj ],
			],
		];
	}

	public function testKeyNotFound() {
		$this->expectException( InvalidArgumentException::class );
		$this->expectExceptionMessage( 'Key "a" not found in input sources' );

		new ServiceOptions( [ 'a' ], [ 'b' => 'bval' ], [ 'c' => 'cval' ] );
	}

	public function testOutOfOrderAssertRequiredOptions() {
		$options = new ServiceOptions( [ 'a', 'b' ], [ 'a' => '', 'b' => '' ] );
		$options->assertRequiredOptions( [ 'b', 'a' ] );
		$this->assertTrue( true, 'No exception thrown' );
	}

	public function testGetUnrecognized() {
		$this->expectException( InvalidArgumentException::class );
		$this->expectExceptionMessage( 'Unrecognized option "b"' );

		$options = new ServiceOptions( [ 'a' ], [ 'a' => '' ] );
		$options->get( 'b' );
	}

	public function testExtraKeys() {
		$this->expectException( Wikimedia\Assert\PreconditionException::class );
		$this->expectExceptionMessage( 'Precondition failed: Unsupported options passed: b, c!' );

		$options = new ServiceOptions( [ 'a', 'b', 'c' ], [ 'a' => '', 'b' => '', 'c' => '' ] );
		$options->assertRequiredOptions( [ 'a' ] );
	}

	public function testMissingKeys() {
		$this->expectException( Wikimedia\Assert\PreconditionException::class );
		$this->expectExceptionMessage( 'Precondition failed: Required options missing: a, b!' );

		$options = new ServiceOptions( [ 'c' ], [ 'c' => '' ] );
		$options->assertRequiredOptions( [ 'a', 'b', 'c' ] );
	}

	public function testExtraAndMissingKeys() {
		$this->expectException( Wikimedia\Assert\PreconditionException::class );
		$this->expectExceptionMessage(
			'Precondition failed: Unsupported options passed: b! Required options missing: c!' );

		$options = new ServiceOptions( [ 'a', 'b' ], [ 'a' => '', 'b' => '' ] );
		$options->assertRequiredOptions( [ 'a', 'c' ] );
	}
}