Thiemo Mättig (WMDE) has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/392033 )

Change subject: Make Form extend EntityDocument
......................................................................

Make Form extend EntityDocument

I intentionally leave two unfinished decisions in this patch:

1. Form::setId is short-circuited with an exception. I'm aware this is
awkward, but I believe this is the best we can do for now.

2. I believe lists of grammatical features should also be equal if the
order is different. But this heavily depends on an other decision: is the
first grammatical feature somehow "special", e.g. in certain formatters?
The order might be relevant then. For this patch here I go with the basic
solution and consider different orders different.

Bug: T180469
Change-Id: Ib6a7ca4b0e42b3fc1535a20eb48976aeb1abaf8f
---
M src/DataModel/Form.php
M tests/phpunit/composer/DataModel/FormTest.php
2 files changed, 194 insertions(+), 2 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/WikibaseLexeme 
refs/changes/33/392033/1

diff --git a/src/DataModel/Form.php b/src/DataModel/Form.php
index 2ba1464..887d7e1 100644
--- a/src/DataModel/Form.php
+++ b/src/DataModel/Form.php
@@ -3,6 +3,8 @@
 namespace Wikibase\Lexeme\DataModel;
 
 use InvalidArgumentException;
+use LogicException;
+use Wikibase\DataModel\Entity\EntityDocument;
 use Wikibase\DataModel\Statement\StatementList;
 use Wikibase\DataModel\Statement\StatementListProvider;
 use Wikibase\DataModel\Entity\ItemId;
@@ -12,7 +14,7 @@
 /**
  * @license GPL-2.0+
  */
