Mwjames has uploaded a new change for review.

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


Change subject: SMW\Settings use for composition instead for $GLOBALS
......................................................................

SMW\Settings use for composition instead for $GLOBALS

This class will help to avoid using $GLOBALS within class and
instead invoke Settings as composition component.

Change-Id: I546f79d60e08e487dc12733b1c3c9c76983dd8d6
---
M includes/RecurringEvents.php
A includes/Settings.php
M includes/Setup.php
M includes/parserhooks/RecurringEventsParserFunction.php
M includes/parserhooks/SubobjectParserFunction.php
M tests/phpunit/includes/RecurringEventsTest.php
A tests/phpunit/includes/SettingsTest.php
M tests/phpunit/includes/parserhooks/RecurringEventsParserFunctionTest.php
M tests/phpunit/includes/parserhooks/SubobjectParserFunctionTest.php
9 files changed, 260 insertions(+), 56 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/SemanticMediaWiki 
refs/changes/93/58293/1

diff --git a/includes/RecurringEvents.php b/includes/RecurringEvents.php
index f7e5783..c06ebc7 100644
--- a/includes/RecurringEvents.php
+++ b/includes/RecurringEvents.php
@@ -2,8 +2,6 @@
 
 namespace SMW;
 
-use MWException;
-
 use SMWDITime;
 use SMWTimeValue;
 use SMWDataValueFactory;
@@ -28,20 +26,28 @@
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  * http://www.gnu.org/copyleft/gpl.html
  *
- * @file
  * @since 1.9
+ *
+ * @file
  * @ingroup SMW
  *
  * @author Yaron Koren
  * @author Jeroen De Dauw
  * @author mwjames
  */
