jenkins-bot has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/362212 )

Change subject: Introduce `NewStatement` builder for tests
......................................................................


Introduce `NewStatement` builder for tests

Change-Id: I0c6f491541a9c120545e02dfee5cdc10b838e709
---
M repo/tests/phpunit/includes/ChangeOp/ChangeOpsMergeTest.php
M repo/tests/phpunit/includes/NewItem.php
A repo/tests/phpunit/includes/NewStatement.php
3 files changed, 133 insertions(+), 84 deletions(-)

Approvals:
  jenkins-bot: Verified
  Thiemo Mättig (WMDE): Looks good to me, approved



diff --git a/repo/tests/phpunit/includes/ChangeOp/ChangeOpsMergeTest.php 
b/repo/tests/phpunit/includes/ChangeOp/ChangeOpsMergeTest.php
index b8eb1a2..21a9d71 100644
--- a/repo/tests/phpunit/includes/ChangeOp/ChangeOpsMergeTest.php
+++ b/repo/tests/phpunit/includes/ChangeOp/ChangeOpsMergeTest.php
@@ -22,6 +22,7 @@
 use Wikibase\DataModel\Snak\SnakList;
 use Wikibase\DataModel\Statement\Statement;
 use Wikibase\Repo\Tests\NewItem;
+use Wikibase\Repo\Tests\NewStatement;
 use Wikibase\Repo\Validators\EntityConstraintProvider;
 use Wikibase\Repo\Validators\EntityValidator;
 
