Skip to content

Commit

Permalink
Merge "REST: Create PropertyDescriptionsRetriever"
Browse files Browse the repository at this point in the history
  • Loading branch information
jenkins-bot authored and Gerrit Code Review committed Sep 20, 2023
2 parents 92641c9 + 42c221a commit f39d91b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php declare( strict_types=1 );

namespace Wikibase\Repo\RestApi\Domain\Services;

use Wikibase\DataModel\Entity\PropertyId;
use Wikibase\Repo\RestApi\Domain\ReadModel\Descriptions;

/**
* @license GPL-2.0-or-later
*/
interface PropertyDescriptionsRetriever {

public function getDescriptions( PropertyId $propertyId ): ?Descriptions;

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Wikibase\Repo\RestApi\Domain\Services\ItemDescriptionsRetriever;
use Wikibase\Repo\RestApi\Domain\Services\ItemLabelRetriever;
use Wikibase\Repo\RestApi\Domain\Services\ItemLabelsRetriever;
use Wikibase\Repo\RestApi\Domain\Services\PropertyDescriptionsRetriever;
use Wikibase\Repo\RestApi\Domain\Services\PropertyLabelsRetriever;

/**
Expand All @@ -25,7 +26,8 @@ class TermLookupEntityTermsRetriever implements
ItemLabelsRetriever,
ItemDescriptionRetriever,
ItemDescriptionsRetriever,
PropertyLabelsRetriever
PropertyLabelsRetriever,
PropertyDescriptionsRetriever
{

private TermLookup $termLookup;
Expand All @@ -51,7 +53,7 @@ public function getLabels( EntityId $entityId ): ?Labels {
try {
$labels = $this->termLookup->getLabels( $entityId, $this->termLanguages->getLanguages() );
} catch ( TermLookupException $e ) {
// this probably means that the item does not exist, which should be checked prior to calling this method
// this probably means that the entity does not exist, which should be checked prior to calling this method
return null;
}

Expand All @@ -72,11 +74,11 @@ public function getDescription( ItemId $itemId, string $languageCode ): ?Descrip
return $descriptionText !== null ? new Description( $languageCode, $descriptionText ) : null;
}

public function getDescriptions( ItemId $itemId ): ?Descriptions {
public function getDescriptions( EntityId $entityId ): ?Descriptions {
try {
$descriptions = $this->termLookup->getDescriptions( $itemId, $this->termLanguages->getLanguages() );
$descriptions = $this->termLookup->getDescriptions( $entityId, $this->termLanguages->getLanguages() );
} catch ( TermLookupException $e ) {
// this probably means that the item does not exist, which should be checked prior to calling this method
// this probably means that the entity does not exist, which should be checked prior to calling this method
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public function testGivenNoLabelInRequestedLanguage_getLabelReturnsNull(): void
);
}

public function testGetDescriptions(): void {
public function testGetItemDescriptions(): void {
$itemId = new ItemId( self::ITEM_ID );

$termLookup = $this->createMock( TermLookup::class );
Expand All @@ -130,14 +130,37 @@ public function testGetDescriptions(): void {
);
}

public function testGetPropertyDescriptions(): void {
$propertyId = new NumericPropertyId( self::PROPERTY_ID );

$termLookup = $this->createMock( TermLookup::class );
$termLookup->expects( $this->once() )
->method( 'getDescriptions' )
->with( $propertyId, self::ALL_TERM_LANGUAGES )
->willReturn( [
'en' => 'english test property',
'de' => 'deutsche Test-Eigenschaft',
'ko' => '한국어 시험 속성',
] );

$this->assertEquals(
( $this->newTermRetriever( $termLookup ) )->getDescriptions( $propertyId ),
new Descriptions(
new Description( 'en', 'english test property' ),
new Description( 'de', 'deutsche Test-Eigenschaft' ),
new Description( 'ko', '한국어 시험 속성' ),
)
);
}

public function testDescriptionsLookupThrowsLookupException_returnsNull(): void {
$itemId = new ItemId( self::ITEM_ID );
$entityId = $this->createStub( EntityId::class );

$termLookup = $this->createStub( TermLookup::class );
$termLookup->method( 'getDescriptions' )
->willThrowException( new TermLookupException( $itemId, [] ) );
->willThrowException( new TermLookupException( $entityId, [] ) );

$this->assertNull( $this->newTermRetriever( $termLookup )->getDescriptions( $itemId ) );
$this->assertNull( $this->newTermRetriever( $termLookup )->getDescriptions( $entityId ) );
}

private function newTermRetriever( TermLookup $termLookup ): TermLookupEntityTermsRetriever {
Expand Down

0 comments on commit f39d91b

Please sign in to comment.