diff --git a/src/Connector/Connector.php b/src/Connector/Connector.php index 46e814c7..6fd56d4c 100644 --- a/src/Connector/Connector.php +++ b/src/Connector/Connector.php @@ -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. */ @@ -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'], @@ -52,6 +62,14 @@ public function __construct(array $config) $this->client = new GuzzleClient(); } + /** + * @return string + */ + public function getBaseUri(): string + { + return $this->baseUri; + } + /** * @inheritdoc */ @@ -69,7 +87,7 @@ public function createRequest($verb, $path) return $this->provider->getAuthenticatedRequest( $verb, - self::BASE_URI . $path, + $this->baseUri . $path, $this->accessToken ); } diff --git a/src/Connector/ConnectorInterface.php b/src/Connector/ConnectorInterface.php index 53510eb9..a1c16d8d 100644 --- a/src/Connector/ConnectorInterface.php +++ b/src/Connector/ConnectorInterface.php @@ -52,4 +52,9 @@ public function createRequest($verb, $path); * @return ResponseInterface */ public function sendRequest($verb, $path, $options); + + /** + * @return string + */ + public function getBaseUri(): string; } diff --git a/tests/Connector/ConnectorTest.php b/tests/Connector/ConnectorTest.php index 39d6fb72..85747ef5 100644 --- a/tests/Connector/ConnectorTest.php +++ b/tests/Connector/ConnectorTest.php @@ -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 @@ -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. @@ -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); + } }