Aleksey Bekh-Ivanov (WMDE) has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/355427 )

Change subject: Simplify ChangeOpsMergeTest with ItemBuilder
......................................................................

Simplify ChangeOpsMergeTest with ItemBuilder

Change-Id: I158ec2719a3d8ffd4943ec10b041996890f6e18e
---
M repo/tests/phpunit/includes/ChangeOp/ChangeOpsMergeTest.php
A repo/tests/phpunit/includes/ItemBuilder.php
2 files changed, 300 insertions(+), 103 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase 
refs/changes/27/355427/1

diff --git a/repo/tests/phpunit/includes/ChangeOp/ChangeOpsMergeTest.php 
b/repo/tests/phpunit/includes/ChangeOp/ChangeOpsMergeTest.php
index cd4d64c..876d363 100644
--- a/repo/tests/phpunit/includes/ChangeOp/ChangeOpsMergeTest.php
+++ b/repo/tests/phpunit/includes/ChangeOp/ChangeOpsMergeTest.php
@@ -21,7 +21,7 @@
 use Wikibase\DataModel\Snak\PropertyValueSnak;
 use Wikibase\DataModel\Snak\SnakList;
 use Wikibase\DataModel\Statement\Statement;
-use Wikibase\DataModel\Statement\StatementList;
+use Wikibase\Repo\Tests\ItemBuilder;
 use Wikibase\Repo\Validators\EntityConstraintProvider;
 use Wikibase\Repo\Validators\EntityValidator;
 
