Aude has uploaded a new change for review.

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

Change subject: Throw StorageException in EntityTermLookup for non-existing 
entities
......................................................................

Throw StorageException in EntityTermLookup for non-existing entities

consistent with the EntityRetrievingTermLookup and to
allow different handling of the case of Entity does
not exist vs. Entity exists but term not found.

also updated documentation of LabelLookup.

Change-Id: I63ccb80447a19396cde607308ef75d38b4234861
---
M lib/includes/store/EntityTermLookup.php
M lib/includes/store/LabelLookup.php
M lib/tests/phpunit/store/EntityTermLookupTest.php
3 files changed, 65 insertions(+), 56 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase 
refs/changes/52/172652/1

diff --git a/lib/includes/store/EntityTermLookup.php 
b/lib/includes/store/EntityTermLookup.php
index a8b282b..227f7e9 100644
--- a/lib/includes/store/EntityTermLookup.php
+++ b/lib/includes/store/EntityTermLookup.php
@@ -20,16 +20,24 @@
        private $termIndex;
 
        /**
-        * @param TermIndex $termIndex
+        * @var EntityLookup
         */
-       public function __construct( TermIndex $termIndex ) {
+       private $entityLookup;
+
+       /**
+        * @param TermIndex $termIndex
+        * @param EntityLookup $entityLookup
+        */
+       public function __construct( TermIndex $termIndex, EntityLookup 
$entityLookup ) {
                $this->termIndex = $termIndex;
+               $this->entityLookup = $entityLookup;
        }
 
        /**
         * @param EntityId $entityId
         * @param string $languageCode
         *
+        * @throws StorageException if entity does not exist
         * @throws OutOfBoundsException
         * @return string
         */
@@ -41,6 +49,7 @@
        /**
         * @param EntityId $entityId
         *
+        * @throws StorageException if entity does not exist
         * @return string[]
         */
        public function getLabels( EntityId $entityId ) {
@@ -51,6 +60,7 @@
         * @param EntityId $entityId
         * @param string $languageCode
         *
+        * @throws StorageException if entity does not exist
         * @throws OutOfBoundsException
         * @return string
         */
@@ -62,6 +72,7 @@
        /**
         * @param EntityId $entityId
         *
+        * @throws StorageException if entity does not exist
         * @return string[]
         */
        public function getDescriptions( EntityId $entityId ) {
@@ -72,26 +83,35 @@
         * @param EntityId $entityId
         * @param string $termType
         *
+        * @throws StorageException if entity does not exist.
         * @return string[]
         */
        private function getTermsOfType( EntityId $entityId, $termType ) {
                $wikibaseTerms = $this->termIndex->getTermsOfEntity( $entityId 
);
+
+               if ( $wikibaseTerms === array() ) {
+                       if ( !$this->entityLookup->hasEntity( $entityId ) ) {
+                               throw new StorageException( 'Entity not found 
for '
+                                       . $entityId->getSerialization() );
+                       }
+               }
+
                return $this->convertTermsToTermTypeArray( $wikibaseTerms, 
$termType );
        }
 
        /**
-        * @param string[] $labels
+        * @param string[] $terms
         * @param string $languageCode
         *
         * @throws OutOfBoundsException
         * @return string
         */
-       private function filterByLanguage( array $labels, $languageCode ) {
-               if ( array_key_exists( $languageCode, $labels ) ) {
-                       return $labels[$languageCode];
+       private function filterByLanguage( array $terms, $languageCode ) {
+               if ( array_key_exists( $languageCode, $terms ) ) {
+                       return $terms[$languageCode];
                }
 
-               throw new OutOfBoundsException( 'Label not found for ' . 
$languageCode );
+               throw new OutOfBoundsException( 'Term not found for ' . 
$languageCode );
        }
 
        /**
diff --git a/lib/includes/store/LabelLookup.php 
b/lib/includes/store/LabelLookup.php
index 2c9d947..c91f36c 100644
--- a/lib/includes/store/LabelLookup.php
+++ b/lib/includes/store/LabelLookup.php
@@ -17,6 +17,7 @@
         * @param EntityId $entityId
         *
         * @throws OutOfBoundsException
+        * @throws StorageException for entity not found
         * @return string
         */
        public function getLabel( EntityId $entityId );
diff --git a/lib/tests/phpunit/store/EntityTermLookupTest.php 
b/lib/tests/phpunit/store/EntityTermLookupTest.php
index f1236b8..507a2dc 100644
--- a/lib/tests/phpunit/store/EntityTermLookupTest.php
+++ b/lib/tests/phpunit/store/EntityTermLookupTest.php
@@ -21,35 +21,33 @@
                $this->assertEquals( 'New York City', $label );
        }
 
-       public function testGetLabel_notFoundThrowsException() {
+       public function testGetLabel_noLabelFoundThrowsException() {
                $termLookup = $this->getEntityTermLookup();
 
                $this->setExpectedException( 'OutOfBoundsException' );
-               $termLookup->getLabel( new ItemId( 'Q120' ), 'en' );
+               $termLookup->getLabel( new ItemId( 'Q116' ), 'fa' );
        }
 
-       /**
-        * @dataProvider getLabelsProvider
-        */
-       public function testGetLabels( $expected, EntityId $entityId ) {
+       public function testGetLabels() {
                $termLookup = $this->getEntityTermLookup();
 
-               $labels = $termLookup->getLabels( $entityId );
+               $expected =  array(
+                       'en' => 'New York City',
+                       'es' => 'Nueva York'
+               );
+
+               $labels = $termLookup->getLabels( new ItemId( 'Q116' ) );
                $this->assertEquals( $expected, $labels );
        }
 
-       public function getLabelsProvider() {
-               return array(
-                       array(
-                               array( 'en' => 'New York City', 'es' => 'Nueva 
York' ),
-                               new ItemId( 'Q116' )
-                       ),
-                       array(
-                               array(),
-                               new ItemId( 'Q120' )
-                       )
-               );
+       public function testGetLabels_entityNotFoundThrowsStorageException() {
+               $termLookup = $this->getEntityTermLookup();
+
+               $this->setExpectedException( 
'Wikibase\Lib\Store\StorageException' );
+
+               $termLookup->getLabels( new ItemId( 'Q9999' ) );
        }
+
 
        public function testGetDescription() {
                $termLookup = $this->getEntityTermLookup();
@@ -60,42 +58,39 @@
                $this->assertEquals( $expected, $description );
        }
 
-       public function testGetDescription_notFoundThrowsException() {
+       public function testGetDescription_descriptionNotFoundThrowsException() 
{
                $termLookup = $this->getEntityTermLookup();
 
                $this->setExpectedException( 'OutOfBoundsException' );
-               $termLookup->getDescription( new ItemId( 'Q90000' ), 'fr' );
+               $termLookup->getDescription( new ItemId( 'Q116' ), 'fr' );
        }
 
-       /**
-        * @dataProvider getDescriptionsProvider
-        */
-       public function getDescriptions( $expected, EntityId $entityId ) {
+       public function getDescriptions() {
                $termLookup = $this->getEntityTermLookup();
 
-               $descriptions = $termLookup->getDescriptions( $entityId );
+               $descriptions = $termLookup->getDescriptions( new ItemId( 
'Q116' ) );
+
+               $expected = array(
+                       'de' => 'Metropole an der Ostküste der Vereinigten 
Staaten',
+                       'en' => 'largest city in New York and the United States 
of America',
+               );
+
                $this->assertEquals( $expected, $descriptions );
        }
 
-       public function getDescriptionsProvider() {
-               return array(
-                       array(
-                               array(
-                                       'de' => 'Metropole an der Ostküste der 
Vereinigten Staaten',
-                                       'en' => 'largest city in New York and 
the United States of America',
-                               ),
-                               new ItemId( 'Q116' )
-                       ),
-                       array(
-                               array(),
-                               new ItemId( 'Q90001' )
-                       )
-               );
-       }
-
        private function getEntityTermLookup() {
+               $entityLookup = $this->getMockBuilder( 
'Wikibase\Lib\Store\EntityLookup' )
+                       ->disableOriginalConstructor()
+                       ->getMock();
+
+               $entityLookup->expects( $this->any() )
+                       ->method( 'hasEntity' )
+                       ->will( $this->returnCallback( function( EntityId 
$entityId ) {
+                               return $entityId->getSerialization() === 'Q116';
+                       } ) );
+
                $termIndex = $this->getTermIndex();
-               return new EntityTermLookup( $termIndex );
+               return new EntityTermLookup( $termIndex, $entityLookup );
        }
 
        private function getTermIndex() {
@@ -127,13 +122,6 @@
                                'termType' => 'description',
                                'termLanguage' => 'de',
                                'termText' => 'Metropole an der Ostküste der 
Vereinigten Staaten'
-                       ) ),
-                       new \Wikibase\Term( array(
-                               'entityId' => 117,
-                               'entityType' => 'item',
-                               'termType' => 'label',
-                               'termLanguage' => 'en',
-                               'termText' => 'Berlin'
                        ) ),
                );
 

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I63ccb80447a19396cde607308ef75d38b4234861
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Aude <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to