File: MaintenanceParametersTest.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 (485 lines) | stat: -rw-r--r-- 13,607 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
<?php

use MediaWiki\Maintenance\MaintenanceParameters;
use PHPUnit\Framework\TestCase;

/**
 * @covers \MediaWiki\Maintenance\MaintenanceParameters
 */
class MaintenanceParametersTest extends TestCase {

	public function testOption() {
		$params = new MaintenanceParameters();

		$params->addOption( 'test', 'Test Option' );

		$this->assertTrue( $params->supportsOption( 'test' ) );
		$this->assertFalse( $params->supportsOption( 'xyzzy' ) );

		$this->assertFalse( $params->hasOption( 'test' ) );
		$this->assertNull( $params->getOption( 'test' ) );
		$this->assertSame( 'default', $params->getOption( 'test', 'default' ) );

		$this->assertSame( [], $params->getOptions() );
		$this->assertSame( [], $params->getOptionsSequence() );
		$this->assertSame( [ 'test' ], $params->getOptionNames() );

		// Provide option value
		$params->setOptionsAndArgs( [ 'test' => 'foo' ], [] );

		$this->assertTrue( $params->hasOption( 'test' ) );
		$this->assertSame( 'foo', $params->getOption( 'test' ) );
		$this->assertSame( 'foo', $params->getOption( 'test', 'default' ) );

		$this->assertSame( [ 'test' => 'foo' ], $params->getOptions() );
		$this->assertSame( [ [ 'test', 'foo' ] ], $params->getOptionsSequence() );
		$this->assertSame( [ 'test' ], $params->getOptionNames() );

		// Delete option
		$params->deleteOption( 'test' );

		$this->assertFalse( $params->hasOption( 'test' ) );
		$this->assertFalse( $params->supportsOption( 'test' ) );
		$this->assertNull( $params->getOption( 'test' ) );

		$this->assertSame( [], $params->getOptions() );
		$this->assertSame( [], $params->getOptionsSequence() );
		$this->assertSame( [], $params->getOptionNames() );
	}

	public function testOptionSequence() {
		$params = new MaintenanceParameters();

		$params->addOption( 'foo', 'Foo', false, false );
		$params->addOption( 'bar', 'Bar', false, true );

		$params->loadWithArgv( [ '--bar', 'A', '--bar=B', '--foo', '--bar', 'C' ] );

		$this->assertSame(
			[
				[ 'bar', 'A' ],
				[ 'bar', 'B' ],
				[ 'foo', 1 ],
				[ 'bar', 'C' ],
			],
			$params->getOptionsSequence()
		);

		$params->setOptionsAndArgs( [ 'bar' => [ 'x', 'y' ], 'foo' => 1 ], [] );

		$this->assertSame(
			[
				[ 'bar', 'x' ],
				[ 'bar', 'y' ],
				[ 'foo', 1 ],
			],
			$params->getOptionsSequence()
		);
	}

	public function testArg() {
		$params = new MaintenanceParameters();

		$this->assertSame( 0, $params->addArg( 'test', 'Test arg', true ) );
		$this->assertSame( 1, $params->addArg( 'best', 'Best arg' ) );

		$this->assertSame( 'test', $params->getArgName( 0 ) );
		$this->assertSame( 'best', $params->getArgName( 1 ) );
		$this->assertNull( $params->getArgName( 2 ) );

		$this->assertFalse( $params->hasArg( 0 ) );
		$this->assertNull( $params->getArg( 0 ) );

		$this->assertSame( [], $params->getArgs() );

		// Provide argument values
		$params->setOptionsAndArgs( [], [ 'foo', 'bar', 'cuzz' ] );

		$this->assertTrue( $params->hasArg( 0 ) );
		$this->assertTrue( $params->hasArg( 'test' ) );
		$this->assertTrue( $params->hasArg( 1 ) );
		$this->assertTrue( $params->hasArg( 'best' ) );
		$this->assertTrue( $params->hasArg( 2 ) );
		$this->assertFalse( $params->hasArg( 3 ) );

		$this->assertSame( 'foo', $params->getArg( 0 ) );
		$this->assertSame( 'foo', $params->getArg( 'test' ) );
		$this->assertSame( 'bar', $params->getArg( 1 ) );
		$this->assertSame( 'bar', $params->getArg( 'best' ) );
		$this->assertSame( 'cuzz', $params->getArg( 2 ) );
		$this->assertNull( $params->getArg( 3 ) );

		$this->assertSame( [ 'foo', 'bar', 'cuzz' ], $params->getArgs() );
	}

