From 9ece509c99f7708fe1731e112da4b99f01471a7c Mon Sep 17 00:00:00 2001 From: Piotr Miazga Date: Wed, 31 Jul 2019 22:47:27 +0200 Subject: [PATCH] Hygiene: SkinOptions should validate options Changes: - when trying to set unknown option, throw exception - added unit tests Bug: T221012 Change-Id: I0d1a43fb5a7179b8e9cd578e3e33bab12ddfd61c --- includes/SkinOptions.php | 5 +++++ tests/phpunit/unit/SkinOptionsTest.php | 20 ++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/includes/SkinOptions.php b/includes/SkinOptions.php index 4544d26..780736c 100644 --- a/includes/SkinOptions.php +++ b/includes/SkinOptions.php @@ -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 ); } diff --git a/tests/phpunit/unit/SkinOptionsTest.php b/tests/phpunit/unit/SkinOptionsTest.php index 491c500..c29cd84 100644 --- a/tests/phpunit/unit/SkinOptionsTest.php +++ b/tests/phpunit/unit/SkinOptionsTest.php @@ -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 + ] ); + } }