Jeroen De Dauw has uploaded a new change for review. https://gerrit.wikimedia.org/r/71383
Change subject: Ground work for deserialization code ...................................................................... Ground work for deserialization code * Added Deserializer interface * Added DeserializationException * Added objectType key to serialization so objects can be unserialized without the need for further context * Changed various keys and values in the serialization from alllowercase to camelCase Change-Id: Ibb143de40736a789fd60d864a7c6644453021569 --- M Tests/Integration/Serializers/QuerySerialializationTest.php A Tests/Phpunit/Deserializers/Exceptions/UnsupportedTypeExceptionTest.php M Tests/Phpunit/Serializers/DescriptionSerializerTest.php M Tests/Phpunit/Serializers/QueryOptionsSerializerTest.php M Tests/Phpunit/Serializers/QuerySerializerTest.php M Tests/Phpunit/Serializers/SelectionRequestSerializerTest.php M Tests/Phpunit/Serializers/SortExpressionSerializerTest.php A includes/Ask/Deserializers/Deserializer.php A includes/Ask/Deserializers/Exceptions/DeserializationException.php A includes/Ask/Deserializers/Exceptions/UnsupportedTypeException.php M includes/Ask/Language/Description/AnyValue.php M includes/Ask/Language/Description/SomeProperty.php M includes/Ask/Language/Description/ValueDescription.php M includes/Ask/Serializers/DescriptionSerializer.php M includes/Ask/Serializers/Exceptions/SerializationException.php M includes/Ask/Serializers/Exceptions/UnsupportedObjectException.php M includes/Ask/Serializers/QueryOptionsSerializer.php M includes/Ask/Serializers/QuerySerializer.php M includes/Ask/Serializers/SelectionRequestSerializer.php M includes/Ask/Serializers/SortExpressionSerializer.php 20 files changed, 209 insertions(+), 36 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Ask refs/changes/83/71383/1 diff --git a/Tests/Integration/Serializers/QuerySerialializationTest.php b/Tests/Integration/Serializers/QuerySerialializationTest.php index 63f6307..6bd2be3 100644 --- a/Tests/Integration/Serializers/QuerySerialializationTest.php +++ b/Tests/Integration/Serializers/QuerySerialializationTest.php @@ -74,66 +74,77 @@ protected function getExpectedSerialization( StringValue $p42, StringValue $p9001, StringValue $foo ) { return array( + 'objectType' => 'query', 'description' => array( - 'type' => 'conjunction', + 'objectType' => 'description', + 'descriptionType' => 'conjunction', 'value' => array( 'descriptions' => array( array( - 'type' => 'someproperty', + 'objectType' => 'description', + 'descriptionType' => 'someProperty', 'value' => array( 'property' => $p42->toArray(), 'description' => array( - 'type' => 'anyvalue', + 'objectType' => 'description', + 'descriptionType' => 'anyValue', 'value' => null ), - 'issubproperty' => false + 'isSubProperty' => false ), ), array( - 'type' => 'someproperty', + 'objectType' => 'description', + 'descriptionType' => 'someProperty', 'value' => array( 'property' => $p9001->toArray(), 'description' => array( - 'type' => 'valuedescription', + 'objectType' => 'description', + 'descriptionType' => 'valueDescription', 'value' => array( 'value' => $foo->toArray(), - 'comparator' => 1 + 'comparator' => 1 // TODO: this should be a string ) ), - 'issubproperty' => false + 'isSubProperty' => false ), ) ) ) ), 'options' => array( + 'objectType' => 'queryOptions', 'limit' => 100, 'offset' => 42, 'sort' => array( 'expressions' => (object)array( array( - 'type' => 'PropertyValue', + 'objectType' => 'sortExpression', + 'sortExpressionType' => 'PropertyValue', 'value' => array( - 'property' => $p42->toArray(), 'direction' => SortExpression::ASCENDING, // TODO: this should be a string + 'property' => $p42->toArray(), ) ) ), ), ), - 'selectionrequests' => (object)array( + 'selectionRequests' => (object)array( array( - 'type' => 'subject', + 'objectType' => 'selectionRequest', + 'selectionType' => 'subject', 'value' => null, ), array( - 'type' => 'property', + 'objectType' => 'selectionRequest', + 'selectionType' => 'property', 'value' => array( 'property' => $p42->toArray(), ), ), array( - 'type' => 'property', + 'objectType' => 'selectionRequest', + 'selectionType' => 'property', 'value' => array( 'property' => $p9001->toArray(), ), diff --git a/Tests/Phpunit/Deserializers/Exceptions/UnsupportedTypeExceptionTest.php b/Tests/Phpunit/Deserializers/Exceptions/UnsupportedTypeExceptionTest.php new file mode 100644 index 0000000..18d0e0d --- /dev/null +++ b/Tests/Phpunit/Deserializers/Exceptions/UnsupportedTypeExceptionTest.php @@ -0,0 +1,48 @@ +<?php + +namespace Ask\Tests\Phpunit\Deserializers\Exceptions; + +use Ask\Deserializers\Exceptions\UnsupportedTypeException; + +/** + * @covers Ask\Deserializers\Exceptions\UnsupportedTypeException + * + * @file + * @since 0.1 + * + * @ingroup Ask + * @group Ask + * + * @licence GNU GPL v2+ + * @author Jeroen De Dauw < jeroended...@gmail.com > + */ +class UnsupportedTypeExceptionTest extends \PHPUnit_Framework_TestCase { + + public function testConstructorWithOnlyRequiredArguments() { + $serialization = array( 'the' => 'game' ); + $deserializer = $this->getMock( 'Ask\Deserializers\Deserializer' ); + + $exception = new UnsupportedTypeException( $serialization, $deserializer ); + + $this->assertRequiredFieldsAreSet( $exception, $serialization, $deserializer ); + } + + public function testConstructorWithAllArguments() { + $serialization = array( 'the' => 'game' ); + $deserializer = $this->getMock( 'Ask\Deserializers\Deserializer' ); + $message = 'NyanData all the way across the sky!'; + $previous = new \Exception( 'Onoez!' ); + + $exception = new UnsupportedTypeException( $serialization, $deserializer, $message, $previous ); + + $this->assertRequiredFieldsAreSet( $exception, $serialization, $deserializer ); + $this->assertEquals( $message, $exception->getMessage() ); + $this->assertEquals( $previous, $exception->getPrevious() ); + } + + protected function assertRequiredFieldsAreSet( UnsupportedTypeException $exception, $object, $deserializer ) { + $this->assertEquals( $object, $exception->getUnsupportedType() ); + $this->assertEquals( $deserializer, $exception->getDeserializer() ); + } + +} diff --git a/Tests/Phpunit/Serializers/DescriptionSerializerTest.php b/Tests/Phpunit/Serializers/DescriptionSerializerTest.php index 0c267e0..032c78b 100644 --- a/Tests/Phpunit/Serializers/DescriptionSerializerTest.php +++ b/Tests/Phpunit/Serializers/DescriptionSerializerTest.php @@ -65,7 +65,8 @@ new AnyValue( ), array( - 'type' => 'anyvalue', + 'objectType' => 'description', + 'descriptionType' => 'anyValue', 'value' => null ) ); @@ -76,14 +77,16 @@ new AnyValue() ), array( - 'type' => 'someproperty', + 'objectType' => 'description', + 'descriptionType' => 'someProperty', 'value' => array( 'property' => $p1337->toArray(), 'description' => array( - 'type' => 'anyvalue', + 'objectType' => 'description', + 'descriptionType' => 'anyValue', 'value' => null ), - 'issubproperty' => false + 'isSubProperty' => false ), ) ); diff --git a/Tests/Phpunit/Serializers/QueryOptionsSerializerTest.php b/Tests/Phpunit/Serializers/QueryOptionsSerializerTest.php index f8a1c6c..24fd9c1 100644 --- a/Tests/Phpunit/Serializers/QueryOptionsSerializerTest.php +++ b/Tests/Phpunit/Serializers/QueryOptionsSerializerTest.php @@ -30,6 +30,7 @@ $actualSerialization = $serializer->serialize( $options ); $expectedSerialization = array( + 'objectType' => 'queryOptions', 'limit' => $options->getLimit(), 'offset' => $options->getOffset(), 'sort' => array( diff --git a/Tests/Phpunit/Serializers/QuerySerializerTest.php b/Tests/Phpunit/Serializers/QuerySerializerTest.php index 50b96df..31a1f29 100644 --- a/Tests/Phpunit/Serializers/QuerySerializerTest.php +++ b/Tests/Phpunit/Serializers/QuerySerializerTest.php @@ -47,9 +47,10 @@ $actualSerialization = $serializer->serialize( $query ); $expectedSerialization = array( + 'objectType' => 'query', 'description' => 'foo bar baz', 'options' => 'foo bar baz', - 'selectionrequests' => (object)array(), + 'selectionRequests' => (object)array(), ); $this->assertEquals( $expectedSerialization, $actualSerialization ); diff --git a/Tests/Phpunit/Serializers/SelectionRequestSerializerTest.php b/Tests/Phpunit/Serializers/SelectionRequestSerializerTest.php index 1a3e262..67e17a3 100644 --- a/Tests/Phpunit/Serializers/SelectionRequestSerializerTest.php +++ b/Tests/Phpunit/Serializers/SelectionRequestSerializerTest.php @@ -49,7 +49,8 @@ $argLists[] = array( new SubjectSelection(), array( - 'type' => 'subject', + 'objectType' => 'selectionRequest', + 'selectionType' => 'subject', 'value' => null, ) ); @@ -59,7 +60,8 @@ $argLists[] = array( new PropertySelection( $stringValue ), array( - 'type' => 'property', + 'objectType' => 'selectionRequest', + 'selectionType' => 'property', 'value' => array( 'property' => $stringValue->toArray(), ), diff --git a/Tests/Phpunit/Serializers/SortExpressionSerializerTest.php b/Tests/Phpunit/Serializers/SortExpressionSerializerTest.php index 110b0fb..d3852b6 100644 --- a/Tests/Phpunit/Serializers/SortExpressionSerializerTest.php +++ b/Tests/Phpunit/Serializers/SortExpressionSerializerTest.php @@ -57,7 +57,8 @@ SortExpression::ASCENDING ), array( - 'type' => 'PropertyValue', + 'objectType' => 'sortExpression', + 'sortExpressionType' => 'PropertyValue', 'value' => array( 'property' => $p1337->toArray(), 'direction' => SortExpression::ASCENDING, @@ -71,7 +72,8 @@ SortExpression::DESCENDING ), array( - 'type' => 'PropertyValue', + 'objectType' => 'sortExpression', + 'sortExpressionType' => 'PropertyValue', 'value' => array( 'property' => $p1337->toArray(), 'direction' => SortExpression::DESCENDING, diff --git a/includes/Ask/Deserializers/Deserializer.php b/includes/Ask/Deserializers/Deserializer.php new file mode 100644 index 0000000..61842f0 --- /dev/null +++ b/includes/Ask/Deserializers/Deserializer.php @@ -0,0 +1,28 @@ +<?php + +namespace Ask\Deserializers; + +use Ask\Deserializers\Exceptions\DeserializationException; + +/** + * @since 0.1 + * + * @file + * @ingroup Ask + * + * @licence GNU GPL v2+ + * @author Jeroen De Dauw < jeroended...@gmail.com > + */ +interface Deserializer { + + /** + * @since 0.1 + * + * @param mixed $askObject + * + * @return object + * @throws DeserializationException + */ + public function deserialize( $askObject ); + +} diff --git a/includes/Ask/Deserializers/Exceptions/DeserializationException.php b/includes/Ask/Deserializers/Exceptions/DeserializationException.php new file mode 100644 index 0000000..40145ef --- /dev/null +++ b/includes/Ask/Deserializers/Exceptions/DeserializationException.php @@ -0,0 +1,33 @@ +<?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 > + */ +abstract class DeserializationException extends \RuntimeException { + + protected $deserializer; + + public function __construct( Deserializer $deserializer, $message = '', \Exception $previous = null ) { + $this->deserializer = $deserializer; + + parent::__construct( $message, 0, $previous ); + } + + /** + * @return Deserializer + */ + public function getDeserializer() { + return $this->deserializer; + } + +} diff --git a/includes/Ask/Deserializers/Exceptions/UnsupportedTypeException.php b/includes/Ask/Deserializers/Exceptions/UnsupportedTypeException.php new file mode 100644 index 0000000..0f420e8 --- /dev/null +++ b/includes/Ask/Deserializers/Exceptions/UnsupportedTypeException.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 UnsupportedTypeException extends DeserializationException { + + protected $unsupportedType; + + /** + * @param mixed $unsupportedType + * @param Deserializer $deserializer + * @param string $message + * @param \Exception $previous + */ + public function __construct( $unsupportedType, Deserializer $deserializer, $message = '', \Exception $previous = null ) { + $this->unsupportedType = $unsupportedType; + + parent::__construct( $deserializer, $message, $previous ); + } + + /** + * @return mixed + */ + public function getUnsupportedType() { + return $this->unsupportedType; + } + +} diff --git a/includes/Ask/Language/Description/AnyValue.php b/includes/Ask/Language/Description/AnyValue.php index 6a51c10..ea36197 100644 --- a/includes/Ask/Language/Description/AnyValue.php +++ b/includes/Ask/Language/Description/AnyValue.php @@ -64,7 +64,7 @@ * @return string */ public function getType() { - return 'anyvalue'; + return 'anyValue'; } /** diff --git a/includes/Ask/Language/Description/SomeProperty.php b/includes/Ask/Language/Description/SomeProperty.php index caf4b09..1a142c6 100644 --- a/includes/Ask/Language/Description/SomeProperty.php +++ b/includes/Ask/Language/Description/SomeProperty.php @@ -166,7 +166,7 @@ * @return string */ public function getType() { - return 'someproperty'; + return 'someProperty'; } /** diff --git a/includes/Ask/Language/Description/ValueDescription.php b/includes/Ask/Language/Description/ValueDescription.php index 455c6b6..b077085 100644 --- a/includes/Ask/Language/Description/ValueDescription.php +++ b/includes/Ask/Language/Description/ValueDescription.php @@ -140,7 +140,7 @@ * @return string */ public function getType() { - return 'valuedescription'; + return 'valueDescription'; } /** diff --git a/includes/Ask/Serializers/DescriptionSerializer.php b/includes/Ask/Serializers/DescriptionSerializer.php index 80bbfd6..ea91a2d 100644 --- a/includes/Ask/Serializers/DescriptionSerializer.php +++ b/includes/Ask/Serializers/DescriptionSerializer.php @@ -27,7 +27,8 @@ protected function getSerializedDescription( Description $description ) { return array( - 'type' => $description->getType(), + 'objectType' => 'description', + 'descriptionType' => $description->getType(), 'value' => $this->getDescriptionValueSerialization( $description ), ); } @@ -41,7 +42,7 @@ return array( 'property' => $description->getPropertyId()->toArray(), 'description' => $this->serialize( $description->getSubDescription() ), - 'issubproperty' => $description->isSubProperty(), + 'isSubProperty' => $description->isSubProperty(), ); } diff --git a/includes/Ask/Serializers/Exceptions/SerializationException.php b/includes/Ask/Serializers/Exceptions/SerializationException.php index c12d862..d7ac062 100644 --- a/includes/Ask/Serializers/Exceptions/SerializationException.php +++ b/includes/Ask/Serializers/Exceptions/SerializationException.php @@ -17,8 +17,8 @@ protected $serializer; - public function __construct( Serializer $serializer, $message = '', \Exception $previous = null ) { - $this->serializer = $serializer; + public function __construct( Serializer $deserializer, $message = '', \Exception $previous = null ) { + $this->serializer = $deserializer; parent::__construct( $message, 0, $previous ); } diff --git a/includes/Ask/Serializers/Exceptions/UnsupportedObjectException.php b/includes/Ask/Serializers/Exceptions/UnsupportedObjectException.php index 21419e0..0284026 100644 --- a/includes/Ask/Serializers/Exceptions/UnsupportedObjectException.php +++ b/includes/Ask/Serializers/Exceptions/UnsupportedObjectException.php @@ -19,14 +19,14 @@ /** * @param mixed $unsupportedObject - * @param Serializer $serializer + * @param Serializer $deserializer * @param string $message * @param \Exception $previous */ - public function __construct( $unsupportedObject, Serializer $serializer, $message = '', \Exception $previous = null ) { + public function __construct( $unsupportedObject, Serializer $deserializer, $message = '', \Exception $previous = null ) { $this->unsupportedObject = $unsupportedObject; - parent::__construct( $serializer, $message, $previous ); + parent::__construct( $deserializer, $message, $previous ); } /** diff --git a/includes/Ask/Serializers/QueryOptionsSerializer.php b/includes/Ask/Serializers/QueryOptionsSerializer.php index a24db71..bb37936 100644 --- a/includes/Ask/Serializers/QueryOptionsSerializer.php +++ b/includes/Ask/Serializers/QueryOptionsSerializer.php @@ -42,6 +42,7 @@ $expressionSerializer = $this->componentSerializer; return array( + 'objectType' => 'queryOptions', 'limit' => $options->getLimit(), 'offset' => $options->getOffset(), diff --git a/includes/Ask/Serializers/QuerySerializer.php b/includes/Ask/Serializers/QuerySerializer.php index 08f02d2..93138c6 100644 --- a/includes/Ask/Serializers/QuerySerializer.php +++ b/includes/Ask/Serializers/QuerySerializer.php @@ -51,9 +51,10 @@ } return array( + 'objectType' => 'query', 'description' => $this->componentSerializer->serialize( $query->getDescription() ), 'options' => $this->componentSerializer->serialize( $query->getOptions() ), - 'selectionrequests' => (object)$selectionRequests, + 'selectionRequests' => (object)$selectionRequests, ); } diff --git a/includes/Ask/Serializers/SelectionRequestSerializer.php b/includes/Ask/Serializers/SelectionRequestSerializer.php index c99e832..dbbf972 100644 --- a/includes/Ask/Serializers/SelectionRequestSerializer.php +++ b/includes/Ask/Serializers/SelectionRequestSerializer.php @@ -32,7 +32,8 @@ protected function getSerializedSelectionRequest( SelectionRequest $request ) { return array( - 'type' => $request->getType(), + 'objectType' => 'selectionRequest', + 'selectionType' => $request->getType(), 'value' => $this->getValueSerialization( $request ), ); } diff --git a/includes/Ask/Serializers/SortExpressionSerializer.php b/includes/Ask/Serializers/SortExpressionSerializer.php index 432a42e..31cd423 100644 --- a/includes/Ask/Serializers/SortExpressionSerializer.php +++ b/includes/Ask/Serializers/SortExpressionSerializer.php @@ -39,7 +39,8 @@ ); return array( - 'type' => $expression->getType(), + 'objectType' => 'sortExpression', + 'sortExpressionType' => $expression->getType(), 'value' => $valueArray, ); } -- To view, visit https://gerrit.wikimedia.org/r/71383 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ibb143de40736a789fd60d864a7c6644453021569 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Ask 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