jenkins-bot has submitted this change and it was merged.
Change subject: Adding tests for claims to ItemTest.
......................................................................
Adding tests for claims to ItemTest.
Contains minor cleanup in Entity and Item classes.
Change-Id: Ia12a91f50c50cb47d627e3f232265d69fcb147c7
---
M DataModel/Entity/Entity.php
M DataModel/Entity/Item.php
M tests/phpunit/Entity/EntityTest.php
M tests/phpunit/Entity/ItemTest.php
M tests/phpunit/Entity/PropertyTest.php
5 files changed, 145 insertions(+), 13 deletions(-)
Approvals:
Tobias Gritschacher: Looks good to me, approved
jenkins-bot: Verified
diff --git a/DataModel/Entity/Entity.php b/DataModel/Entity/Entity.php
index 3a6486e..8dc5759 100644
--- a/DataModel/Entity/Entity.php
+++ b/DataModel/Entity/Entity.php
@@ -653,6 +653,7 @@
}
$this->data['claims'] = $this->getStubbedClaims( empty(
$this->data['claims'] ) ? array() : $this->data['claims'] );
+ $this->claims = null;
}
private function getStubbedId() {
diff --git a/DataModel/Entity/Item.php b/DataModel/Entity/Item.php
index f5517c0..7723b42 100644
--- a/DataModel/Entity/Item.php
+++ b/DataModel/Entity/Item.php
@@ -39,13 +39,6 @@
const ENTITY_TYPE = 'item';
/**
- * @since 0.2
- *
- * @var Claims|null
- */
- protected $statements = null;
-
- /**
* Adds a site link to the list of site links.
* If there already is a site link with the site id of the provided
site link,
* then that one will be overridden by the provided one.
diff --git a/tests/phpunit/Entity/EntityTest.php
b/tests/phpunit/Entity/EntityTest.php
index 19b7b44..d434781 100644
--- a/tests/phpunit/Entity/EntityTest.php
+++ b/tests/phpunit/Entity/EntityTest.php
@@ -8,12 +8,13 @@
use Diff\DiffOpChange;
use Diff\DiffOpRemove;
use Wikibase\Claim;
+use Wikibase\Claims;
use Wikibase\Entity;
use Wikibase\EntityDiff;
use Wikibase\EntityId;
use Wikibase\Item;
-use Wikibase\Lib\ClaimGuidGenerator;
use Wikibase\ObjectComparer;
+use Wikibase\Property;
use Wikibase\PropertyNoValueSnak;
use Wikibase\PropertySomeValueSnak;
use Wikibase\PropertyValueSnak;
@@ -52,6 +53,13 @@
* @author Daniel Kinzler
*/
abstract class EntityTest extends \PHPUnit_Framework_TestCase {
+
+ /**
+ * Returns several more or less complex claims
+ *
+ * @return array
+ */
+ public abstract function makeClaims();
/**
* @since 0.1
@@ -488,8 +496,17 @@
public function instanceProvider() {
$entities = array();
- $entities[] = $this->getNewEmpty();
+ // empty
+ $entity = $this->getNewEmpty();
+ $entities[] = $entity;
+ // ID only
+ $entity = clone $entity;
+ $entity->setId( 44 );
+
+ $entities[] = $entity;
+
+ // with labels and stuff
$entity = $this->getNewEmpty();
$entity->setAliases( 'en', array( 'o', 'noez' ) );
$entity->setLabel( 'de', 'spam' );
@@ -497,15 +514,16 @@
$entities[] = $entity;
+ // with labels etc and ID
$entity = clone $entity;
-
$entity->setId( 42 );
$entities[] = $entity;
+ // With claims
$entity = $this->getNewEmpty();
-
- $entity->setId( 42 );
+ $entity->setClaims( new Claims( $this->makeClaims() ) );
+ $entity->setId( 55 );
$entities[] = $entity;
@@ -543,7 +561,7 @@
$this->assertEquals( $entity->getId(), $copy->getId() );
// More checks that should also pass
- $this->assertEquals( $entity, $copy );
+ $this->assertEquals( $entity->toArray(), $copy->toArray() );
$this->assertFalse( $entity === $copy );
}
@@ -865,6 +883,33 @@
*
* @param Entity $entity
*/
+ public function testGetClaims( Entity $entity ) {
+ $claims = $entity->getClaims();
+
+ $this->assertInternalType( 'array', $claims );
+ }
+
+ public function testSetClaims() {
+ $entity = $this->getNewEmpty();
+ $this->assertCount( 0, $entity->getClaims(), "initially, no
claims" );
+
+ $claims = array(
+ $claim0 = new Claim( new PropertyNoValueSnak( 42 ) ),
+ $claim1 = new Claim( new PropertySomeValueSnak( 42 ) ),
+ );
+
+ $entity->setClaims( new Claims( $claims ) );
+ $this->assertSameSize( $claims, $entity->getClaims(), "added
some claims" );
+
+ $entity->setClaims( new Claims() );
+ $this->assertCount( 0, $entity->getClaims(), "should be empty
again" );
+ }
+
+ /**
+ * @dataProvider instanceProvider
+ *
+ * @param Entity $entity
+ */
public function testGetAllSnaks( Entity $entity ) {
$snaks = $entity->getAllSnaks();
$claims = $entity->getClaims();
@@ -872,6 +917,32 @@
$this->assertInternalType( 'array', $snaks );
$this->assertGreaterThanOrEqual( count( $claims ), count(
$snaks ), "At least one snak per Claim" );
+
+ foreach ( $claims as $claim ) {
+ $snak = $claim->getMainSnak();
+ $this->assertContains( $snak, $snaks, "main snak" );
+
+ $qualifiers = $claim->getQualifiers();
+
+ // check the first qualifier
+ foreach ( $qualifiers as $snak ) {
+ $this->assertContains( $snak, $snaks,
"qualifier snak" );
+ }
+
+ // check the first reference
+ if ( $claim instanceof Statement ) {
+ $references = $claim->getReferences();
+
+ /* @var Reference $ref */
+ foreach ( $qualifiers as $ref ) {
+ $refSnaks = $ref->getSnaks();
+
+ foreach ( $refSnaks as $snak ) {
+ $this->assertContains( $snak,
$snaks, "reference snak" );
+ }
+ }
+ }
+ }
}
/**
diff --git a/tests/phpunit/Entity/ItemTest.php
b/tests/phpunit/Entity/ItemTest.php
index 5bb97a0..958cde4 100644
--- a/tests/phpunit/Entity/ItemTest.php
+++ b/tests/phpunit/Entity/ItemTest.php
@@ -2,16 +2,23 @@
namespace Wikibase\Test;
+use DataValues\StringValue;
use Diff\Diff;
use Diff\DiffOpAdd;
use Diff\DiffOpChange;
use Diff\DiffOpRemove;
+use Wikibase\Claim;
use Wikibase\DataModel\SimpleSiteLink;
use Wikibase\EntityId;
use Wikibase\Item;
use Wikibase\ItemDiff;
use Wikibase\Property;
use Wikibase\PropertyNoValueSnak;
+use Wikibase\PropertySomeValueSnak;
+use Wikibase\PropertyValueSnak;
+use Wikibase\Reference;
+use Wikibase\ReferenceList;
+use Wikibase\SnakList;
use Wikibase\Statement;
/**
@@ -52,6 +59,57 @@
class ItemTest extends EntityTest {
/**
+ * Returns several more or less complex claims
+ *
+ * @return array
+ */
+ public function makeClaims() {
+ $id9001 = new EntityId( Item::ENTITY_TYPE, 9001 );
+ $id1 = new EntityId( Item::ENTITY_TYPE, 1 );
+
+ $claims = array();
+
+ $claims[] = new Claim( new PropertyNoValueSnak( 42 ) );
+
+ $claims[] = new Statement(
+ new PropertyNoValueSnak( 42 ),
+ null,
+ new ReferenceList( array(
+ new Reference( new SnakList( array(
+ new PropertyNoValueSnak( 24 ),
+ new PropertyValueSnak( 1, new
StringValue( 'onoez' ) ) ) )
+ ),
+ new Reference( new SnakList( array(
+ new PropertyValueSnak( 1,
$id9001 ) ) )
+ )
+ ) )
+ );
+
+ $claims[] = new Claim( new PropertySomeValueSnak( 43 ) );
+
+ $claims[] = new Claim(
+ new PropertyNoValueSnak( 42 ),
+ new SnakList( array(
+ new PropertyNoValueSnak( 42 ),
+ new PropertySomeValueSnak( 43 ),
+ new PropertyValueSnak( 1, new StringValue(
'onoez' ) ),
+ ) )
+ );
+
+ $claims[] = new Claim(
+ new PropertyValueSnak( 2, $id9001 ),
+ new SnakList( array(
+ new PropertyNoValueSnak( 42 ),
+ new PropertySomeValueSnak( 43 ),
+ new PropertyValueSnak( 1, new StringValue(
'onoez' ) ),
+ new PropertyValueSnak( 2, $id1 ),
+ ) )
+ );
+
+ return $claims;
+ }
+
+ /**
* @see EntityTest::getNewEmpty
*
* @since 0.1
diff --git a/tests/phpunit/Entity/PropertyTest.php
b/tests/phpunit/Entity/PropertyTest.php
index f0437b4..684c94e 100644
--- a/tests/phpunit/Entity/PropertyTest.php
+++ b/tests/phpunit/Entity/PropertyTest.php
@@ -43,6 +43,15 @@
class PropertyTest extends EntityTest {
/**
+ * Returns no claims
+ *
+ * @return array
+ */
+ public function makeClaims() {
+ return array();
+ }
+
+ /**
* @see EntityTest::getNewEmpty
*
* @since 0.1
--
To view, visit https://gerrit.wikimedia.org/r/72974
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: Ia12a91f50c50cb47d627e3f232265d69fcb147c7
Gerrit-PatchSet: 8
Gerrit-Project: mediawiki/extensions/WikibaseDataModel
Gerrit-Branch: master
Gerrit-Owner: Daniel Kinzler <[email protected]>
Gerrit-Reviewer: Aude <[email protected]>
Gerrit-Reviewer: Daniel Kinzler <[email protected]>
Gerrit-Reviewer: Daniel Werner <[email protected]>
Gerrit-Reviewer: Denny Vrandecic <[email protected]>
Gerrit-Reviewer: Jeroen De Dauw <[email protected]>
Gerrit-Reviewer: Tobias Gritschacher <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits