Jeroen De Dauw has uploaded a new change for review. https://gerrit.wikimedia.org/r/75625
Change subject: Finished implementation of QueryEntitySerializer and added integration test ...................................................................... Finished implementation of QueryEntitySerializer and added integration test Change-Id: If8878e877d37dcdd6f87b477081a1690befdb609 --- A Tests/Integration/Wikibase/Query/QueryEntitySerializationTest.php M Tests/Phpunit/Wikibase/Query/QueryEntitySerializerTest.php M src/Wikibase/Query/QueryEntitySerializer.php 3 files changed, 348 insertions(+), 18 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/WikibaseQuery refs/changes/25/75625/1 diff --git a/Tests/Integration/Wikibase/Query/QueryEntitySerializationTest.php b/Tests/Integration/Wikibase/Query/QueryEntitySerializationTest.php new file mode 100644 index 0000000..ba2e4d9 --- /dev/null +++ b/Tests/Integration/Wikibase/Query/QueryEntitySerializationTest.php @@ -0,0 +1,195 @@ +<?php + +namespace Tests\Integration\Wikibase\Query; + +use Ask\Language\Description\Disjunction; +use Ask\Language\Description\SomeProperty; +use Ask\Language\Description\ValueDescription; +use Ask\Language\Option\QueryOptions; +use Ask\Language\Query; +use Ask\Language\Selection\PropertySelection; +use Ask\SerializerFactory; +use DataValues\StringValue; +use Wikibase\Claim; +use Wikibase\EntityId; +use Wikibase\PropertySomeValueSnak; +use Wikibase\PropertyValueSnak; +use Wikibase\Query\DIC\ExtensionAccess; +use Wikibase\Query\QueryEntity; +use Wikibase\Query\QueryEntitySerializer; +use Wikibase\SnakList; + +/** + * @file + * @ingroup WikibaseQuery + * @group WikibaseQuery + * @group WikibaseQueryIntegration + * + * @licence GNU GPL v2+ + * @author Jeroen De Dauw < [email protected] > + */ +class QueryEntitySerializationTest extends \PHPUnit_Framework_TestCase { + + public function testQueryEntitySerialization() { + $queryEntity = $this->newQueryEntity(); + + $askSerializerFactory = new SerializerFactory(); + + $serializer = new QueryEntitySerializer( $askSerializerFactory->newQuerySerializer() ); + + $actualSerialization = $serializer->serialize( $queryEntity ); + + $this->assertEquals( $this->newQueryEntitySerialization(), $actualSerialization ); + } + + protected function newQueryEntity() { + $awesomePropertyId = new EntityId( 'property', 42 ); + + $query = new Query( + new SomeProperty( + $awesomePropertyId, + new Disjunction( array( + new ValueDescription( new StringValue( 'foo' ) ), + new ValueDescription( new StringValue( 'bar' ) ), + ) ) + ), + array( + new PropertySelection( $awesomePropertyId ) + ), + new QueryOptions( 10, 0 ) + ); + + $queryEntity = new QueryEntity( $query ); + + $queryEntity->setId( 1337 ); + + $queryEntity->setLabel( 'en', 'Awesome' ); + $queryEntity->setLabel( 'de', 'Awesome' ); + $queryEntity->setDescription( 'en', 'ohi' ); + $queryEntity->setDescription( 'de', 'there' ); + $queryEntity->addAliases( 'en', array( 'foo', 'bar' ) ); + $queryEntity->addAliases( 'nl', array( 'baz', 'hax' ) ); + + $queryEntity->addClaim( new Claim( + new PropertySomeValueSnak( 42 ) + ) ); + + $queryEntity->addClaim( new Claim( + new PropertyValueSnak( 42, new StringValue( 'baz' ) ) + ) ); + + $queryEntity->addClaim( new Claim( + new PropertyValueSnak( 123, new StringValue( 'baz' ) ), + new SnakList( array( + new PropertySomeValueSnak( 42 ), + new PropertySomeValueSnak( 43 ) + ) ) + ) ); + + return $queryEntity; + } + + private function newQueryEntitySerialization() { + $awesomePropertyId = new EntityId( 'property', 42 ); + + return array( + 'entity' => array( 'query', 1337 ), + + 'label' => array( + 'en' => 'Awesome', + 'de' => 'Awesome', + ), + + 'description' => array( + 'en' => 'ohi', + 'de' => 'there', + ), + + 'aliases' => array( + 'en' => array( 'foo', 'bar' ), + 'nl' => array( 'baz', 'hax' ), + ), + + 'claim' => array( + array( + 'm' => array( 'somevalue', 42 ), + 'q' => array(), + 'g' => null, + ), + array( + 'm' => array( 'value', 42, 'string', 'baz' ), + 'q' => array(), + 'g' => null, + ), + array( + 'm' => array( 'value', 123, 'string', 'baz' ), + 'q' => array( + array( 'somevalue', 42 ), + array( 'somevalue', 43 ), + ), + 'g' => null, + ) + ), + + 'query' => array( + 'objectType' => 'query', + 'description' => array( + 'objectType' => 'description', + 'descriptionType' => 'someProperty', + 'value' => array( + 'property' => $awesomePropertyId->toArray(), + 'description' => array( + 'objectType' => 'description', + 'descriptionType' => 'disjunction', + 'value' => array( + 'descriptions' => array( + array( + 'objectType' => 'description', + 'descriptionType' => 'valueDescription', + 'value' => array( + 'comparator' => 'equal', + 'value' => array( + 'type' => 'string', + 'value' => 'foo', + ) + ), + ), + array( + 'objectType' => 'description', + 'descriptionType' => 'valueDescription', + 'value' => array( + 'value' => array( + 'type' => 'string', + 'value' => 'bar', + ), + 'comparator' => 'equal', + ), + ) + ) + ) + ), + 'isSubProperty' => false, + ) + ), + 'selectionRequests' => array( + array( + 'objectType' => 'selectionRequest', + 'selectionRequestType' => 'property', + 'value' => array( + 'property' => $awesomePropertyId->toArray() + ), + ) + ), + 'options' => array( + 'objectType' => 'queryOptions', + 'limit' => 10, + 'offset' => 0, + 'sort' => array( + 'expressions' => array() + ) + ), + ), + ); + } + +} diff --git a/Tests/Phpunit/Wikibase/Query/QueryEntitySerializerTest.php b/Tests/Phpunit/Wikibase/Query/QueryEntitySerializerTest.php index 8dbf073..7b6dd42 100644 --- a/Tests/Phpunit/Wikibase/Query/QueryEntitySerializerTest.php +++ b/Tests/Phpunit/Wikibase/Query/QueryEntitySerializerTest.php @@ -5,8 +5,13 @@ use Ask\Language\Description\AnyValue; use Ask\Language\Option\QueryOptions; use Ask\Language\Query; +use Wikibase\Claim; +use Wikibase\Claims; +use Wikibase\PropertyNoValueSnak; +use Wikibase\PropertySomeValueSnak; use Wikibase\Query\QueryEntity; use Wikibase\Query\QueryEntitySerializer; +use Wikibase\Test\ClaimListAccessTest; /** * @covers Wikibase\Query\QueryEntitySerializer @@ -42,6 +47,29 @@ ); } + /** + * @dataProvider notAQueryEntityProvider + */ + public function testAttemptToSerializeANonQueryEntityCausesException( $notAQueryEntity ) { + $serializer = $this->newSimpleQueryEntitySerializer(); + + $this->setExpectedException( 'InvalidArgumentException' ); + $serializer->serialize( $notAQueryEntity ); + } + + public function notAQueryEntityProvider() { + $argLists = array(); + + $argLists[] = array( null ); + $argLists[] = array( true ); + $argLists[] = array( 42 ); + $argLists[] = array( 'foo bar baz' ); + $argLists[] = array( array( 1, 2, '3' ) ); + $argLists[] = array( (object)array( 1, 2, '3' ) ); + + return $argLists; + } + public function testSerializationCallsQuerySerialization() { $querySerializer = $this->getMock( 'Serializers\Serializer' ); @@ -50,7 +78,7 @@ $querySerializer->expects( $this->once() ) ->method( 'serialize' ) - ->with( $this->equalTo( $queryEntity ) ) + ->with( $this->equalTo( $queryEntity->getQuery() ) ) ->will( $this->returnValue( $mockSerialization ) ); $serializer = new QueryEntitySerializer( $querySerializer ); @@ -132,26 +160,119 @@ } /** - * @dataProvider notAQueryEntityProvider + * @dataProvider labelListProvider */ - public function testAttemptToSerializeANonQueryEntityCausesException( $notAQueryEntity ) { - $serializer = $this->newSimpleQueryEntitySerializer(); + public function testSerializationContainsLabels( array $labelList ) { + $queryEntity = $this->newSimpleEntity(); - $this->setExpectedException( 'InvalidArgumentException' ); - $serializer->serialize( $notAQueryEntity ); + $queryEntity->setLabels( $labelList ); + + $serialization = $this->newSimpleQueryEntitySerializer()->serialize( $queryEntity ); + + $this->assertHasSerializedLabels( $serialization, $labelList ); } - public function notAQueryEntityProvider() { - $argLists = array(); + public function labelListProvider() { + return array( + array( array() ), - $argLists[] = array( null ); - $argLists[] = array( true ); - $argLists[] = array( 42 ); - $argLists[] = array( 'foo bar baz' ); - $argLists[] = array( array( 1, 2, '3' ) ); - $argLists[] = array( (object)array( 1, 2, '3' ) ); + array( array( + 'en' => 'Test Label' + ) ), - return $argLists; + array( array( + 'en' => 'Test Label', + 'de' => 'Die Teste Descript' + ) ), + ); + } + + protected function assertHasSerializedLabels( $serialization, array $expectedLabels ) { + $this->assertInternalType( 'array', $serialization ); + $this->assertArrayHasKey( 'label', $serialization ); + $this->assertEquals( $expectedLabels, $serialization['label'] ); + } + + /** + * @dataProvider aliasListProvider + */ + public function testSerializationContainsAliases( array $aliasLists ) { + $queryEntity = $this->newSimpleEntity(); + + $queryEntity->setAllAliases( $aliasLists ); + + $serialization = $this->newSimpleQueryEntitySerializer()->serialize( $queryEntity ); + + $this->assertHasSerializedAliases( $serialization, $aliasLists ); + } + + public function aliasListProvider() { + return array( + array( array() ), + + array( array( + 'en' => array( 'foo' ), + ) ), + + array( array( + 'en' => array( 'foo', 'bar' ), + ) ), + + array( array( + 'en' => array( 'foo', 'bar' ), + 'de' => array( 'die', 'bar' ), + ) ), + ); + } + + protected function assertHasSerializedAliases( $serialization, array $expectedLabels ) { + $this->assertInternalType( 'array', $serialization ); + $this->assertArrayHasKey( 'aliases', $serialization ); + $this->assertEquals( $expectedLabels, $serialization['aliases'] ); + } + + /** + * @dataProvider claimListProvider + */ + public function testSerializationContainsClaims( array $claimList ) { + $queryEntity = $this->newSimpleEntity(); + + $queryEntity->setClaims( new Claims( $claimList ) ); + + $serialization = $this->newSimpleQueryEntitySerializer()->serialize( $queryEntity ); + + $this->assertHasSerializedClaims( $serialization, $claimList ); + } + + public function claimListProvider() { + return array( + array( array() ), + + array( array( + new Claim( new PropertySomeValueSnak( 42 ) ) + ) ), + + array( array( + new Claim( new PropertySomeValueSnak( 42 ) ), + new Claim( new PropertyNoValueSnak( 1337 ) ) + ) ), + ); + } + + /** + * @param string $serialization + * @param Claim[] $expectedClaims + */ + protected function assertHasSerializedClaims( $serialization, array $expectedClaims ) { + $expectedSerialization = array(); + + foreach ( $expectedClaims as $claim ) { + $expectedSerialization[] = $claim->toArray(); + } + + $this->assertInternalType( 'array', $serialization ); + $this->assertArrayHasKey( 'claim', $serialization ); + $this->assertEquals( $expectedSerialization, $serialization['claim'] ); } } diff --git a/src/Wikibase/Query/QueryEntitySerializer.php b/src/Wikibase/Query/QueryEntitySerializer.php index 92f3b87..919dab0 100644 --- a/src/Wikibase/Query/QueryEntitySerializer.php +++ b/src/Wikibase/Query/QueryEntitySerializer.php @@ -27,14 +27,20 @@ public function serialize( $queryEntity ) { if( !( $queryEntity instanceof QueryEntity ) ){ - throw new InvalidArgumentException('Not instance of queryEntity'); + throw new InvalidArgumentException( 'Not instance of queryEntity' ); } - $querySerialization = $this->querySerializer->serialize( $queryEntity ); + $querySerialization = $this->querySerializer->serialize( $queryEntity->getQuery() ); return array( 'entity' => $this->getSerializedId( $queryEntity ), + 'description' => $queryEntity->getDescriptions(), + 'label' => $queryEntity->getLabels(), + 'aliases' => $queryEntity->getAllAliases(), + + 'claim' => $this->getSerializedClaims( $queryEntity ), + 'query' => $querySerialization, ); } @@ -50,6 +56,14 @@ } } - // TODO + protected function getSerializedClaims( Entity $entity ){ + $serializedClaims = array(); + + foreach ( $entity->getClaims() as $claim ){ + $serializedClaims[] = $claim->toArray(); + } + + return $serializedClaims; + } } -- To view, visit https://gerrit.wikimedia.org/r/75625 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: If8878e877d37dcdd6f87b477081a1690befdb609 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/WikibaseQuery Gerrit-Branch: master Gerrit-Owner: Jeroen De Dauw <[email protected]> _______________________________________________ MediaWiki-commits mailing list [email protected] https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits
