Jakob has uploaded a new change for review.

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

Change subject: Add DispatchingPropertyInfoLookup
......................................................................

Add DispatchingPropertyInfoLookup

This service implements PropertyInfoLookup. It dispatches calls to
getPropertyInfo to the respective repository-specific service and
combines the results of all injected PropertyInfoLookups for
getPropertyInfoForDataType and getAllPropertyInfo.

Bug: T152103
Change-Id: Iadb1539a749a602b62ab65d8d745b594d831bc0b
---
A lib/includes/Store/DispatchingPropertyInfoLookup.php
A lib/tests/phpunit/Store/DispatchingPropertyInfoLookupTest.php
2 files changed, 221 insertions(+), 0 deletions(-)


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

diff --git a/lib/includes/Store/DispatchingPropertyInfoLookup.php 
b/lib/includes/Store/DispatchingPropertyInfoLookup.php
new file mode 100644
index 0000000..db8cf0d
--- /dev/null
+++ b/lib/includes/Store/DispatchingPropertyInfoLookup.php
@@ -0,0 +1,86 @@
+<?php
+
+namespace Wikibase\Lib\Store;
+
+use Wikibase\DataModel\Assert\RepositoryNameAssert;
+use Wikibase\DataModel\Entity\PropertyId;
+use Wikimedia\Assert\Assert;
+
+class DispatchingPropertyInfoLookup implements PropertyInfoLookup {
+
+       /**
+        * @var PropertyInfoLookup[]
+        */
+       private $lookups;
+
+       /**
+        * @param PropertyInfoLookup[] $lookups
+        */
+       public function __construct( array $lookups ) {
+               Assert::parameter( !empty( $lookups ), '$lookups', 'must not be 
empty' );
+               
RepositoryNameAssert::assertParameterKeysAreValidRepositoryNames( $lookups, 
'$lookups' );
+               Assert::parameterElementType( PropertyInfoLookup::class, 
$lookups, '$lookups' );
+
+               $this->lookups = $lookups;
+       }
+
+       /**
+        * @see PropertyInfoLookup::getPropertyInfo
+        *
+        * @param PropertyId $propertyId
+        *
+        * @return array|null
+        *
+        * @throws StorageException
+        * @throws DBError
+        */
+       public function getPropertyInfo( PropertyId $propertyId ) {
+               $lookup = $this->getLookupForPropertyId( $propertyId );
+               return !is_null( $lookup ) ? $lookup->getPropertyInfo( 
$propertyId ) : null;
+       }
+
+       /**
+        * @see PropertyInfoLookup::getPropertyInfoForDataType
+        *
+        * Combines the results of getPropertyInfoForDataType from each of the 
injected PropertyInfoLookups.
+        *
+        * @param string $dataType
+        *
+        * @return array[]
+        *
+        * @throws StorageException
+        * @throws DBError
+        */
+       public function getPropertyInfoForDataType( $dataType ) {
+               return array_reduce( $this->lookups, function( array $info, 
PropertyInfoLookup $lookup ) use ( $dataType ) {
+                       return array_merge( $info, 
$lookup->getPropertyInfoForDataType( $dataType ) );
+               }, [] );
+       }
+
+       /**
+        * @see PropertyInfoLookup::getAllPropertyInfo
+        *
+        * Combines the results of getAllPropertyInfo from each of the injected 
PropertyInfoLookups.
+        *
+        * @return array[]
+        *
+        * @throws StorageException
+        * @throws DBError
+        */
+       public function getAllPropertyInfo() {
+               return array_reduce( $this->lookups, function( array $info, 
PropertyInfoLookup $lookup ) {
+                       return array_merge( $info, 
$lookup->getAllPropertyInfo() );
+               }, [] );
+       }
+
+       /**
+        * @param PropertyId $id
+        *
+        * @return PropertyInfoLookup|null
+        */
+       private function getLookupForPropertyId( PropertyId $id ) {
+               $repo = $id->getRepositoryName();
+               return isset( $this->lookups[$repo] ) ? $this->lookups[$repo] : 
null;
+       }
+
+}
diff --git a/lib/tests/phpunit/Store/DispatchingPropertyInfoLookupTest.php 
b/lib/tests/phpunit/Store/DispatchingPropertyInfoLookupTest.php
new file mode 100644
index 0000000..54a8a3d
--- /dev/null
+++ b/lib/tests/phpunit/Store/DispatchingPropertyInfoLookupTest.php
@@ -0,0 +1,135 @@
+<?php
+
+namespace Wikibase\Lib\Tests\Store;
+
+use InvalidArgumentException;
+use Wikibase\DataModel\Entity\ItemId;
+use Wikibase\DataModel\Entity\PropertyId;
+use Wikibase\Lib\Store\DispatchingPropertyInfoLookup;
+use Wikibase\Lib\Store\PropertyInfoLookup;
+use Wikibase\PropertyInfoStore;
+
+/**
+ * @covers Wikibase\Lib\Store\DispatchingPropertyInfoLookup
+ *
+ * @group WikibaseLib
+ * @group WikibaseStore
+ * @group Wikibase
+ *
+ * @license GPL-2.0+
+ */
+class DispatchingPropertyInfoLookupTest extends \PHPUnit_Framework_TestCase {
+
+       private $localPropertyInfo;
+       private $fooPropertyInfo;
+
+       public function __construct( $name = null, $data = [], $dataName = '' ) 
{
+               parent::__construct( $name, $data, $dataName );
+
+               $this->localPropertyInfo = [
+                       'P23' => [ PropertyInfoStore::KEY_DATA_TYPE => 'string' 
],
+                       'P42' => [ PropertyInfoStore::KEY_DATA_TYPE => 
'commonsMedia', 'foo' => 'bar' ]
+               ];
+               $this->fooPropertyInfo = [
+                       'foo:P123' => [ PropertyInfoStore::KEY_DATA_TYPE => 
'string', 'foo' => 'bar' ],
+                       'foo:P42' => [ PropertyInfoStore::KEY_DATA_TYPE => 
'commonsMedia' ]
+               ];
+       }
+
+       public function 
testGivenDifferentPropertyIds_getPropertyInfoDispatchesAccordingToRepository() {
+               $lookup = new DispatchingPropertyInfoLookup( [
+                       '' => $this->getPropertyInfoLookup( 
$this->localPropertyInfo ),
+                       'foo' => $this->getPropertyInfoLookup( 
$this->fooPropertyInfo ),
+               ] );
+
+               $this->assertSame(
+                       $this->localPropertyInfo['P23'],
+                       $lookup->getPropertyInfo( new PropertyId( 'P23' ) )
+               );
+               $this->assertSame(
+                       $this->fooPropertyInfo['foo:P42'],
+                       $lookup->getPropertyInfo( new PropertyId( 'foo:P42' ) )
+               );
+       }
+
+       public function 
testGivenMultiplePropertyInfoLookups_getAllPropertyInfoCombinesResults() {
+               $lookup = new DispatchingPropertyInfoLookup( [
+                       '' => $this->getPropertyInfoLookup( 
$this->localPropertyInfo ),
+                       'foo' => $this->getPropertyInfoLookup( 
$this->fooPropertyInfo ),
+               ] );
+
+               $this->assertSame(
+                       $lookup->getAllPropertyInfo(),
+                       array_merge( $this->localPropertyInfo, 
$this->fooPropertyInfo )
+               );
+       }
+
+       public function 
testGivenMultiplePropertyInfoLookups_getPropertyInfoForDataTypeCombinesResults()
 {
+               $lookup = new DispatchingPropertyInfoLookup( [
+                       '' => $this->getPropertyInfoLookup( 
$this->localPropertyInfo ),
+                       'foo' => $this->getPropertyInfoLookup( 
$this->fooPropertyInfo ),
+               ] );
+
+               $this->assertSame(
+                       [ 'P23' => $this->localPropertyInfo['P23'], 'foo:P123' 
=> $this->fooPropertyInfo['foo:P123'] ],
+                       $lookup->getPropertyInfoForDataType( 'string' )
+               );
+               $this->assertSame(
+                       [ 'P42' => $this->localPropertyInfo['P42'], 'foo:P42' 
=> $this->fooPropertyInfo['foo:P42'] ],
+                       $lookup->getPropertyInfoForDataType( 'commonsMedia' )
+               );
+       }
+
+       private function getPropertyInfoLookup( $info ) {
+               $lookup = $this->getMock( PropertyInfoLookup::class );
+
+               $lookup->method( 'getPropertyInfo' )
+                       ->willReturnCallback( function( PropertyId $id ) use ( 
$info ) {
+                               return $info[$id->getSerialization()];
+                       } );
+
+               $lookup->method( 'getAllPropertyInfo' )
+                       ->willReturnCallback( function() use ( $info ) {
+                               return $info;
+                       } );
+
+               $lookup->method( 'getPropertyInfoForDataType' )
+                       ->willReturnCallback( function( $dataType ) use ( $info 
) {
+                               return array_filter( $info, function( array 
$propertyInfo ) use ( $dataType ) {
+                                       return 
$propertyInfo[PropertyInfoStore::KEY_DATA_TYPE] === $dataType;
+                               } );
+                       } );
+
+               return $lookup;
+       }
+
+       /**
+        * @dataProvider provideInvalidForeignLookups
+        */
+       public function testGivenInvalidPropertyInfoLookup_exceptionIsThrown( 
$lookups ) {
+               $this->setExpectedException( InvalidArgumentException::class );
+               new DispatchingPropertyInfoLookup( $lookups );
+       }
+
+       public function provideInvalidForeignLookups() {
+               return array(
+                       'no lookups given' => array( array() ),
+                       'not an implementation of PropertyInfoLookup given as a 
lookup' => array(
+                               array( '' => new ItemId( 'Q123' ) ),
+                       ),
+                       'non-string keys' => array(
+                               array(
+                                       '' => $this->getMock( 
PropertyInfoLookup::class ),
+                                       100 => $this->getMock( 
PropertyInfoLookup::class ),
+                               ),
+                       ),
+                       'repo name containing colon' => array(
+                               array(
+                                       '' => $this->getMock( 
PropertyInfoLookup::class ),
+                                       'fo:oo' => $this->getMock( 
PropertyInfoLookup::class ),
+                               ),
+                       ),
+               );
+       }
+
+}

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Iadb1539a749a602b62ab65d8d745b594d831bc0b
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Jakob <jakob.warkot...@wikimedia.de>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to