	public function testClear() {
		$params = new MaintenanceParameters();

		$params->addOption( 'test', 'Test Option' );
		$params->addArg( 'jest', 'Jest arg' );

		$params->setOptionsAndArgs( [ 'test' => 'foo' ], [ 'x', 'y' ] );

		$this->assertTrue( $params->hasOption( 'test' ) );
		$this->assertTrue( $params->hasArg( 'jest' ) );

		$params->clear();

		$this->assertFalse( $params->hasOption( 'test' ) );
		$this->assertFalse( $params->hasArg( 'jest' ) );

		$this->assertSame( 'jest', $params->getArgName( 0 ) );
		$this->assertTrue( $params->supportsOption( 'test' ) );
	}

	public static function provideArgv() {
		yield 'nothing' => [
			[], [], []
		];

		yield 'simple' => [
			[ '--simple', 'test' ], [ 'simple' => 1 ], [ 'test' ]
		];

		yield 'trailing option' => [
			[ 'test', '--simple' ], [ 'simple' => 1 ], [ 'test' ]
		];

		yield 'not a trailing option' => [
			[ 'test', '--', '--simple' ], [], [ 'test', '--simple' ]
		];

		yield 'option value' => [
			[ '--value', 'foo' ], [ 'value' => 'foo' ], []
		];

		yield 'option value swallows option' => [
			[ '--value', '--simple' ], [ 'value' => '--simple' ], []
		];

		yield 'option value swallows delimiter' => [
			[ '--value', '--', '--simple' ], [ 'value' => '--', 'simple' => 1 ], []
		];

		yield 'option value assigned' => [
			[ '--value=foo', 'test' ], [ 'value' => 'foo' ], [ 'test' ]
		];

		yield 'option value short' => [
			[ '-sv', 'foo' ], [ 'simple' => 1, 'value' => 'foo' ], []
		];

		yield 'short option and lonely dash' => [
			[ '-s', '-', 'foo' ], [ 'simple' => 1 ], [ '-', 'foo' ]
		];

		yield 'multi value' => [
			[ '--multi', 'foo', 'test', '--multi', 'bar' ],
			[ 'multi' => [ 'foo', 'bar' ] ],
			[ 'test' ]
		];

		yield 'multi value short' => [
			[ '-mm', 'foo', 'bar', 'test' ],
			[ 'multi' => [ 'foo', 'bar' ] ],
			[ 'test' ]
		];

		yield 'extra option' => [
			[ '--extra', 'test' ],
			[ 'extra' => 1 ],
			[ 'test' ]
		];

		yield 'extra with assignment' => [
			[ '--extra=foo', 'test' ],
			[ 'extra' => 'foo' ],
			[ 'test' ]
		];
	}

	/**
	 * @dataProvider provideArgv
	 */
	public function testLoad( $argv, $expectedOptions, $expectedArgs ) {
		$params = new MaintenanceParameters();
		$params->setAllowUnregisteredOptions( true );

		$params->addOption( 'simple', 'simple option', false, false, 's' );
		$params->addOption( 'value', 'value option', false, true, 'v' );
		$params->addOption( 'multi', 'multi option', false, true, 'm', true );
		$params->addArg( 'foo', 'Foozels', false );

		$params->loadWithArgv( $argv );

		$params->validate();
		$this->assertFalse( $params->hasErrors() );
		$this->assertSame( [], $params->getErrors() );

		$this->assertSame( $expectedOptions, $params->getOptions() );
		$this->assertSame( $expectedArgs, $params->getArgs() );
	}

