Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #711: Adding ide:list-mine command. #712

Merged
merged 7 commits into from
Nov 3, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Command/Ide/IdeCreateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class IdeCreateCommand extends IdeCommandBase {
* {inheritdoc}.
*/
protected function configure() {
$this->setDescription('Create a Cloud IDE for development');
$this->setDescription('Create a Cloud IDE');
$this->acceptApplicationUuid();
$this->addOption('label', NULL, InputOption::VALUE_REQUIRED, 'The label for the IDE');
}
Expand Down
5 changes: 3 additions & 2 deletions src/Command/Ide/IdeListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@
*/
class IdeListCommand extends IdeCommandBase {

protected static $defaultName = 'ide:list';
protected static $defaultName = 'ide:list-app';

/**
* {inheritdoc}.
*/
protected function configure() {
$this->setDescription('List available Cloud IDEs');
$this->setDescription('List available Cloud IDEs belonging to a given application');
$this->setAliases(['ide:list']);
$this->acceptApplicationUuid();
}

Expand Down
67 changes: 67 additions & 0 deletions src/Command/Ide/IdeListMineCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace Acquia\Cli\Command\Ide;

use AcquiaCloudApi\Endpoints\Applications;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Helper\TableSeparator;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Class IdeListMineCommand.
*/
class IdeListMineCommand extends IdeCommandBase {

protected static $defaultName = 'ide:list-mine';

/**
* {inheritdoc}.
*/
protected function configure() {
$this->setDescription('List Cloud IDEs belonging to you');
$this->acceptApplicationUuid();
}

/**
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
*
* @return int 0 if everything went fine, or an exit code
* @throws \Exception
*/
protected function execute(InputInterface $input, OutputInterface $output) {
$acquia_cloud_client = $this->cloudApiClientService->getClient();
$account_ides = $acquia_cloud_client->request('get', '/account/ides');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opened an upstream issue to add this endpoint: typhonius/acquia-php-sdk-v2#208

Not a blocker for this PR, but feel free to weigh in there.

$application_resource = new Applications($acquia_cloud_client);

if (count($account_ides)) {
$table = new Table($output);
$table->setStyle('borderless');
$table->setHeaders(['IDEs']);
foreach ($account_ides as $ide) {
$app_url_parts = explode('/', $ide->_links->application->href);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like it should use the same output format as ide:info. Can we refactor this and ide:info to use a common method that accepts an array of IDE resources and pretty-prints a table?

Maybe even use it for ide:list as well, with a verbose parameter.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Filed CLI-530 so your feedback is not lost.

$app_uuid = end($app_url_parts);
$application = $application_resource->get($app_uuid);
$application_url = str_replace('/api', '/a', $application->links->self->href);

$table->addRows([
["<comment>{$ide->label}</comment>"],
["UUID: {$ide->uuid}"],
["Application: <href={$application_url}>{$application->name}</>"],
["Subscription: {$application->subscription->name}"],
["IDE URL: <href={$ide->_links->ide->href}>{$ide->_links->ide->href}</>"],
["Web URL: <href={$ide->_links->web->href}>{$ide->_links->web->href}</>"],
new TableSeparator(),
]);
}
$table->render();
}
else {
$output->writeln('No IDE exists for your account.');
}

return 0;
}

}
86 changes: 86 additions & 0 deletions tests/phpunit/src/Commands/Ide/IdeListCommandMineTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

namespace Acquia\Cli\Tests\Commands\Ide;

use Acquia\Cli\Command\Ide\IdeListCommand;
use Acquia\Cli\Command\Ide\IdeListMineCommand;
use Acquia\Cli\Tests\CommandTestBase;
use Symfony\Component\Console\Command\Command;

/**
* Class IdeListCommandMineTest.
*
* @property \Acquia\Cli\Command\Ide\IdeListMineCommand $command
* @package Acquia\Cli\Tests\Ide
*/
class IdeListCommandMineTest extends CommandTestBase {

/**
* {@inheritdoc}
*/
protected function createCommand(): Command {
return $this->injectCommand(IdeListMineCommand::class);
}

/**
* Tests the 'ide:list-mine' commands.
*
* @throws \Psr\Cache\InvalidArgumentException
*/
public function testIdeListMineCommand(): void {
$applications_response = $this->getMockResponseFromSpec('/applications', 'get', '200');
$ides_response = $this->mockAccountIdeListRequest();
foreach ($ides_response->{'_embedded'}->items as $key => $ide) {
$application_response = $applications_response->{'_embedded'}->items[$key];
$app_url_parts = explode('/', $ide->_links->application->href);
$app_uuid = end($app_url_parts);
$application_response->uuid = $app_uuid;
$this->clientProphecy->request('get', '/applications/' . $app_uuid)
->willReturn($application_response)
->shouldBeCalled();
}

$inputs = [
// Would you like Acquia CLI to search for a Cloud application that matches your local git config?
'n',
// Please select the application.
0,
// Would you like to link the project at ... ?
'y',
];
$this->executeCommand([], $inputs);

// Assert.
$this->prophet->checkPredictions();
$output = $this->getDisplay();
$this->assertStringContainsString('IDE Label 1', $output);
$this->assertStringContainsString('UUID: 9a83c081-ef78-4dbd-8852-11cc3eb248f7', $output);
$this->assertStringContainsString('Application: Sample application 1', $output);
$this->assertStringContainsString('Subscription: Sample subscription', $output);
$this->assertStringContainsString('IDE URL: https://9a83c081-ef78-4dbd-8852-11cc3eb248f7.ide.ahdev.cloud', $output);
$this->assertStringContainsString('Web URL: https://9a83c081-ef78-4dbd-8852-11cc3eb248f7.web.ahdev.cloud', $output);

$this->assertStringContainsString('IDE Label 2', $output);
$this->assertStringContainsString('UUID: 9a83c081-ef78-4dbd-8852-11cc3eb248f7', $output);
$this->assertStringContainsString('Application: Sample application 2', $output);
$this->assertStringContainsString('Subscription: Sample subscription', $output);
$this->assertStringContainsString('IDE URL: https://feea197a-9503-4441-9f49-b4d420b0ecf8.ide.ahdev.cloud', $output);
$this->assertStringContainsString('Web URL: https://feea197a-9503-4441-9f49-b4d420b0ecf8.web.ahdev.cloud', $output);
}

/**
* @return object
* @throws \Psr\Cache\InvalidArgumentException
*/
protected function mockAccountIdeListRequest() {
$response = $this->getMockResponseFromSpec('/account/ides',
'get', '200');
$this->clientProphecy->request('get',
'/account/ides')
->willReturn($response->{'_embedded'}->items)
->shouldBeCalled();

return $response;
}

}