Skip to content

Commit

Permalink
Fixes #120: Keeping case intact. (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
grasmash authored Jun 9, 2020
1 parent 69de1e1 commit a7e8837
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 58 deletions.
73 changes: 36 additions & 37 deletions src/Command/Api/ApiCommandHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,13 @@ protected function addArgumentExampleToUsageForGetEndpoint($param_definition, st

/**
* @param $param_definition
* @param $param_name
* @param string $usage
*
* @return string
*/
protected function addOptionExampleToUsageForGetEndpoint($param_definition, $param_name, string $usage): string {
protected function addOptionExampleToUsageForGetEndpoint($param_definition, string $usage): string {
if (array_key_exists('example', $param_definition)) {
$usage .= '--' . strtolower($param_name) . '="' . $param_definition['example'] . '" ';
$usage .= '--' . $param_definition['name'] . '="' . $param_definition['example'] . '" ';
}

return $usage;
Expand All @@ -209,10 +208,10 @@ protected function addApiCommandParameters($schema, $acquia_cloud_spec, ApiComma
foreach ($query_input_definition as $parameter_definition) {
$token = '{' . $parameter_definition->getName() . '}';
if (strpos($command->getPath(), $token) !== FALSE) {
$command->addPathParameter(strtolower($parameter_definition->getName()));
$command->addPathParameter($parameter_definition->getName());
}
else {
$command->addQueryParameter(strtolower($parameter_definition->getName()));
$command->addQueryParameter($parameter_definition->getName());
}
}
$usage .= $query_param_usage_suffix;
Expand All @@ -224,7 +223,7 @@ protected function addApiCommandParameters($schema, $acquia_cloud_spec, ApiComma
[$body_input_definition, $request_body_param_usage_suffix] = $this->addApiCommandParametersForRequestBody($schema, $acquia_cloud_spec);
/** @var \Symfony\Component\Console\Input\InputOption|InputArgument $parameter_definition */
foreach ($body_input_definition as $parameter_definition) {
$command->addPostParameter(strtolower($parameter_definition->getName()));
$command->addPostParameter($parameter_definition->getName());
}
$usage .= $request_body_param_usage_suffix;
$input_definition += $body_input_definition;
Expand Down Expand Up @@ -255,32 +254,31 @@ protected function addApiCommandParametersForRequestBody($schema, $acquia_cloud_
// If this is a reference to the top level schema, go grab the referenced component.
if (array_key_exists('$ref', $request_body_schema)) {
$parts = explode('/', $request_body_schema['$ref']);
$param_name = end($parts);
$request_body_schema = $this->getParameterSchemaFromSpec($param_name, $acquia_cloud_spec);
$param_key = end($parts);
$request_body_schema = $this->getParameterSchemaFromSpec($param_key, $acquia_cloud_spec);
}

if (!array_key_exists('properties', $request_body_schema)) {
return [];
}
foreach ($request_body_schema['properties'] as $param_name => $param_definition) {
$key = strtolower($param_name);
$is_required = array_key_exists('required', $request_body_schema) && in_array($key, $request_body_schema['required'], TRUE);
foreach ($request_body_schema['properties'] as $prop_key => $param_definition) {
$is_required = array_key_exists('required', $request_body_schema) && in_array($prop_key, $request_body_schema['required'], TRUE);
if ($is_required) {
$input_definition[] = new InputArgument(
$key,
$prop_key,
$param_definition['type'] === 'array' ? InputArgument::IS_ARRAY | InputArgument::REQUIRED : InputArgument::REQUIRED,
$param_definition['description']
);
$usage = $this->addPostArgumentUsageToExample($schema["requestBody"], $key, $param_definition, 'argument', $usage);
$usage = $this->addPostArgumentUsageToExample($schema['requestBody'], $prop_key, $param_definition, 'argument', $usage);
}
else {
$input_definition[] = new InputOption(
$key,
$prop_key,
NULL,
$param_definition['type'] === 'array' ? InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED : InputOption::VALUE_REQUIRED,
$param_definition['description']
);
$usage = $this->addPostArgumentUsageToExample($schema["requestBody"], $key, $param_definition, 'option', $usage);
$usage = $this->addPostArgumentUsageToExample($schema["requestBody"], $prop_key, $param_definition, 'option', $usage);
// @todo Add validator for $param['enum'] values?
}
}
Expand All @@ -298,14 +296,14 @@ protected function addApiCommandParametersForRequestBody($schema, $acquia_cloud_

/**
* @param $request_body
* @param $param_name
* @param $prop_key
* @param $param_definition
* @param $type
* @param $usage
*
* @return string
*/
protected function addPostArgumentUsageToExample($request_body, $param_name, $param_definition, $type, $usage): string {
protected function addPostArgumentUsageToExample($request_body, $prop_key, $param_definition, $type, $usage): string {
if (!array_key_exists('application/json', $request_body['content'])) {
$request_body_schema = $request_body['content']['application/x-www-form-urlencoded'];
}
Expand All @@ -315,35 +313,35 @@ protected function addPostArgumentUsageToExample($request_body, $param_name, $pa

if (array_key_exists('example', $request_body_schema)) {
$example = $request_body['content']['application/json']['example'];
$prefix = $type === 'argument' ? '' : strtolower("--{$param_name}=");
if (array_key_exists($param_name, $example)) {
$prefix = $type === 'argument' ? '' : "--{$prop_key}=";
if (array_key_exists($prop_key, $example)) {
switch ($param_definition['type']) {
case 'object':
$usage .= $prefix . '"' . json_encode($example[$param_name]) . '"" ';
$usage .= $prefix . '"' . json_encode($example[$prop_key]) . '"" ';
break;

case 'array':
$is_multidimensional = count($example[$param_name]) !== count($example[$param_name], COUNT_RECURSIVE);
$is_multidimensional = count($example[$prop_key]) !== count($example[$prop_key], COUNT_RECURSIVE);
if (!$is_multidimensional) {
$value = implode(',', $example[$param_name]);
$value = implode(',', $example[$prop_key]);
}
else {
// @todo Pretty sure prevents the user from using the arguments.
// Probably a bug. How can we allow users to specify a multidimensional array as an
// argument?
$value = json_encode($example[$param_name]);
$value = json_encode($example[$prop_key]);
}
$usage .= $prefix . "\"$value\" ";
break;

case 'string':
case 'boolean':
case 'integer':
if (is_array($example[$param_name])) {
$value = reset($example[$param_name]);
if (is_array($example[$prop_key])) {
$value = reset($example[$prop_key]);
}
else {
$value = $example[$param_name];
$value = $example[$prop_key];
}
$usage .= $prefix . "\"{$value}\" ";
break;
Expand All @@ -364,48 +362,49 @@ protected function addApiCommandParametersForPathAndQuery($schema, $acquia_cloud
$input_definition = [];
foreach ($schema['parameters'] as $parameter) {
$parts = explode('/', $parameter['$ref']);
$param_name = end($parts);
$param_definition = $this->getParameterDefinitionFromSpec($param_name, $acquia_cloud_spec);
$param_key = end($parts);
$param_definition = $this->getParameterDefinitionFromSpec($param_key, $acquia_cloud_spec);
$required = array_key_exists('required', $param_definition) && $param_definition['required'];
if ($required) {
$input_definition[] = new InputArgument(
strtolower($param_definition['name']),
$param_definition['name'],
InputArgument::REQUIRED,
$param_definition['description']
);
$usage = $this->addArgumentExampleToUsageForGetEndpoint($param_definition, $usage);
}
else {
$input_definition[] = new InputOption(
strtolower($param_definition['name']),
$param_definition['name'],
NULL,
InputOption::VALUE_REQUIRED,
$param_definition['description']
);
$usage = $this->addOptionExampleToUsageForGetEndpoint($param_definition, $param_name, $usage);
$usage = $this->addOptionExampleToUsageForGetEndpoint($param_definition, $usage);
}
}

return [$input_definition, $usage];
}

/**
* @param $param_name
* @param $param_key
* @param $acquia_cloud_spec
*
* @return mixed
*/
protected function getParameterDefinitionFromSpec($param_name, $acquia_cloud_spec) {
return $acquia_cloud_spec['components']['parameters'][$param_name];
protected function getParameterDefinitionFromSpec($param_key, $acquia_cloud_spec) {
return $acquia_cloud_spec['components']['parameters'][$param_key];
}

/**
* @param $param_name
* @param $param_key
* @param $acquia_cloud_spec
*
* @return mixed
*/
protected function getParameterSchemaFromSpec($param_name, $acquia_cloud_spec) {
return $acquia_cloud_spec['components']['schemas'][$param_name];
protected function getParameterSchemaFromSpec($param_key, $acquia_cloud_spec) {
return $acquia_cloud_spec['components']['schemas'][$param_key];
}

/**
Expand Down
37 changes: 17 additions & 20 deletions tests/phpunit/src/Commands/Api/ApiCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ class ApiCommandTest extends CommandTestBase {

public function setUp($output = NULL): void {
parent::setUp($output);
putenv('ACQUIA_CLI_USE_COMMAND_CACHE=0');
putenv('ACQUIA_CLI_USE_CLOUD_API_SPEC_CACHE=1');
}

Expand Down Expand Up @@ -74,17 +73,16 @@ public function testApiCommandExecutionForHttpPost(): void {
public function providerTestApiCommandDefinitionParameters(): array {
$api_accounts_ssh_keys_list_usage = 'api:accounts:ssh-keys-list --from="-7d" --to="-1d" --sort="field1,-field2" --limit="10" --offset="10" ';
return [
['0', '0', 'api:accounts:ssh-keys-list', 'get', $api_accounts_ssh_keys_list_usage],
['1', '0', 'api:accounts:ssh-keys-list', 'get', $api_accounts_ssh_keys_list_usage],
['1', '1', 'api:accounts:ssh-keys-list', 'get', $api_accounts_ssh_keys_list_usage],
['1', '1', 'api:environments:domains-clear-varnish', 'post', '12-d314739e-296f-11e9-b210-d663bd873d93 --domains="domain1.example.com,domain2.example.com"'],
['0', 'api:accounts:ssh-keys-list', 'get', $api_accounts_ssh_keys_list_usage],
['1', 'api:accounts:ssh-keys-list', 'get', $api_accounts_ssh_keys_list_usage],
['1', 'api:accounts:ssh-keys-list', 'get', $api_accounts_ssh_keys_list_usage],
['1', 'api:environments:domains-clear-varnish', 'post', '12-d314739e-296f-11e9-b210-d663bd873d93 --domains="domain1.example.com,domain2.example.com"'],
];
}

/**
* @dataProvider providerTestApiCommandDefinitionParameters
*
* @param $use_command_cache
* @param $use_spec_cache
* @param $command_name
* @param $method
Expand All @@ -93,9 +91,8 @@ public function providerTestApiCommandDefinitionParameters(): array {
* @throws \Psr\Cache\InvalidArgumentException
* @group noCache
*/
public function testApiCommandDefinitionParameters($use_spec_cache, $use_command_cache, $command_name, $method, $usage): void {
public function testApiCommandDefinitionParameters($use_spec_cache, $command_name, $method, $usage): void {
putenv('ACQUIA_CLI_USE_CLOUD_API_SPEC_CACHE=' . $use_spec_cache);
putenv('ACQUIA_CLI_USE_COMMAND_CACHE=' . $use_command_cache);

$this->command = $this->getApiCommandByName($command_name);
$resource = $this->getResourceFromSpec($this->command->getPath(), $method);
Expand All @@ -105,20 +102,21 @@ public function testApiCommandDefinitionParameters($use_spec_cache, $use_command
$this->assertEquals($expected_command_name, $this->command->getName());

foreach ($resource['parameters'] as $parameter) {
$param_name = strtolower(str_replace('#/components/parameters/', '', $parameter['$ref']));
$param_key = str_replace('#/components/parameters/', '', $parameter['$ref']);
$param = $this->getCloudApiSpec()['components']['parameters'][$param_key];
$this->assertTrue(
$this->command->getDefinition()->hasOption($param_name) ||
$this->command->getDefinition()->hasArgument($param_name),
"Command $expected_command_name does not have expected argument or option $param_name"
$this->command->getDefinition()->hasOption($param['name']) ||
$this->command->getDefinition()->hasArgument($param['name']),
"Command $expected_command_name does not have expected argument or option {$param['name']}"
);
}
$this->assertStringContainsString($usage, $this->command->getUsages()[0]);
}

public function providerTestApiCommandDefinitionRequestBody(): array {
return [
['1', 'api:accounts:ssh-key-create', 'post', 'api:accounts:ssh-key-create "mykey" "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAklOUpkDHrfHY17SbrmTIpNLTGK9Tjom/BWDSUGPl+nafzlHDTYW7hdI4yZ5ew18JH4JW9jbhUFrviQzM7xlELEVf4h9lFX5QVkbPppSwg0cda3Pbv7kOdJ/MTyBlWXFCR+HAo3FXRitBqxiX1nKhXpHAZsMciLq8V6RjsNAQwdsdMFvSlVK/7XAt3FaoJoAsncM1Q9x5+3V0Ww68/eIFmb1zuUFljQJKprrX88XypNDvjYNby6vw/Pb0rwert/EnmZ+AW4OZPnTPI89ZPmVMLuayrD2cE86Z/il8b+gw3r3+1nKatmIkjn2so1d01QraTlMqVSsbxNrRFi9wrf+M7Q== [email protected]" '],
['1', 'api:environments:domains-clear-varnish', 'post', 'api:environments:domains-clear-varnish 12-d314739e-296f-11e9-b210-d663bd873d93 --domains="domain1.example.com,domain2.example.com" '],
['api:accounts:ssh-key-create', 'post', 'api:accounts:ssh-key-create "mykey" "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAklOUpkDHrfHY17SbrmTIpNLTGK9Tjom/BWDSUGPl+nafzlHDTYW7hdI4yZ5ew18JH4JW9jbhUFrviQzM7xlELEVf4h9lFX5QVkbPppSwg0cda3Pbv7kOdJ/MTyBlWXFCR+HAo3FXRitBqxiX1nKhXpHAZsMciLq8V6RjsNAQwdsdMFvSlVK/7XAt3FaoJoAsncM1Q9x5+3V0Ww68/eIFmb1zuUFljQJKprrX88XypNDvjYNby6vw/Pb0rwert/EnmZ+AW4OZPnTPI89ZPmVMLuayrD2cE86Z/il8b+gw3r3+1nKatmIkjn2so1d01QraTlMqVSsbxNrRFi9wrf+M7Q== [email protected]" '],
['api:environments:domains-clear-varnish', 'post', 'api:environments:domains-clear-varnish 12-d314739e-296f-11e9-b210-d663bd873d93 --domains="domain1.example.com,domain2.example.com" '],
];
}

Expand All @@ -131,14 +129,13 @@ public function providerTestApiCommandDefinitionRequestBody(): array {
*
* @throws \Psr\Cache\InvalidArgumentException
*/
public function testApiCommandDefinitionRequestBody($use_command_cache, $command_name, $method, $usage): void {
public function testApiCommandDefinitionRequestBody($command_name, $method, $usage): void {
$this->command = $this->getApiCommandByName($command_name);
$resource = $this->getResourceFromSpec($this->command->getPath(), $method);
foreach ($resource['requestBody']['content']['application/json']['example'] as $key => $value) {
$param_name = strtolower($key);
$this->assertTrue($this->command->getDefinition()->hasArgument($param_name) || $this->command->getDefinition()
->hasOption($param_name),
"Command {$this->command->getName()} does not have expected argument or option $param_name");
foreach ($resource['requestBody']['content']['application/json']['example'] as $prop_key => $value) {
$this->assertTrue($this->command->getDefinition()->hasArgument($prop_key) || $this->command->getDefinition()
->hasOption($prop_key),
"Command {$this->command->getName()} does not have expected argument or option $prop_key");
}
$this->assertStringContainsString($usage, $this->command->getUsages()[0]);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/src/TestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ public function getMockRequestBodyFromSpec($path) {
* @return mixed
* @throws \Psr\Cache\InvalidArgumentException
*/
private function getCloudApiSpec() {
protected function getCloudApiSpec() {
// We cache the yaml file because it's 20k+ lines and takes FOREVER
// to parse when xDebug is enabled.
$acquia_cloud_spec_file = __DIR__ . '/../../../assets/acquia-spec.yaml';
Expand Down

0 comments on commit a7e8837

Please sign in to comment.