-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
REST: Extract getStatementSubject to a new helper class
* As the function is now needed in multiple places, I thought having it in a seperate class would be a good idea. Change-Id: I7677486e57b68e5c186e530c5e723272e79ed92e
- Loading branch information
1 parent
910c646
commit 33b354d
Showing
5 changed files
with
183 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
repo/rest-api/src/Infrastructure/DataAccess/StatementSubjectRetriever.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php declare( strict_types=1 ); | ||
|
||
namespace Wikibase\Repo\RestApi\Infrastructure\DataAccess; | ||
|
||
use LogicException; | ||
use Wikibase\DataModel\Entity\EntityId; | ||
use Wikibase\DataModel\Entity\StatementListProvidingEntity; | ||
use Wikibase\Lib\Store\EntityRevisionLookup; | ||
use Wikibase\Lib\Store\RevisionedUnresolvedRedirectException; | ||
|
||
/** | ||
* @license GPL-2.0-or-later | ||
*/ | ||
class StatementSubjectRetriever { | ||
|
||
private EntityRevisionLookup $entityRevisionLookup; | ||
|
||
public function __construct( EntityRevisionLookup $entityRevisionLookup ) { | ||
$this->entityRevisionLookup = $entityRevisionLookup; | ||
} | ||
|
||
public function getStatementSubject( EntityId $subjectId ): ?StatementListProvidingEntity { | ||
try { | ||
$entityRevision = $this->entityRevisionLookup->getEntityRevision( $subjectId ); | ||
} catch ( RevisionedUnresolvedRedirectException $e ) { | ||
return null; | ||
} | ||
|
||
if ( !$entityRevision ) { | ||
return null; | ||
} | ||
|
||
$subject = $entityRevision->getEntity(); | ||
if ( !$subject instanceof StatementListProvidingEntity ) { | ||
throw new LogicException( 'Entity is not a ' . StatementListProvidingEntity::class ); | ||
} | ||
|
||
return $subject; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
repo/rest-api/tests/phpunit/Infrastructure/DataAccess/StatementSubjectRetrieverTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
<?php declare( strict_types=1 ); | ||
|
||
namespace Wikibase\Repo\Tests\RestApi\Infrastructure\DataAccess; | ||
|
||
use Generator; | ||
use LogicException; | ||
use PHPUnit\Framework\TestCase; | ||
use Wikibase\DataModel\Entity\EntityDocument; | ||
use Wikibase\DataModel\Entity\EntityId; | ||
use Wikibase\DataModel\Entity\ItemId; | ||
use Wikibase\DataModel\Entity\NumericPropertyId; | ||
use Wikibase\DataModel\Entity\Property; | ||
use Wikibase\DataModel\Entity\StatementListProvidingEntity; | ||
use Wikibase\DataModel\Fixtures\CustomEntityId; | ||
use Wikibase\DataModel\Services\Fixtures\FakeEntityDocument; | ||
use Wikibase\DataModel\Statement\StatementGuid; | ||
use Wikibase\DataModel\Statement\StatementList; | ||
use Wikibase\DataModel\Term\Fingerprint; | ||
use Wikibase\DataModel\Tests\NewItem; | ||
use Wikibase\DataModel\Tests\NewStatement; | ||
use Wikibase\Lib\Store\EntityRevision; | ||
use Wikibase\Lib\Store\EntityRevisionLookup; | ||
use Wikibase\Lib\Store\RevisionedUnresolvedRedirectException; | ||
use Wikibase\Repo\RestApi\Infrastructure\DataAccess\StatementSubjectRetriever; | ||
|
||
/** | ||
* @covers \Wikibase\Repo\RestApi\Infrastructure\DataAccess\StatementSubjectRetriever | ||
* | ||
* @group Wikibase | ||
* | ||
* @license GPL-2.0-or-later | ||
*/ | ||
class StatementSubjectRetrieverTest extends TestCase { | ||
|
||
private EntityRevisionLookup $entityRevisionLookup; | ||
|
||
protected function setUp(): void { | ||
parent::setUp(); | ||
|
||
$this->entityRevisionLookup = $this->createStub( EntityRevisionLookup::class ); | ||
} | ||
|
||
/** | ||
* @dataProvider provideSubjectAndSubjectId | ||
*/ | ||
public function testGetStatementSubject( EntityId $subjectId, EntityDocument $expectedSubject ): void { | ||
$this->entityRevisionLookup = $this->newLookupForIdWithReturnValue( $subjectId, $expectedSubject ); | ||
$subject = $this->newStatementSubjectRetriever()->getStatementSubject( $subjectId ); | ||
|
||
$this->assertEquals( $expectedSubject, $subject ); | ||
} | ||
|
||
/** | ||
* @dataProvider provideSubjectId | ||
*/ | ||
public function testGivenSubjectDoesNotExist_getStatementSubjectReturnsNull( EntityId $subjectId ): void { | ||
$this->entityRevisionLookup = $this->newLookupForIdWithReturnValue( $subjectId, null ); | ||
|
||
$this->assertNull( $this->newStatementSubjectRetriever()->getStatementSubject( $subjectId ) ); | ||
} | ||
|
||
public function testGivenEntityIsNotStatementSubject_getStatementSubjectThrows(): void { | ||
$subjectId = new CustomEntityId( 'A123' ); | ||
$subject = new FakeEntityDocument( $subjectId ); | ||
$this->entityRevisionLookup = $this->newLookupForIdWithReturnValue( $subjectId, $subject ); | ||
|
||
try { | ||
$this->newStatementSubjectRetriever()->getStatementSubject( $subjectId ); | ||
$this->fail( 'Expected exception not thrown' ); | ||
} catch ( LogicException $e ) { | ||
$this->assertEquals( 'Entity is not a ' . StatementListProvidingEntity::class, $e->getMessage() ); | ||
} | ||
} | ||
|
||
public function testGivenItemRedirected_getStatementSubjectReturnsNull(): void { | ||
$itemId = new ItemId( 'Q321' ); | ||
$this->entityRevisionLookup = $this->newLookupForIdWithRedirect( $itemId ); | ||
|
||
$this->assertNull( $this->newStatementSubjectRetriever()->getStatementSubject( $itemId ) ); | ||
} | ||
|
||
public function provideSubjectAndSubjectId(): Generator { | ||
$itemId = new ItemId( 'Q123' ); | ||
$statementId = new StatementGuid( $itemId, 'c48c32c3-42b5-498f-9586-84608b88747c' ); | ||
$statement = NewStatement::forProperty( 'P123' ) | ||
->withValue( 'potato' ) | ||
->withGuid( $statementId ) | ||
->build(); | ||
|
||
yield 'Item' => [ $itemId, NewItem::withId( $itemId )->andStatement( $statement )->build() ]; | ||
|
||
$propertyId = new NumericPropertyId( 'P567' ); | ||
$statementId = new StatementGuid( $propertyId, 'c48c32c3-42b5-498f-9586-84608b88747c' ); | ||
$statement = NewStatement::forProperty( 'P123' ) | ||
->withValue( 'potato' ) | ||
->withGuid( $statementId ) | ||
->build(); | ||
|
||
yield 'Property' => [ | ||
$propertyId, | ||
new Property( $propertyId, new Fingerprint(), 'string', new StatementList( $statement ) ), | ||
]; | ||
} | ||
|
||
public function provideSubjectId(): Generator { | ||
yield 'Item ID' => [ new ItemId( 'Q123' ) ]; | ||
yield 'Property ID' => [ new NumericPropertyId( 'P123' ) ]; | ||
} | ||
|
||
private function newLookupForIdWithRedirect( EntityId $id ): EntityRevisionLookup { | ||
$entityRevisionLookup = $this->createMock( EntityRevisionLookup::class ); | ||
$entityRevisionLookup->expects( $this->once() ) | ||
->method( 'getEntityRevision' ) | ||
->with( $id ) | ||
->willThrowException( $this->createStub( RevisionedUnresolvedRedirectException::class ) ); | ||
|
||
return $entityRevisionLookup; | ||
} | ||
|
||
private function newLookupForIdWithReturnValue( EntityId $id, ?EntityDocument $returnValue ): EntityRevisionLookup { | ||
$entityRevisionLookup = $this->createMock( EntityRevisionLookup::class ); | ||
$entityRevisionLookup->expects( $this->once() ) | ||
->method( 'getEntityRevision' ) | ||
->with( $id ) | ||
->willReturn( $returnValue ? new EntityRevision( $returnValue ) : null ); | ||
|
||
return $entityRevisionLookup; | ||
} | ||
|
||
private function newStatementSubjectRetriever(): StatementSubjectRetriever { | ||
return new StatementSubjectRetriever( $this->entityRevisionLookup ); | ||
} | ||
|
||
} |