Jeroen De Dauw has submitted this change and it was merged.

Change subject: Move SMWDataValueFactory and add tests
......................................................................


Move SMWDataValueFactory and add tests

Change-Id: Ia8e39a3e33c623cf12b9cac6ced766c3a0d0af4a
---
R includes/DataValueFactory.php
M includes/Setup.php
M tests/phpunit/includes/DataValueFactoryTest.php
3 files changed, 201 insertions(+), 20 deletions(-)

Approvals:
  Jeroen De Dauw: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/includes/SMW_DataValueFactory.php b/includes/DataValueFactory.php
similarity index 98%
rename from includes/SMW_DataValueFactory.php
rename to includes/DataValueFactory.php
index f34f20b..e490460 100644
--- a/includes/SMW_DataValueFactory.php
+++ b/includes/DataValueFactory.php
@@ -1,4 +1,12 @@
 <?php
+
+namespace SMW;
+
+use SMWDataItem;
+use SMWErrorValue;
+use SMWDIProperty;
+use SMWPropertyValue;
+
 /**
  * This file contains the SMWDataValueFactory class.
  *
@@ -23,7 +31,7 @@
  *
  * @ingroup SMWDataValues
  */
-class SMWDataValueFactory {
+class DataValueFactory {
 
        /**
         * Array of type labels indexed by type ids. Used for datatype 
resolution.
@@ -423,3 +431,10 @@
        }
 
 }
+
+/**
+ * SMWDataValueFactory
+ *
+ * @deprecated since SMW 1.9
+ */
+class_alias( 'SMW\DataValueFactory', 'SMWDataValueFactory' );
diff --git a/includes/Setup.php b/includes/Setup.php
index 5a91352..faa8469 100644
--- a/includes/Setup.php
+++ b/includes/Setup.php
@@ -120,7 +120,6 @@
        $wgAutoloadClasses['SMW\ParameterInput']        = $incDir . 
'ParameterInput.php';
        $wgAutoloadClasses['SMW\MessageReporter']        = $incDir . 
'MessageReporter.php';
        $wgAutoloadClasses['SMW\ObservableMessageReporter'] = $incDir . 
'MessageReporter.php';
-       $wgAutoloadClasses['SMWDataValueFactory']       = $incDir . 
'SMW_DataValueFactory.php';
        $wgAutoloadClasses['SMWFactbox']                = $incDir . 
'SMW_Factbox.php';
        $wgAutoloadClasses['SMWInfolink']               = $incDir . 
'SMW_Infolink.php';
        $wgAutoloadClasses['SMWOutputs']                = $incDir . 
'SMW_Outputs.php';
@@ -129,6 +128,9 @@
        $wgAutoloadClasses['SMWSemanticData']           = $incDir . 
'SMW_SemanticData.php';
        $wgAutoloadClasses['SMWPageLister']             = $incDir . 
'SMW_PageLister.php';
 
+       $wgAutoloadClasses['SMWDataValueFactory']       = $incDir . 
'DataValueFactory.php';
+       $wgAutoloadClasses['SMW\DataValueFactory']      = $incDir . 
'DataValueFactory.php';
+
        $wgAutoloadClasses['SMWParseData']              = $incDir . 
'SMW_ParseData.php';
        $wgAutoloadClasses['SMW\IParserData']           = $incDir . 
'ParserData.php';
        $wgAutoloadClasses['SMW\ParserData']            = $incDir . 
'ParserData.php';
diff --git a/tests/phpunit/includes/DataValueFactoryTest.php 
b/tests/phpunit/includes/DataValueFactoryTest.php
index 092fbae..9be3895 100644
--- a/tests/phpunit/includes/DataValueFactoryTest.php
+++ b/tests/phpunit/includes/DataValueFactoryTest.php
@@ -2,11 +2,12 @@
 
 namespace SMW\Test;
 
-use SMWDataValueFactory;
+use SMW\DataValueFactory;
 use SMWDataItem;
+use SMWPropertyValue;
 
 /**
- * Tests for the SMWDataValueFactory class
+ * Tests for the SMW\DataValueFactory 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
@@ -37,7 +38,7 @@
  */
 
 /**
- * Tests for the SMWDataValueFactory class
+ * Tests for the SMW\DataValueFactory class
  *
  * @ingroup SMW
  * @ingroup Test
@@ -49,33 +50,119 @@
         *
         * @return array
         */
-       public function getPropertyValueDataProvider() {
+       public function getDataItemIdDataProvider() {
                return array(
-                       array( 'Foo'  , 'Bar'          , 'Bar'          , 
'SMWDataValue' ),
-                       array( 'Foo'  , 'Bar[[ Foo ]]' , 'Bar[[ Foo ]]' , 
'SMWDataValue' ),
-                       array( 'Foo'  , '9001'         , '9001'         , 
'SMWDataValue' ),
-                       array( 'Foo'  , 1001           , '1001'         , 
'SMWDataValue' ),
-                       array( 'Foo'  , '-%&$*'        , '-%&$*'        , 
'SMWDataValue' ),
-                       array( 'Foo'  , '_Bar'         , 'Bar'          , 
'SMWDataValue' ),
-                       array( 'Foo'  , 'bar'          , 'Bar'          , 
'SMWDataValue' ),
-                       array( '-Foo' , 'Bar'          , ''             , 
'SMWErrorValue' ),
-                       array( '_Foo' , 'Bar'          , ''             , 
'SMWPropertyValue' ),
+                       array( '_txt' , SMWDataItem::TYPE_BLOB ), // #0
+                       array( '_wpg' , SMWDataItem::TYPE_WIKIPAGE ), // #1
+                       array( '_num' , SMWDataItem::TYPE_NUMBER ), // #2
+                       array( '_dat' , SMWDataItem::TYPE_TIME ), // #3
+                       array( '_uri' , SMWDataItem::TYPE_URI ), // #4
                );
        }
 
        /**
-        * @dataProvider getPropertyValueDataProvider
-        *
-        * @see SMWDataValueFactory::addPropertyValue
+        * Test DataValueFactory::getDataItemId
         * @since 1.9
         *
+        * @dataProvider getDataItemIdDataProvider
+        * @param $typeId
+        * @param $expectedId
+        */
+       public function testGetDataItemId( $typeId, $expectedId ) {
+               $this->assertEquals( $expectedId, 
DataValueFactory::getDataItemId( $typeId ) );
+       }
+
+       /**
+        * DataProvider
+        *
+        * @return array
+        */
+       public function getTypeIdValueDataProvider() {
+               return array(
+                       array( '_txt'  , 'Bar'          , 'Bar'          , 
'SMWStringValue' ), // #0
+                       array( '_txt'  , 'Bar[[ Foo ]]' , 'Bar[[ Foo ]]' , 
'SMWStringValue' ), // #1
+                       array( '_txt'  , '9001'         , '9001'         , 
'SMWStringValue' ), // #2
+                       array( '_txt'  , 1001           , '1001'         , 
'SMWStringValue' ), // #3
+                       array( '_txt'  , '-%&$*'        , '-%&$*'        , 
'SMWStringValue' ), // #4
+                       array( '_txt'  , '_Bar'         , '_Bar'         , 
'SMWStringValue' ), // #5
+                       array( '_txt'  , 'bar'          , 'bar'          , 
'SMWStringValue' ), // #6
+                       array( '-_txt' , 'Bar'          , 'Bar'          , 
'SMWErrorValue' ), // #7
+
+                       array( '_wpg'  , 'Bar'          , 'Bar'          , 
'SMWWikiPageValue' ), // #8
+                       array( '_wpg'  , 'Bar'          , 'Bar'          , 
'SMWWikiPageValue' ), // #9
+                       array( '_wpg'  , 'Bar[[ Foo ]]' , 'Bar[[ Foo ]]' , 
'SMWWikiPageValue' ), // #10
+                       array( '_wpg'  , '9001'         , '9001'         , 
'SMWWikiPageValue' ), // #11
+                       array( '_wpg'  , 1001           , '1001'         , 
'SMWWikiPageValue' ), // #12
+                       array( '_wpg'  , '-%&$*'        , '-%&$*'        , 
'SMWWikiPageValue' ), // #13
+                       array( '_wpg'  , '_Bar'         , 'Bar'          , 
'SMWWikiPageValue' ), // #14
+                       array( '_wpg'  , 'bar'          , 'Bar'          , 
'SMWWikiPageValue' ), // #15
+                       array( '-_wpg' , 'Bar'          , 'Bar'          , 
'SMWErrorValue' ), // #16
+
+                       array( '_dat' , '1 Jan 1970', '1 Jan 1970' , 
'SMWTimeValue' ), // #0
+                       array( '_uri' , 'Foo', 'Foo' , 'SMWURIValue' ), // #0
+                       array( '_num' , 9001, '9,001' , 'SMWNumberValue' ), // 
#0
+               );
+       }
+
+       /**
+        * Test DataValueFactory::newTypeIdValue
+        * @since 1.9
+        *
+        * @dataProvider getTypeIdValueDataProvider
+        * @param $typeId
+        * @param $value
+        * @param $expectedValue
+        * @param $expectedInstance
+        */
+       public function testNewTypeIdValue( $typeId, $value, $expectedValue, 
$expectedInstance ) {
+               $dataValue = DataValueFactory::newTypeIdValue( $typeId, $value 
);
+               $this->assertInstanceOf( $expectedInstance , $dataValue );
+
+               if ( $dataValue->getErrors() === array() ){
+                       $this->assertEquals( $expectedValue, 
$dataValue->getWikiValue() );
+               } else {
+                       $this->assertInternalType( 'array', 
$dataValue->getErrors() );
+               }
+       }
+
+       /**
+        * DataProvider
+        *
+        * @return array
+        */
+       public function getPropertyObjectValueDataProvider() {
+               return array(
+                       array( 'Foo'  , 'Bar'          , 'Bar'          , 
'SMWDataValue' ), // #0
+                       array( 'Foo'  , 'Bar[[ Foo ]]' , 'Bar[[ Foo ]]' , 
'SMWDataValue' ), // #1
+                       array( 'Foo'  , '9001'         , '9001'         , 
'SMWDataValue' ), // #2
+                       array( 'Foo'  , 1001           , '1001'         , 
'SMWDataValue' ), // #3
+                       array( 'Foo'  , '-%&$*'        , '-%&$*'        , 
'SMWDataValue' ), // #4
+                       array( 'Foo'  , '_Bar'         , 'Bar'          , 
'SMWDataValue' ), // #5
+                       array( 'Foo'  , 'bar'          , 'Bar'          , 
'SMWDataValue' ), // #6
+                       array( '-Foo' , 'Bar'          , 'Bar'          , 
'SMWWikiPageValue' ), // #7
+
+                       // Will fail with "must be an instance of 
SMWDIProperty, instance of SMWDIError give"
+                       // as propertyDI isn't checked therefore 
addPropertyValue() should be
+                       // used as it will return a proper object
+                       // array( '_Foo' , 'Bar'          , ''             , 
'SMWDIProperty' ), // #8
+               );
+       }
+
+       /**
+        * Test DataValueFactory::newPropertyObjectValue
+        * @since 1.9
+        *
+        * @dataProvider getPropertyObjectValueDataProvider
         * @param $propertyName
         * @param $value
         * @param $expectedValue
         * @param $expectedInstance
         */
-       public function testAddPropertyValue( $propertyName, $value, 
$expectedValue, $expectedInstance ) {
-               $dataValue = SMWDataValueFactory::newPropertyValue( 
$propertyName, $value );
+       public function testNewPropertyObjectValue( $propertyName, $value, 
$expectedValue, $expectedInstance ) {
+               $propertyDV = SMWPropertyValue::makeUserProperty( $propertyName 
);
+               $propertyDI = $propertyDV->getDataItem();
+
+               $dataValue = DataValueFactory::newPropertyObjectValue( 
$propertyDI, $value );
 
                // Check the returned instance
                $this->assertInstanceOf( $expectedInstance , $dataValue );
@@ -90,4 +177,81 @@
                        $this->assertInternalType( 'array', 
$dataValue->getErrors() );
                }
        }
+
+       /**
+        * DataProvider
+        *
+        * @return array
+        */
+       public function getPropertyValueDataProvider() {
+               return array(
+                       array( 'Foo'  , 'Bar'          , 'Bar'          , 
'SMWDataValue' ), // #0
+                       array( 'Foo'  , 'Bar[[ Foo ]]' , 'Bar[[ Foo ]]' , 
'SMWDataValue' ), // #1
+                       array( 'Foo'  , '9001'         , '9001'         , 
'SMWDataValue' ), // #2
+                       array( 'Foo'  , 1001           , '1001'         , 
'SMWDataValue' ), // #3
+                       array( 'Foo'  , '-%&$*'        , '-%&$*'        , 
'SMWDataValue' ), // #4
+                       array( 'Foo'  , '_Bar'         , 'Bar'          , 
'SMWDataValue' ), // #5
+                       array( 'Foo'  , 'bar'          , 'Bar'          , 
'SMWDataValue' ), // #6
+                       array( '-Foo' , 'Bar'          , ''             , 
'SMWErrorValue' ), // #7
+                       array( '_Foo' , 'Bar'          , ''             , 
'SMWPropertyValue' ), // #8
+               );
+       }
+
+       /**
+        * Test DataValueFactory::addPropertyValue
+        * @since 1.9
+        *
+        * @dataProvider getPropertyValueDataProvider
+        * @param $propertyName
+        * @param $value
+        * @param $expectedValue
+        * @param $expectedInstance
+        */
+       public function testAddPropertyValue( $propertyName, $value, 
$expectedValue, $expectedInstance ) {
+               $dataValue = DataValueFactory::newPropertyValue( $propertyName, 
$value );
+
+               // Check the returned instance
+               $this->assertInstanceOf( $expectedInstance , $dataValue );
+
+               if ( $dataValue->getErrors() === array() ){
+                       $this->assertInstanceOf( 'SMWDIProperty', 
$dataValue->getProperty() );
+                       $this->assertContains( $propertyName, 
$dataValue->getProperty()->getLabel() );
+                       if ( $dataValue->getDataItem()->getDIType() === 
SMWDataItem::TYPE_WIKIPAGE ){
+                               $this->assertEquals( $expectedValue, 
$dataValue->getWikiValue() );
+                       }
+               } else {
+                       $this->assertInternalType( 'array', 
$dataValue->getErrors() );
+               }
+       }
+
+       /**
+        * DataProvider
+        *
+        * @return array
+        */
+       public function getFindTypeIdDataProvider() {
+               return array(
+                       array( 'URL'      , '_uri' ), // #0
+                       array( 'Page'     , '_wpg' ), // #1
+                       array( 'String'   , '_txt' ), // #2
+                       array( 'Text'     , '_txt' ), // #3
+                       array( 'Number'   , '_num' ), // #4
+                       array( 'Quantity' , '_qty' ), // #5
+                       array( 'Date'     , '_dat' ), // #6
+                       array( 'Email'    , '_ema' ), // #7
+               );
+       }
+
+       /**
+        * Test DataValueFactory::findTypeID
+        * @since 1.9
+        *
+        * @dataProvider getFindTypeIdDataProvider
+        * @param $typeId
+        * @param $expectedId
+        */
+       public function testFindTypeID( $typeId, $expectedId ) {
+               $this->assertEquals( $expectedId, DataValueFactory::findTypeID( 
$typeId ) );
+       }
+
 }

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

Gerrit-MessageType: merged
Gerrit-Change-Id: Ia8e39a3e33c623cf12b9cac6ced766c3a0d0af4a
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/SemanticMediaWiki
Gerrit-Branch: master
Gerrit-Owner: Mwjames <[email protected]>
Gerrit-Reviewer: Jeroen De Dauw <[email protected]>
Gerrit-Reviewer: jenkins-bot

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

Reply via email to