@@ -150,7 +150,7 @@
        }
 
        private function newItemWithId( $idString ) {
-               return new Item( new ItemId( $idString ) );
+               return ItemBuilder::create()->withId( $idString )->build();
        }
 
        /**
@@ -195,55 +195,61 @@
         */
        public function provideData() {
                $testCases = array();
+               $emptyItem = ItemBuilder::create()->build();
 
-               $itemWithEnLabel = new Item();
-               $itemWithEnLabel->getFingerprint()->setLabel( 'en', 'foo' );
+               $itemWithEnLabel = ItemBuilder::create()
+                       ->withLabel( 'en', 'foo' )
+                       ->build();
 
                $testCases['labelMerge'] = array(
                        $itemWithEnLabel->copy(),
-                       new Item(),
-                       new Item(),
+                       $emptyItem->copy(),
+                       $emptyItem->copy(),
                        $itemWithEnLabel->copy(),
                );
                $testCases['identicalLabelMerge'] = array(
                        $itemWithEnLabel->copy(),
                        $itemWithEnLabel->copy(),
-                       new Item(),
+                       $emptyItem->copy(),
                        $itemWithEnLabel->copy(),
                );
 
-               $itemWithEnBarLabel = new Item();
-               $itemWithEnBarLabel->setLabel( 'en', 'bar' );
+               $itemWithEnBarLabel = ItemBuilder::create()
+                       ->withLabel( 'en', 'bar' )
+                       ->build();
 
-               $itemWithLabelAndAlias = new Item();
-               $itemWithLabelAndAlias->setLabel( 'en', 'bar' );
-               $itemWithLabelAndAlias->setAliases( 'en', [ 'foo' ] );
+               $itemWithLabelAndAlias = ItemBuilder::create()
+                       ->withLabel( 'en', 'bar' )
+                       ->withAliases( 'en', [ 'foo' ] )
+                       ->build();
 
                $testCases['labelAsAliasMerge'] = array(
                        $itemWithEnLabel->copy(),
                        $itemWithEnBarLabel->copy(),
-                       new Item(),
+                       $emptyItem->copy(),
                        $itemWithLabelAndAlias->copy()
                );
 
-               $itemWithDescription = new Item();
-               $itemWithDescription->setDescription( 'en', 'foo' );
+               $itemWithDescription = ItemBuilder::create()
+                       ->withDescription( 'en', 'foo' )
+                       ->build();
 
                $testCases['descriptionMerge'] = array(
                        $itemWithDescription->copy(),
-                       new Item(),
-                       new Item(),
+                       $emptyItem->copy(),
+                       $emptyItem->copy(),
                        $itemWithDescription->copy(),
                );
                $testCases['identicalDescriptionMerge'] = array(
                        $itemWithDescription->copy(),
                        $itemWithDescription->copy(),
-                       new Item(),
+                       $emptyItem->copy(),
                        $itemWithDescription->copy(),
                );
 
-               $itemWithBarDescription = new Item();
-               $itemWithBarDescription->setDescription( 'en', 'bar' );
+               $itemWithBarDescription = ItemBuilder::create()
+                       ->withDescription( 'en', 'bar' )
+                       ->build();
                $testCases['ignoreConflictDescriptionMerge'] = array(
                        $itemWithDescription->copy(),
                        $itemWithBarDescription->copy(),
@@ -252,45 +258,49 @@
                        array( 'description' )
                );
 
-               $itemWithFooBarAliases = new Item();
-               $itemWithFooBarAliases->setAliases( 'en', [ 'foo', 'bar' ] );
+               $itemWithFooBarAliases = ItemBuilder::create()
+                       ->withAliases( 'en', [ 'foo', 'bar' ] )
+                       ->build();
 
                $testCases['aliasMerge'] = array(
                        $itemWithFooBarAliases->copy(),
-                       new Item(),
-                       new Item(),
+                       $emptyItem->copy(),
+                       $emptyItem->copy(),
                        $itemWithFooBarAliases->copy(),
                );
 
-               $itemWithFooBarBazAliases = new Item();
-               $itemWithFooBarBazAliases->setAliases( 'en', [ 'foo', 'bar', 
'baz' ] );
+               $itemWithFooBarBazAliases = ItemBuilder::create()
+                       ->withAliases( 'en', [ 'foo', 'bar', 'baz' ] )
+                       ->build();
 
                $testCases['duplicateAliasMerge'] = array(
                        $itemWithFooBarAliases->copy(),
                        $itemWithFooBarBazAliases->copy(),
-                       new Item(),
+                       $emptyItem->copy(),
                        $itemWithFooBarBazAliases->copy(),
                );
 
-               $itemWithLink = new Item();
-               $itemWithLink->getSiteLinkList()->addNewSiteLink( 'enwiki', 
'foo' );
+               $itemWithLink = ItemBuilder::create()
+                       ->withSiteLink( 'enwiki', 'foo' )
+                       ->build();
 
                $testCases['linkMerge'] = array(
                        $itemWithLink->copy(),
-                       new Item(),
-                       new Item(),
+                       $emptyItem->copy(),
+                       $emptyItem->copy(),
                        $itemWithLink->copy(),
                );
 
                $testCases['sameLinkLinkMerge'] = array(
                        $itemWithLink->copy(),
                        $itemWithLink->copy(),
-                       new Item(),
+                       $emptyItem->copy(),
                        $itemWithLink->copy(),
                );
 
-               $itemWithBarLink = new Item();
-               $itemWithBarLink->getSiteLinkList()->addNewSiteLink( 'enwiki', 
'bar' );
+               $itemWithBarLink = ItemBuilder::create()
+                       ->withSiteLink( 'enwiki', 'bar' )
+                       ->build();
 
                $testCases['ignoreConflictLinkMerge'] = array(
                        $itemWithLink->copy(),
@@ -303,12 +313,13 @@
                $statement = new Statement( new PropertyNoValueSnak( new 
PropertyId( 'P56' ) ) );
                $statement->setGuid( 
'Q111$D8404CDA-25E4-4334-AF13-A390BCD9C556' );
 
-               $itemWithStatement = new Item();
-               $itemWithStatement->getStatements()->addStatement( $statement );
+               $itemWithStatement = ItemBuilder::create()
+                       ->withStatement( $statement )
+                       ->build();
                $testCases['statementMerge'] = array(
                        $itemWithStatement->copy(),
-                       new Item(),
-                       new Item(),
+                       $emptyItem->copy(),
+                       $emptyItem->copy(),
                        $itemWithStatement->copy()
                );
 
@@ -318,13 +329,14 @@
                );
                $qualifiedStatement->setGuid( 
'Q111$D8404CDA-25E4-4334-AF13-A390BCD9C556' );
 
-               $itemWithQualifiedStatement = new Item();
-               $itemWithQualifiedStatement->getStatements()->addStatement( 
$qualifiedStatement );
+               $itemWithQualifiedStatement = ItemBuilder::create()
+                       ->withStatement( $qualifiedStatement )
+                       ->build();
 
                $testCases['statementWithQualifierMerge'] = array(
                        $itemWithQualifiedStatement->copy(),
-                       new Item(),
-                       new Item(),
+                       $emptyItem->copy(),
+                       $emptyItem->copy(),
                        $itemWithQualifiedStatement->copy()
                );
 
@@ -339,23 +351,23 @@
                );
                $selfReferencingStatement->setGuid( 
'Q111$D74D43D7-BD8F-4240-A058-24C5171ABBFA' );
 
