Jeroen De Dauw has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/50586


Change subject: Cleanup settings class
......................................................................

Cleanup settings class

Change-Id: Ifad4902077f4c8e608490c18d0a33f8923078bf9
---
M config/DefaultConfig.php
M includes/ParamProcessor/Settings.php
A tests/SettingsTest.php
3 files changed, 84 insertions(+), 73 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Validator 
refs/changes/86/50586/1

diff --git a/config/DefaultConfig.php b/config/DefaultConfig.php
index b8c19a0..9a6426a 100644
--- a/config/DefaultConfig.php
+++ b/config/DefaultConfig.php
@@ -33,7 +33,9 @@
        die( 'Not an entry point.' );
 }
 
-$egValidatorSettings = array();
+$egValidatorSettings = array(
+       'errorListMinSeverity' => 'minor',
+);
 
 $wgParamDefinitions = array(
        'boolean' => array(
diff --git a/includes/ParamProcessor/Settings.php 
b/includes/ParamProcessor/Settings.php
index 4121a30..6b4c20b 100644
--- a/includes/ParamProcessor/Settings.php
+++ b/includes/ParamProcessor/Settings.php
@@ -1,10 +1,9 @@
 <?php
 
 namespace ParamProcessor;
-use MWException;
 
 /**
- * File defining the settings for the Validator extension.
+ * File defining the settings for the ParamProcessor extension.
  * More info can be found at 
https://www.mediawiki.org/wiki/Extension:Validator#Settings
  *
  * NOTICE:
@@ -22,101 +21,48 @@
 final class Settings {
 
        /**
-        * Protected constructor - force singleton usage.
+        * Constructs a new instance of the settings object from global state.
+        *
         * @since 1.0
+        *
+        * @param array $globalVariables
+        *
+        * @return Settings
         */
-       public function __construct() {
-
+       public static function newFromGlobals( array $globalVariables ) {
+               return new self( $globalVariables['egValidatorSettings'] );
        }
 
        /**
         * @since 1.0
-        * @var Settings|null
+        *
+        * @var array
         */
-       protected static $instance = null;
+       protected $settings;
 
        /**
-        * @since 1.0
-        * @var array|null
-        */
-       protected $settings = null;
-
-       /**
-        * Returns the default values for the settings.
-        * setting name (string) => setting value (mixed)
+        * Constructor.
         *
         * @since 1.0
         *
-        * @return array
+        * @param array $settings
         */
-       protected function getDefaultSettings() {
-               return array(
-                       'errorListMinSeverity' => 'minor',
-               );
+       public function __construct( array $settings ) {
+               $this->settings = $settings;
        }
 
        /**
-        * Builds the settings if needed.
-        * This includes merging the set settings over the default ones.
-        *
-        * @since 1.0
-        */
-       protected function buildSettings() {
-               if ( is_null( $this->settings ) ) {
-                       $this->settings = array_merge(
-                               self::getDefaultSettings(),
-                               $GLOBALS['egValidatorSettings']
-                       );
-               }
-       }
-
-       /**
-        * Retruns an array with all settings after making sure they are
-        * initialized (ie set settings have been merged with the defaults).
-        * setting name (string) => setting value (mixed)
-        *
-        * @since 1.0
-        *
-        * @return array
-        */
-       public function getSettings() {
-               $this->buildSettings();
-               return $this->settings;
-       }
-
-       /**
-        * Gets the value of the specified setting.
+        * Returns the setting with the provided name.
+        * The specified setting needs to exist.
         *
         * @since 1.0
         *
         * @param string $settingName
         *
-        * @throws MWException
         * @return mixed
         */
        public function get( $settingName ) {
-               $this->buildSettings();
-
-               if ( !array_key_exists( $settingName, $this->settings ) ) {
-                       throw new MWException( 'Attempt to get non-existing 
setting "' . $settingName . '"' );
-               }
-
                return $this->settings[$settingName];
-       }
-
-       /**
-        * Returns if a certain setting is set, and can therefor be obtained 
via getSetting.
-        *
-        * @since 1.0
-        *
-        * @param string $settingName
-        *
-        * @throws MWException
-        * @return mixed
-        */
-       public function has( $settingName ) {
-               $this->buildSettings();
-               return array_key_exists( $settingName, $this->settings );
        }
 
 }
diff --git a/tests/SettingsTest.php b/tests/SettingsTest.php
new file mode 100644
index 0000000..08de276
--- /dev/null
+++ b/tests/SettingsTest.php
@@ -0,0 +1,63 @@
+<?php
+
+namespace ParamProcessor\Test;
+
+use ParamProcessor\Settings;
+
+/**
+ * Tests for the ParamProcessor\Settings class.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @since 1.0
+ *
+ * @ingroup ParamProcessorTest
+ *
+ * @group ParamProcessor
+ *
+ * @licence GNU GPL v2+
+ * @author Jeroen De Dauw < jeroended...@gmail.com >
+ */
+class SettingsTest extends \MediaWikiTestCase {
+
+       public function constructorProvider() {
+               $settingArrays = array(
+                       array(),
+                       array( 'foo' => 'bar' ),
+                       array( 'foo' => 'bar', 'baz' => 'BAH' ),
+                       array( '~[,,_,,]:3' => array( 9001, 4.2 ) ),
+               );
+
+               return $this->arrayWrap( $settingArrays );
+       }
+
+       /**
+        * @dataProvider constructorProvider
+        *
+        * @param array $settings
+        */
+       public function testConstructor( array $settings ) {
+               $settingsObject = new Settings( $settings );
+
+               foreach ( $settings as $name => $value ) {
+                       $this->assertEquals( $value, $settingsObject->get( 
$name ) );
+               }
+
+               $this->assertTrue( true );
+       }
+
+}

-- 
To view, visit https://gerrit.wikimedia.org/r/50586
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ifad4902077f4c8e608490c18d0a33f8923078bf9
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Validator
Gerrit-Branch: master
Gerrit-Owner: Jeroen De Dauw <jeroended...@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to