| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Maintenance\MaintenanceParameters; |
| 4 |
use PHPUnit\Framework\TestCase; |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
class MaintenanceParametersTest extends TestCase { |
| 10 |
|
| 11 |
public function testOption() { |
| 12 |
$params = new MaintenanceParameters(); |
| 13 |
|
| 14 |
$params->addOption( 'test', 'Test Option' ); |
| 15 |
|
| 16 |
$this->assertTrue( $params->supportsOption( 'test' ) ); |
| 17 |
$this->assertFalse( $params->supportsOption( 'xyzzy' ) ); |
| 18 |
|
| 19 |
$this->assertFalse( $params->hasOption( 'test' ) ); |
| 20 |
$this->assertNull( $params->getOption( 'test' ) ); |
| 21 |
$this->assertSame( 'default', $params->getOption( 'test', 'default' ) ); |
| 22 |
|
| 23 |
$this->assertSame( [], $params->getOptions() ); |
| 24 |
$this->assertSame( [], $params->getOptionsSequence() ); |
| 25 |
$this->assertSame( [ 'test' ], $params->getOptionNames() ); |
| 26 |
|
| 27 |
|
| 28 |
$params->setOptionsAndArgs( [ 'test' => 'foo' ], [] ); |
| 29 |
|
| 30 |
$this->assertTrue( $params->hasOption( 'test' ) ); |
| 31 |
$this->assertSame( 'foo', $params->getOption( 'test' ) ); |
| 32 |
$this->assertSame( 'foo', $params->getOption( 'test', 'default' ) ); |
| 33 |
|
| 34 |
$this->assertSame( [ 'test' => 'foo' ], $params->getOptions() ); |
| 35 |
$this->assertSame( [ [ 'test', 'foo' ] ], $params->getOptionsSequence() ); |
| 36 |
$this->assertSame( [ 'test' ], $params->getOptionNames() ); |
| 37 |
|
| 38 |
|
| 39 |
$params->deleteOption( 'test' ); |
| 40 |
|
| 41 |
$this->assertFalse( $params->hasOption( 'test' ) ); |
| 42 |
$this->assertFalse( $params->supportsOption( 'test' ) ); |
| 43 |
$this->assertNull( $params->getOption( 'test' ) ); |
| 44 |
|
| 45 |
$this->assertSame( [], $params->getOptions() ); |
| 46 |
$this->assertSame( [], $params->getOptionsSequence() ); |
| 47 |
$this->assertSame( [], $params->getOptionNames() ); |
| 48 |
} |
| 49 |
|
| 50 |
public function testOptionSequence() { |
| 51 |
$params = new MaintenanceParameters(); |
| 52 |
|
| 53 |
$params->addOption( 'foo', 'Foo', false, false ); |
| 54 |
$params->addOption( 'bar', 'Bar', false, true ); |
| 55 |
|
| 56 |
$params->loadWithArgv( [ '--bar', 'A', '--bar=B', '--foo', '--bar', 'C' ] ); |
| 57 |
|
| 58 |
$this->assertSame( |
| 59 |
[ |
| 60 |
[ 'bar', 'A' ], |
| 61 |
[ 'bar', 'B' ], |
| 62 |
[ 'foo', 1 ], |
| 63 |
[ 'bar', 'C' ], |
| 64 |
], |
| 65 |
$params->getOptionsSequence() |
| 66 |
); |
| 67 |
|
| 68 |
$params->setOptionsAndArgs( [ 'bar' => [ 'x', 'y' ], 'foo' => 1 ], [] ); |
| 69 |
|
| 70 |
$this->assertSame( |
| 71 |
[ |
| 72 |
[ 'bar', 'x' ], |
| 73 |
[ 'bar', 'y' ], |
| 74 |
[ 'foo', 1 ], |
| 75 |
], |
| 76 |
$params->getOptionsSequence() |
| 77 |
); |
| 78 |
} |
| 79 |
|
| 80 |
public function testArg() { |
| 81 |
$params = new MaintenanceParameters(); |
| 82 |
|
| 83 |
$this->assertSame( 0, $params->addArg( 'test', 'Test arg', true ) ); |
| 84 |
$this->assertSame( 1, $params->addArg( 'best', 'Best arg' ) ); |
| 85 |
|
| 86 |
$this->assertSame( 'test', $params->getArgName( 0 ) ); |
| 87 |
$this->assertSame( 'best', $params->getArgName( 1 ) ); |
| 88 |
$this->assertNull( $params->getArgName( 2 ) ); |
| 89 |
|
| 90 |
$this->assertFalse( $params->hasArg( 0 ) ); |
| 91 |
$this->assertNull( $params->getArg( 0 ) ); |
| 92 |
|
| 93 |
$this->assertSame( [], $params->getArgs() ); |
| 94 |
|
| 95 |
|
| 96 |
$params->setOptionsAndArgs( [], [ 'foo', 'bar', 'cuzz' ] ); |
| 97 |
|
| 98 |
$this->assertTrue( $params->hasArg( 0 ) ); |
| 99 |
$this->assertTrue( $params->hasArg( 'test' ) ); |
| 100 |
$this->assertTrue( $params->hasArg( 1 ) ); |
| 101 |
$this->assertTrue( $params->hasArg( 'best' ) ); |
| 102 |
$this->assertTrue( $params->hasArg( 2 ) ); |
| 103 |
$this->assertFalse( $params->hasArg( 3 ) ); |
| 104 |
|
| 105 |
$this->assertSame( 'foo', $params->getArg( 0 ) ); |
| 106 |
$this->assertSame( 'foo', $params->getArg( 'test' ) ); |
| 107 |
$this->assertSame( 'bar', $params->getArg( 1 ) ); |
| 108 |
$this->assertSame( 'bar', $params->getArg( 'best' ) ); |
| 109 |
$this->assertSame( 'cuzz', $params->getArg( 2 ) ); |
| 110 |
$this->assertNull( $params->getArg( 3 ) ); |
| 111 |
|
| 112 |
$this->assertSame( [ 'foo', 'bar', 'cuzz' ], $params->getArgs() ); |
| 113 |
} |
| 114 |
|
| 115 |
public function testClear() { |
| 116 |
$params = new MaintenanceParameters(); |
| 117 |
|
| 118 |
$params->addOption( 'test', 'Test Option' ); |
| 119 |
$params->addArg( 'jest', 'Jest arg' ); |
| 120 |
|
| 121 |
$params->setOptionsAndArgs( [ 'test' => 'foo' ], [ 'x', 'y' ] ); |
| 122 |
|
| 123 |
$this->assertTrue( $params->hasOption( 'test' ) ); |
| 124 |
$this->assertTrue( $params->hasArg( 'jest' ) ); |
| 125 |
|
| 126 |
$params->clear(); |
| 127 |
|
| 128 |
$this->assertFalse( $params->hasOption( 'test' ) ); |
| 129 |
$this->assertFalse( $params->hasArg( 'jest' ) ); |
| 130 |
|
| 131 |
$this->assertSame( 'jest', $params->getArgName( 0 ) ); |
| 132 |
$this->assertTrue( $params->supportsOption( 'test' ) ); |
| 133 |
} |
| 134 |
|
| 135 |
public static function provideArgv() { |
| 136 |
yield 'nothing' => [ |
| 137 |
[], [], [] |
| 138 |
]; |
| 139 |
|
| 140 |
yield 'simple' => [ |
| 141 |
[ '--simple', 'test' ], [ 'simple' => 1 ], [ 'test' ] |
| 142 |
]; |
| 143 |
|
| 144 |
yield 'trailing option' => [ |
| 145 |
[ 'test', '--simple' ], [ 'simple' => 1 ], [ 'test' ] |
| 146 |
]; |
| 147 |
|
| 148 |
yield 'not a trailing option' => [ |
| 149 |
[ 'test', '--', '--simple' ], [], [ 'test', '--simple' ] |
| 150 |
]; |
| 151 |
|
| 152 |
yield 'option value' => [ |
| 153 |
[ '--value', 'foo' ], [ 'value' => 'foo' ], [] |
| 154 |
]; |
| 155 |
|
| 156 |
yield 'option value swallows option' => [ |
| 157 |
[ '--value', '--simple' ], [ 'value' => '--simple' ], [] |
| 158 |
]; |
| 159 |
|
| 160 |
yield 'option value swallows delimiter' => [ |
| 161 |
[ '--value', '--', '--simple' ], [ 'value' => '--', 'simple' => 1 ], [] |
| 162 |
]; |
| 163 |
|
| 164 |
yield 'option value assigned' => [ |
| 165 |
[ '--value=foo', 'test' ], [ 'value' => 'foo' ], [ 'test' ] |
| 166 |
]; |
| 167 |
|
| 168 |
yield 'option value short' => [ |
| 169 |
[ '-sv', 'foo' ], [ 'simple' => 1, 'value' => 'foo' ], [] |
| 170 |
]; |
| 171 |
|
| 172 |
yield 'short option and lonely dash' => [ |
| 173 |
[ '-s', '-', 'foo' ], [ 'simple' => 1 ], [ '-', 'foo' ] |
| 174 |
]; |
| 175 |
|
| 176 |
yield 'multi value' => [ |
| 177 |
[ '--multi', 'foo', 'test', '--multi', 'bar' ], |
| 178 |
[ 'multi' => [ 'foo', 'bar' ] ], |
| 179 |
[ 'test' ] |
| 180 |
]; |
| 181 |
|
| 182 |
yield 'multi value short' => [ |
| 183 |
[ '-mm', 'foo', 'bar', 'test' ], |
| 184 |
[ 'multi' => [ 'foo', 'bar' ] ], |
| 185 |
[ 'test' ] |
| 186 |
]; |
| 187 |
|
| 188 |
yield 'extra option' => [ |
| 189 |
[ '--extra', 'test' ], |
| 190 |
[ 'extra' => 1 ], |
| 191 |
[ 'test' ] |
| 192 |
]; |
| 193 |
|
| 194 |
yield 'extra with assignment' => [ |
| 195 |
[ '--extra=foo', 'test' ], |
| 196 |
[ 'extra' => 'foo' ], |
| 197 |
[ 'test' ] |
| 198 |
]; |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
public function testLoad( $argv, $expectedOptions, $expectedArgs ) { |
| 205 |
$params = new MaintenanceParameters(); |
| 206 |
$params->setAllowUnregisteredOptions( true ); |
| 207 |
|
| 208 |
$params->addOption( 'simple', 'simple option', false, false, 's' ); |
| 209 |
$params->addOption( 'value', 'value option', false, true, 'v' ); |
| 210 |
$params->addOption( 'multi', 'multi option', false, true, 'm', true ); |
| 211 |
$params->addArg( 'foo', 'Foozels', false ); |
| 212 |
|
| 213 |
$params->loadWithArgv( $argv ); |
| 214 |
|
| 215 |
$params->validate(); |
| 216 |
$this->assertFalse( $params->hasErrors() ); |
| 217 |
$this->assertSame( [], $params->getErrors() ); |
| 218 |
|
| 219 |
$this->assertSame( $expectedOptions, $params->getOptions() ); |
| 220 |
$this->assertSame( $expectedArgs, $params->getArgs() ); |
| 221 |
} |
| 222 |
|
| 223 |
public function testMultiArg() { |
| 224 |
$params = new MaintenanceParameters(); |
| 225 |
$params->setAllowUnregisteredOptions( true ); |
| 226 |
|
| 227 |
$params->addArg( 'foo', 'Foozels', true ); |
| 228 |
$params->addArg( 'bar', 'Bazels', false, true ); |
| 229 |
|
| 230 |
$params->loadWithArgv( [ 'a', 'b', 'c', 'd' ] ); |
| 231 |
|
| 232 |
$this->assertSame( 'a', $params->getArg( 0 ) ); |
| 233 |
$this->assertSame( 'a', $params->getArg( 'foo' ) ); |
| 234 |
|
| 235 |
$this->assertSame( 'b', $params->getArg( 1 ) ); |
| 236 |
$this->assertSame( 'b', $params->getArg( 'bar' ) ); |
| 237 |
|
| 238 |
$this->assertSame( [ 'a', 'b', 'c', 'd' ], $params->getArgs() ); |
| 239 |
$this->assertSame( [ 'c', 'd' ], $params->getArgs( 2 ) ); |
| 240 |
$this->assertSame( [ 'a', 'b', 'c', 'd' ], $params->getArgs( 'foo' ) ); |
| 241 |
$this->assertSame( [ 'b', 'c', 'd' ], $params->getArgs( 'bar' ) ); |
| 242 |
} |
| 243 |
|
| 244 |
public static function provideAddArgFailure() { |
| 245 |
yield 'Already defined' => [ |
| 246 |
[ 'foo', 'testin 1' ], |
| 247 |
[ 'foo', 'testin 2' ], |
| 248 |
]; |
| 249 |
|
| 250 |
yield 'Required after optional' => [ |
| 251 |
[ 'foo', 'Foo test', false ], |
| 252 |
[ 'bar', 'Bar test', true ], |
| 253 |
]; |
| 254 |
|
| 255 |
yield 'after multi' => [ |
| 256 |
[ 'foo', 'Foo test', false, true ], |
| 257 |
[ 'bar', 'Bar test' ], |
| 258 |
]; |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
public function testAddArgFailure( $arg1, $arg2 ) { |
| 265 |
$params = new MaintenanceParameters(); |
| 266 |
$params->addArg( ...$arg1 ); |
| 267 |
|
| 268 |
$this->expectException( UnexpectedValueException::class ); |
| 269 |
$params->addArg( ...$arg2 ); |
| 270 |
} |
| 271 |
|
| 272 |
public function testLoadSkip() { |
| 273 |
$params = new MaintenanceParameters(); |
| 274 |
$params->addOption( 'value', 'value option', false, true, 'v' ); |
| 275 |
|
| 276 |
$argv = [ 'A', '--value', 'V', 'B' ]; |
| 277 |
|
| 278 |
$params->loadWithArgv( $argv ); |
| 279 |
$this->assertSame( 'A', $params->getArg( 0 ) ); |
| 280 |
|
| 281 |
$params->loadWithArgv( $argv, 1 ); |
| 282 |
$this->assertSame( 'B', $params->getArg( 0 ) ); |
| 283 |
|
| 284 |
$params->loadWithArgv( $argv, 2 ); |
| 285 |
$this->assertSame( 'V', $params->getArg( 0 ) ); |
| 286 |
} |
| 287 |
|
| 288 |
public static function provideBadArgv() { |
| 289 |
yield 'nothing' => [ |
| 290 |
[], |
| 291 |
[ |
| 292 |
'Option --simple is required!', |
| 293 |
'Argument <foo> is required!' |
| 294 |
] |
| 295 |
]; |
| 296 |
|
| 297 |
yield 'missing option' => [ |
| 298 |
[ 'arg' ], |
| 299 |
[ 'Option --simple is required!' ] |
| 300 |
]; |
| 301 |
|
| 302 |
yield 'missing arg' => [ |
| 303 |
[ '--simple' ], |
| 304 |
[ 'Argument <foo> is required!' ] |
| 305 |
]; |
| 306 |
|
| 307 |
yield 'option given twice' => [ |
| 308 |
[ '--simple', 'arg', '-s' ], |
| 309 |
[ 'Option --simple given twice' ] |
| 310 |
]; |
| 311 |
|
| 312 |
yield 'Unexpected option' => [ |
| 313 |
[ '--extra', 'arg', '-s' ], |
| 314 |
[ 'Unexpected option --extra!' ] |
| 315 |
]; |
| 316 |
|
| 317 |
yield 'Missing value' => [ |
| 318 |
[ '-s', 'arg', '--value' ], |
| 319 |
[ 'Option --value needs a value after it!' ] |
| 320 |
]; |
| 321 |
|
| 322 |
yield 'Missing value short' => [ |
| 323 |
[ '-s', 'arg', '-v' ], |
| 324 |
[ 'Option --value needs a value after it!' ] |
| 325 |
]; |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
public function testValidationErrors( $argv, $errors ) { |
| 332 |
$params = new MaintenanceParameters(); |
| 333 |
|
| 334 |
$params->addOption( 'simple', 'simple option', true, false, 's' ); |
| 335 |
$params->addOption( 'value', 'value option', false, true, 'v' ); |
| 336 |
$params->addArg( 'foo', 'Foozels', true ); |
| 337 |
|
| 338 |
$params->loadWithArgv( $argv ); |
| 339 |
|
| 340 |
$params->validate(); |
| 341 |
$this->assertTrue( $params->hasErrors() ); |
| 342 |
$this->assertSame( $errors, $params->getErrors() ); |
| 343 |
} |
| 344 |
|
| 345 |
private function findInLines( array $lines, $regex, $start = 0 ) { |
| 346 |
for ( $i = $start; $i < count( $lines ); $i++ ) { |
| 347 |
if ( str_contains( $lines[ $i ], $regex ) ) { |
| 348 |
return $i; |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
return false; |
| 353 |
} |
| 354 |
|
| 355 |
public function testHelp() { |
| 356 |
$params = new MaintenanceParameters(); |
| 357 |
|
| 358 |
$params->setName( 'Foo' ); |
| 359 |
$params->setDescription( 'Frobs the foo' ); |
| 360 |
|
| 361 |
$params->addOption( 'flag', 'First flag', false, false, 'f' ); |
| 362 |
$params->addOption( 'value', 'Some value', true, true ); |
| 363 |
|
| 364 |
$params->assignGroup( 'Test flags', [ 'flag' ] ); |
| 365 |
|
| 366 |
$help = $params->getHelp(); |
| 367 |
$lines = preg_split( '/\s*[\r\n]\s*/', $help ); |
| 368 |
$lines = array_values( array_filter( $lines ) ); |
| 369 |
|
| 370 |
$synopsisIndex = $this->findInLines( $lines, 'Usage: php maintenance/run.php Foo' ); |
| 371 |
$this->assertNotFalse( $synopsisIndex ); |
| 372 |
$synopsis = $lines[$synopsisIndex]; |
| 373 |
|
| 374 |
$this->assertStringContainsString( '[OPTION]... --value <VALUE>', $synopsis ); |
| 375 |
|
| 376 |
$this->assertNotFalse( $this->findInLines( $lines, 'Frobs the foo' ) ); |
| 377 |
$this->assertNotFalse( $this->findInLines( $lines, '--flag (-f): First flag' ) ); |
| 378 |
$this->assertNotFalse( $this->findInLines( $lines, '--value <VALUE>: Some value' ) ); |
| 379 |
$this->assertNotFalse( $this->findInLines( $lines, 'Test flags' ) ); |
| 380 |
$this->assertNotFalse( $this->findInLines( $lines, 'Script specific options' ) ); |
| 381 |
|
| 382 |
$sectionOffset = $this->findInLines( $lines, 'Script specific parameters' ); |
| 383 |
$this->assertGreaterThan( |
| 384 |
$sectionOffset, |
| 385 |
$this->findInLines( $lines, '--value', $sectionOffset + 1 ) |
| 386 |
); |
| 387 |
|
| 388 |
$sectionOffset = $this->findInLines( $lines, 'Test flags' ); |
| 389 |
$this->assertGreaterThan( |
| 390 |
$sectionOffset, |
| 391 |
$this->findInLines( $lines, '--flag', $sectionOffset + 1 ) |
| 392 |
); |
| 393 |
} |
| 394 |
|
| 395 |
public static function provideArgHelp() { |
| 396 |
yield [ |
| 397 |
[ 'foo', 'Foo arg' ], |
| 398 |
'<foo>', |
| 399 |
'<foo>: Foo arg' |
| 400 |
]; |
| 401 |
yield [ |
| 402 |
[ 'foo', 'Foo arg', false ], |
| 403 |
'[foo]', |
| 404 |
'[foo]: Foo arg' |
| 405 |
]; |
| 406 |
yield [ |
| 407 |
[ 'foo', 'Foo arg', true, true ], |
| 408 |
'<foo>...', |
| 409 |
'<foo>...: Foo arg' |
| 410 |
]; |
| 411 |
yield [ |
| 412 |
[ 'foo', 'Foo arg', false, true ], |
| 413 |
'[foo]...', |
| 414 |
'[foo]...: Foo arg' |
| 415 |
]; |
| 416 |
} |
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
public function testArgHelp( $arg, $expectedSynopsis, $expectedLine ) { |
| 422 |
$params = new MaintenanceParameters(); |
| 423 |
|
| 424 |
$params->setName( 'Foo' ); |
| 425 |
|
| 426 |
$params->addArg( ...$arg ); |
| 427 |
$help = $params->getHelp(); |
| 428 |
|
| 429 |
$lines = preg_split( '/\s*[\r\n]\s*/', $help ); |
| 430 |
$lines = array_values( array_filter( $lines ) ); |
| 431 |
|
| 432 |
$synopsisIndex = $this->findInLines( $lines, 'Usage: php maintenance/run.php Foo' ); |
| 433 |
$this->assertNotFalse( $synopsisIndex ); |
| 434 |
$synopsis = $lines[$synopsisIndex]; |
| 435 |
|
| 436 |
$this->assertStringContainsString( $expectedSynopsis, $synopsis ); |
| 437 |
|
| 438 |
$this->assertNotFalse( $this->findInLines( $lines, $expectedLine ) ); |
| 439 |
} |
| 440 |
|
| 441 |
public function testMergeOptions() { |
| 442 |
$params = new MaintenanceParameters(); |
| 443 |
$params->setName( 'Foo' ); |
| 444 |
$params->setDescription( 'Frobs the foo' ); |
| 445 |
|
| 446 |
$params->addOption( 'flag', 'First flag', false, false, 'f' ); |
| 447 |
|
| 448 |
$params->setOptionsAndArgs( [ 'flag' => 1 ], [] ); |
| 449 |
|
| 450 |
$other = new MaintenanceParameters(); |
| 451 |
$other->setName( 'Bar' ); |
| 452 |
$other->setDescription( 'Mars the bar' ); |
| 453 |
|
| 454 |
$other->addOption( 'value', 'Some value', true, true ); |
| 455 |
|
| 456 |
$params->mergeOptions( $other ); |
| 457 |
|
| 458 |
|
| 459 |
$this->assertTrue( $params->supportsOption( 'flag' ) ); |
| 460 |
$this->assertTrue( $params->supportsOption( 'value' ) ); |
| 461 |
|
| 462 |
|
| 463 |
$this->assertFalse( $params->hasOption( 'flag' ) ); |
| 464 |
} |
| 465 |
|
| 466 |
public function testSetOptionsAndArgs() { |
| 467 |
$params = new MaintenanceParameters(); |
| 468 |
|
| 469 |
$params->setOptionsAndArgs( [ 'foo' => 'X' ], [ 'one' ] ); |
| 470 |
|
| 471 |
$this->assertTrue( $params->hasOption( 'foo' ) ); |
| 472 |
$this->assertTrue( $params->hasArg( 0 ) ); |
| 473 |
$this->assertSame( [ 'one' ], $params->getArgs() ); |
| 474 |
$this->assertSame( [ 'foo' => 'X' ], $params->getOptions() ); |
| 475 |
|
| 476 |
$params->setOptionsAndArgs( [ 'bar' => 'Y' ], [] ); |
| 477 |
|
| 478 |
$this->assertTrue( $params->hasOption( 'bar' ) ); |
| 479 |
$this->assertFalse( $params->hasOption( 'foo' ) ); |
| 480 |
$this->assertFalse( $params->hasArg( 0 ) ); |
| 481 |
$this->assertSame( [], $params->getArgs() ); |
| 482 |
$this->assertSame( [ 'bar' => 'Y' ], $params->getOptions() ); |
| 483 |
} |
| 484 |
|
| 485 |
} |