-               $bigItem = new Item();
-               $bigItem->setId( 111 );
-               $bigItem->setLabel( 'en', 'foo' );
-               $bigItem->setLabel( 'pt', 'ptfoo' );
-               $bigItem->setDescription( 'en', 'foo' );
-               $bigItem->setDescription( 'pl', 'pldesc' );
-               $bigItem->setAliases( 'en', [ 'foo', 'bar' ] );
-               $bigItem->setAliases( 'de', [ 'defoo', 'debar' ] );
-               $bigItem->getSiteLinkList()->addNewSiteLink( 'dewiki', 'foo' );
-               $bigItem->getStatements()->addStatement( 
$anotherQualifiedStatement );
-               $bigItem->getStatements()->addStatement( 
$selfReferencingStatement );
+               $bigItemBuilder = ItemBuilder::create()
+                       ->withId( 'Q111' )
+                       ->withLabel( 'en', 'foo' )
+                       ->withLabel( 'pt', 'ptfoo' )
+                       ->withDescription( 'en', 'foo' )
+                       ->withDescription( 'pl', 'pldesc' )
+                       ->withAliases( 'en', [ 'foo', 'bar' ] )
+                       ->withAliases( 'de', [ 'defoo', 'debar' ] )
+                       ->withSiteLink( 'dewiki', 'foo' )
+                       ->withStatement( $anotherQualifiedStatement )
+                       ->withStatement( $selfReferencingStatement );
 
                $testCases['itemMerge'] = array(
-                       $bigItem->copy(),
-                       new Item(),
-                       new Item(),
-                       $bigItem->copy(),
+                       $bigItemBuilder->build(),
+                       $emptyItem->copy(),
+                       $emptyItem->copy(),
+                       $bigItemBuilder->build(),
                );
 
                $referencingStatement = new Statement(
@@ -363,37 +375,40 @@
                );
                $referencingStatement->setGuid( 
'Q111$949A4D27-0EBC-46A7-BF5F-AA2DD33C0443' );
 
-               $bigItem->getSiteLinkList()->addNewSiteLink( 'nlwiki', 'bar' );
-               $bigItem->getStatements()->addStatement( $referencingStatement 
);
+               $anotherBigItem = $bigItemBuilder->withSiteLink( 'nlwiki', 
'bar' )
+                       ->withStatement( $referencingStatement )
+                       ->build();
 
-               $smallerItem = new Item();
-               $smallerItem->setId( 222 );
-               $smallerItem->getFingerprint()->setLabel( 'en', 'toLabel' );
-               $smallerItem->getFingerprint()->setDescription( 'pl', 
'toDescription' );
-               $smallerItem->getSiteLinkList()->addNewSiteLink( 'nlwiki', 
'toLink' );
+               $smallerItem = ItemBuilder::create()
+                       ->withId( 'Q222' )
+                       ->withLabel( 'en', 'toLabel' )
+                       ->withDescription( 'pl', 'toDescription' )
+                       ->withSiteLink( 'nlwiki', 'toLink' )
+                       ->build();
 
-               $smallerMergedItem = new Item();
-               $smallerMergedItem->setId( 222 );
-               $smallerMergedItem->getFingerprint()->setDescription( 'pl', 
'pldesc' );
-               $smallerMergedItem->getSiteLinkList()->addNewSiteLink( 
'nlwiki', 'bar' );
+               $smallerMergedItem = ItemBuilder::create()
+                       ->withId( 'Q222' )
+                       ->withDescription( 'pl', 'pldesc' )
+                       ->withSiteLink( 'nlwiki', 'bar' )
+                       ->build();
 
