-
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.
* This includes adding the route handler, and the e2e test. Bug: T346752 Change-Id: Ib6cd40d0faba3a9788a77511ff3e96a579ca8a79
- Loading branch information
1 parent
90d0870
commit d42cddb
Showing
10 changed files
with
243 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,7 @@ | ||
[ ] | ||
[ | ||
{ | ||
"path": "/wikibase/v0/entities/properties/{property_id}/aliases", | ||
"method": "GET", | ||
"factory": "Wikibase\\Repo\\RestApi\\RouteHandlers\\GetPropertyAliasesRouteHandler::factory" | ||
} | ||
] |
28 changes: 28 additions & 0 deletions
28
repo/rest-api/src/Application/UseCases/GetPropertyAliases/GetPropertyAliases.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,28 @@ | ||
<?php declare( strict_types = 1 ); | ||
|
||
namespace Wikibase\Repo\RestApi\Application\UseCases\GetPropertyAliases; | ||
|
||
use Wikibase\DataModel\Entity\NumericPropertyId; | ||
use Wikibase\Repo\RestApi\Domain\Services\PropertyAliasesRetriever; | ||
|
||
/** | ||
* @license GPL-2.0-or-later | ||
*/ | ||
class GetPropertyAliases { | ||
|
||
private PropertyAliasesRetriever $propertyAliasesRetriever; | ||
|
||
public function __construct( PropertyAliasesRetriever $propertyAliasesRetriever ) { | ||
$this->propertyAliasesRetriever = $propertyAliasesRetriever; | ||
} | ||
|
||
public function execute( GetPropertyAliasesRequest $request ): GetPropertyAliasesResponse { | ||
$propertyId = new NumericPropertyId( $request->getPropertyId() ); | ||
|
||
return new GetPropertyAliasesResponse( | ||
// @phan-suppress-next-line PhanTypeMismatchArgumentNullable | ||
$this->propertyAliasesRetriever->getAliases( $propertyId ) | ||
); | ||
} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
repo/rest-api/src/Application/UseCases/GetPropertyAliases/GetPropertyAliasesRequest.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,18 @@ | ||
<?php declare( strict_types = 1 ); | ||
|
||
namespace Wikibase\Repo\RestApi\Application\UseCases\GetPropertyAliases; | ||
|
||
/** | ||
* @license GPL-2.0-or-later | ||
*/ | ||
class GetPropertyAliasesRequest { | ||
private string $propertyId; | ||
|
||
public function __construct( string $propertyId ) { | ||
$this->propertyId = $propertyId; | ||
} | ||
|
||
public function getPropertyId(): string { | ||
return $this->propertyId; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
repo/rest-api/src/Application/UseCases/GetPropertyAliases/GetPropertyAliasesResponse.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,22 @@ | ||
<?php declare( strict_types = 1 ); | ||
|
||
namespace Wikibase\Repo\RestApi\Application\UseCases\GetPropertyAliases; | ||
|
||
use Wikibase\Repo\RestApi\Domain\ReadModel\Aliases; | ||
|
||
/** | ||
* @license GPL-2.0-or-later | ||
*/ | ||
class GetPropertyAliasesResponse { | ||
|
||
private Aliases $aliases; | ||
|
||
public function __construct( Aliases $aliases ) { | ||
$this->aliases = $aliases; | ||
} | ||
|
||
public function getAliases(): Aliases { | ||
return $this->aliases; | ||
} | ||
|
||
} |
60 changes: 60 additions & 0 deletions
60
repo/rest-api/src/RouteHandlers/GetPropertyAliasesRouteHandler.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,60 @@ | ||
<?php declare( strict_types=1 ); | ||
|
||
namespace Wikibase\Repo\RestApi\RouteHandlers; | ||
|
||
use MediaWiki\Rest\Response; | ||
use MediaWiki\Rest\SimpleHandler; | ||
use MediaWiki\Rest\StringStream; | ||
use Wikibase\Repo\RestApi\Application\Serialization\AliasesSerializer; | ||
use Wikibase\Repo\RestApi\Application\UseCases\GetPropertyAliases\GetPropertyAliases; | ||
use Wikibase\Repo\RestApi\Application\UseCases\GetPropertyAliases\GetPropertyAliasesRequest; | ||
use Wikibase\Repo\RestApi\WbRestApi; | ||
use Wikimedia\ParamValidator\ParamValidator; | ||
|
||
/** | ||
* @license GPL-2.0-or-later | ||
*/ | ||
class GetPropertyAliasesRouteHandler extends SimpleHandler { | ||
|
||
private const PROPERTY_ID_PATH_PARAM = 'property_id'; | ||
|
||
private GetPropertyAliases $useCase; | ||
private AliasesSerializer $aliasesSerializer; | ||
|
||
public function __construct( | ||
GetPropertyAliases $useCase, | ||
AliasesSerializer $aliasesSerializer | ||
) { | ||
$this->useCase = $useCase; | ||
$this->aliasesSerializer = $aliasesSerializer; | ||
} | ||
|
||
public static function factory(): self { | ||
return new self( | ||
WbRestApi::getGetPropertyAliases(), | ||
new AliasesSerializer(), | ||
); | ||
} | ||
|
||
public function run( string $propertyId ): Response { | ||
$useCaseResponse = $this->useCase->execute( new GetPropertyAliasesRequest( $propertyId ) ); | ||
$httpResponse = $this->getResponseFactory()->create(); | ||
$httpResponse->setHeader( 'Content-Type', 'application/json' ); | ||
$httpResponse->setBody( | ||
new StringStream( json_encode( $this->aliasesSerializer->serialize( $useCaseResponse->getAliases() ) ) ) | ||
); | ||
|
||
return $httpResponse; | ||
} | ||
|
||
public function getParamSettings(): array { | ||
return [ | ||
self::PROPERTY_ID_PATH_PARAM => [ | ||
self::PARAM_SOURCE => 'path', | ||
ParamValidator::PARAM_TYPE => 'string', | ||
ParamValidator::PARAM_REQUIRED => true, | ||
], | ||
]; | ||
} | ||
|
||
} |
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
33 changes: 33 additions & 0 deletions
33
repo/rest-api/tests/mocha/api-testing/GetPropertyAliasesTest.js
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,33 @@ | ||
'use strict'; | ||
|
||
const { assert } = require( 'api-testing' ); | ||
const { expect } = require( '../helpers/chaiHelper' ); | ||
const { createEntity } = require( '../helpers/entityHelper' ); | ||
const { newGetPropertyAliasesRequestBuilder } = require( '../helpers/RequestBuilderFactory' ); | ||
|
||
describe( newGetPropertyAliasesRequestBuilder().getRouteDescription(), () => { | ||
|
||
let propertyId; | ||
|
||
before( async () => { | ||
const testProperty = await createEntity( 'property', { | ||
aliases: { | ||
en: [ | ||
{ language: 'en', value: 'en-alias1' }, | ||
{ language: 'en', value: 'en-alias2' } | ||
] | ||
}, | ||
datatype: 'string' | ||
} ); | ||
propertyId = testProperty.entity.id; | ||
} ); | ||
|
||
it( 'can get the aliases of a property', async () => { | ||
const response = await newGetPropertyAliasesRequestBuilder( propertyId ).assertValidRequest() | ||
.makeRequest(); | ||
|
||
expect( response ).to.have.status( 200 ); | ||
assert.deepEqual( response.body, { en: [ 'en-alias1', 'en-alias2' ] } ); | ||
} ); | ||
|
||
} ); |
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
53 changes: 53 additions & 0 deletions
53
...rest-api/tests/phpunit/Application/UseCases/GetPropertyAliases/GetPropertyAliasesTest.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,53 @@ | ||
<?php declare( strict_types = 1 ); | ||
|
||
namespace Wikibase\Repo\Tests\RestApi\Application\UseCases\GetPropertyAliases; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Wikibase\DataModel\Entity\NumericPropertyId; | ||
use Wikibase\Repo\RestApi\Application\UseCases\GetPropertyAliases\GetPropertyAliases; | ||
use Wikibase\Repo\RestApi\Application\UseCases\GetPropertyAliases\GetPropertyAliasesRequest; | ||
use Wikibase\Repo\RestApi\Domain\ReadModel\Aliases; | ||
use Wikibase\Repo\RestApi\Domain\ReadModel\AliasesInLanguage; | ||
use Wikibase\Repo\RestApi\Domain\Services\PropertyAliasesRetriever; | ||
|
||
/** | ||
* @covers \Wikibase\Repo\RestApi\Application\UseCases\GetPropertyAliases\GetPropertyAliases | ||
* | ||
* @group Wikibase | ||
* | ||
* @license GPL-2.0-or-later | ||
*/ | ||
class GetPropertyAliasesTest extends TestCase { | ||
|
||
private PropertyAliasesRetriever $propertyAliasesRetriever; | ||
|
||
protected function setUp(): void { | ||
parent::setUp(); | ||
|
||
$this->propertyAliasesRetriever = $this->createStub( PropertyAliasesRetriever::class ); | ||
} | ||
|
||
public function testSuccess(): void { | ||
$propertyId = 'P123'; | ||
|
||
$aliases = new Aliases( | ||
new AliasesInLanguage( 'en', [ 'en-alias1', 'en-alias2' ] ), | ||
new AliasesInLanguage( 'de', [ 'de-alias1', 'de-alias2' ] ) | ||
); | ||
|
||
$this->propertyAliasesRetriever = $this->createMock( PropertyAliasesRetriever::class ); | ||
$this->propertyAliasesRetriever->expects( $this->once() ) | ||
->method( 'getAliases' ) | ||
->with( new NumericPropertyId( $propertyId ) ) | ||
->willReturn( $aliases ); | ||
|
||
$response = $this->newUseCase()->execute( new GetPropertyAliasesRequest( $propertyId ) ); | ||
|
||
$this->assertSame( $aliases, $response->getAliases() ); | ||
} | ||
|
||
public function newUseCase(): GetPropertyAliases { | ||
return new GetPropertyAliases( $this->propertyAliasesRetriever ); | ||
} | ||
|
||
} |