Skip to content

Commit

Permalink
CLI-1302: [api:applications:environment-create] databases must be arr…
Browse files Browse the repository at this point in the history
…ay (acquia#1715)

* CLI-1302: [api:applications:environment-create] databases must be array

* fix tests

* fix tests

* fix tests
  • Loading branch information
danepowell authored Mar 29, 2024
1 parent f756e57 commit 72cdee2
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 11 deletions.
5 changes: 4 additions & 1 deletion src/Command/Api/ApiBaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$acquiaCloudClient = $this->cloudApiClientService->getClient();
$this->addQueryParamsToClient($input, $acquiaCloudClient);
$this->addPostParamsToClient($input, $acquiaCloudClient);
// Acquia PHP SDK cannot set the Accept header itself because it would break
// API calls returning octet streams (e.g., db backups). It's safe to use
// here because the API command should always return JSON.
$acquiaCloudClient->addOption('headers', [
'Accept' => 'application/json',
'Accept' => 'application/hal+json, version=2',
]);

try {
Expand Down
10 changes: 3 additions & 7 deletions src/Command/Api/ApiCommandHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,6 @@ private function addApiCommandParameters(array $schema, array $acquiaCloudSpec,
elseif ($parameterSpecification['in'] === 'path') {
$command->addPathParameter($parameterDefinition->getName(), $parameterSpecification);
}
// @todo Remove this! It is a workaround for CLI-769.
elseif ($parameterSpecification['in'] === 'header') {
$command->addPostParameter($parameterDefinition->getName(), $parameterSpecification);
}
}
$usage .= $queryParamUsageSuffix;
$inputDefinition = array_merge($inputDefinition, $queryInputDefinition);
Expand Down Expand Up @@ -484,16 +480,16 @@ private function generateApiListCommands(array $apiCommands, string $commandPref
}

/**
* @param $requestBody
* @param array<mixed> $requestBody
* @return array<mixed>
*/
private function getRequestBodyContent(mixed $requestBody): array {
private function getRequestBodyContent(array $requestBody): array {
$content = $requestBody['content'];
$knownContentTypes = [
'application/hal+json',
'application/json',
'application/x-www-form-urlencoded',
'multipart/form-data',
'application/hal+json',
];
foreach ($knownContentTypes as $contentType) {
if (array_key_exists($contentType, $content)) {
Expand Down
2 changes: 1 addition & 1 deletion src/Command/Auth/AuthAcsfLoginCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ protected function configure(): void {
$this
->addOption('username', 'u', InputOption::VALUE_REQUIRED, "Your Site Factory username")
->addOption('key', 'k', InputOption::VALUE_REQUIRED, "Your Site Factory key")
->addOption('factory-url', 'f', InputOption::VALUE_REQUIRED, "Your Site Factory URL");
->addOption('factory-url', 'f', InputOption::VALUE_REQUIRED, "Your Site Factory URL (including https://)");
}

protected function execute(InputInterface $input, OutputInterface $output): int {
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/src/Commands/Acsf/AcsfApiCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class AcsfApiCommandTest extends AcsfCommandTestBase {

public function setUp(): void {
parent::setUp();
$this->clientProphecy->addOption('headers', ['Accept' => 'application/json']);
$this->clientProphecy->addOption('headers', ['Accept' => 'application/hal+json, version=2']);
putenv('ACQUIA_CLI_USE_CLOUD_API_SPEC_CACHE=1');
}

Expand Down
15 changes: 14 additions & 1 deletion tests/phpunit/src/Commands/Api/ApiCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ class ApiCommandTest extends CommandTestBase {

public function setUp(): void {
parent::setUp();
$this->clientProphecy->addOption('headers', ['Accept' => 'application/json']);
putenv('ACQUIA_CLI_USE_CLOUD_API_SPEC_CACHE=1');
}

Expand Down Expand Up @@ -79,6 +78,7 @@ public function testArgumentsInteractionValidationFormat(): void {
*/
public function testApiCommandErrorResponse(): void {
$invalidUuid = '257a5440-22c3-49d1-894d-29497a1cf3b9';
$this->clientProphecy->addOption('headers', ['Accept' => 'application/hal+json, version=2'])->shouldBeCalled();
$this->command = $this->getApiCommandByName('api:applications:find');
$mockBody = $this->getMockResponseFromSpec($this->command->getPath(), $this->command->getMethod(), '404');
$this->clientProphecy->request('get', '/applications/' . $invalidUuid)->willThrow(new ApiErrorException($mockBody))->shouldBeCalled();
Expand All @@ -104,6 +104,7 @@ public function testApiCommandErrorResponse(): void {
}

public function testApiCommandExecutionForHttpGet(): void {
$this->clientProphecy->addOption('headers', ['Accept' => 'application/hal+json, version=2'])->shouldBeCalled();
$mockBody = $this->getMockResponseFromSpec('/account/ssh-keys', 'get', '200');
$this->clientProphecy->addQuery('limit', '1')->shouldBeCalled();
$this->clientProphecy->request('get', '/account/ssh-keys')->willReturn($mockBody->{'_embedded'}->items)->shouldBeCalled();
Expand Down Expand Up @@ -136,6 +137,7 @@ public function testObjectParam(): void {
}

public function testInferApplicationUuidArgument(): void {
$this->clientProphecy->addOption('headers', ['Accept' => 'application/hal+json, version=2'])->shouldBeCalled();
$applications = $this->mockRequest('getApplications');
$application = $this->mockRequest('getApplicationByUuid', $applications[0]->uuid);
$this->command = $this->getApiCommandByName('api:applications:find');
Expand Down Expand Up @@ -171,6 +173,7 @@ public function providerTestConvertApplicationAliasToUuidArgument(): array {
*/
public function testConvertApplicationAliasToUuidArgument(bool $support): void {
ClearCacheCommand::clearCaches();
$this->clientProphecy->addOption('headers', ['Accept' => 'application/hal+json, version=2'])->shouldBeCalled();
$tamper = function (&$response): void {
unset($response[1]);
};
Expand Down Expand Up @@ -232,7 +235,11 @@ public function testConvertNonUniqueApplicationAliasToUuidArgument(): void {

}

/**
* @serial
*/
public function testConvertApplicationAliasWithRealmToUuidArgument(): void {
$this->clientProphecy->addOption('headers', ['Accept' => 'application/hal+json, version=2'])->shouldBeCalled();
$this->mockApplicationsRequest(1, FALSE);
$this->clientProphecy->addQuery('filter', 'hosting=@devcloud:devcloud2')->shouldBeCalled();
$this->mockApplicationRequest();
Expand All @@ -248,6 +255,7 @@ public function testConvertApplicationAliasWithRealmToUuidArgument(): void {
*/
public function testConvertEnvironmentAliasToUuidArgument(): void {
ClearCacheCommand::clearCaches();
$this->clientProphecy->addOption('headers', ['Accept' => 'application/hal+json, version=2'])->shouldBeCalled();
$applicationsResponse = $this->mockApplicationsRequest(1);
$this->clientProphecy->addQuery('filter', 'hosting=@*:devcloud2')->shouldBeCalled();
$this->mockEnvironmentsRequest($applicationsResponse);
Expand Down Expand Up @@ -291,6 +299,7 @@ public function testConvertInvalidEnvironmentAliasToUuidArgument(): void {
}

public function testApiCommandExecutionForHttpPost(): void {
$this->clientProphecy->addOption('headers', ['Accept' => 'application/hal+json, version=2'])->shouldBeCalled();
$mockRequestArgs = $this->getMockRequestBodyFromSpec('/account/ssh-keys');
$mockResponseBody = $this->getMockResponseFromSpec('/account/ssh-keys', 'post', '202');
foreach ($mockRequestArgs as $name => $value) {
Expand All @@ -308,6 +317,7 @@ public function testApiCommandExecutionForHttpPost(): void {
}

public function testApiCommandExecutionForHttpPut(): void {
$this->clientProphecy->addOption('headers', ['Accept' => 'application/hal+json, version=2'])->shouldBeCalled();
$mockRequestOptions = $this->getMockRequestBodyFromSpec('/environments/{environmentId}', 'put');
$mockRequestOptions['max_input_vars'] = 1001;
$mockResponseBody = $this->getMockEnvironmentResponse('put', '202');
Expand Down Expand Up @@ -416,6 +426,7 @@ public function testApiCommandDefinitionRequestBody(string $commandName, string
}

public function testGetApplicationUuidFromBltYml(): void {
$this->clientProphecy->addOption('headers', ['Accept' => 'application/hal+json, version=2'])->shouldBeCalled();
$mockBody = $this->getMockResponseFromSpec('/applications/{applicationUuid}', 'get', '200');
$this->clientProphecy->request('get', '/applications/' . $mockBody->uuid)->willReturn($mockBody)->shouldBeCalled();
$this->command = $this->getApiCommandByName('api:applications:find');
Expand All @@ -431,6 +442,7 @@ public function testOrganizationMemberDeleteByUserUuid(): void {
$orgId = 'bfafd31a-83a6-4257-b0ec-afdeff83117a';
$memberUuid = '26c4af83-545b-45cb-b165-d537adc9e0b4';

$this->clientProphecy->addOption('headers', ['Accept' => 'application/hal+json, version=2'])->shouldBeCalled();
$this->mockRequest('postOrganizationMemberDelete', [$orgId, $memberUuid], NULL, 'Member removed');

$this->command = $this->getApiCommandByName('api:organizations:member-delete');
Expand All @@ -450,6 +462,7 @@ public function testOrganizationMemberDeleteByUserUuid(): void {
* Test of deletion of the user from organization by user email.
*/
public function testOrganizationMemberDeleteByUserEmail(): void {
$this->clientProphecy->addOption('headers', ['Accept' => 'application/hal+json, version=2'])->shouldBeCalled();
$membersResponse = $this->getMockResponseFromSpec('/organizations/{organizationUuid}/members', 'get', 200);
$orgId = 'bfafd31a-83a6-4257-b0ec-afdeff83117a';
$memberMail = $membersResponse->_embedded->items[0]->mail;
Expand Down

0 comments on commit 72cdee2

Please sign in to comment.