	public function testMultiArg() {
		$params = new MaintenanceParameters();
		$params->setAllowUnregisteredOptions( true );

		$params->addArg( 'foo', 'Foozels', true );
		$params->addArg( 'bar', 'Bazels', false, true );

		$params->loadWithArgv( [ 'a', 'b', 'c', 'd' ] );

		$this->assertSame( 'a', $params->getArg( 0 ) );
		$this->assertSame( 'a', $params->getArg( 'foo' ) );

		$this->assertSame( 'b', $params->getArg( 1 ) );
		$this->assertSame( 'b', $params->getArg( 'bar' ) );

		$this->assertSame( [ 'a', 'b', 'c', 'd' ], $params->getArgs() );
		$this->assertSame( [ 'c', 'd' ], $params->getArgs( 2 ) );
		$this->assertSame( [ 'a', 'b', 'c', 'd' ], $params->getArgs( 'foo' ) );
		$this->assertSame( [ 'b', 'c', 'd' ], $params->getArgs( 'bar' ) );
	}

	public static function provideAddArgFailure() {
		yield 'Already defined' => [
			[ 'foo', 'testin 1' ],
			[ 'foo', 'testin 2' ],
		];

		yield 'Required after optional' => [
			[ 'foo', 'Foo test', false ],
			[ 'bar', 'Bar test', true ],
		];

		yield 'after multi' => [
			[ 'foo', 'Foo test', false, true ],
			[ 'bar', 'Bar test' ],
		];
	}

	/**
	 * @dataProvider provideAddArgFailure
	 */
	public function testAddArgFailure( $arg1, $arg2 ) {
		$params = new MaintenanceParameters();
		$params->addArg( ...$arg1 );

		$this->expectException( UnexpectedValueException::class );
		$params->addArg( ...$arg2 );
	}

	public function testLoadSkip() {
		$params = new MaintenanceParameters();
		$params->addOption( 'value', 'value option', false, true, 'v' );

		$argv = [ 'A', '--value', 'V', 'B' ];

		$params->loadWithArgv( $argv );
		$this->assertSame( 'A', $params->getArg( 0 ) );

		$params->loadWithArgv( $argv, 1 );
		$this->assertSame( 'B', $params->getArg( 0 ) );

		$params->loadWithArgv( $argv, 2 );
		$this->assertSame( 'V', $params->getArg( 0 ) );
	}

	public static function provideBadArgv() {
		yield 'nothing' => [
			[],
			[
				'Option --simple is required!',
				'Argument <foo> is required!'
			]
		];

		yield 'missing option' => [
			[ 'arg' ],
			[ 'Option --simple is required!' ]
		];

		yield 'missing arg' => [
			[ '--simple' ],
			[ 'Argument <foo> is required!' ]
		];

		yield 'option given twice' => [
			[ '--simple', 'arg', '-s' ],
			[ 'Option --simple given twice' ]
		];

		yield 'Unexpected option' => [
			[ '--extra', 'arg', '-s' ],
			[ 'Unexpected option --extra!' ]
		];

		yield 'Missing value' => [
			[ '-s', 'arg', '--value' ],
			[ 'Option --value needs a value after it!' ]
		];

		yield 'Missing value short' => [
			[ '-s', 'arg', '-v' ],
			[ 'Option --value needs a value after it!' ]
		];
	}

	/**
	 * @dataProvider provideBadArgv
	 */
	public function testValidationErrors( $argv, $errors ) {
		$params = new MaintenanceParameters();

		$params->addOption( 'simple', 'simple option', true, false, 's' );
		$params->addOption( 'value', 'value option', false, true, 'v' );
		$params->addArg( 'foo', 'Foozels', true );

		$params->loadWithArgv( $argv );

		$params->validate();
		$this->assertTrue( $params->hasErrors() );
		$this->assertSame( $errors, $params->getErrors() );
	}

	private function findInLines( array $lines, $regex, $start = 0 ) {
		for ( $i = $start; $i < count( $lines ); $i++ ) {
			if ( str_contains( $lines[ $i ], $regex ) ) {
				return $i;
			}
		}

		return false;
	}

