Jeroen De Dauw has uploaded a new change for review.

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


Change subject: Added extra tests for EntityIdValue
......................................................................

Added extra tests for EntityIdValue

Change-Id: Id3d16d04ff9469d65988d8dc460d0cdc1b739598
---
M DataModel/Internal/LegacyIdInterpreter.php
A tests/phpunit/Entity/EntityIdValueTest.php
2 files changed, 190 insertions(+), 1 deletion(-)


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

diff --git a/DataModel/Internal/LegacyIdInterpreter.php 
b/DataModel/Internal/LegacyIdInterpreter.php
index 195c724..0fd3725 100644
--- a/DataModel/Internal/LegacyIdInterpreter.php
+++ b/DataModel/Internal/LegacyIdInterpreter.php
@@ -5,6 +5,7 @@
 use InvalidArgumentException;
 use Wikibase\DataModel\Entity\BasicEntityIdParser;
 use Wikibase\DataModel\Entity\EntityId;
+use Wikibase\DataModel\Entity\EntityIdParsingException;
 
 /**
  * Turns legacy entity id serializations consisting of entity type + numeric id
@@ -27,10 +28,19 @@
         * @param int|string $numericId
         *
         * @return EntityId
+        * @throws InvalidArgumentException
         */
        public static function newIdFromTypeAndNumber( $entityType, $numericId 
) {
                $idParser = new BasicEntityIdParser();
-               return $idParser->parse( self::constructSerialization( 
$entityType, $numericId ) );
+
+               try {
+                       $id = $idParser->parse( self::constructSerialization( 
$entityType, $numericId ) );
+               }
+               catch ( EntityIdParsingException $ex ) {
+                       throw new InvalidArgumentException( $ex->getMessage(), 
0, $ex );
+               }
+
+               return $id;
        }
 
        /**
@@ -43,6 +53,10 @@
         * @throws InvalidArgumentException
         */
        protected static function constructSerialization( $entityType, 
$numericId ) {
+               if ( !is_string( $entityType ) ) {
+                       throw new InvalidArgumentException( '$entityType needs 
to be a string' );
+               }
+
                $entityTypes = array(
                        'item' => 'Q',
                        'property' => 'P',
diff --git a/tests/phpunit/Entity/EntityIdValueTest.php 
b/tests/phpunit/Entity/EntityIdValueTest.php
new file mode 100644
index 0000000..2a9d3e5
--- /dev/null
+++ b/tests/phpunit/Entity/EntityIdValueTest.php
@@ -0,0 +1,175 @@
+<?php
+
+namespace Wikibase\Test;
+
+use Wikibase\DataModel\Entity\EntityIdValue;
+use Wikibase\DataModel\Entity\ItemId;
+use Wikibase\DataModel\Entity\PropertyId;
+
+/**
+ * @covers Wikibase\DataModel\Entity\EntityIdValue
+ *
+ * @group Wikibase
+ * @group WikibaseDataModel
+ * @group EntityIdTest
+ *
+ * @licence GNU GPL v2+
+ * @author Jeroen De Dauw < [email protected] >
+ */
+class EntityIdValueTest extends \PHPUnit_Framework_TestCase {
+
+       public function testCanConstruct() {
+               $entityId = new ItemId( 'Q123' );
+               $entityIdValue = new EntityIdValue( $entityId );
+               $this->assertEquals( $entityId, $entityIdValue->getEntityId() );
+       }
+
+       /**
+        * @dataProvider instanceProvider
+        */
+       public function testSerialzationRoundtrip( EntityIdValue $id ) {
+               $newId = unserialize( serialize( $id ) );
+
+               $this->assertEquals( $id, $newId );
+       }
+
+       public function instanceProvider() {
+               $ids = array(
+                       new ItemId( 'Q1' ),
+                       new ItemId( 'Q42' ),
+                       new ItemId( 'Q31337' ),
+                       new PropertyId( 'P1' ),
+                       new PropertyId( 'P42' ),
+                       new PropertyId( 'P31337' ),
+               );
+
+               $argLists = array();
+
+               foreach ( $ids as $id ) {
+                       $argLists[] = array( new EntityIdValue( $id ) );
+               }
+
+               return $argLists;
+       }
+
+       /**
+        * @dataProvider instanceProvider
+        */
+       public function testGetType( EntityIdValue $id ) {
+               $this->assertEquals( 'wikibase-entityid', $id->getType() );
+       }
+
+       /**
+        * @dataProvider instanceProvider
+        */
+       public function testGetValue( EntityIdValue $id ) {
+               $this->assertEquals( $id, $id->getValue() );
+       }
+
+       /**
+        * @dataProvider instanceProvider
+        */
+       public function testGetSortKey( EntityIdValue $id ) {
+               $this->assertInternalType( 'string', $id->getSortKey() );
+       }
+
+       /**
+        * @dataProvider instanceProvider
+        */
+       public function testGetArrayValueRoundtrip( EntityIdValue $id ) {
+               $newId = EntityIdValue::newFromArray( $id->getArrayValue() );
+
+               $this->assertEquals( $id, $newId );
+       }
+
+       public function testSerializationCompatibility() {
+               $id = new EntityIdValue( new ItemId( 'Q31337' ) );
+
+               // This is the serialization format from when the EntityIdValue 
was still together with EntityId.
+               $this->assertEquals( '["item",31337]', $id->serialize() );
+       }
+
+       public function testDeserializationCompatibility() {
+               $expected = new EntityIdValue( new ItemId( 'Q31337' ) );
+
+               // This is the serialization format from 
f5b8b64823ff215c3796a79d916b6eaa65f4be33, version 0.5 alpha.
+               $id = unserialize( 
'C:39:"Wikibase\DataModel\Entity\EntityIdValue":14:{["item",31337]}' );
+               $this->assertEquals( $expected, $id );
+       }
+
+       public function testGetArrayValueCompatibility() {
+               $id = new EntityIdValue( new ItemId( 'Q31337' ) );
+
+               $this->assertEquals(
+                       // This is the serialization format from when the 
EntityIdValue was still together with EntityId.
+                       array(
+                               'entity-type' => 'item',
+                               'numeric-id' => 31337,
+                       ),
+                       $id->getArrayValue()
+               );
+       }
+
+       public function testNewFromArrayCompatibility() {
+               $id = new EntityIdValue( new ItemId( 'Q31337' ) );
+
+               $this->assertEquals(
+                       $id,
+                       EntityIdValue::newFromArray(
+                               // This is the serialization format from when 
the EntityIdValue was still together with EntityId.
+                               array(
+                                       'entity-type' => 'item',
+                                       'numeric-id' => 31337,
+                               )
+                       )
+               );
+       }
+
+       /**
+        * @dataProvider invalidArrayProvider
+        */
+       public function testCannotDeserializeInvalidSerialization( 
$invalidArray ) {
+               $this->setExpectedException( 'DataValues\IllegalValueException' 
);
+
+               EntityIdValue::newFromArray( $invalidArray );
+       }
+
+       public function invalidArrayProvider() {
+               return array(
+                       array( null ),
+
+                       array( 'foo' ),
+
+                       array( array() ),
+
+                       array( array(
+                               'entity-type' => 'item',
+                       ) ),
+
+                       array( array(
+                               'numeric-id' => 42,
+                       ) ),
+
+                       array( array(
+                               'entity-type' => 'foo',
+                               'numeric-id' => 42,
+                       ) ),
+
+                       array( array(
+                               'entity-type' => 42,
+                               'numeric-id' => 42,
+                       ) ),
+
+                       array( array(
+                               'entity-type' => 'item',
+                               'numeric-id' => -1,
+                       ) ),
+
+                       array( array(
+                               'entity-type' => 'item',
+                               'numeric-id' => 'foo',
+                       ) ),
+               );
+       }
+
+}

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Id3d16d04ff9469d65988d8dc460d0cdc1b739598
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/WikibaseDataModel
Gerrit-Branch: master
Gerrit-Owner: Jeroen De Dauw <[email protected]>

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

Reply via email to