Denny Vrandecic has submitted this change and it was merged.

Change subject: Preliminary implementation of DescriptionDeserializer
......................................................................


Preliminary implementation of DescriptionDeserializer

* Added preliminary DescriptionDeserializer (remaining TODOs are marked)
* Added InvalidAttributeException
* Added MissingAttributeException

Change-Id: I4eefd6e59b83fb3b28438cfc5fd9d9abc679a519
---
A Tests/Phpunit/Deserializers/DescriptionDeserializerTest.php
A Tests/Phpunit/Deserializers/Exceptions/InvalidAttributeExceptionTest.php
A Tests/Phpunit/Deserializers/Exceptions/MissingAttributeExceptionTest.php
A includes/Ask/Deserializers/DescriptionDeserializer.php
A includes/Ask/Deserializers/Exceptions/InvalidAttributeException.php
A includes/Ask/Deserializers/Exceptions/MissingAttributeException.php
6 files changed, 389 insertions(+), 0 deletions(-)

Approvals:
  Denny Vrandecic: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/Tests/Phpunit/Deserializers/DescriptionDeserializerTest.php 
b/Tests/Phpunit/Deserializers/DescriptionDeserializerTest.php
new file mode 100644
index 0000000..43897e3
--- /dev/null
+++ b/Tests/Phpunit/Deserializers/DescriptionDeserializerTest.php
@@ -0,0 +1,114 @@
+<?php
+
+namespace Ask\Tests\Phpunit\Deserializers;
+
+use Ask\Language\Description\AnyValue;
+use Ask\Language\Description\Description;
+use Ask\Language\Description\SomeProperty;
+use Ask\Deserializers\DescriptionDeserializer;
+use DataTypes\DataTypeFactory;
+use DataValues\DataValueFactory;
+use DataValues\StringValue;
+
+/**
+ * @covers Ask\Deserializers\DescriptionDeserializer
+ *
+ * @file
+ * @since 0.1
+ *
+ * @ingroup Ask
+ * @group Ask
+ *
+ * @licence GNU GPL v2+
+ * @author Jeroen De Dauw < jeroended...@gmail.com >
+ */
+class DescriptionDeserializerTest extends \PHPUnit_Framework_TestCase {
+
+       /**
+        * @dataProvider nonDescriptionProvider
+        */
+       public function testCannotSerializeNonDescriptions( $notADescription ) {
+               $serializer = $this->newDescriptionDeserializer();
+
+               $this->assertFalse( $serializer->canDeserialize( 
$notADescription ) );
+
+               $this->setExpectedException( 
'Ask\Deserializers\Exceptions\UnsupportedTypeException' );
+               $serializer->deserialize( $notADescription );
+       }
+
+       protected function newDescriptionDeserializer() {
+               $dvFactory = new DataValueFactory();
+               $dvFactory->registerDataValue( 'string', 
'DataValues\StringValue' );
+
+               return new DescriptionDeserializer( $dvFactory );
+       }
+
+       public function nonDescriptionProvider() {
+               $argLists = array();
+
+//             $argLists[] = array( null );
+//             $argLists[] = array( array() );
+//             $argLists[] = array( 'foo bar' );
+//
+//             $argLists[] = array( array(
+//                     'descriptionType' => 'anyValue',
+//                     'value' => null
+//             ) );
+
+               $argLists[] = array( array(
+                       'objectType' => 'foobar',
+                       'descriptionType' => 'anyValue',
+                       'value' => null
+               ) );
+
+               return $argLists;
+       }
+
+       /**
+        * @dataProvider descriptionProvider
+        */
+       public function testSerializeDescription( Description 
$expectedDescription, $serialization ) {
+               $actualDescription = 
$this->newDescriptionDeserializer()->deserialize( $serialization );
+
+               $this->assertEquals( $expectedDescription, $actualDescription );
+       }
+
+       public function descriptionProvider() {
+               $argLists = array();
+
+               $p1337 = new StringValue( '1337prop' );
+
+               $argLists[] = array(
+                       new AnyValue(
+                       ),
+                       array(
+                               'objectType' => 'description',
+                               'descriptionType' => 'anyValue',
+                               'value' => null
+                       )
+               );
+
+               $argLists[] = array(
+                       new SomeProperty(
+                               $p1337,
+                               new AnyValue()
+                       ),
+                       array(
+                               'objectType' => 'description',
+                               'descriptionType' => 'someProperty',
+                               'value' => array(
+                                       'property' => $p1337->toArray(),
+                                       'description' => array(
+                                               'objectType' => 'description',
+                                               'descriptionType' => 'anyValue',
+                                               'value' => null
+                                       ),
+                                       'isSubProperty' => false
+                               ),
+                       )
+               );
+
+               return $argLists;
+       }
+
+}
diff --git 
a/Tests/Phpunit/Deserializers/Exceptions/InvalidAttributeExceptionTest.php 
b/Tests/Phpunit/Deserializers/Exceptions/InvalidAttributeExceptionTest.php
new file mode 100644
index 0000000..d8c8eb4
--- /dev/null
+++ b/Tests/Phpunit/Deserializers/Exceptions/InvalidAttributeExceptionTest.php
@@ -0,0 +1,48 @@
+<?php
+
+namespace Ask\Tests\Phpunit\Deserializers\Exceptions;
+
+use Ask\Deserializers\Exceptions\InvalidAttributeException;
+
+/**
+ * @covers Ask\Deserializers\Exceptions\InvalidAttributeException
+ *
+ * @file
+ * @since 0.1
+ *
+ * @ingroup Ask
+ * @group Ask
+ *
+ * @licence GNU GPL v2+
+ * @author Jeroen De Dauw < jeroended...@gmail.com >
+ */
+class InvalidAttributeExceptionTest extends \PHPUnit_Framework_TestCase {
+
+       public function testConstructorWithOnlyRequiredArguments() {
+               $attributeName = 'theGame';
+               $deserializer = $this->getMock( 
'Ask\Deserializers\Deserializer' );
+
+               $exception = new InvalidAttributeException( $attributeName, 
$deserializer );
+
+               $this->assertRequiredFieldsAreSet( $exception, $attributeName, 
$deserializer );
+       }
+
+       public function testConstructorWithAllArguments() {
+               $attributeName = 'theGame';
+               $deserializer = $this->getMock( 
'Ask\Deserializers\Deserializer' );
+               $message = 'NyanData all the way across the sky!';
+               $previous = new \Exception( 'Onoez!' );
+
+               $exception = new InvalidAttributeException( $attributeName, 
$deserializer, $message, $previous );
+
+               $this->assertRequiredFieldsAreSet( $exception, $attributeName, 
$deserializer );
+               $this->assertEquals( $message, $exception->getMessage() );
+               $this->assertEquals( $previous, $exception->getPrevious() );
+       }
+
+       protected function assertRequiredFieldsAreSet( 
InvalidAttributeException $exception, $attributeName, $deserializer ) {
+               $this->assertEquals( $attributeName, 
$exception->getAttributeName() );
+               $this->assertEquals( $deserializer, 
$exception->getDeserializer() );
+       }
+
+}
diff --git 
a/Tests/Phpunit/Deserializers/Exceptions/MissingAttributeExceptionTest.php 
b/Tests/Phpunit/Deserializers/Exceptions/MissingAttributeExceptionTest.php
new file mode 100644
index 0000000..6f28588
--- /dev/null
+++ b/Tests/Phpunit/Deserializers/Exceptions/MissingAttributeExceptionTest.php
@@ -0,0 +1,48 @@
+<?php
+
+namespace Ask\Tests\Phpunit\Deserializers\Exceptions;
+
+use Ask\Deserializers\Exceptions\MissingAttributeException;
+
+/**
+ * @covers Ask\Deserializers\Exceptions\MissingAttributeException
+ *
+ * @file
+ * @since 0.1
+ *
+ * @ingroup Ask
+ * @group Ask
+ *
+ * @licence GNU GPL v2+
+ * @author Jeroen De Dauw < jeroended...@gmail.com >
+ */
+class MissingAttributeExceptionTest extends \PHPUnit_Framework_TestCase {
+
+       public function testConstructorWithOnlyRequiredArguments() {
+               $attributeName = 'theGame';
+               $deserializer = $this->getMock( 
'Ask\Deserializers\Deserializer' );
+
+               $exception = new MissingAttributeException( $attributeName, 
$deserializer );
+
+               $this->assertRequiredFieldsAreSet( $exception, $attributeName, 
$deserializer );
+       }
+
+       public function testConstructorWithAllArguments() {
+               $attributeName = 'theGame';
+               $deserializer = $this->getMock( 
'Ask\Deserializers\Deserializer' );
+               $message = 'NyanData all the way across the sky!';
+               $previous = new \Exception( 'Onoez!' );
+
+               $exception = new MissingAttributeException( $attributeName, 
$deserializer, $message, $previous );
+
+               $this->assertRequiredFieldsAreSet( $exception, $attributeName, 
$deserializer );
+               $this->assertEquals( $message, $exception->getMessage() );
+               $this->assertEquals( $previous, $exception->getPrevious() );
+       }
+
+       protected function assertRequiredFieldsAreSet( 
MissingAttributeException $exception, $attributeName, $deserializer ) {
+               $this->assertEquals( $attributeName, 
$exception->getAttributeName() );
+               $this->assertEquals( $deserializer, 
$exception->getDeserializer() );
+       }
+
+}
diff --git a/includes/Ask/Deserializers/DescriptionDeserializer.php 
b/includes/Ask/Deserializers/DescriptionDeserializer.php
new file mode 100644
index 0000000..c20669e
--- /dev/null
+++ b/includes/Ask/Deserializers/DescriptionDeserializer.php
@@ -0,0 +1,101 @@
+<?php
+
+namespace Ask\Deserializers;
+
+use Ask\Language\Description\AnyValue;
+use Ask\Language\Description\Conjunction;
+use Ask\Language\Description\Description;
+use Ask\Language\Description\Disjunction;
+use Ask\Language\Description\SomeProperty;
+use Ask\Language\Description\ValueDescription;
+use Ask\Deserializers\Exceptions\UnsupportedTypeException;
+use DataValues\DataValueFactory;
+
+/**
+ * @since 0.1
+ *
+ * @file
+ * @ingroup Ask
+ *
+ * @licence GNU GPL v2+
+ * @author Jeroen De Dauw < jeroended...@gmail.com >
+ */
+class DescriptionDeserializer implements Deserializer {
+
+       protected $dataValueFactory;
+
+       public function __construct( DataValueFactory $dataValueFactory ) {
+               $this->dataValueFactory = $dataValueFactory;
+       }
+
+       public function deserialize( $serialization ) {
+               $this->assertCanDeserialize( $serialization );
+               return $this->getDeserializedDescription( $serialization );
+       }
+
+       protected function assertCanDeserialize( $serialization ) {
+               if ( !$this->canDeserialize( $serialization ) ) {
+                       throw new UnsupportedTypeException( 
$serialization['objectType'], $this );
+               }
+       }
+
+       public function canDeserialize( $askObject ) {
+               return $askObject['objectType'] === 'description'; // TODO: 
check element existence
+       }
+
+       protected function getDeserializedDescription( array $serialization ) {
+               $descriptionType = $serialization['descriptionType'];
+               $descriptionValue = $serialization['value'];
+
+               if ( $descriptionType === 'anyValue' ) {
+                       return new AnyValue();
+               }
+
+               if ( $descriptionType === 'someProperty' ) {
+                       return new SomeProperty(
+                               $this->dataValueFactory->newFromArray( 
$descriptionValue['property'] ),
+                               $this->deserialize( 
$descriptionValue['description'] ),
+                               $descriptionValue['isSubProperty']
+                       );
+               }
+
+               if ( $descriptionType === 'valueDescription' ) {
+                       return new ValueDescription(
+                               $this->dataValueFactory->newFromArray( 
$descriptionValue['value'] ),
+                               $descriptionValue['comparator']
+                       );
+               }
+
+               if ( $descriptionType === 'conjunction' ) {
+                       return new Conjunction(
+                               $this->deserializeDescriptions( 
$descriptionValue['descriptions'] )
+                       );
+               }
+
+               if ( $descriptionType === 'disjunction' ) {
+                       return new Disjunction(
+                               $this->deserializeDescriptions( 
$descriptionValue['descriptions'] )
+                       );
+               }
+
+               // TODO: handle desc type unknown
+               // TODO: validate elements are there
+               // TODO: validate element types
+       }
+
+       /**
+        * @param array $descriptionSerializations
+        *
+        * @return Description[]
+        */
+       protected function deserializeDescriptions( array 
$descriptionSerializations ) {
+               $descriptions = array();
+
+               foreach ( $descriptionSerializations as $serialization ) {
+                       $descriptions[] = $this->deserialize( $serialization );
+               }
+
+               return $descriptions;
+       }
+
+}
diff --git 
a/includes/Ask/Deserializers/Exceptions/InvalidAttributeException.php 
b/includes/Ask/Deserializers/Exceptions/InvalidAttributeException.php
new file mode 100644
index 0000000..d4f2620
--- /dev/null
+++ b/includes/Ask/Deserializers/Exceptions/InvalidAttributeException.php
@@ -0,0 +1,39 @@
+<?php
+
+namespace Ask\Deserializers\Exceptions;
+
+use Ask\Deserializers\Deserializer;
+
+/**
+ * @since 0.1
+ *
+ * @file
+ * @ingroup Ask
+ *
+ * @licence GNU GPL v2+
+ * @author Jeroen De Dauw < jeroended...@gmail.com >
+ */
+class InvalidAttributeException extends DeserializationException {
+
+       protected $attributeName;
+
+       /**
+        * @param string $attributeName
+        * @param Deserializer $deserializer
+        * @param string $message
+        * @param \Exception $previous
+        */
+       public function __construct( $attributeName, Deserializer 
$deserializer, $message = '', \Exception $previous = null ) {
+               $this->attributeName = $attributeName;
+
+               parent::__construct( $deserializer, $message, $previous );
+       }
+
+       /**
+        * @return string
+        */
+       public function getAttributeName() {
+               return $this->attributeName;
+       }
+
+}
diff --git 
a/includes/Ask/Deserializers/Exceptions/MissingAttributeException.php 
b/includes/Ask/Deserializers/Exceptions/MissingAttributeException.php
new file mode 100644
index 0000000..19d2bda
--- /dev/null
+++ b/includes/Ask/Deserializers/Exceptions/MissingAttributeException.php
@@ -0,0 +1,39 @@
+<?php
+
+namespace Ask\Deserializers\Exceptions;
+
+use Ask\Deserializers\Deserializer;
+
+/**
+ * @since 0.1
+ *
+ * @file
+ * @ingroup Ask
+ *
+ * @licence GNU GPL v2+
+ * @author Jeroen De Dauw < jeroended...@gmail.com >
+ */
+class MissingAttributeException extends DeserializationException {
+
+       protected $attributeName;
+
+       /**
+        * @param string $attributeName
+        * @param Deserializer $deserializer
+        * @param string $message
+        * @param \Exception $previous
+        */
+       public function __construct( $attributeName, Deserializer 
$deserializer, $message = '', \Exception $previous = null ) {
+               $this->attributeName = $attributeName;
+
+               parent::__construct( $deserializer, $message, $previous );
+       }
+
+       /**
+        * @return string
+        */
+       public function getAttributeName() {
+               return $this->attributeName;
+       }
+
+}

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I4eefd6e59b83fb3b28438cfc5fd9d9abc679a519
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Ask
Gerrit-Branch: master
Gerrit-Owner: Jeroen De Dauw <jeroended...@gmail.com>
Gerrit-Reviewer: Addshore <addshorew...@gmail.com>
Gerrit-Reviewer: Anja Jentzsch <a...@anjeve.de>
Gerrit-Reviewer: Ataherivand <abraham.taheriv...@wikimedia.de>
Gerrit-Reviewer: Aude <aude.w...@gmail.com>
Gerrit-Reviewer: Daniel Kinzler <daniel.kinz...@wikimedia.de>
Gerrit-Reviewer: Daniel Werner <daniel.wer...@wikimedia.de>
Gerrit-Reviewer: Denny Vrandecic <denny.vrande...@wikimedia.de>
Gerrit-Reviewer: Henning Snater <henning.sna...@wikimedia.de>
Gerrit-Reviewer: Jens Ohlig <jens.oh...@wikimedia.de>
Gerrit-Reviewer: Jeroen De Dauw <jeroended...@gmail.com>
Gerrit-Reviewer: John Erling Blad <jeb...@gmail.com>
Gerrit-Reviewer: Liangent <liang...@gmail.com>
Gerrit-Reviewer: Lydia Pintscher <lydia.pintsc...@wikimedia.de>
Gerrit-Reviewer: Markus Kroetzsch <mar...@semantic-mediawiki.org>
Gerrit-Reviewer: Nikola Smolenski <smole...@eunet.rs>
Gerrit-Reviewer: Tobias Gritschacher <tobias.gritschac...@wikimedia.de>
Gerrit-Reviewer: jenkins-bot

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

Reply via email to