	public function testHelp() {
		$params = new MaintenanceParameters();

		$params->setName( 'Foo' );
		$params->setDescription( 'Frobs the foo' );

		$params->addOption( 'flag', 'First flag', false, false, 'f' );
		$params->addOption( 'value', 'Some value', true, true );

		$params->assignGroup( 'Test flags', [ 'flag' ] );

		$help = $params->getHelp();
		$lines = preg_split( '/\s*[\r\n]\s*/', $help );
		$lines = array_values( array_filter( $lines ) );

		$synopsisIndex = $this->findInLines( $lines, 'Usage: php maintenance/run.php Foo' );
		$this->assertNotFalse( $synopsisIndex );
		$synopsis = $lines[$synopsisIndex];

		$this->assertStringContainsString( '[OPTION]... --value <VALUE>', $synopsis );

		$this->assertNotFalse( $this->findInLines( $lines, 'Frobs the foo' ) );
		$this->assertNotFalse( $this->findInLines( $lines, '--flag (-f): First flag' ) );
		$this->assertNotFalse( $this->findInLines( $lines, '--value <VALUE>: Some value' ) );
		$this->assertNotFalse( $this->findInLines( $lines, 'Test flags' ) );
		$this->assertNotFalse( $this->findInLines( $lines, 'Script specific options' ) );

		$sectionOffset = $this->findInLines( $lines, 'Script specific parameters' );
		$this->assertGreaterThan(
			$sectionOffset,
			$this->findInLines( $lines, '--value', $sectionOffset + 1 )
		);

		$sectionOffset = $this->findInLines( $lines, 'Test flags' );
		$this->assertGreaterThan(
			$sectionOffset,
			$this->findInLines( $lines, '--flag', $sectionOffset + 1 )
		);
	}

	public static function provideArgHelp() {
		yield [
			[ 'foo', 'Foo arg' ],
			'<foo>',
			'<foo>: Foo arg'
		];
		yield [
			[ 'foo', 'Foo arg', false ],
			'[foo]',
			'[foo]: Foo arg'
		];
		yield [
			[ 'foo', 'Foo arg', true, true ],
			'<foo>...',
			'<foo>...: Foo arg'
		];
		yield [
			[ 'foo', 'Foo arg', false, true ],
			'[foo]...',
			'[foo]...: Foo arg'
		];
	}

	/**
	 * @dataProvider provideArgHelp
	 */
	public function testArgHelp( $arg, $expectedSynopsis, $expectedLine ) {
		$params = new MaintenanceParameters();

		$params->setName( 'Foo' );

		$params->addArg( ...$arg );
		$help = $params->getHelp();

		$lines = preg_split( '/\s*[\r\n]\s*/', $help );
		$lines = array_values( array_filter( $lines ) );

		$synopsisIndex = $this->findInLines( $lines, 'Usage: php maintenance/run.php Foo' );
		$this->assertNotFalse( $synopsisIndex );
		$synopsis = $lines[$synopsisIndex];

		$this->assertStringContainsString( $expectedSynopsis, $synopsis );

		$this->assertNotFalse( $this->findInLines( $lines, $expectedLine ) );
	}

	public function testMergeOptions() {
		$params = new MaintenanceParameters();
		$params->setName( 'Foo' );
		$params->setDescription( 'Frobs the foo' );

		$params->addOption( 'flag', 'First flag', false, false, 'f' );

		$params->setOptionsAndArgs( [ 'flag' => 1 ], [] );

		$other = new MaintenanceParameters();
		$other->setName( 'Bar' );
		$other->setDescription( 'Mars the bar' );

		$other->addOption( 'value', 'Some value', true, true );

		$params->mergeOptions( $other );

		// declarations from both objects are presetn
		$this->assertTrue( $params->supportsOption( 'flag' ) );
		$this->assertTrue( $params->supportsOption( 'value' ) );

		// values are reset
		$this->assertFalse( $params->hasOption( 'flag' ) );
	}

	public function testSetOptionsAndArgs() {
		$params = new MaintenanceParameters();

		$params->setOptionsAndArgs( [ 'foo' => 'X' ], [ 'one' ] );

		$this->assertTrue( $params->hasOption( 'foo' ) );
		$this->assertTrue( $params->hasArg( 0 ) );
		$this->assertSame( [ 'one' ], $params->getArgs() );
		$this->assertSame( [ 'foo' => 'X' ], $params->getOptions() );

		$params->setOptionsAndArgs( [ 'bar' => 'Y' ], [] );

		$this->assertTrue( $params->hasOption( 'bar' ) );
		$this->assertFalse( $params->hasOption( 'foo' ) );
		$this->assertFalse( $params->hasArg( 0 ) );
		$this->assertSame( [], $params->getArgs() );
		$this->assertSame( [ 'bar' => 'Y' ], $params->getOptions() );
	}

}