Jeroen De Dauw has uploaded a new change for review.

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


Change subject: Added test for Entity and fixes inconsistency in id field 
handling
......................................................................

Added test for Entity and fixes inconsistency in id field handling

Change-Id: I1d371ff484db5a86d183a99d8488987cea96b0ba
---
M DataModel/Entity/Entity.php
M tests/phpunit/Entity/EntityTest.php
2 files changed, 39 insertions(+), 10 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/WikibaseDataModel 
refs/changes/49/75149/1

diff --git a/DataModel/Entity/Entity.php b/DataModel/Entity/Entity.php
index e042052..fa36c53 100644
--- a/DataModel/Entity/Entity.php
+++ b/DataModel/Entity/Entity.php
@@ -46,8 +46,13 @@
        protected $data;
 
        /**
-        * Id of the item (the 42 in q42 used as page name and in exports).
-        * Integer when set. False when not initialized. Null when the item is 
new and unsaved.
+        * Id of the entity.
+        *
+        * This field can have several types:
+        *
+        * * EntityId: This means the entity has this id.
+        * * Null: This means the entity does not have an associated id.
+        * * False: This means the entity has an id, but it is stubbed in the 
$data field. Call getId to get an unstubbed version.
         *
         * @since 0.1
         * @var EntityId|bool|null
@@ -62,6 +67,15 @@
        protected $claims;
 
        /**
+        * Returns a type identifier for the entity.
+        *
+        * @since 0.1
+        *
+        * @return string
+        */
+       public abstract function getType();
+
+       /**
         * Constructor.
         * Do not use to construct new stuff from outside of this class, use 
the static newFoobar methods.
         * In other words: treat as protected (which it was, but now cannot be 
since we derive from Content).
@@ -74,16 +88,14 @@
        public function __construct( array $data ) {
                $this->data = $data;
                $this->cleanStructure();
+               $this->initializeIdField();
        }
 
-       /**
-        * Returns a type identifier for the entity.
-        *
-        * @since 0.1
-        *
-        * @return string
-        */
-       public abstract function getType();
+       protected function initializeIdField() {
+               if ( !array_key_exists( 'entity', $this->data ) ) {
+                       $this->id = null;
+               }
+       }
 
        /**
         * Get an array representing the Entity.
diff --git a/tests/phpunit/Entity/EntityTest.php 
b/tests/phpunit/Entity/EntityTest.php
index d4af535..c590fb4 100644
--- a/tests/phpunit/Entity/EntityTest.php
+++ b/tests/phpunit/Entity/EntityTest.php
@@ -849,4 +849,21 @@
                $this->assertGreaterThanOrEqual( count( $claims ), count( 
$snaks ), "At least one snak per Claim" );
        }
 
+       /**
+        * @dataProvider instanceProvider
+        */
+       public function testArraySerlialzationRoundtrip( Entity $entity ) {
+               $class = get_class( $entity );
+
+               /**
+                * @var Entity $newEntity
+                */
+               $newEntity = new $class( $entity->toArray() );
+
+               $entity->stub();
+               $newEntity->stub();
+
+               $this->assertEquals( $entity, $newEntity );
+       }
+
 }

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I1d371ff484db5a86d183a99d8488987cea96b0ba
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/WikibaseDataModel
Gerrit-Branch: master
Gerrit-Owner: Jeroen De Dauw <jeroended...@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to