From ef31c8e8cba87b89781ecbbbe84a6e06d7a9eba1 Mon Sep 17 00:00:00 2001 From: Adam Malone Date: Mon, 9 Mar 2020 10:49:27 +1100 Subject: [PATCH] Bumps acquia-php-sdk to take advantage of updated Fixtures for testing. --- src/AcquiaCli.php | 5 ++ src/CloudApi.php | 21 +++++- src/Commands/AcquiaCommand.php | 45 +----------- src/Commands/InsightsCommand.php | 36 +++++----- src/Commands/NotificationsCommand.php | 25 +++---- src/Injector/AcquiaCliInjector.php | 6 ++ tests/Commands/InsightsCommandTest.php | 98 ++++++++++++++++++++++++++ 7 files changed, 157 insertions(+), 79 deletions(-) create mode 100644 tests/Commands/InsightsCommandTest.php diff --git a/src/AcquiaCli.php b/src/AcquiaCli.php index e52b84c..d198739 100644 --- a/src/AcquiaCli.php +++ b/src/AcquiaCli.php @@ -154,6 +154,11 @@ public function injectParameters($container) $parameterInjection->register('AcquiaCloudApi\Endpoints\Teams', new \AcquiaCli\Injector\AcquiaCliInjector); $parameterInjection->register('AcquiaCloudApi\Endpoints\Variables', new \AcquiaCli\Injector\AcquiaCliInjector); $parameterInjection->register('AcquiaCloudApi\Endpoints\Logs', new \AcquiaCli\Injector\AcquiaCliInjector); + $parameterInjection->register( + 'AcquiaCloudApi\Endpoints\Notifications', + new \AcquiaCli\Injector\AcquiaCliInjector + ); + $parameterInjection->register('AcquiaCloudApi\Endpoints\Insights', new \AcquiaCli\Injector\AcquiaCliInjector); } /** diff --git a/src/CloudApi.php b/src/CloudApi.php index 4ccd50e..1ffde7d 100644 --- a/src/CloudApi.php +++ b/src/CloudApi.php @@ -25,10 +25,11 @@ class CloudApi protected $acquia; - public function __construct(Config $config) + public function __construct(Config $config, Client $client = null) { $this->extraConfig = $config->get('extraconfig'); $this->acquia = $config->get('acquia'); + $this->client = $client; } public function createClient() @@ -50,6 +51,24 @@ public function createClient() return $client; } + /** + * @param string $name + * @return mixed + * @throws \Exception + */ + public function getApplicationUuid($name) + { + $app = new Applications($this->client); + $applications = $app->getAll(); + + foreach ($applications as $application) { + if ($name === $application->hosting->id) { + return $application->uuid; + } + } + throw new \Exception('Unable to find UUID for application'); + } + /** * @param string $uuid * @param string $environment diff --git a/src/Commands/AcquiaCommand.php b/src/Commands/AcquiaCommand.php index 14253ab..c94a12e 100644 --- a/src/Commands/AcquiaCommand.php +++ b/src/Commands/AcquiaCommand.php @@ -142,53 +142,10 @@ public function validateUuidHook(CommandData $commandData) $uuid = $commandData->input()->getOption('realm') . ':' . $uuid; } } - $uuid = $this->getUuidFromHostingName($uuid); + $uuid = $this->cloudapiService->getApplicationUuid($uuid); $commandData->input()->setArgument('uuid', $uuid); } } - // Convert Organization name to UUID. - if ($commandData->input()->hasArgument('organization')) { - $organizationName = $commandData->input()->getArgument('organization'); - $organization = $this->getOrganizationFromOrganizationName($organizationName); - $commandData->input()->setArgument('organization', $organization); - } - } - - /** - * @param string $organizationName - * @return OrganizationResponse - * @throws Exception - */ - protected function getOrganizationFromOrganizationName($organizationName) - { - $org = new Organizations($this->getCloudApi()); - $organizations = $org->getAll(); - - foreach ($organizations as $organization) { - if ($organizationName === $organization->name) { - return $organization; - } - } - - throw new Exception('Unable to find ID for environment'); - } - - /** - * @param string $name - * @return mixed - * @throws Exception - */ - protected function getUuidFromHostingName($name) - { - $app = $this->getApplications(); - $applications = $app->getAll(); - - foreach ($applications as $application) { - if ($name === $application->hosting->id) { - return $application->uuid; - } - } - throw new Exception('Unable to find UUID for application'); } /** diff --git a/src/Commands/InsightsCommand.php b/src/Commands/InsightsCommand.php index f59013f..4dc8e84 100644 --- a/src/Commands/InsightsCommand.php +++ b/src/Commands/InsightsCommand.php @@ -8,6 +8,7 @@ use AcquiaCloudApi\Response\EnvironmentResponse; use AcquiaCloudApi\Endpoints\Insights; use Symfony\Component\Console\Helper\Table; +use AcquiaCli\CloudApi; /** * Class InsightsCommand @@ -16,30 +17,22 @@ class InsightsCommand extends AcquiaCommand { - protected $insightsAdapter; - - public function __construct() - { - parent::__construct(); - - $this->insightsAdapter = new Insights($this->getCloudApi()); - } - /** * Shows Insights information for specified applications. * * @param string $uuid - * @param EnvironmentResponse $environment + * @param string $environment * * @command insights:info */ - public function insightsInfo($uuid, $environment = null) + public function insightsInfo(CloudApi $cloudapi, Insights $insightsAdapter, $uuid, $environment = null) { if (null === $environment) { - $insights = $this->insightsAdapter->getAll($uuid); + $insights = $insightsAdapter->getAll($uuid); } else { - $insights = $this->insightsAdapter->getEnvironment($environment->uuid); + $environment = $cloudapi->getEnvironment($uuid, $environment); + $insights = $insightsAdapter->getEnvironment($environment->uuid); } foreach ($insights as $insight) { /** @var InsightResponse $insight */ @@ -56,9 +49,9 @@ public function insightsInfo($uuid, $environment = null) * * @command insights:alerts:list */ - public function insightsAlertsList($siteId, $options = ['failed' => null]) + public function insightsAlertsList(Insights $insightsAdapter, $siteId, $options = ['failed' => null]) { - $alerts = $this->insightsAdapter->getAllAlerts($siteId); + $alerts = $insightsAdapter->getAllAlerts($siteId); $output = $this->output(); $table = new Table($output); @@ -97,9 +90,9 @@ public function insightsAlertsList($siteId, $options = ['failed' => null]) * * @command insights:alerts:get */ - public function insightsAlertsGet($siteId, $alertUuid) + public function insightsAlertsGet(Insights $insightsAdapter, $siteId, $alertUuid) { - $alert = $this->insightsAdapter->getAlert($siteId, $alertUuid); + $alert = $insightsAdapter->getAlert($siteId, $alertUuid); $this->say(sprintf('UUID: %s', $alert->uuid)); $this->say(sprintf('Name: %s', $alert->name)); @@ -115,9 +108,12 @@ public function insightsAlertsGet($siteId, $alertUuid) * * @command insights:modules */ - public function insightsModules($siteId, $options = ['enabled' => null, 'upgradeable' => null]) - { - $modules = $this->insightsAdapter->getModules($siteId); + public function insightsModules( + Insights $insightsAdapter, + $siteId, + $options = ['enabled' => null, 'upgradeable' => null] + ) { + $modules = $insightsAdapter->getModules($siteId); $output = $this->output(); $table = new Table($output); diff --git a/src/Commands/NotificationsCommand.php b/src/Commands/NotificationsCommand.php index a4020c2..d8d59df 100644 --- a/src/Commands/NotificationsCommand.php +++ b/src/Commands/NotificationsCommand.php @@ -14,15 +14,6 @@ class NotificationsCommand extends AcquiaCommand { - protected $notificationsAdapter; - - public function __construct() - { - parent::__construct(); - - $this->notificationsAdapter = new Notifications($this->getCloudApi()); - } - /** * Gets all notifications associated with a site. * @@ -35,8 +26,14 @@ public function __construct() * @command notification:list * @alias n:l */ - public function notificationList(Client $client, $uuid, $limit = 50, $filter = null, $sort = '~created_at') - { + public function notificationList( + Client $client, + Notifications $notificationsAdapter, + $uuid, + $limit = 50, + $filter = null, + $sort = '~created_at' + ) { // Allows for limits and sort criteria. $sort = str_replace('~', '-', $sort); @@ -46,7 +43,7 @@ public function notificationList(Client $client, $uuid, $limit = 50, $filter = n $client->addQuery('filter', "name=${filter}"); } - $notifications = $this->notificationsAdapter->getAll($uuid); + $notifications = $notificationsAdapter->getAll($uuid); $client->clearQuery(); $output = $this->output(); @@ -87,7 +84,7 @@ public function notificationList(Client $client, $uuid, $limit = 50, $filter = n * @alias n:i * @throws \Exception */ - public function notificationInfo($uuid, $notificationUuid) + public function notificationInfo(Notifications $notificationsAdapter, $uuid, $notificationUuid) { $extraConfig = $this->cloudapiService->getExtraConfig(); @@ -95,7 +92,7 @@ public function notificationInfo($uuid, $notificationUuid) $format = $extraConfig['format']; $timezone = new \DateTimeZone($tz); - $notification = $this->notificationsAdapter->get($notificationUuid); + $notification = $notificationsAdapter->get($notificationUuid); $createdDate = new \DateTime($notification->created_at); $createdDate->setTimezone($timezone); diff --git a/src/Injector/AcquiaCliInjector.php b/src/Injector/AcquiaCliInjector.php index 9020a47..8984619 100644 --- a/src/Injector/AcquiaCliInjector.php +++ b/src/Injector/AcquiaCliInjector.php @@ -19,6 +19,8 @@ use AcquiaCloudApi\Endpoints\Teams; use AcquiaCloudApi\Endpoints\Variables; use AcquiaCloudApi\Endpoints\Logs; +use AcquiaCloudApi\Endpoints\Notifications; +use AcquiaCloudApi\Endpoints\Insights; class AcquiaCliInjector implements ParameterInjector { @@ -71,6 +73,10 @@ public function get(CommandData $commandData, $interfaceName) return new Variables($this->client); case 'AcquiaCloudApi\Endpoints\Logs': return new Logs($this->client); + case 'AcquiaCloudApi\Endpoints\Notifications': + return new Notifications($this->client); + case 'AcquiaCloudApi\Endpoints\Insights': + return new Insights($this->client); } } } diff --git a/tests/Commands/InsightsCommandTest.php b/tests/Commands/InsightsCommandTest.php new file mode 100644 index 0000000..557e73b --- /dev/null +++ b/tests/Commands/InsightsCommandTest.php @@ -0,0 +1,98 @@ +execute($command); + $this->assertSame($expected, $actualResponse); + } + + public function insightsProvider() + { + + // phpcs:disable Generic.Files.LineLength.TooLong + $insightAlert = << UUID: a47ac10b-58cc-4372-a567-0e02b2c3d470 +> Name: PHP errors visible +> Message: Your website is configured to display PHP error messages to users. These error messages can reveal sensitive information about your website and its server to site visitors. +ALERT; + + // phpcs:enable + + $insightModules = << Site ID: 50227ff0-2a53-11e9-b210-d663bd873d93 +> Status: ok ++----------------+------+------+---------+-------+----+ +| Type | Pass | Fail | Ignored | Total | % | ++----------------+------+------+---------+-------+----+ +| Best Practices | 5 | 1 | 0 | 6 | 83 | +| Performance | 9 | 10 | 0 | 19 | 47 | +| Security | 10 | 10 | 0 | 20 | 50 | ++----------------+------+------+---------+-------+----+ + + Test Site: prod (test.example.com) Score: 62 + +> Site ID: 50227ff0-2a53-11e9-b210-d663bd873d93 +> Status: ok ++----------------+------+------+---------+-------+----+ +| Type | Pass | Fail | Ignored | Total | % | ++----------------+------+------+---------+-------+----+ +| Best Practices | 5 | 1 | 0 | 6 | 83 | +| Performance | 10 | 9 | 0 | 19 | 53 | +| Security | 11 | 9 | 0 | 20 | 55 | ++----------------+------+------+---------+-------+----+ +INFO; + + + return [ + [ + ['insights:alerts:get', 'siteId', 'alertUuid'], + $insightAlert . PHP_EOL + ], + [ + ['insights:alerts:list', 'siteId'], + $insightAlerts . PHP_EOL + ], + [ + ['insights:info', 'uuid', 'environment'], + $insightInfo . PHP_EOL + ], + [ + ['insights:modules', 'siteId'], + $insightModules . PHP_EOL + ] + ]; + } +}