Skip to content

Commit

Permalink
CLI-1226: Support objects as API params (#1646)
Browse files Browse the repository at this point in the history
* CLI-1226: Support objects as API params

* cleanup

* remove mixed case

* cleanup
  • Loading branch information
danepowell authored Dec 11, 2023
1 parent 95d00d4 commit 835f643
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 40 deletions.
20 changes: 0 additions & 20 deletions src/Command/Acsf/AcsfApiBaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Acquia\Cli\Command\Api\ApiBaseCommand;
use Acquia\Cli\Exception\AcquiaCliException;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputInterface;

#[AsCommand(name: 'acsf:base')]
class AcsfApiBaseCommand extends ApiBaseCommand {
Expand All @@ -19,23 +18,4 @@ protected function checkAuthentication(): void {
}
}

/**
* @todo Remove this method when CLI-791 is resolved.
*/
protected function getRequestPath(InputInterface $input): string {
$path = $this->path;

$arguments = $input->getArguments();
// The command itself is the first argument. Remove it.
array_shift($arguments);
foreach ($arguments as $key => $value) {
$token = '%' . $key;
if (str_contains($path, $token)) {
return str_replace($token, $value, $path);
}
}

return parent::getRequestPath($input);
}

}
19 changes: 9 additions & 10 deletions src/Command/Api/ApiBaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,11 @@ public function getMethod(): string {
return $this->method;
}

public function addPostParameter(mixed $paramName, mixed $value): void {
public function addPostParameter(string $paramName, mixed $value): void {
$this->postParams[$paramName] = $value;
}

public function addQueryParameter(mixed $paramName, mixed $value): void {
public function addQueryParameter(string $paramName, mixed $value): void {
$this->queryParams[$paramName] = $value;
}

Expand All @@ -177,7 +177,7 @@ private function getParamFromInput(InputInterface $input, string $paramName): ar
return NULL;
}

private function castParamType(array $paramSpec, array|string|bool|int $value): array|bool|int|string {
private function castParamType(array $paramSpec, array|string|bool|int $value): array|bool|int|string|object {
$oneOf = $this->getParamTypeOneOf($paramSpec);
if (isset($oneOf)) {
$types = [];
Expand All @@ -187,8 +187,7 @@ private function castParamType(array $paramSpec, array|string|bool|int $value):
}
$types[] = $type['type'];
}
if ((in_array('integer', $types, TRUE) || in_array('int', $types, TRUE))
&& ctype_digit($value)) {
if (in_array('integer', $types, TRUE) && ctype_digit($value)) {
return $this->doCastParamType('integer', $value);
}
}
Expand All @@ -208,13 +207,13 @@ private function castParamType(array $paramSpec, array|string|bool|int $value):
return $this->doCastParamType($type, $value);
}

private function doCastParamType(string $type, mixed $value): array|bool|int|string {
private function doCastParamType(string $type, mixed $value): array|bool|int|string|object {
return match ($type) {
'int', 'integer' => (int) $value,
'bool', 'boolean' => $this->castBool($value),
'integer' => (int) $value,
'boolean' => $this->castBool($value),
'array' => is_string($value) ? explode(',', $value) : (array) $value,
'string' => (string) $value,
'mixed' => $value,
'object' => json_decode($value, FALSE, 512, JSON_THROW_ON_ERROR),
};
}

Expand Down Expand Up @@ -401,7 +400,7 @@ private function getParamTypeOneOf(array $paramSpec): ?array {
return $oneOf;
}

private function castParamToArray(mixed $paramSpec, array|string $originalValue): string|array|bool|int {
private function castParamToArray(array $paramSpec, array|string $originalValue): string|array|bool|int {
if (array_key_exists('items', $paramSpec) && array_key_exists('type', $paramSpec['items'])) {
if (!is_array($originalValue)) {
$originalValue = $this->doCastParamType('array', $originalValue);
Expand Down
28 changes: 18 additions & 10 deletions tests/phpunit/src/Commands/Api/ApiCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,20 @@ public function testArgumentsInteractionValidation(): void {
'AH_SOMETHING',
]);
}
catch (MissingInputException $exception) {

catch (MissingInputException) {
}
$output = $this->getDisplay();
$this->assertStringContainsString('It must match the pattern', $output);
}

public function testArgumentsInteractionValdationFormat(): void {
public function testArgumentsInteractionValidationFormat(): void {
$this->command = $this->getApiCommandByName('api:notifications:find');
try {
$this->executeCommand([], [
'test',
]);
}
catch (MissingInputException $exception) {

catch (MissingInputException) {
}
$output = $this->getDisplay();
$this->assertStringContainsString('This is not a valid UUID', $output);
Expand Down Expand Up @@ -122,10 +120,20 @@ public function testApiCommandExecutionForHttpGet(): void {
$this->assertArrayHasKey('uuid', $contents[0]);
}

public function testObjectParam(): void {
$this->mockRequest('putEnvironmentCloudActions', '24-a47ac10b-58cc-4372-a567-0e02b2c3d470');
$this->command = $this->getApiCommandByName('api:environments:cloud-actions-update');
$this->executeCommand([
'cloud-actions' => '{"fb4aa87a-8be2-42c6-bdf0-ef9d09a3de70":true}',
'environmentId' => '24-a47ac10b-58cc-4372-a567-0e02b2c3d470',
]);
$output = $this->getDisplay();
$this->assertStringContainsString('Cloud Actions have been updated.', $output);
}

public function testInferApplicationUuidArgument(): void {
$mockBody = $this->getMockResponseFromSpec('/applications/{applicationUuid}', 'get', '200');
$this->clientProphecy->request('get', '/applications')->willReturn([$mockBody])->shouldBeCalled();
$this->clientProphecy->request('get', '/applications/' . $mockBody->uuid)->willReturn($mockBody)->shouldBeCalled();
$applications = $this->mockRequest('getApplications');
$application = $this->mockRequest('getApplicationByUuid', $applications[0]->uuid);
$this->command = $this->getApiCommandByName('api:applications:find');
$this->executeCommand([], [
// Would you like Acquia CLI to search for a Cloud application that matches your local git config?
Expand All @@ -140,7 +148,7 @@ public function testInferApplicationUuidArgument(): void {
$this->prophet->checkPredictions();
$output = $this->getDisplay();
$this->assertStringContainsString('Inferring Cloud Application UUID for this command since none was provided...', $output);
$this->assertStringContainsString('Set application uuid to ' . $mockBody->uuid, $output);
$this->assertStringContainsString('Set application uuid to ' . $application->uuid, $output);
$this->assertEquals(0, $this->getStatusCode());
}

Expand Down Expand Up @@ -398,7 +406,7 @@ public function providerTestApiCommandDefinitionRequestBody(): array {
* @param $method
* @param $usage
*/
public function testApiCommandDefinitionRequestBody(mixed $commandName, mixed $method, mixed $usage): void {
public function testApiCommandDefinitionRequestBody(string $commandName, string $method, string $usage): void {
$this->command = $this->getApiCommandByName($commandName);
$resource = $this->getResourceFromSpec($this->command->getPath(), $method);
foreach ($resource['requestBody']['content']['application/json']['example'] as $propKey => $value) {
Expand Down

0 comments on commit 835f643

Please sign in to comment.