Skip to content

Commit

Permalink
Bumps acquia-php-sdk to take advantage of updated Fixtures for testing.
Browse files Browse the repository at this point in the history
  • Loading branch information
typhonius committed Mar 21, 2020
1 parent 7434f86 commit ef31c8e
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 79 deletions.
5 changes: 5 additions & 0 deletions src/AcquiaCli.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
21 changes: 20 additions & 1 deletion src/CloudApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand Down
45 changes: 1 addition & 44 deletions src/Commands/AcquiaCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

/**
Expand Down
36 changes: 16 additions & 20 deletions src/Commands/InsightsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use AcquiaCloudApi\Response\EnvironmentResponse;
use AcquiaCloudApi\Endpoints\Insights;
use Symfony\Component\Console\Helper\Table;
use AcquiaCli\CloudApi;

/**
* Class InsightsCommand
Expand All @@ -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 */
Expand All @@ -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);
Expand Down Expand Up @@ -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));
Expand All @@ -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);
Expand Down
25 changes: 11 additions & 14 deletions src/Commands/NotificationsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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);
Expand All @@ -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();
Expand Down Expand Up @@ -87,15 +84,15 @@ 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();
$tz = $extraConfig['timezone'];
$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);
Expand Down
6 changes: 6 additions & 0 deletions src/Injector/AcquiaCliInjector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);
}
}
}
98 changes: 98 additions & 0 deletions tests/Commands/InsightsCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

namespace AcquiaCli\Tests\Commands;

use AcquiaCli\Tests\AcquiaCliTestCase;

class InsightsCommandTest extends AcquiaCliTestCase
{

/**
* @dataProvider insightsProvider
*/
public function testLogsCommands($command, $expected)
{
$actualResponse = $this->execute($command);
$this->assertSame($expected, $actualResponse);
}

public function insightsProvider()
{

// phpcs:disable Generic.Files.LineLength.TooLong
$insightAlert = <<<ALERT
> 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 = <<<TABLE
+-----------------+----------------+---------+-------------+
| Name | Version | Enabled | Upgradeable |
+-----------------+----------------+---------+-------------+
| Acquia agent | 7.x-3.0-alpha1 | ✓ | ✓ |
| Aggregator | 7.50 | | |
| A Custom Module | | | |
+-----------------+----------------+---------+-------------+
TABLE;

$insightAlerts = <<<ALERTS
+--------------------------------------+------------------------------+--------+----------+---------+
| UUID | Description | Failed | Resolved | Ignored |
+--------------------------------------+------------------------------+--------+----------+---------+
| a47ac10b-58cc-4372-a567-0e02b2c3d470 | PHP errors visible | ✓ | | |
| f938d912-a6a0-11e2-b0d3-12313931d529 | Scheduler module not enabled | ✓ | | |
| f93dbb1f-a6a0-11e2-b0d3-12313931d529 | CSS optimization disabled | ✓ | ✓ | |
+--------------------------------------+------------------------------+--------+----------+---------+
ALERTS;

$insightInfo = <<<INFO
Example local development (local.example.com:8443) Score: 62
> 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
]
];
}
}

0 comments on commit ef31c8e

Please sign in to comment.