Jeroen De Dauw has uploaded a new change for review.

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


Change subject: Added SelectionRequestDeserializer and refactored to get rid of 
duplicate code
......................................................................

Added SelectionRequestDeserializer and refactored to get rid of duplicate code

Change-Id: Ibaaf7d0c4a263370a65c190939e96b20d721880e
---
A Tests/Phpunit/Deserializers/SelectionRequestDeserializerTest.php
M Tests/Phpunit/Deserializers/SortExpressionDeserializerTest.php
A includes/Ask/Deserializers/SelectionRequestDeserializer.php
M includes/Ask/Deserializers/SortExpressionDeserializer.php
4 files changed, 286 insertions(+), 21 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Ask 
refs/changes/19/72619/1

diff --git a/Tests/Phpunit/Deserializers/SelectionRequestDeserializerTest.php 
b/Tests/Phpunit/Deserializers/SelectionRequestDeserializerTest.php
new file mode 100644
index 0000000..65e6960
--- /dev/null
+++ b/Tests/Phpunit/Deserializers/SelectionRequestDeserializerTest.php
@@ -0,0 +1,194 @@
+<?php
+
+namespace Ask\Tests\Phpunit\Deserializers;
+
+use Ask\Deserializers\SelectionRequestDeserializer;
+use DataValues\DataValueFactory;
+
+/**
+ * @covers Ask\Deserializers\SelectionRequestDeserializer
+ *
+ * @file
+ * @since 0.1
+ *
+ * @ingroup Ask
+ * @group Ask
+ *
+ * @licence GNU GPL v2+
+ * @author Jeroen De Dauw < [email protected] >
+ */
+class SelectionRequestDeserializerTest extends \PHPUnit_Framework_TestCase {
+
+       protected function newSelectionRequestDeserializer() {
+               $dvFactory = new DataValueFactory();
+               $dvFactory->registerDataValue( 'string', 
'DataValues\StringValue' );
+
+               return new SelectionRequestDeserializer( $dvFactory );
+       }
+
+       /**
+        * @dataProvider invalidObjectTypeProvider
+        */
+       public function testCannotDeserializeWithInvalidObjectType( 
$notASelectionRequest ) {
+               $serializer = $this->newSelectionRequestDeserializer();
+
+               $this->assertFalse( $serializer->canDeserialize( 
$notASelectionRequest ) );
+
+               $this->setExpectedException( 
'Ask\Deserializers\Exceptions\UnsupportedTypeException' );
+               $serializer->deserialize( $notASelectionRequest );
+       }
+
+       public function invalidObjectTypeProvider() {
+               $argLists = array();
+
+               $argLists[] = array( array(
+                       'objectType' => 'foobar',
+                       'selectionRequestType' => 'property',
+                       'value' => array()
+               ) );
+
+               $argLists[] = array( array(
+                       'objectType' => 'DESCRIPTION',
+                       'selectionRequestType' => 'property',
+                       'value' => array()
+               ) );
+
+               $argLists[] = array( array(
+                       'objectType' => null,
+                       'selectionRequestType' => 'property',
+                       'value' => array()
+               ) );
+
+               $argLists[] = array( array(
+                       'objectType' => array(),
+                       'selectionRequestType' => 'property',
+                       'value' => array()
+               ) );
+
+               $argLists[] = array( array(
+                       'objectType' => 42,
+                       'selectionRequestType' => 'property',
+                       'value' => array()
+               ) );
+
+               return $argLists;
+       }
+
+       /**
+        * @dataProvider missingObjectTypeProvider
+        */
+       public function testCannotDeserilaizeWithoutObjectType( 
$notASelectionRequest ) {
+               $serializer = $this->newSelectionRequestDeserializer();
+
+               $this->assertFalse( $serializer->canDeserialize( 
$notASelectionRequest ) );
+
+               $this->setExpectedException( 
'Ask\Deserializers\Exceptions\MissingTypeException' );
+               $serializer->deserialize( $notASelectionRequest );
+       }
+
+       public function missingObjectTypeProvider() {
+               $argLists = array();
+
+               $argLists[] = array( null );
+               $argLists[] = array( array() );
+               $argLists[] = array( 'foo bar' );
+
+               $argLists[] = array( array(
+                       'selectionRequestType' => 'property',
+                       'value' => array()
+               ) );
+
+               $argLists[] = array( array(
+                       'ObjectType' => 'sortExpression',
+                       'selectionRequestType' => 'property',
+                       'value' => array()
+               ) );
+
+               $argLists[] = array( array(
+                       'OBJECTTYPE' => 'sortExpression',
+                       'selectionRequestType' => 'property',
+                       'value' => array()
+               ) );
+
+               return $argLists;
+       }
+
+       public function testCannotDeserilaizeWithUnknownDescriptionType() {
+               $notASelectionRequest = array(
+                       'objectType' => 'selectionRequest',
+                       'selectionRequestType' => 'fooBar',
+                       'value' => array()
+               );
+
+               $this->setExpectedException( 
'Ask\Deserializers\Exceptions\InvalidAttributeException' );
+               $this->newSelectionRequestDeserializer()->deserialize( 
$notASelectionRequest );
+       }
+
+       public function testCannotDeserilaizeWithoutDescriptionType() {
+               $notASortExpression = array(
+                       'objectType' => 'selectionRequest',
+                       'value' => array()
+               );
+
+               $this->setExpectedException( 
'Ask\Deserializers\Exceptions\MissingAttributeException' );
+               $this->newSelectionRequestDeserializer()->deserialize( 
$notASortExpression );
+       }
+
+       /**
+        * @dataProvider propertySelectionWithMissingAttributeProvider
+        */
+       public function testPropertySelectionRequiresAllAttributes( array 
$invalidValue ) {
+               $invalidSelectionRequest = 
$this->newPropertySelectionSerializationFromValue( $invalidValue );
+               $this->setExpectedException( 
'Ask\Deserializers\Exceptions\MissingAttributeException' );
+               $this->newSelectionRequestDeserializer()->deserialize( 
$invalidSelectionRequest );
+       }
+
+       protected function newPropertySelectionSerializationFromValue( $value ) 
{
+               return array(
+                       'objectType' => 'selectionRequest',
+                       'selectionRequestType' => 'property',
+                       'value' => $value,
+               );
+       }
+
+       public function propertySelectionWithMissingAttributeProvider() {
+               $argLists = array();
+
+               $argLists[] = array( array(
+               ) );
+
+               return $argLists;
+       }
+
+       /**
+        * @dataProvider propertySelectionWithInvalidAttributeProvider
+        */
+       public function testPropertySelectionRequiresValidAttributes( array 
$invalidValue ) {
+               $invalidSelectionRequest = 
$this->newPropertySelectionSerializationFromValue( $invalidValue );
+               $this->setExpectedException( 
'Ask\Deserializers\Exceptions\DeserializationException' );
+               $this->newSelectionRequestDeserializer()->deserialize( 
$invalidSelectionRequest );
+       }
+
+       public function propertySelectionWithInvalidAttributeProvider() {
+               $argLists = array();
+
+               $argLists[] = array( array(
+                       'property' => array(),
+               ) );
+
+               $argLists[] = array( array(
+                       'property' => null,
+               ) );
+
+               $argLists[] = array( array(
+                       'property' => 42,
+               ) );
+
+               $argLists[] = array( array(
+                       'property' => array( 42 ),
+               ) );
+
+               return $argLists;
+       }
+
+}
diff --git a/Tests/Phpunit/Deserializers/SortExpressionDeserializerTest.php 
b/Tests/Phpunit/Deserializers/SortExpressionDeserializerTest.php
index 97b1100..6763aef 100644
--- a/Tests/Phpunit/Deserializers/SortExpressionDeserializerTest.php
+++ b/Tests/Phpunit/Deserializers/SortExpressionDeserializerTest.php
@@ -84,13 +84,13 @@
        /**
         * @dataProvider missingObjectTypeProvider
         */
