Hygiene: SkinOptions should validate options

Changes:
 - when trying to set unknown option, throw exception
 - added unit tests

Bug: T221012
Change-Id: I0d1a43fb5a7179b8e9cd578e3e33bab12ddfd61c
This commit is contained in:
Piotr Miazga 2019-07-31 22:47:27 +02:00 committed by Jdlrobson
parent 3dd8ce4822
commit 9ece509c99
2 changed files with 25 additions and 0 deletions

View File

@ -68,6 +68,11 @@ final class SkinOptions {
* @param array $options
*/
public function setMultiple( array $options ) {
foreach ( $options as $option => $value ) {
if ( !array_key_exists( $option, $this->skinOptions ) ) {
throw new \OutOfBoundsException( "SkinOption $option is not defined" );
}
}
$this->skinOptions = array_merge( $this->skinOptions, $options );
}

View File

@ -46,4 +46,24 @@ class SkinOptionsTest extends \MediaWikiUnitTestCase {
] );
$this->assertFalse( $options->hasSkinOptions() );
}
/**
* @covers ::get
* @expectedException \OutOfBoundsException
*/
public function testGettingUnknownKeyShouldThrowException() {
$options = new SkinOptions();
$options->get( 'non_existing_key' );
}
/**
* @covers ::get
* @expectedException \OutOfBoundsException
*/
public function testSettingUnknownKeyShouldThrowException() {
$options = new SkinOptions();
$options->setMultiple( [
'non_existing_key' => 1
] );
}
}