+
+/**
+ * This class determines recurring events based on invoked parameters
+ *
+ * @ingroup SMW
+ */
 class RecurringEvents {
 
        /**
-        * Defines available options from $GLOBALS
+        * Represents Settings object
+        * @var Settings
         */
-       protected $options = array();
+       protected $settings;
 
        /**
         * Defines the property used
@@ -73,14 +79,10 @@
         * @since 1.9
         *
         * @param array $parameters
-        * @param array $options
+        * @param Settings $settings
         */
-       public function __construct( $parameters, $options ) {
-               if ( !is_array( $parameters ) ) {
-                       throw new MWException( 'Parameters array is not 
initialized' );
-               }
-
-               $this->options = $options;
+       public function __construct( array $parameters, Settings $settings ) {
+               $this->settings = $settings;
                $this->parse( $parameters );
        }
 
@@ -140,22 +142,6 @@
        }
 
        /**
-        * Get value for a specific option
-        *
-        * Options are available for local context therefore no public exposure
-        *
-        * @since  1.9
-        *
-        * @return mixed
-        */
-       protected function getOption( $name ) {
-               if ( !isset( $this->options[$name] ) ) {
-                       throw new MWException( "Option {$name} is not 
available" );
-               }
-               return $this->options[$name];
-       }
-
-       /**
         * Returns the "Julian day" value from an object of type
         * SMWTimeValue.
         */
@@ -203,7 +189,7 @@
                                                break;
                                        case 'limit':
                                                // Override default limit with 
query specific limit
-                                               
$this->options['DefaultNumRecurringEvents'] = (int)$value;
+                                               $this->settings->set( 
'smwgDefaultNumRecurringEvents', (int)$value );
                                                break;
                                        case 'unit':
                                                $unit = $value;
@@ -375,9 +361,9 @@
 
                        // should we stop?
                        if ( is_null( $end_date ) ) {
-                               $reached_end_date = $i > $this->getOption( 
'DefaultNumRecurringEvents' );
+                               $reached_end_date = $i > $this->settings->get( 
'smwgDefaultNumRecurringEvents' );
                        } else {
-                               $reached_end_date = ( $cur_date_jd > 
$end_date_jd ) || ( $i > $this->getOption( 'MaxNumRecurringEvents' ) );
+                               $reached_end_date = ( $cur_date_jd > 
$end_date_jd ) || ( $i > $this->settings->get( 'smwgMaxNumRecurringEvents' ) );
                        }
                } while ( !$reached_end_date );
 
@@ -398,5 +384,4 @@
                $timeValue->setDataItem( $timeDataItem );
                return $timeValue;
        }
-
-}
\ No newline at end of file
+}
diff --git a/includes/Settings.php b/includes/Settings.php
new file mode 100644
index 0000000..d92dceb
--- /dev/null
+++ b/includes/Settings.php
@@ -0,0 +1,88 @@
+<?php
+
+namespace SMW;
+
+use MWException;
+
+/**
+ * Encapsulate settings (such as $GLOBALS) in an instantiatable 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
+ *
+ * @since 1.9
+ *
+ * @file
+ * @ingroup SMW
+ *
+ * @licence GNU GPL v2+
+ * @author Jeroen De Dauw < [email protected] >
+ * @author mwjames
+ */
+
+/**
+ * This class encapsulates settings (such as $GLOBALS) in order to make it
+ * an instantiatable object
+ *
+ * @ingroup SMW
+ */
+class Settings {
+
+       /**
+        * Defines settings
+        * @var array
+        */
+       protected $settings;
+
+       /**
+        * Constructor
+        *
+        * @since 1.9
+        *
+        * @param array $settings
+        */
+       public function __construct( array $settings ) {
+               $this->settings = $settings;
+       }
+
+       /**
+        * Overrides settings for a specific name in a provided class context
+        *
+        * @since 1.9
+        *
+        * @param string $name
+        * @param string $value
+        */
+       public function set( $name, $value ) {
+               $this->settings[$name] = $value;
+               return $this;
+       }
+
+       /**
+        * Returns settings for a specific name
+        *
+        * @since 1.9
+        *
+        * @param string $name
+        *
+        * @return mixed
+        */
+       public function get( $name ) {
+               if ( !isset( $this->settings[$name] ) ) {
+                       throw new MWException( "Setting {$name} is not 
available" );
+               }
+               return $this->settings[$name];
+       }
+}
diff --git a/includes/Setup.php b/includes/Setup.php
index 93ccc54..5a91352 100644
--- a/includes/Setup.php
+++ b/includes/Setup.php
@@ -138,6 +138,8 @@
        $wgAutoloadClasses['SMW\IParameterFormatter']      = $incDir . 
'ParserParameterFormatter.php';
        $wgAutoloadClasses['SMW\ParserParameterFormatter'] = $incDir . 
'ParserParameterFormatter.php';
 
+       $wgAutoloadClasses['SMW\Settings']              = $incDir . 
'Settings.php';
+
        // Article pages
        $apDir = $smwgIP . 'includes/articlepages/';
        $wgAutoloadClasses['SMWOrderedListPage']        = $apDir . 
'SMW_OrderedListPage.php';
diff --git a/includes/parserhooks/RecurringEventsParserFunction.php 
b/includes/parserhooks/RecurringEventsParserFunction.php
index fe1885a..bafb67c 100644
--- a/includes/parserhooks/RecurringEventsParserFunction.php
+++ b/includes/parserhooks/RecurringEventsParserFunction.php
@@ -48,8 +48,6 @@
        /**
         * Parse parameters and return results to the ParserOutput object
         *
-        * @todo Replace options array and invoke GLOBALS using Settings::get( 
$GLOBALS )
-        *
         * @since 1.9
         *
         * @param IParameterFormatter $parameters
@@ -58,14 +56,8 @@
         */
        public function parse( IParameterFormatter $parameters ) {
 
-               // Invoke global settings as options array
-               $options = array(
-                       'DefaultNumRecurringEvents' => 
$GLOBALS['smwgDefaultNumRecurringEvents'],
-                       'MaxNumRecurringEvents'=> 
$GLOBALS['smwgMaxNumRecurringEvents']
-               );
-
                // Get recurring events
-               $events = new RecurringEvents( $parameters->toArray(), $options 
);
+               $events = new RecurringEvents( $parameters->toArray(), 
$this->settings );
                $this->parserData->setError( $events->getErrors() );
 
                foreach ( $events->getDates() as $date_str ) {
@@ -121,7 +113,8 @@
        public static function render( Parser &$parser ) {
                $instance = new self(
                        new ParserData( $parser->getTitle(), 
$parser->getOutput() ),
-                       new Subobject( $parser->getTitle() )
+                       new Subobject( $parser->getTitle() ),
+                       new Settings( $GLOBALS )
                );
                return $instance->parse( new ParserParameterFormatter( 
func_get_args() ) );
        }
diff --git a/includes/parserhooks/SubobjectParserFunction.php 
b/includes/parserhooks/SubobjectParserFunction.php
index c551ca4..7a756af 100644
--- a/includes/parserhooks/SubobjectParserFunction.php
+++ b/includes/parserhooks/SubobjectParserFunction.php
@@ -58,10 +58,12 @@
         *
         * @param IParserData $parserData
         * @param Subobject $subobject
+        * @param Settings $settings
         */
-       public function __construct( IParserData $parserData, Subobject 
$subobject ) {
+       public function __construct( IParserData $parserData, Subobject 
$subobject, Settings $settings ) {
                $this->parserData = $parserData;
                $this->subobject = $subobject;
+               $this->settings = $settings;
        }
 
        /**
@@ -144,7 +146,8 @@
        public static function render( Parser &$parser ) {
                $instance = new self(
                        new ParserData( $parser->getTitle(), 
$parser->getOutput() ),
-                       new Subobject( $parser->getTitle() )
+                       new Subobject( $parser->getTitle() ),
+                       new Settings( $GLOBALS )
                );
                return $instance->parse( new ParserParameterFormatter( 
func_get_args() ) );
        }
diff --git a/tests/phpunit/includes/RecurringEventsTest.php 
b/tests/phpunit/includes/RecurringEventsTest.php
index 3c72120..074d987 100644
--- a/tests/phpunit/includes/RecurringEventsTest.php
+++ b/tests/phpunit/includes/RecurringEventsTest.php
@@ -4,6 +4,7 @@
 
 use SMW\RecurringEvents;
 use SMW\ParserParameterFormatter;
+use SMW\Settings;
 
 /**
  * Tests for the SMW\RecurringEvents class.
@@ -23,9 +24,9 @@
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  * http://www.gnu.org/copyleft/gpl.html
  *
- * @file
  * @since 1.9
  *
+ * @file
  * @ingroup SMW
  * @ingroup Test
  *
@@ -249,21 +250,20 @@
        private function getInstance( array $params ) {
                $parameters = $this->getParameters( $params );
 
-               // Options settings
-               $options = array(
-                       'DefaultNumRecurringEvents' => 10,
-                       'MaxNumRecurringEvents'=> 50
+               $settings = new Settings( array(
+                       'smwgDefaultNumRecurringEvents' => 10,
+                       'smwgMaxNumRecurringEvents' => 50 )
                );
 
-               return new RecurringEvents( $parameters, $options );
+               return new RecurringEvents( $parameters, $settings );
        }
 
        /**
         * Test parameters exceptions
         *
-        * @expectedException MWException
         */
        public function testMissingParametersExceptions() {
+               $this->setExpectedException( 'PHPUnit_Framework_Error' );
                $instance = new RecurringEvents( '' , '' );
                $this->assertInstanceOf( 'SMW\RecurringEvents', $instance );
        }
@@ -271,9 +271,9 @@
        /**
         * Test options exceptions
         *
-        * @expectedException MWException
         */
        public function testMissingOptionsExceptions() {
+               $this->setExpectedException( 'PHPUnit_Framework_Error' );
                $params = array( 'property=Has birthday', 'start=01 Feb 1970', 
'has title=Birthday', 'unit=month', 'period=12' );
                $parameters = $this->getParameters( $params );
 
diff --git a/tests/phpunit/includes/SettingsTest.php 
b/tests/phpunit/includes/SettingsTest.php
new file mode 100644
index 0000000..7690edc
--- /dev/null
+++ b/tests/phpunit/includes/SettingsTest.php
@@ -0,0 +1,129 @@
+<?php
+
+namespace SMW\Test;
+
+use SMW\Settings;
+use MWException;
+
+/**
+ * Tests for the SMW\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
+ *
+ * @since 1.9
+ *
+ * @file
+ * @ingroup SMW
+ * @ingroup Test
+ *
+ * @group SMW
+ * @group SMWExtension
+ *
+ * @licence GNU GPL v2+
+ * @author Jeroen De Dauw < [email protected] >
+ * @author mwjames
+ */
+
+/**
+ * This class tests methods provided by the SMW\Settings class
+ *
+ * @ingroup SMW
+ * @ingroup Test
+ */
+class SettingsTest extends \MediaWikiTestCase {
+
+       /**
+        * Data provider
+        *
+        * @return array
+        */
+       public function dataSettingsProvider() {
+               // Using $this->arrayWrap( $settingArrays ) crashed PHP on 
Windows
+               return array( array(
+                       array(),
+                       array( 'foo' => 'bar' ),
+                       array( 'foo' => 'bar', 'baz' => 'BAH' ),
+                       array( 'bar' => array( '9001' ) ),
+                       array( '~[,,_,,]:3' => array( 9001, 4.2 ) ),
+               ) );
+       }
+
+       /**
+        * Test constructor
+        *
+        * @dataProvider dataSettingsProvider
+        *
+        * @since 1.9
+        *
+        * @param array $settings
+        */
+       public function testConstructor( array $settings ) {
+               $instance = new Settings( $settings );
+               $this->assertInstanceOf( 'SMW\Settings', $instance );
+       }
+
+       /**
+        * Test get()
+        *
+        * @covers Settings::get
+        * @dataProvider dataSettingsProvider
+        *
+        * @since 1.9
+        *
+        * @param array $settings
+        */
+       public function testGet( array $settings ) {
+               $settingsObject = new Settings( $settings );
+
+               foreach ( $settings as $name => $value ) {
+                       $this->assertEquals( $value, $settingsObject->get( 
$name ) );
+               }
+
+               $this->assertTrue( true );
+       }
+
+       /**
+        * Test set()
+        *
+        * @covers Settings::set
+        * @dataProvider dataSettingsProvider
+        *
+        * @since 1.9
+        *
+        * @param array $settings
+        */
+       public function testSet( array $settings ) {
+               $settingsObject = new Settings( array() );
+
+               foreach ( $settings as $name => $value ) {
+                       $settingsObject->set( $name, $value );
+                       $this->assertEquals( $value, $settingsObject->get( 
$name ) );
+               }
+
+               $this->assertTrue( true );
+       }
+
+       /**
+        * Test exception
+        *
+        * @since 1.9
+        */
+       public function testSettingsNameExceptions() {
+               $this->setExpectedException( 'MWException' );
+               $settingsObject = new Settings( array( 'Foo' => 'bar' ) );
+               $this->assertEquals( 'bar', $settingsObject->get( 'foo' ) );
+       }
+}
diff --git 
a/tests/phpunit/includes/parserhooks/RecurringEventsParserFunctionTest.php 
b/tests/phpunit/includes/parserhooks/RecurringEventsParserFunctionTest.php
index a4d2916..226918b 100644
--- a/tests/phpunit/includes/parserhooks/RecurringEventsParserFunctionTest.php
+++ b/tests/phpunit/includes/parserhooks/RecurringEventsParserFunctionTest.php
@@ -6,6 +6,7 @@
 use SMW\ParserData;
 use SMW\Subobject;
 use SMW\ParserParameterFormatter;
+use SMW\Settings;
 
 use SMWDIWikiPage;
 use SMWDataValueFactory;
@@ -396,7 +397,8 @@
        private function getInstance( $title, $parserOutput ) {
                return new RecurringEventsParserFunction(
                        new ParserData( $this->getTitle( $title ), 
$parserOutput ),
-                       new Subobject( $this->getTitle( $title ) )
+                       new Subobject( $this->getTitle( $title ) ),
+                       new Settings( $GLOBALS )
                );
        }
 
diff --git a/tests/phpunit/includes/parserhooks/SubobjectParserFunctionTest.php 
b/tests/phpunit/includes/parserhooks/SubobjectParserFunctionTest.php
index 228fb9c..6d6fc1d 100644
--- a/tests/phpunit/includes/parserhooks/SubobjectParserFunctionTest.php
+++ b/tests/phpunit/includes/parserhooks/SubobjectParserFunctionTest.php
@@ -6,6 +6,7 @@
 use SMW\ParserData;
 use SMW\Subobject;
 use SMW\ParserParameterFormatter;
+use SMW\Settings;
 
 use SMWDIProperty;
 use SMWDataItem;
@@ -32,9 +33,9 @@
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  * http://www.gnu.org/copyleft/gpl.html
  *
- * @file
  * @since 1.9
  *
+ * @file
  * @ingroup SMW
  * @ingroup Test
  *
@@ -205,7 +206,8 @@
        private function getInstance( $title, $parserOutput ) {
                return new SubobjectParserFunction(
                        new ParserData( $this->getTitle( $title ), 
$parserOutput ),
-                       new Subobject( $this->getTitle( $title ) )
+                       new Subobject( $this->getTitle( $title ) ),
+                       new Settings( $GLOBALS )
                );
        }
 

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I546f79d60e08e487dc12733b1c3c9c76983dd8d6
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/SemanticMediaWiki
Gerrit-Branch: master
Gerrit-Owner: Mwjames <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to