-class Form implements StatementListProvider {
+class Form implements EntityDocument, StatementListProvider {
 
        /**
         * @var FormId
@@ -61,10 +63,26 @@
        }
 
        /**
+        * @return string
+        */
+       public function getType() {
+               return 'form';
+       }
+
+       /**
         * @return FormId
         */
        public function getId() {
                return $this->id;
+       }
+
+       /**
+        * @param FormId $id
+        *
+        * @throws LogicException
+        */
+       public function setId( $id ) {
+               throw new LogicException( 'Form IDs must be set on construction 
time' );
        }
 
        /**
@@ -105,4 +123,50 @@
                return $this->statementList;
        }
 
+       /**
+        * @see EntityDocument::isEmpty
+        *
+        * @return bool
+        */
+       public function isEmpty() {
+               return $this->representations->count() <= 1
+                       && $this->grammaticalFeatures === []
+                       && $this->statementList->isEmpty();
+       }
+
+       /**
+        * @see Comparable::equals
+        *
+        * @param mixed $target
+        *
+        * @return bool
+        */
+       public function equals( $target ) {
+               if ( $this === $target ) {
+                       return true;
+               }
+
+               return $target instanceof self
+                       && $this->representations->equals( 
$target->representations )
+                       && $this->grammaticalFeatures == 
$target->grammaticalFeatures
+                       && $this->statementList->equals( $target->statementList 
);
+       }
+
+       /**
+        * @see EntityDocument::copy
+        *
+        * @return self
+        */
+       public function copy() {
+               return clone $this;
+       }
+
+       /**
+        * @see http://php.net/manual/en/language.oop5.cloning.php
+        */
+       public function __clone() {
+               $this->representations = clone $this->representations;
+               $this->statementList = clone $this->statementList;
+       }
+
 }
diff --git a/tests/phpunit/composer/DataModel/FormTest.php 
b/tests/phpunit/composer/DataModel/FormTest.php
index 10bd239..615ed57 100644
--- a/tests/phpunit/composer/DataModel/FormTest.php
+++ b/tests/phpunit/composer/DataModel/FormTest.php
@@ -3,12 +3,14 @@
 namespace Wikibase\Lexeme\Tests\DataModel;
 
 use InvalidArgumentException;
+use LogicException;
 use PHPUnit_Framework_TestCase;
 use Wikibase\DataModel\Entity\ItemId;
 use Wikibase\DataModel\Term\Term;
 use Wikibase\DataModel\Term\TermList;
 use Wikibase\Lexeme\DataModel\Form;
 use Wikibase\Lexeme\DataModel\FormId;
+use Wikibase\Repo\Tests\NewStatement;
 
 /**
  * @covers \Wikibase\Lexeme\DataModel\Form
@@ -25,11 +27,12 @@
        }
 
        public function testCreateFormWithOneRepresentation_CreatesIt() {
-               new Form(
+               $form = new Form(
                        new FormId( 'L1-F1' ),
                        new TermList( [ new Term( 'en', 'representation' ) ] ),
                        []
                );
+               $this->assertCount( 1, $form->getRepresentations() );
        }
 
        public function 
testCreateForm_GrammaticalFeaturesIsNotAnArrayOfItemIds_ThrowsAnException() {
@@ -67,4 +70,129 @@
                $form->setGrammaticalFeatures( [ "Q1" ] );
        }
 
+       public function testFormIdCanNotBeChanged() {
+               $form = NewForm::havingId( 'F1' )->build();
+               $this->setExpectedException( LogicException::class );
+               $form->setId( new FormId( 'L1-F2' ) );
+       }
+
+       public function testGivenMinimalForm_isEmpty() {
+               $form = NewForm::havingRepresentation( 'en', '1st' )->build();
+               $this->assertTrue( $form->isEmpty() );
+       }
+
+       public function provideNonEmptyForms() {
+               return [
+                       '2 representations' => [
+                               NewForm::havingRepresentation( 'en', '1st' )
+                                       ->andRepresentation( 'fr', '2nd' )
+                                       ->build()
+                       ],
+                       '1 grammatical feature' => [
+                               NewForm::havingGrammaticalFeature( 'Q1' )
+                                       ->build()
+                       ],
+                       '1 statement' => [
+                               NewForm::havingStatement( 
NewStatement::noValueFor( 'P1' ) )
+                                       ->build()
+                       ],
+               ];
+       }
+
+       /**
+        * @dataProvider provideNonEmptyForms
+        */
+       public function testGivenFormWithOptionalElements_isNotEmpty( Form 
$form ) {
+               $this->assertFalse( $form->isEmpty() );
+       }
+
+       public function provideEqualForms() {
+               $minimal = NewForm::havingRepresentation( 'en', 'minimal' );
+               $nonEmpty = $minimal->andGrammaticalFeature( 'Q1' )
+                       ->andStatement( NewStatement::noValueFor( 'P1' ) );
+
+               $minimalInstance = $minimal->build();
+
+               return [
+                       'same instance' => [
+                               $minimalInstance,
+                               $minimalInstance
+                       ],
+                       'minimal forms' => [
+                               $minimal->build(),
+                               $minimal->build()
+                       ],
+                       'different IDs' => [
+                               $minimal->build(),
+                               $minimal->andId( 'F2' )->build()
+                       ],
+                       'non-empty forms' => [
+                               $nonEmpty->build(),
+                               $nonEmpty->build()
+                       ],
+                       'multiple grammatical features' => [
+                               $nonEmpty->andGrammaticalFeature( 'Q2' 
)->build(),
+                               $nonEmpty->andGrammaticalFeature( 'Q2' 
)->build()
+                       ],
+               ];
+       }
+
+       /**
+        * @dataProvider provideEqualForms
+        */
+       public function testGivenEqualForms_areEqual( Form $form1, Form $form2 
) {
+               $this->assertTrue( $form1->equals( $form2 ) );
+       }
+
+       public function provideUnequalForms() {
+               $form = NewForm::havingId( 'F1' );
+
+               return [
+                       'different representation' => [
+                               $form->build(),
+                               $form->andRepresentation( 'en', 'different' 
)->build()
+                       ],
+                       '+1 grammatical feature' => [
+                               $form->build(),
+                               $form->andGrammaticalFeature( 'Q1' )->build()
+                       ],
+                       '+1 statement' => [
+                               $form->build(),
+                               $form->andStatement( NewStatement::noValueFor( 
'P1' ) )->build()
+                       ],
+                       // FIXME: What kind of equality do we want for lists of 
ItemIds?
+                       'grammatical feature in different order' => [
+                               $form->andGrammaticalFeature( 'Q1' 
)->andGrammaticalFeature( 'Q2' )->build(),
+                               $form->andGrammaticalFeature( 'Q2' 
)->andGrammaticalFeature( 'Q1' )->build()
+                       ],
+               ];
+       }
+
+       /**
+        * @dataProvider provideUnequalForms
+        */
+       public function testGivenUnequalForms_areNotEqual( Form $form1, Form 
$form2 ) {
+               $this->assertFalse( $form1->equals( $form2 ) );
+       }
+
+       public function testCopyIsIndependent() {
+               $original = NewForm::havingId( 'F1' )->build();
+               $copy = $original->copy();
+
+               // Edit all mutable fields on the original
+               $original->getRepresentations()->setTextForLanguage( 'en', 
'added' );
+               $original->setGrammaticalFeatures( [ new ItemId( 'Q2' ) ] );
+               $original->getStatements()->addStatement( 
NewStatement::noValueFor( 'P1' )->build() );
+
+               // Make sure the original changed
+               $this->assertTrue( 
$original->getRepresentations()->hasTermForLanguage( 'en' ) );
+               $this->assertNotEmpty( $original->getGrammaticalFeatures() );
+               $this->assertFalse( $original->getStatements()->isEmpty() );
+
+               // None of these changes should make it to the copy
+               $this->assertFalse( 
$copy->getRepresentations()->hasTermForLanguage( 'en' ) );
+               $this->assertEmpty( $copy->getGrammaticalFeatures() );
+               $this->assertTrue( $copy->getStatements()->isEmpty() );
+       }
+
 }

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib6a7ca4b0e42b3fc1535a20eb48976aeb1abaf8f
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/WikibaseLexeme
Gerrit-Branch: master
Gerrit-Owner: Thiemo Mättig (WMDE) <[email protected]>

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

Reply via email to