Skip to content

Commit

Permalink
Enable default base uri to be overridden (#137)
Browse files Browse the repository at this point in the history
* Update Connector.php
* Add test.
* Add type hints.
  • Loading branch information
grasmash authored Mar 28, 2021
1 parent a861c0a commit a48bc1b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 13 deletions.
22 changes: 20 additions & 2 deletions src/Connector/Connector.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
class Connector implements ConnectorInterface
{

/**
* @var string The base URI for Acquia Cloud API.
*/
protected $baseUri;

/**
* @var GenericProvider The OAuth 2.0 provider to use in communication.
*/
Expand All @@ -37,8 +42,13 @@ class Connector implements ConnectorInterface
/**
* @inheritdoc
*/
public function __construct(array $config)
public function __construct(array $config, string $base_uri = null)
{
$this->baseUri = ConnectorInterface::BASE_URI;
if ($base_uri) {
$this->baseUri = $base_uri;
}

$this->provider = new GenericProvider(
[
'clientId' => $config['key'],
Expand All @@ -52,6 +62,14 @@ public function __construct(array $config)
$this->client = new GuzzleClient();
}

/**
* @return string
*/
public function getBaseUri(): string
{
return $this->baseUri;
}

/**
* @inheritdoc
*/
Expand All @@ -69,7 +87,7 @@ public function createRequest($verb, $path)

return $this->provider->getAuthenticatedRequest(
$verb,
self::BASE_URI . $path,
$this->baseUri . $path,
$this->accessToken
);
}
Expand Down
5 changes: 5 additions & 0 deletions src/Connector/ConnectorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,9 @@ public function createRequest($verb, $path);
* @return ResponseInterface
*/
public function sendRequest($verb, $path, $options);

/**
* @return string
*/
public function getBaseUri(): string;
}
42 changes: 31 additions & 11 deletions tests/Connector/ConnectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,8 @@ class ConnectorTest extends CloudApiTestCase

public function setUp(): void
{
$config = [
'key' => 'key',
'secret' => 'secret'
];

$this->connector = new Connector($config);

// Clear the cache to make sure we get fresh results during testing.
$directory = sprintf('%s%s%s', Path::getHomeDirectory(), \DIRECTORY_SEPARATOR, '.acquia-php-sdk-v2');
$this->cache = new FilesystemAdapter('cache', 0, $directory);
$this->cache->deleteItem('cloudapi-token');
$this->createConnector();
$this->clearCache();
}

public function tearDown(): void
Expand Down Expand Up @@ -83,6 +74,17 @@ public function testConnector(): void
$this->assertEquals('secret', $clientSecret->getValue($provider));
}

public function testConnectorBaseUri(): void
{
$base_uri = 'https://test-cloud.acquia.com/api';
$this->createConnector($base_uri);
$this->assertEquals(
$this->connector->getBaseUri(),
$base_uri
);
}


public function testGetAuthenticatedRequest(): void
{
// Override the provider property set in the constructor.
Expand Down Expand Up @@ -197,4 +199,22 @@ public function testGuzzleRequest(): void
$this->assertEquals(200, $return->getStatusCode());
$this->assertEquals('OK', $return->getReasonPhrase());
}

protected function clearCache(): void
{
// Clear the cache to make sure we get fresh results during testing.
$directory = sprintf('%s%s%s', Path::getHomeDirectory(), \DIRECTORY_SEPARATOR, '.acquia-php-sdk-v2');
$this->cache = new FilesystemAdapter('cache', 0, $directory);
$this->cache->deleteItem('cloudapi-token');
}

protected function createConnector(string $base_url = null): void
{
$config = [
'key' => 'key',
'secret' => 'secret'
];

$this->connector = new Connector($config, $base_url);
}
}

0 comments on commit a48bc1b

Please sign in to comment.