Skip to content

Commit

Permalink
III-2068: Add method to get service consumer by api key
Browse files Browse the repository at this point in the history
  • Loading branch information
bertramakers committed Jun 28, 2017
1 parent e0e20e8 commit 8f73c44
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 26 deletions.
42 changes: 32 additions & 10 deletions CultureFeed/CultureFeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -1890,17 +1890,20 @@ public function updateServiceConsumer(CultureFeed_Consumer $consumer) {
*/
public function getServiceConsumer($consumerKey) {
$result = $this->oauth_client->consumerGetAsXML('serviceconsumer/' . $consumerKey);
return $this->parseServiceConsumerFromXmlString($result);
}

try {
$xml = new CultureFeed_SimpleXMLElement($result);
}
catch (Exception $e) {
throw new CultureFeed_ParseException($result);
}

$element = $xml->xpath('/consumer');

return $this->parseServiceConsumer($element[0]);
/**
* Get An existing service consumer by api key.
*
* @param string $apiKey
* @return \CultureFeed_Consumer
* @throws \CultureFeed_ParseException
*/
public function getServiceConsumerByApiKey($apiKey)
{
$result = $this->oauth_client->consumerGetAsXML('serviceconsumer/apikey/' . $apiKey);
return $this->parseServiceConsumerFromXmlString($result);
}

/**
Expand Down Expand Up @@ -1966,6 +1969,25 @@ public function getClient() {
return $this->oauth_client;
}

/**
* @param string $xml
* @return CultureFeed_Consumer
* @throws CultureFeed_ParseException
*/
protected function parseServiceConsumerFromXmlString($xml)
{
try {
$xml = new CultureFeed_SimpleXMLElement($xml);
}
catch (Exception $e) {
throw new CultureFeed_ParseException($xml);
}

$element = $xml->xpath('/consumer');

return $this->parseServiceConsumer($element[0]);
}

/**
* Initializes a CultureFeed_Consumer object from its XML representation.
*
Expand Down
4 changes: 4 additions & 0 deletions CultureFeed/CultureFeed/ICultureFeedDecoratorBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,10 @@ public function getServiceConsumer($consumerKey) {
return $this->realCultureFeed->getServiceConsumer($consumerKey);
}

public function getServiceConsumerByApiKey($apiKey) {
return $this->realCultureFeed->getServiceConsumerByApiKey($apiKey);
}

public function getServiceConsumers($start = 0, $max = null)
{
return $this->realCultureFeed->getServiceConsumers($start, $max);
Expand Down
2 changes: 2 additions & 0 deletions CultureFeed/ICultureFeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ public function updateServiceConsumer(CultureFeed_Consumer $consumer);

public function getServiceConsumer($consumerKey);

public function getServiceConsumerByApiKey($apiKey);

/**
* @return CultureFeed_Uitpas
*/
Expand Down
91 changes: 75 additions & 16 deletions CultureFeed/test/CultureFeedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,66 +24,125 @@ public function setUp() {
$this->cultureFeed = new CultureFeed($this->oauthClient);
}

public function testGetConsumerWithoutSapi3Properties()
{
/**
* @dataProvider getConsumerMethodDataProvider
*
* @param string $method
* @param string $identifier
* @param string $expectedPath
*/
public function testGetConsumerWithoutSapi3Properties(
$method,
$identifier,
$expectedPath
) {
$xml = file_get_contents(__DIR__ . '/data/consumer_without_sapi3_properties.xml');

$this->oauthClient->expects($this->once())
->method('consumerGetAsXml')
->with('serviceconsumer/5720d908-03cf-46da-8f5a-c43db621df5c')
->with($expectedPath)
->willReturn($xml);

$consumer = $this->cultureFeed->getServiceConsumer('5720d908-03cf-46da-8f5a-c43db621df5c');
$consumer = $this->cultureFeed->{$method}($identifier);

$this->assertNull($consumer->apiKeySapi3);
$this->assertNull($consumer->searchPrefixSapi3);
}

public function testGetConsumerWithEmptySapi3Properties()
{
/**
* @dataProvider getConsumerMethodDataProvider
*
* @param string $method
* @param string $identifier
* @param string $expectedPath
*/
public function testGetConsumerWithEmptySapi3Properties(
$method,
$identifier,
$expectedPath
) {
$xml = file_get_contents(__DIR__ . '/data/consumer_with_empty_sapi3_properties.xml');

$this->oauthClient->expects($this->once())
->method('consumerGetAsXml')
->with('serviceconsumer/5720d908-03cf-46da-8f5a-c43db621df5c')
->with($expectedPath)
->willReturn($xml);

$consumer = $this->cultureFeed->getServiceConsumer('5720d908-03cf-46da-8f5a-c43db621df5c');
$consumer = $this->cultureFeed->{$method}($identifier);

$this->assertEquals('', $consumer->apiKeySapi3);
$this->assertEquals('', $consumer->searchPrefixSapi3);
}

public function testGetConsumerWithApiKeySapi3()
{
/**
* @dataProvider getConsumerMethodDataProvider
*
* @param string $method
* @param string $identifier
* @param string $expectedPath
*/
public function testGetConsumerWithApiKeySapi3(
$method,
$identifier,
$expectedPath
) {
$xml = file_get_contents(__DIR__ . '/data/consumer_with_api_key_sapi3.xml');

$this->oauthClient->expects($this->once())
->method('consumerGetAsXml')
->with('serviceconsumer/5720d908-03cf-46da-8f5a-c43db621df5c')
->with($expectedPath)
->willReturn($xml);

$consumer = $this->cultureFeed->getServiceConsumer('5720d908-03cf-46da-8f5a-c43db621df5c');
$consumer = $this->cultureFeed->{$method}($identifier);

$this->assertEquals('c2436351-f314-4b83-916d-2e0a37502358', $consumer->apiKeySapi3);
$this->assertEquals('', $consumer->searchPrefixSapi3);
}

public function testGetConsumerWithSearchPrefixSapi3()
{
/**
* @dataProvider getConsumerMethodDataProvider
*
* @param string $method
* @param string $identifier
* @param string $expectedPath
*/
public function testGetConsumerWithSearchPrefixSapi3(
$method,
$identifier,
$expectedPath
) {
$xml = file_get_contents(__DIR__ . '/data/consumer_with_search_prefix_sapi3.xml');

$this->oauthClient->expects($this->once())
->method('consumerGetAsXml')
->with('serviceconsumer/5720d908-03cf-46da-8f5a-c43db621df5c')
->with($expectedPath)
->willReturn($xml);

$consumer = $this->cultureFeed->getServiceConsumer('5720d908-03cf-46da-8f5a-c43db621df5c');
$consumer = $this->cultureFeed->{$method}($identifier);

$this->assertEquals('c2436351-f314-4b83-916d-2e0a37502358', $consumer->apiKeySapi3);
$this->assertEquals('labels:foo AND regions:gem-leuven', $consumer->searchPrefixSapi3);
}

/**
* @return array
*/
public function getConsumerMethodDataProvider()
{
return [
[
'method' => 'getServiceConsumer',
'identifier' => '5720d908-03cf-46da-8f5a-c43db621df5c',
'expectedPath' => 'serviceconsumer/5720d908-03cf-46da-8f5a-c43db621df5c',
],
[
'method' => 'getServiceConsumerByApiKey',
'identifier' => 'c2436351-f314-4b83-916d-2e0a37502358',
'expectedPath' => 'serviceconsumer/apikey/c2436351-f314-4b83-916d-2e0a37502358',
],
];
}

/**
* Test the handling of a succesfull user light call.
*/
Expand Down

0 comments on commit 8f73c44

Please sign in to comment.