File: ServiceOptionsTest.php

package info (click to toggle)
mediawiki 1%3A1.35.13-1%2Bdeb11u2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 274,932 kB
  • sloc: php: 677,563; javascript: 572,709; sql: 11,565; python: 4,447; xml: 3,145; sh: 892; perl: 788; ruby: 496; pascal: 365; makefile: 128
file content (150 lines) | stat: -rw-r--r-- 4,201 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php

use MediaWiki\Config\ServiceOptions;

/**
 * @coversDefaultClass \MediaWiki\Config\ServiceOptions
 */
class ServiceOptionsTest extends \MediaWikiUnitTestCase {
	public static $testObj;

	public static function setUpBeforeClass(): void {
		parent::setUpBeforeClass();

		self::$testObj = (object)[];
	}

	/**
	 * @dataProvider provideConstructor
	 * @covers ::__construct
	 * @covers ::assertRequiredOptions
	 * @covers ::get
	 */
	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 function provideConstructor() {
		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' ] ),
			],
			'Three different sources' => [
				[ 'a' => 'aval', 'b' => 'bval' ],
				[ 'a', 'b' ],
				[ 'z' => 'zval' ],
				new HashConfig( [ 'a' => 'aval', 'c' => 'cval' ] ),
				[ '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' => self::$testObj ],
				[ 'a' ],
				[ 'a' => self::$testObj ],
			],
		];
	}

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

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

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

	/**
	 * @covers ::__construct
	 * @covers ::get
	 */
	public function testGetUnrecognized() {
		$this->expectException( InvalidArgumentException::class );
		$this->expectExceptionMessage( 'Unrecognized option "b"' );

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

	/**
	 * @covers ::__construct
	 * @covers ::assertRequiredOptions
	 */
	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' ] );
	}

	/**
	 * @covers ::__construct
	 * @covers ::assertRequiredOptions
	 */
	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' ] );
	}

	/**
	 * @covers ::__construct
	 * @covers ::assertRequiredOptions
	 */
	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' ] );
	}
}