-       public function testCannotDeserilaizeWithoutObjectType( 
$notADescription ) {
+       public function testCannotDeserilaizeWithoutObjectType( 
$notASortExpression ) {
                $serializer = $this->newSortExpressionDeserializer();
 
-               $this->assertFalse( $serializer->canDeserialize( 
$notADescription ) );
+               $this->assertFalse( $serializer->canDeserialize( 
$notASortExpression ) );
 
                $this->setExpectedException( 
'Ask\Deserializers\Exceptions\MissingTypeException' );
-               $serializer->deserialize( $notADescription );
+               $serializer->deserialize( $notASortExpression );
        }
 
        public function missingObjectTypeProvider() {
diff --git a/includes/Ask/Deserializers/SelectionRequestDeserializer.php 
b/includes/Ask/Deserializers/SelectionRequestDeserializer.php
new file mode 100644
index 0000000..e86a570
--- /dev/null
+++ b/includes/Ask/Deserializers/SelectionRequestDeserializer.php
@@ -0,0 +1,89 @@
+<?php
+
+namespace Ask\Deserializers;
+
+use Ask\Deserializers\Exceptions\DeserializationException;
+use Ask\Deserializers\Exceptions\InvalidAttributeException;
+use Ask\Language\Selection\PropertySelection;
+use Ask\Language\Selection\SubjectSelection;
+use DataValues\DataValueFactory;
+use InvalidArgumentException;
+
+/**
+ * @since 0.1
+ *
+ * @file
+ * @ingroup Ask
+ *
+ * @licence GNU GPL v2+
+ * @author Jeroen De Dauw < [email protected] >
+ */
+class SelectionRequestDeserializer extends TypedObjectDeserializer {
+
+       protected $dataValueFactory;
+
+       public function __construct( DataValueFactory $dataValueFactory ) {
+               $this->dataValueFactory = $dataValueFactory;
+       }
+
+       /**
+        * @see TypedObjectDeserializer::getObjectType
+        *
+        * @since 0.1
+        *
+        * @return string
+        */
+       protected function getObjectType() {
+               return 'selectionRequest';
+       }
+
+       /**
+        * @see TypedObjectDeserializer::getSubTypeKey
+        *
+        * @since 0.1
+        *
+        * @return string
+        */
+       protected function getSubTypeKey() {
+               return 'selectionRequestType';
+       }
+
+       /**
+        * @see TypedObjectDeserializer::getDeserializedValue
+        *
+        * @since 0.1
+        *
+        * @param string $sortExpressionType
+        * @param array $valueSerialization
+        *
+        * @return object
+        * @throws DeserializationException
+        */
+       protected function getDeserializedValue( $sortExpressionType, array 
$valueSerialization ) {
+               switch ( $sortExpressionType ) {
+                       case 'property':
+                               return $this->newPropertySelectionRequest( 
$valueSerialization );
+                               break;
+                       case 'subject':
+                               return new SubjectSelection();
+                               break;
+               }
+
+               throw new InvalidAttributeException( 'selectionRequestType', 
$this );
+       }
+
+       protected function newPropertySelectionRequest( array $value ) {
+               $this->requireAttribute( $value, 'property' );
+               $this->assertAttributeIsArray( $value, 'property' );
+
+               try {
+                       $propertyId = $this->dataValueFactory->newFromArray( 
$value['property'] );
+               }
+               catch ( InvalidArgumentException $ex ) {
+                       throw new DeserializationException( $this, '', $ex );
+               }
+
+               return new PropertySelection( $propertyId );
+       }
+
+}
diff --git a/includes/Ask/Deserializers/SortExpressionDeserializer.php 
b/includes/Ask/Deserializers/SortExpressionDeserializer.php
index 7af3006..cf388cc 100644
--- a/includes/Ask/Deserializers/SortExpressionDeserializer.php
+++ b/includes/Ask/Deserializers/SortExpressionDeserializer.php
@@ -83,22 +83,4 @@
                return $expression;
        }
 
-       protected function requireAttribute( array $array, $attributeName ) {
-               if ( !array_key_exists( $attributeName, $array ) ) {
-                       throw new MissingAttributeException(
-                               $attributeName,
-                               $this
-                       );
-               }
-       }
-
-       protected function assertAttributeIsArray( array $array, $attributeName 
) {
-               if ( !is_array( $array[$attributeName] ) ) {
-                       throw new InvalidAttributeException(
-                               $attributeName,
-                               $this
-                       );
-               }
-       }
-
 }

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ibaaf7d0c4a263370a65c190939e96b20d721880e
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Ask
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