@@ -497,9 +498,10 @@
        }
 
        public function testExceptionThrownWhenFromHasLink() {
-
                $from = NewItem::withId( 'Q111' )
-                       ->andPropertyValueSnak( 'P42', new ItemId( 'Q222' ) )
+                       ->andStatement(
+                               NewStatement::forProperty( 'P42' )->withValue( 
new ItemId( 'Q222' ) )
+                       )
                        ->build();
 
                $to = NewItem::withId( 'Q222' )
@@ -519,7 +521,9 @@
                        ->build();
 
                $to = NewItem::withId( 'Q222' )
-                       ->andPropertyValueSnak( 'P42', new ItemId( 'Q111' ) )
+                       ->andStatement(
+                               NewStatement::forProperty( 'P42' )->withValue( 
new ItemId( 'Q111' ) )
+                       )
                        ->build();
 
                $changeOps = $this->makeChangeOpsMerge( $from, $to );
diff --git a/repo/tests/phpunit/includes/NewItem.php 
b/repo/tests/phpunit/includes/NewItem.php
index 873cf91..8364c8f 100644
--- a/repo/tests/phpunit/includes/NewItem.php
+++ b/repo/tests/phpunit/includes/NewItem.php
@@ -2,17 +2,8 @@
 
 namespace Wikibase\Repo\Tests;
 
-use DataValues\DataValue;
-use DataValues\StringValue;
-use InvalidArgumentException;
-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\PropertyNoValueSnak;
-use Wikibase\DataModel\Snak\PropertySomeValueSnak;
-use Wikibase\DataModel\Snak\PropertyValueSnak;
 use Wikibase\DataModel\Snak\Snak;
 use Wikibase\DataModel\Statement\Statement;
 
@@ -196,87 +187,19 @@
        }
 
        /**
-        * @param Statement|Snak $statement
+        * @param NewStatement|Statement|Snak $statement
         *
         * @return self
         */
        public function andStatement( $statement ) {
                $copy = clone $this;
-               if ( $statement instanceof Snak ) {
+               if ( $statement instanceof NewStatement ) {
+                       $statement = $statement->build();
+               } elseif ( $statement instanceof Snak ) {
                        $statement = new Statement( $statement );
                }
                $copy->statements[] = clone $statement;
                return $copy;
-       }
-
-       /**
-        * @see andPropertyNoValueSnak
-        */
-       public static function withPropertyNoValueSnak( $propertyId ) {
-               return ( new self() )->andPropertyNoValueSnak( $propertyId );
-       }
-
-       /**
-        * @param PropertyId|string $propertyId
-        *
-        * @return self
-        */
-       public function andPropertyNoValueSnak( $propertyId ) {
-               if ( !( $propertyId instanceof PropertyId ) ) {
-                       $propertyId = new PropertyId( $propertyId );
-               }
-
-               return $this->andStatement( new PropertyNoValueSnak( 
$propertyId ) );
-       }
-
-       /**
-        * @see andPropertySomeValueSnak
-        */
-       public static function withPropertySomeValueSnak( $propertyId ) {
-               return ( new self() )->andPropertySomeValueSnak( $propertyId );
-       }
-
-       /**
-        * @param PropertyId|string $propertyId
-        *
-        * @return self
-        */
-       public function andPropertySomeValueSnak( $propertyId ) {
-               if ( !( $propertyId instanceof PropertyId ) ) {
-                       $propertyId = new PropertyId( $propertyId );
-               }
-
-               return $this->andStatement( new PropertySomeValueSnak( 
$propertyId ) );
-       }
-
-       /**
-        * @see andPropertyValueSnak
-        */
-       public static function withPropertyValueSnak( $propertyId, $dataValue ) 
{
-               return ( new self() )->andPropertyValueSnak( $propertyId, 
$dataValue );
-       }
-
-       /**
-        * @param PropertyId|string $propertyId
-        * @param DataValue|EntityId|string $dataValue If not a DataValue 
object, the builder tries to
-        *  guess the type and turns it into a DataValue object.
-        *
-        * @return self
-        */
-       public function andPropertyValueSnak( $propertyId, $dataValue ) {
-               if ( !( $propertyId instanceof PropertyId ) ) {
-                       $propertyId = new PropertyId( $propertyId );
-               }
-
-               if ( $dataValue instanceof EntityId ) {
-                       $dataValue = new EntityIdValue( $dataValue );
-               } elseif ( is_string( $dataValue ) ) {
-                       $dataValue = new StringValue( $dataValue );
-               } elseif ( !( $dataValue instanceof DataValue ) ) {
-                       throw new InvalidArgumentException( 'Unsupported 
$dataValue type' );
-               }
-
-               return $this->andStatement( new PropertyValueSnak( $propertyId, 
$dataValue ) );
        }
 
 }
diff --git a/repo/tests/phpunit/includes/NewStatement.php 
b/repo/tests/phpunit/includes/NewStatement.php
new file mode 100644
index 0000000..3887cc7
--- /dev/null
+++ b/repo/tests/phpunit/includes/NewStatement.php
@@ -0,0 +1,122 @@
+<?php
+
+namespace Wikibase\Repo\Tests;
+
+use DataValues\DataValue;
+use DataValues\StringValue;
+use InvalidArgumentException;
+use Wikibase\DataModel\Entity\EntityId;
+use Wikibase\DataModel\Entity\EntityIdValue;
+use Wikibase\DataModel\Entity\PropertyId;
+use Wikibase\DataModel\Snak\PropertyNoValueSnak;
+use Wikibase\DataModel\Snak\PropertySomeValueSnak;
+use Wikibase\DataModel\Snak\PropertyValueSnak;
+use Wikibase\DataModel\Statement\Statement;
+
+class NewStatement {
+
+       /**
+        * @var PropertyId
+        */
+       private $propertyId;
+
+       /**
+        * @var string|null
+        */
+       private $type;
+
+       /**
+        * @var DataValue|null
+        */
+       private $dataValue;
+
+       /**
+        * @param PropertyId|string $propertyId
+        * @return self
+        */
+       public static function forProperty( $propertyId ) {
+               $result = new self();
+               if ( is_string( $propertyId ) ) {
+                       $propertyId = new PropertyId( $propertyId );
+               }
+               $result->propertyId = $propertyId;
+
+               return $result;
+       }
+
+       /**
+        * @param PropertyId|string $propertyId
+        * @return self
+        */
+       public static function someValueFor( $propertyId ) {
+               $result = self::forProperty( $propertyId );
+               $result->type = PropertySomeValueSnak::class;
+
+               return $result;
+       }
+
+       /**
+        * @param PropertyId|string $propertyId
+        * @return self
+        */
+       public static function noValueFor( $propertyId ) {
+               $result = self::forProperty( $propertyId );
+               $result->type = PropertyNoValueSnak::class;
+
+               return $result;
+       }
+
+       /**
+        * @param DataValue|EntityId|string $dataValue If not a DataValue 
object, the builder tries to
+        *  guess the type and turns it into a DataValue object.
+        * @return self
+        */
+       public function withValue( $dataValue ) {
+               $result = clone $this;
+
+               if ( $dataValue instanceof EntityId ) {
+                       $dataValue = new EntityIdValue( $dataValue );
+               } elseif ( is_string( $dataValue ) ) {
+                       $dataValue = new StringValue( $dataValue );
+               } elseif ( !( $dataValue instanceof DataValue ) ) {
+                       throw new InvalidArgumentException( 'Unsupported 
$dataValue type' );
+               }
+
+               $result->dataValue = $dataValue;
+               $result->type = PropertyValueSnak::class;
+
+               return $result;
+       }
+
+       private function __construct() {
+       }
+
+       /**
+        * @return Statement
+        */
+       public function build() {
+               if ( !$this->type ) {
+                       $possibleTypes = [ PropertySomeValueSnak::class, 
PropertyNoValueSnak::class ];
+                       $type = $possibleTypes[array_rand( $possibleTypes )];
+               } else {
+                       $type = $this->type;
+               }
+
+               switch ( $type ) {
+                       case PropertySomeValueSnak::class:
+                               $snack = new PropertySomeValueSnak( 
$this->propertyId );
+                               break;
+                       case PropertyNoValueSnak::class:
+                               $snack = new PropertyNoValueSnak( 
$this->propertyId );
+                               break;
+                       case PropertyValueSnak::class:
+                               $snack = new PropertyValueSnak( 
$this->propertyId, $this->dataValue );
+                               break;
+                       default:
+                               throw new \LogicException( "Unknown statement 
type: '{$this->type}'" );
+               }
+
+               return new Statement( $snack );
+       }
+
+}

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I0c6f491541a9c120545e02dfee5cdc10b838e709
Gerrit-PatchSet: 3
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Aleksey Bekh-Ivanov (WMDE) <[email protected]>
Gerrit-Reviewer: Aleksey Bekh-Ivanov (WMDE) <[email protected]>
Gerrit-Reviewer: Jonas Kress (WMDE) <[email protected]>
Gerrit-Reviewer: Thiemo Mättig (WMDE) <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to