Jeroen De Dauw has uploaded a new change for review.

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


Change subject: Add error handling to EntitiesByPropertyValue
......................................................................

Add error handling to EntitiesByPropertyValue

Change-Id: I14da12256386e25879954daa0f97b9a4364c7362
---
M Tests/System/Wikibase/Query/EntitiesByPropertyValueApiTest.php
M phpunit.xml.dist
M src/Wikibase/Query/Api/EntitiesByPropertyValue.php
3 files changed, 95 insertions(+), 33 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/WikibaseQuery 
refs/changes/08/75108/1

diff --git a/Tests/System/Wikibase/Query/EntitiesByPropertyValueApiTest.php 
b/Tests/System/Wikibase/Query/EntitiesByPropertyValueApiTest.php
index ff25b78..dca50a2 100644
--- a/Tests/System/Wikibase/Query/EntitiesByPropertyValueApiTest.php
+++ b/Tests/System/Wikibase/Query/EntitiesByPropertyValueApiTest.php
@@ -23,6 +23,12 @@
  */
 class EntitiesByPropertyValueApiTest extends \ApiTestCase {
 
+       const MODULE_NAME = 'entitiesByPropertyValue';
+       const PROPERTY_ID = 31337;
+       const ITEM_ID = 42;
+       const PROPERTY_ID_STRING = 'p31337';
+       const ITEM_ID_STRING = 'q42';
+
        protected function getQueryStore() {
                return ExtensionAccess::getWikibaseQuery()->getQueryStore();
        }
@@ -38,6 +44,9 @@
        public function setUp() {
                parent::setUp();
                $this->reinitializeStore();
+
+               $this->createNewProperty();
+               $this->insertNewItem();
        }
 
        public function tearDown() {
@@ -45,33 +54,9 @@
                parent::tearDown();
        }
 
-       public function testMakeApiRequest() {
-               $this->createNewProperty();
-
-               $storeUpdater = $this->getQueryStore()->getUpdater();
-
-               $item = $this->newMockItem();
-
-               $storeUpdater->insertEntity( $item );
-
-               $params = array(
-                       'action' => 'entitiesByPropertyValue',
-                       'property' => 'p31337',
-                       'value' => json_encode( 
$this->newMockValue()->toArray() ),
-               );
-
-               list( $resultArray, ) = $this->doApiRequest( $params );
-
-               $this->assertArrayHasKey( 'entities', $resultArray );
-
-               $entities = $resultArray['entities'];
-
-               $this->assertEquals( array( 'q42' ), $entities );
-       }
-
        protected function createNewProperty() {
                $property = Property::newEmpty();
-               $property->setId( new EntityId( 'property', 31337 ) );
+               $property->setId( new EntityId( 'property', self::PROPERTY_ID ) 
);
                $property->setDataTypeId( 'string' );
 
                $propertyContent = PropertyContent::newFromProperty( $property 
);
@@ -79,14 +64,22 @@
                $propertyContent->save();
        }
 
+       protected function insertNewItem() {
+               $storeUpdater = $this->getQueryStore()->getUpdater();
+
+               $item = $this->newMockItem();
+
+               $storeUpdater->insertEntity( $item );
+       }
+
        protected function newMockItem() {
                $item = Item::newEmpty();
 
-               $item->setId( new EntityId( 'item', 42 ) );
+               $item->setId( new EntityId( 'item', self::ITEM_ID ) );
 
                $item->addClaim( new Statement(
                        new PropertyValueSnak(
-                               new EntityId( 'property', 31337 ),
+                               new EntityId( 'property', self::PROPERTY_ID ),
                                $this->newMockValue()
                        )
                ) );
@@ -98,4 +91,58 @@
                return new StringValue( 'API tests really suck' );
        }
 
+       protected function newMockValueString() {
+               return '{"value":"API tests really suck","type":"string"}';
+       }
+
+       public function testMakeApiRequest() {
+               $resultArray = $this->getResultForRequestWithValue( 
$this->newMockValueString() );
+
+               $this->assertArrayHasKey( 'entities', $resultArray );
+
+               $entities = $resultArray['entities'];
+
+               $this->assertEquals( array( self::ITEM_ID_STRING ), $entities );
+       }
+
+       protected function getResultForRequestWithValue( $value ) {
+               $params = array(
+                       'action' => self::MODULE_NAME,
+                       'property' => self::PROPERTY_ID_STRING,
+                       'value' => $value,
+               );
+
+               return $this->getResultForRequest( $params );
+       }
+
+       protected function getResultForRequest( array $requestParams ) {
+               list( $resultArray, ) = $this->doApiRequest( $requestParams );
+
+               return $resultArray;
+       }
+
+       public function testMakeRequestWithInvalidValue() {
+               $this->setExpectedException(
+                       'UsageException',
+                       'The provided value needs to be a serialization of a 
DataValue'
+               );
+
+               $this->getResultForRequestWithValue(
+                       'Im an invalid value in your API request!'
+               );
+       }
+
+       public function testMakeRequestWithUnknownProperty() {
+               $this->setExpectedException(
+                       'UsageException',
+                       'The specified property does not exist'
+               );
+
+               $this->getResultForRequest( array(
+                       'action' => self::MODULE_NAME,
+                       'property' => 'p7201010',
+                       'value' => $this->newMockValueString(),
+               ) );
+       }
+
 }
diff --git a/phpunit.xml.dist b/phpunit.xml.dist
index 68df2e4..bede6f1 100644
--- a/phpunit.xml.dist
+++ b/phpunit.xml.dist
@@ -16,6 +16,7 @@
         <testsuite name="WikibaseQuery">
             <directory>Tests/Phpunit</directory>
             <directory>Tests/Integration</directory>
+            <directory>Tests/System</directory>
         </testsuite>
     </testsuites>
 </phpunit>
diff --git a/src/Wikibase/Query/Api/EntitiesByPropertyValue.php 
b/src/Wikibase/Query/Api/EntitiesByPropertyValue.php
index 7b0ab1a..25d01cd 100644
--- a/src/Wikibase/Query/Api/EntitiesByPropertyValue.php
+++ b/src/Wikibase/Query/Api/EntitiesByPropertyValue.php
@@ -3,6 +3,8 @@
 namespace Wikibase\Query\Api;
 
 use ApiBase;
+use InvalidArgumentException;
+use Wikibase\Lib\PropertyNotFoundException;
 use Wikibase\Query\DIC\ExtensionAccess;
 
 /**
@@ -24,13 +26,25 @@
        public function execute() {
                $entityFinder = 
ExtensionAccess::getWikibaseQuery()->getByPropertyValueEntityFinder();
 
-               // TODO: handle exceptions
-               $entityIds = $entityFinder->findEntities( 
$this->extractRequestParams() );
+               try {
+                       $entityIds = $entityFinder->findEntities( 
$this->extractRequestParams() );
+               }
+               catch ( InvalidArgumentException $ex ) {
+                       $this->dieUsage(
+                               'The provided value needs to be a serialization 
of a DataValue',
+                               'invalid-value'
+                       );
+               }
+               catch ( PropertyNotFoundException $ex ) {
+                       $this->dieUsage(
+                               'The specified property does not exist',
+                               'unknown-property'
+                       );
+               }
 
-               $this->getResult()->addValue( null, 'entities', $entityIds );
-
-               // TODO: add to API output
-               // TODO: system test
+               if ( isset( $entityIds ) ) {
+                       $this->getResult()->addValue( null, 'entities', 
$entityIds );
+               }
        }
 
        /**

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I14da12256386e25879954daa0f97b9a4364c7362
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

Reply via email to