Jeroen De Dauw has uploaded a new change for review.
https://gerrit.wikimedia.org/r/73983
Change subject: Work on ByPropertyValueEntityFinder
......................................................................
Work on ByPropertyValueEntityFinder
Change-Id: Ib3ac22e8e30dcc22286c80bec6133201f2705031
---
M Tests/Phpunit/Wikibase/Query/ByPropertyValueEntityFinderTest.php
M src/Wikibase/Query/Api/EntitiesByPropertyValueWrapper.php
M src/Wikibase/Query/ByPropertyValueEntityFinder.php
3 files changed, 208 insertions(+), 23 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/WikibaseQuery
refs/changes/83/73983/1
diff --git a/Tests/Phpunit/Wikibase/Query/ByPropertyValueEntityFinderTest.php
b/Tests/Phpunit/Wikibase/Query/ByPropertyValueEntityFinderTest.php
index c70e75f..daa36d2 100644
--- a/Tests/Phpunit/Wikibase/Query/ByPropertyValueEntityFinderTest.php
+++ b/Tests/Phpunit/Wikibase/Query/ByPropertyValueEntityFinderTest.php
@@ -2,12 +2,13 @@
namespace Tests\Phpunit\Wikibase\Query;
+use Ask\Language\Description\AnyValue;
use Ask\Language\Description\Description;
use Ask\Language\Description\SomeProperty;
use Ask\Language\Description\ValueDescription;
use Ask\Language\Option\QueryOptions;
-use Ask\Language\Query;
use DataValues\DataValue;
+use DataValues\DataValueFactory;
use DataValues\StringValue;
use Wikibase\EntityId;
use Wikibase\Query\ByPropertyValueEntityFinder;
@@ -27,7 +28,24 @@
/**
* @dataProvider queryProvider
*/
- public function testCanConstruct( EntityId $propertyId, DataValue
$dataValue, Description $description, QueryOptions $options ) {
+ public function testFindEntities( $propertyIdString,
$dataValueSerialization, Description $description, QueryOptions $options ) {
+ $expectedIds = array(
+ new EntityId( 'hax', 1337 )
+ );
+
+ $entityFinder = $this->newEntityFinder( $description, $options,
$expectedIds, $propertyIdString );
+
+ $entityIds = $entityFinder->findEntities( array(
+ 'property' => $propertyIdString,
+ 'value' => $dataValueSerialization,
+ 'limit' => (string)$options->getLimit(),
+ 'offset' => (string)$options->getOffset()
+ ) );
+
+ $this->assertEntityIdsEqual( $expectedIds, $entityIds );
+ }
+
+ protected function newEntityFinder( $description, $options,
$expectedIds, $propertyIdString ) {
$queryEngine = $this->getMock(
'Wikibase\QueryEngine\QueryEngine' );
$queryEngine->expects( $this->once() )
@@ -35,38 +53,54 @@
->with(
$this->equalTo( $description ),
$this->equalTo( $options )
- );
+ )
+ ->will( $this->returnValue( $expectedIds ) );
- $entityFinder = new ByPropertyValueEntityFinder( $queryEngine );
- $entities = $entityFinder->findEntities( $propertyId,
$dataValue, $options->getLimit(), $options->getOffset() );
+ $dvFactory = new DataValueFactory();
+ $dvFactory->registerDataValue( 'string',
'DataValues\StringValue' );
- $this->assertInternalType( 'array', $entities );
- $this->assertContainsOnlyInstancesOf( 'Wikibase\EntityId',
$entities );
+ $idParser = $this->getMockBuilder(
'Wikibase\Lib\EntityIdParser' )
+ ->disableOriginalConstructor()->getMock();
+
+ $idParser->expects( $this->once() )
+ ->method( 'parse' )
+ ->with( $this->equalTo( $propertyIdString ) )
+ ->will( $this->returnValue( $this->mockProperty() ) );
+
+ return new ByPropertyValueEntityFinder( $queryEngine,
$dvFactory, $idParser );
+ }
+
+ protected function mockProperty() {
+ return new EntityId( 'property', 4242 );
+ }
+
+ protected function assertEntityIdsEqual( array $expected, $actual ) {
+ $this->assertInternalType( 'array', $actual );
+ $this->assertContainsOnlyInstancesOf( 'Wikibase\EntityId',
$actual );
+ $this->assertEquals( $expected, $actual );
}
public function queryProvider() {
$argLists = array();
- $p42 = new EntityId( 'property', 42 );
- $p9001 = new EntityId( 'property', 9001 );
$fooString = new StringValue( 'foo' );
$barString = new StringValue( 'bar baz' );
$argLists[] = array(
- $p42,
- $fooString,
+ 'p42',
+ $fooString->toArray(),
new SomeProperty(
- $p42,
+ $this->mockProperty(),
new ValueDescription( $fooString )
),
new QueryOptions( 10, 0 )
);
$argLists[] = array(
- $p9001,
- $barString,
+ 'p9001',
+ $barString->toArray(),
new SomeProperty(
- $p9001,
+ $this->mockProperty(),
new ValueDescription( $barString )
),
new QueryOptions( 42, 100 )
@@ -75,4 +109,106 @@
return $argLists;
}
+ /**
+ * @dataProvider invalidLimitProvider
+ */
+ public function testInvalidLimitCausesException( $limit ) {
+ $this->setExpectedException( 'InvalidArgumentException' );
+
+ $fooString = new StringValue( 'foo' );
+
+ $this->newSimpleEntityIdFiner()->findEntities(
+ array(
+ 'property' => $this->mockProperty()->toArray(),
+ 'value' => $fooString->toArray(),
+ 'limit' => $limit,
+ 'offset' => '0'
+ )
+ );
+ }
+
+ protected function newSimpleEntityIdFiner() {
+ $queryEngine = $this->getMock(
'Wikibase\QueryEngine\QueryEngine' );
+
+ $queryEngine->expects( $this->any() )
+ ->method( 'getMatchingEntities' )
+ ->will( $this->returnValue( array() ) );
+
+ $dvFactory = new DataValueFactory();
+ $dvFactory->registerDataValue( 'string',
'DataValues\StringValue' );
+
+ $idParser = $this->getMockBuilder(
'Wikibase\Lib\EntityIdParser' )
+ ->disableOriginalConstructor()->getMock();
+
+ $idParser->expects( $this->any() )
+ ->method( 'parse' )
+ ->will( $this->returnValue( $this->mockProperty() ) );
+
+ return new ByPropertyValueEntityFinder( $queryEngine,
$dvFactory, $idParser );
+ }
+
+ public function invalidLimitProvider() {
+ return array(
+ array( 10 ),
+ array( 4.2 ),
+ array( '' ),
+ array( '4.2' ),
+ array( '-2' ),
+ array( '0' ),
+ );
+ }
+
+ /**
+ * @dataProvider invalidOffsetProvider
+ */
+ public function testInvalidOffsetCausesException( $offset ) {
+ $this->setExpectedException( 'InvalidArgumentException' );
+
+ $fooString = new StringValue( 'foo' );
+
+ $this->newSimpleEntityIdFiner()->findEntities(
+ array(
+ 'property' => $this->mockProperty()->toArray(),
+ 'value' => $fooString->toArray(),
+ 'limit' => '100',
+ 'offset' => $offset
+ )
+ );
+ }
+
+ public function invalidOffsetProvider() {
+ return array(
+ array( 10 ),
+ array( 4.2 ),
+ array( '' ),
+ array( '4.2' ),
+ array( '-2' ),
+ );
+ }
+
+ /**
+ * @dataProvider invalidValueProvider
+ */
+ public function testInvalidValueCausesException( $value ) {
+ $this->setExpectedException( 'InvalidArgumentException' );
+
+ $this->newSimpleEntityIdFiner()->findEntities(
+ array(
+ 'property' => $this->mockProperty()->toArray(),
+ 'value' => $value,
+ 'limit' => '100',
+ 'offset' => '0'
+ )
+ );
+ }
+
+ public function invalidValueProvider() {
+ return array(
+ array( array() ),
+ array( null ),
+ array( true ),
+ array( array( 'foo' ) ),
+ );
+ }
+
}
diff --git a/src/Wikibase/Query/Api/EntitiesByPropertyValueWrapper.php
b/src/Wikibase/Query/Api/EntitiesByPropertyValueWrapper.php
index 9e7960f..c40575a 100644
--- a/src/Wikibase/Query/Api/EntitiesByPropertyValueWrapper.php
+++ b/src/Wikibase/Query/Api/EntitiesByPropertyValueWrapper.php
@@ -22,7 +22,11 @@
* @since 0.1
*/
public function execute() {
-
ExtensionAccess::getWikibaseQuery()->getByPropertyValueEntityFinder(); // TODO
->findEntities()
+ $entityFinder =
ExtensionAccess::getWikibaseQuery()->getByPropertyValueEntityFinder();
+ $entityFinder->findEntities( $this->extractRequestParams() );
+
+ // TODO: add to API output
+ // TODO: system test
}
/**
diff --git a/src/Wikibase/Query/ByPropertyValueEntityFinder.php
b/src/Wikibase/Query/ByPropertyValueEntityFinder.php
index a620155..d598d2b 100644
--- a/src/Wikibase/Query/ByPropertyValueEntityFinder.php
+++ b/src/Wikibase/Query/ByPropertyValueEntityFinder.php
@@ -5,11 +5,13 @@
use Ask\Language\Description\SomeProperty;
use Ask\Language\Description\ValueDescription;
use Ask\Language\Option\QueryOptions;
-use Ask\Language\Query;
use DataValues\DataValue;
+use DataValues\DataValueFactory;
+use InvalidArgumentException;
+use RuntimeException;
use Wikibase\EntityId;
+use Wikibase\Lib\EntityIdParser;
use Wikibase\QueryEngine\QueryEngine;
-use Wikibase\QueryEngine\SQLStore\Engine\DescriptionMatchFinder;
/**
* @since 0.1
@@ -23,9 +25,54 @@
class ByPropertyValueEntityFinder {
protected $queryEngine;
+ protected $dvFactory;
- public function __construct( QueryEngine $queryEngine ) {
+ public function __construct( QueryEngine $queryEngine, DataValueFactory
$dvFactory, EntityIdParser $idParser ) {
$this->queryEngine = $queryEngine;
+ $this->dvFactory = $dvFactory;
+ $this->idParser = $idParser;
+ }
+
+ public function findEntities( array $requestArguments ) {
+ return $this->findEntitiesGivenRawArguments(
+ $requestArguments['property'],
+ $requestArguments['value'],
+ $requestArguments['limit'],
+ $requestArguments['offset']
+ );
+ }
+
+ /**
+ * @param string $propertyIdString
+ * @param string $valueString
+ * @param string $limit
+ * @param string $offset
+ *
+ * @return EntityId[]
+ * @throws InvalidArgumentException
+ */
+ protected function findEntitiesGivenRawArguments( $propertyIdString,
$valueString, $limit, $offset ) {
+ if ( !is_string( $limit ) || !ctype_digit( $limit ) ||
(int)$limit < 1 ) {
+ throw new InvalidArgumentException( '$limit needs to be
a string representing a strictly positive integer' );
+ }
+
+ if ( !is_string( $offset ) || !ctype_digit( $offset ) ) {
+ throw new InvalidArgumentException( '$offset needs to
be a string representing a positive integer' );
+ }
+
+ if ( !is_array( $valueString ) ) {
+ throw new InvalidArgumentException( '$valueString needs
to be a string serialization of a DataValue' );
+ }
+
+ try {
+ $propertyId = $this->idParser->parse( $propertyIdString
);
+ $value = $this->dvFactory->newFromArray( $valueString );
+ }
+ catch ( RuntimeException $ex ) {
+ throw new InvalidArgumentException( '', 0, $ex );
+ }
+
+ return $this->findByPropertyValue( $propertyId, $value, $limit,
$offset );
}
/**
@@ -36,7 +83,7 @@
*
* @return EntityId[]
*/
- public function findEntities( EntityId $propertyId, DataValue $value,
$limit, $offset ) {
+ protected function findByPropertyValue( EntityId $propertyId, DataValue
$value, $limit, $offset ) {
$description = new SomeProperty(
$propertyId,
new ValueDescription( $value )
@@ -44,9 +91,7 @@
$options = new QueryOptions( $limit, $offset );
- $this->queryEngine->getMatchingEntities( $description, $options
);
-
- return array(); // TODO
+ return $this->queryEngine->getMatchingEntities( $description,
$options );
}
}
--
To view, visit https://gerrit.wikimedia.org/r/73983
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib3ac22e8e30dcc22286c80bec6133201f2705031
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