-               $bigMergedItem = new Item();
-               $bigMergedItem->setId( 111 );
-               $bigMergedItem->setLabel( 'en', 'toLabel' );
-               $bigMergedItem->setLabel( 'pt', 'ptfoo' );
-               $bigMergedItem->setDescription( 'en', 'foo' );
-               $bigMergedItem->setDescription( 'pl', 'toDescription' );
-               $bigMergedItem->setAliases( 'en', [ 'foo', 'bar' ] );
-               $bigMergedItem->setAliases( 'de', [ 'defoo', 'debar' ] );
-
-               $bigMergedItem->getSiteLinkList()->addNewSiteLink( 'dewiki', 
'foo' );
-               $bigMergedItem->getSiteLinkList()->addNewSiteLink( 'nlwiki', 
'toLink' );
-               $bigMergedItem->setStatements(
-                       new StatementList( $anotherQualifiedStatement, 
$selfReferencingStatement, $referencingStatement )
-               );
+               $bigMergedItem = ItemBuilder::create()
+                       ->withId( 'Q111' )
+                       ->withLabel( 'en', 'toLabel' )
+                       ->withLabel( 'pt', 'ptfoo' )
+                       ->withDescription( 'en', 'foo' )
+                       ->withDescription( 'pl', 'toDescription' )
+                       ->withAliases( 'en', [ 'foo', 'bar' ] )
+                       ->withAliases( 'de', [ 'defoo', 'debar' ] )
+                       ->withSiteLink( 'dewiki', 'foo' )
+                       ->withSiteLink( 'nlwiki', 'toLink' )
+                       ->withStatement( $anotherQualifiedStatement )
+                       ->withStatement( $selfReferencingStatement )
+                       ->withStatement( $referencingStatement )
+                       ->build();
 
                $testCases['ignoreConflictItemMerge'] = array(
-                       $bigItem->copy(),
+                       $anotherBigItem->copy(),
                        $smallerItem->copy(),
                        $smallerMergedItem->copy(),
                        $bigMergedItem->copy(),
@@ -404,11 +419,15 @@
        }
 
        public function testSitelinkConflictNormalization() {
-               $from = new Item( new ItemId( 'Q111' ) );
-               $from->getSiteLinkList()->addNewSiteLink( 'enwiki', 'FOo' );
+               $from = ItemBuilder::create()
+                       ->withId( 'Q111' )
+                       ->withSiteLink( 'enwiki', 'FOo' )
+                       ->build();
 
-               $to = new Item( new ItemId( 'Q222' ) );
-               $to->getSiteLinkList()->addNewSiteLink( 'enwiki', 'Foo' );
+               $to = ItemBuilder::create()
+                       ->withId( 'Q222' )
+                       ->withSiteLink( 'enwiki', 'Foo' )
+                       ->build();
 
                $enwiki = $this->getMock( Site::class );
                $enwiki->expects( $this->once() )
@@ -439,12 +458,16 @@
        }
 
        public function testExceptionThrownWhenNormalizingSiteNotFound() {
-               $from = new Item();
-               $from->setId( new ItemId( 'Q111' ) );
-               $from->getSiteLinkList()->addNewSiteLink( 'enwiki', 'FOo' );
-               $to = new Item();
-               $to->setId( new ItemId( 'Q222' ) );
-               $to->getSiteLinkList()->addNewSiteLink( 'enwiki', 'Foo' );
+               $from = ItemBuilder::create()
+                       ->withId( 'Q111' )
+                       ->withSiteLink( 'enwiki', 'FOo' )
+                       ->build();
+
+               $to = ItemBuilder::create()
+                       ->withId( 'Q222' )
+                       ->withSiteLink( 'enwiki', 'Foo' )
+                       ->build();
+
 
                $changeOps = $this->makeChangeOpsMerge(
                        $from,
@@ -494,12 +517,13 @@
        }
 
        public function testExceptionThrownWhenFromHasLink() {
-               $from = new Item( new ItemId( 'Q111' ) );
-               $from->getStatements()->addNewStatement(
-                       new PropertyValueSnak( new PropertyId( 'P42' ), new 
EntityIdValue( new ItemId( 'Q222' ) ) )
-               );
 
-               $to = new Item( new ItemId( 'Q222' ) );
+               $from = ItemBuilder::create()
+                       ->withId( 'Q111' )
+                       ->withPropertyValueStatement( 'P42', new ItemId( 'Q222' 
) )
+                       ->build();
+
+               $to = ItemBuilder::create()->withId( 'Q222' )->build();
 
                $changeOps = $this->makeChangeOpsMerge( $from, $to );
 
@@ -511,12 +535,12 @@
        }
 
        public function testExceptionThrownWhenToHasLink() {
-               $from = new Item( new ItemId( 'Q111' ) );
+               $from = ItemBuilder::create()->withId( 'Q111' )->build();
 
-               $to = new Item( new ItemId( 'Q222' ) );
-               $to->getStatements()->addNewStatement(
-                       new PropertyValueSnak( new PropertyId( 'P42' ), new 
EntityIdValue( new ItemId( 'Q111' ) ) )
-               );
+               $to = ItemBuilder::create()
+                       ->withId( 'Q222' )
+                       ->withPropertyValueStatement( 'P42', new ItemId( 'Q111' 
) )
+                       ->build();
 
                $changeOps = $this->makeChangeOpsMerge( $from, $to );
 
diff --git a/repo/tests/phpunit/includes/ItemBuilder.php 
b/repo/tests/phpunit/includes/ItemBuilder.php
new file mode 100644
index 0000000..c668f71
--- /dev/null
+++ b/repo/tests/phpunit/includes/ItemBuilder.php
@@ -0,0 +1,173 @@
+<?php
+
+namespace Wikibase\Repo\Tests;
+
+use DataValues\DataValue;
+use Wikibase\DataModel\Entity\EntityId;
+use Wikibase\DataModel\Entity\EntityIdValue;
+use Wikibase\DataModel\Entity\Item;
+use Wikibase\DataModel\Entity\ItemId;
+use Wikibase\DataModel\Entity\PropertyId;
+use Wikibase\DataModel\Snak\PropertyValueSnak;
+use Wikibase\DataModel\Snak\Snak;
+use Wikibase\DataModel\Statement\Statement;
+
+/**
+ * Immutable
+ */
+class ItemBuilder {
+       /**
+        * @var ItemId
+        */
+       private $itemId;
+
+       /**
+        * @var string[] Indexed by language
+        */
+       private $labels = [];
+
+       /**
+        * @var string[] Indexed by language
+        */
+       private $descriptions = [];
+
+       /**
+        * @var string[][] Indexed by language on the first level
+        */
+       private $aliases = [];
+
+       /**
+        * @var string[] Indexed by siteId
+        */
+       private $siteLinks = [];
+
+       /**
+        * @var Statement[]
+        */
+       private $statements = [];
+
+       public static function create() {
+               return new self();
+       }
+
+       public function __construct() {
+       }
+
+       public function build() {
+               $item = new Item( $this->itemId );
+
+               foreach ( $this->labels as $language => $label ) {
+                       $item->setLabel( $language, $label );
+               }
+               foreach ( $this->descriptions as $language => $description ) {
+                       $item->setDescription( $language, $description );
+               }
+               foreach ( $this->aliases as $language => $aliases ) {
+                       $item->setAliases( $language, $aliases );
+               }
+               foreach ( $this->siteLinks as $siteId => $pageName ) {
+                       $item->getSiteLinkList()->addNewSiteLink( $siteId, 
$pageName );
+               }
+               foreach ( $this->statements as $statement ) {
+                       $item->getStatements()->addStatement( $statement );
+               }
+               return $item;
+       }
+
+       /**
+        * @param ItemId|string $itemId
+        * @return self
+        */
+       public function withId( $itemId ) {
+               $result = clone $this;
+               if ( !$itemId instanceof ItemId ) {
+                       $itemId = new ItemId( $itemId );
+               }
+               $result->itemId = $itemId;
+               return $result;
+       }
+
+       /**
+        * @param string $language
+        * @param string $label
+        * @return self
+        */
+       public function withLabel( $language, $label ) {
+               $result = clone $this;
+               $result->labels[$language] = $label;
+               return $result;
+       }
+
+       /**
+        * @param string $language
+        * @param string $description
+        * @return self
+        */
+       public function withDescription( $language, $description ) {
+               $result = clone $this;
+               $result->descriptions[$language] = $description;
+               return $result;
+       }
+
+       /**
+        * @param string $language
+        * @param array $aliases
+        * @return self
+        */
+       public function withAliases( $language, array $aliases ) {
+               $result = clone $this;
+               $result->aliases[$language] = $aliases;
+               return $result;
+       }
+
+       /**
+        * @param string $siteId
+        * @param string $pageName
+        * @return self
+        */
+       public function withSiteLink( $siteId, $pageName ) {
+               $result = clone $this;
+               $result->siteLinks[$siteId] = $pageName;
+               return $result;
+       }
+
+       /**
+        * @param Statement|Snak $statement
+        * @return self
+        */
+       public function withStatement( $statement ) {
+               $result = clone $this;
+               if ( $statement instanceof Snak ) {
+                       $statement = new Statement( $statement );
+               }
+               $result->statements[] = clone $statement;
+               return $result;
+       }
+
+       /**
+        * @param PropertyId|string $propertyId
+        * @param EntityId|DataValue $value If given not DataValue tries to 
guess type and create
+        *        correct DataValue object
+        * @return ItemBuilder
+        */
+       public function withPropertyValueStatement( $propertyId, $value ) {
+               if ( !$propertyId instanceof PropertyId ) {
+                       $propertyId = new PropertyId( $propertyId );
+               }
+
+               if ( $value instanceof EntityId ) {
+                       $value = new EntityIdValue( $value );
+               } else {
+                       //Assume that $value is of type DataValue
+               }
+
+               return $this->withStatement(
+                       new PropertyValueSnak(
+                               $propertyId,
+                               $value
+                       )
+               );
+
+       }
+
+}

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I158ec2719a3d8ffd4943ec10b041996890f6e18e
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Aleksey Bekh-Ivanov (WMDE) <[email protected]>

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

Reply via email to