Skip to content

Commit

Permalink
Adds code and cron tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
typhonius committed Mar 21, 2020
1 parent 3cf28e5 commit 54c866b
Show file tree
Hide file tree
Showing 14 changed files with 182 additions and 72 deletions.
4 changes: 4 additions & 0 deletions src/AcquiaCli.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ public function getContainer($input, $output, $application, $config, $client)
$parameterInjection->register('AcquiaCloudApi\Endpoints\Servers', new \AcquiaCli\Injector\AcquiaCliInjector);
$parameterInjection->register('AcquiaCloudApi\Endpoints\Domains', new \AcquiaCli\Injector\AcquiaCliInjector);
$parameterInjection->register('AcquiaCloudApi\Endpoints\Code', new \AcquiaCli\Injector\AcquiaCliInjector);
$parameterInjection->register(
'AcquiaCloudApi\Endpoints\DatabaseBackups',
new \AcquiaCli\Injector\AcquiaCliInjector
);

return $container;
}
Expand Down
5 changes: 1 addition & 4 deletions src/Commands/AcquiaCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,15 +292,12 @@ protected function waitForNotification($response)
*/
protected function backupAndMoveDbs($uuid, $environmentFrom, $environmentTo, $dbName = null)
{
<<<<<<< HEAD
$dbAdapter = new Databases($this->getCloudApi());
=======

if (null !== $dbName) {
$this->cloudapi->addQuery('filter', "name=${dbName}");
}

$dbAdapter = new Databases($this->cloudapi);
>>>>>>> Various updates to the code and more tests.
$databases = $dbAdapter->getAll($uuid);
$this->cloudapi->clearQuery();

Expand Down
5 changes: 5 additions & 0 deletions src/Commands/CodeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ public function codeDeploy(
$environmentFrom,
$environmentTo
) {
$environmentFrom = $this->cloudapiService->getEnvironment($uuid, $environmentFrom);
$environmentTo = $this->cloudapiService->getEnvironment($uuid, $environmentTo);
if (!$this->confirm(
sprintf(
'Are you sure you want to deploy code from %s to %s?',
Expand Down Expand Up @@ -102,6 +105,8 @@ public function codeDeploy(
*/
public function codeSwitch(Code $codeAdapter, $uuid, $environment, $branch)
{
$environment = $this->cloudapiService->getEnvironment($uuid, $environment);
if (!$this->confirm(
sprintf(
'Are you sure you want to switch code on the %s environment to branch: %s?',
Expand Down
42 changes: 21 additions & 21 deletions src/Commands/CronCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,6 @@
class CronCommand extends AcquiaCommand
{

protected $cronAdapter;

public function __construct()
{
parent::__construct();

$this->cronAdapter = new Crons($this->getCloudApi());
}

/**
* Shows all cron tasks associated with an environment.
*
Expand All @@ -31,10 +22,11 @@ public function __construct()
*
* @command cron:list
*/
public function crons($uuid, $environment)
public function crons(Crons $cronAdapter, $uuid, $environment)
{
$environment = $this->cloudapiService->getEnvironment($uuid, $environment);

$crons = $this->cronAdapter->getAll($environment->uuid);
$crons = $cronAdapter->getAll($environment->uuid);

$output = $this->output();
$table = new Table($output);
Expand Down Expand Up @@ -69,9 +61,11 @@ public function crons($uuid, $environment)
* @command cron:create
* @aliases cron:add
*/
public function cronAdd($uuid, $environment, $commandString, $frequency, $label)
public function cronAdd(Crons $cronAdapter, $uuid, $environment, $commandString, $frequency, $label)
{
$response = $this->cronAdapter->create($environment->uuid, $commandString, $frequency, $label);
$environment = $this->cloudapiService->getEnvironment($uuid, $environment);
$this->say(sprintf('Adding new cron task on %s environment', $environment->name));
$response = $cronAdapter->create($environment->uuid, $commandString, $frequency, $label);
$this->waitForNotification($response);
}

Expand All @@ -85,11 +79,12 @@ public function cronAdd($uuid, $environment, $commandString, $frequency, $label)
* @command cron:delete
* @aliases cron:remove
*/
public function cronDelete($uuid, $environment, $cronId)
public function cronDelete(Crons $cronAdapter, $uuid, $environment, $cronId)
{
$environment = $this->cloudapiService->getEnvironment($uuid, $environment);
if ($this->confirm("Are you sure you want to delete the cron task?")) {
$this->say(sprintf('Deleting cron task %s from %s', $cronId, $environment->label));
$response = $this->cronAdapter->delete($environment->uuid, $cronId);
$response = $cronAdapter->delete($environment->uuid, $cronId);
$this->waitForNotification($response);
}
}
Expand All @@ -103,9 +98,11 @@ public function cronDelete($uuid, $environment, $cronId)
*
* @command cron:enable
*/
public function cronEnable($uuid, $environment, $cronId)
public function cronEnable(Crons $cronAdapter, $uuid, $environment, $cronId)
{
$response = $this->cronAdapter->enable($environment->uuid, $cronId);
$environment = $this->cloudapiService->getEnvironment($uuid, $environment);
$this->say(sprintf('Enabling cron task %s on %s environment', $cronId, $environment->name));
$response = $cronAdapter->enable($environment->uuid, $cronId);
$this->waitForNotification($response);
}

Expand All @@ -118,10 +115,12 @@ public function cronEnable($uuid, $environment, $cronId)
*
* @command cron:disable
*/
public function cronDisable($uuid, $environment, $cronId)
public function cronDisable(Crons $cronAdapter, $uuid, $environment, $cronId)
{
$environment = $this->cloudapiService->getEnvironment($uuid, $environment);
if ($this->confirm("Are you sure you want to disable the cron task?")) {
$response = $this->cronAdapter->disable($environment->uuid, $cronId);
$this->say(sprintf('Disabling cron task %s on %s environment', $cronId, $environment->name));
$response = $cronAdapter->disable($environment->uuid, $cronId);
$this->waitForNotification($response);
}
}
Expand All @@ -135,9 +134,10 @@ public function cronDisable($uuid, $environment, $cronId)
*
* @command cron:info
*/
public function cronInfo($uuid, $environment, $cronId)
public function cronInfo(Crons $cronAdapter, $uuid, $environment, $cronId)
{
$cron = $this->cronAdapter->get($environment->uuid, $cronId);
$environment = $this->cloudapiService->getEnvironment($uuid, $environment);
$cron = $cronAdapter->get($environment->uuid, $cronId);

$enabled = $cron->flags->enabled ? '' : ' ';
$system = $cron->flags->system ? '' : ' ';
Expand Down
30 changes: 13 additions & 17 deletions src/Commands/DbBackupCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,10 @@
class DbBackupCommand extends AcquiaCommand
{

protected $databaseBackupsAdapter;

private $downloadProgress;

private $lastStep;

public function __construct()
{
parent::__construct();

$this->databaseBackupsAdapter = new DatabaseBackups($this->getCloudApi());
}

/**
* Backs up all DBs in an environment.
*
Expand All @@ -56,7 +47,7 @@ public function dbBackup($uuid, $environment)
* @command database:backup:list
* @aliases db:backup:list
*/
public function dbBackupList($uuid, $environment, $dbName = null)
public function dbBackupList(DatabaseBackups $databaseBackupsAdapter, $uuid, $environment, $dbName = null)
{
$environment = $this->cloudapiService->getEnvironment($uuid, $environment);

Expand All @@ -71,7 +62,7 @@ public function dbBackupList($uuid, $environment, $dbName = null)
$table->setHeaders(['ID', 'Type', 'Timestamp']);

foreach ($databases as $database) {
$backups = $this->databaseBackupsAdapter->getAll($environment->uuid, $database->name);
$backups = $databaseBackupsAdapter->getAll($environment->uuid, $database->name);
$table
->addRows(
[
Expand Down Expand Up @@ -105,15 +96,15 @@ public function dbBackupList($uuid, $environment, $dbName = null)
* @command database:backup:restore
* @aliases db:backup:restore
*/
public function dbBackupRestore($uuid, $environment, $dbName, $backupId)
public function dbBackupRestore(DatabaseBackups $databaseBackupsAdapter, $uuid, $environment, $dbName, $backupId)
{
$environment = $this->cloudapiService->getEnvironment($uuid, $environment);

if ($this->confirm(
sprintf('Are you sure you want to restore backup id %s to %s?', $backupId, $environment->label)
)) {
$this->say(sprintf('Restoring backup %s to %s on %s', $backupId, $dbName, $environment->label));
$response = $this->databaseBackupsAdapter->restore($environment->uuid, $dbName, $backupId);
$response = $databaseBackupsAdapter->restore($environment->uuid, $dbName, $backupId);
$this->waitForNotification($response);
}
}
Expand Down Expand Up @@ -158,12 +149,17 @@ public function dbBackupLink($uuid, $environment, $dbName, $backupId)
* @option $backup Select which backup to download by backup ID. If omitted, the latest will be downloaded.
* @option $path Select a path to download the backup to. If omitted, the system temp directory will be used.
*/
public function dbBackupDownload($uuid, $environment, $dbName, $opts = ['backup' => null, 'path' => null])
{
public function dbBackupDownload(
DatabaseBackups $databaseBackupsAdapter,
$uuid,
$environment,
$dbName,
$opts = ['backup' => null, 'path' => null]
) {
if (!$opts['backup']) {
$this->cloudapi->addQuery('sort', '-created');
$this->cloudapi->addQuery('limit', 1);
$backup = $this->databaseBackupsAdapter->getAll($environment->uuid, $dbName);
$backup = $databaseBackupsAdapter->getAll($environment->uuid, $dbName);
$this->cloudapi->clearQuery();
if (empty($backup)) {
throw new \Exception('Unable to find a database backup to download.');
Expand Down Expand Up @@ -204,7 +200,7 @@ public function dbBackupDownload($uuid, $environment, $dbName, $opts = ['backup'
$this->lastStep = $downloadedBytes;
});

$this->databaseBackupsAdapter->download($environment->uuid, $dbName, $backupId);
$databaseBackupsAdapter->download($environment->uuid, $dbName, $backupId);
$this->downloadProgress->setMessage(sprintf('Database backup downloaded to %s', $location));
$this->downloadProgress->finish();

Expand Down
6 changes: 6 additions & 0 deletions src/Injector/AcquiaCliInjector.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use AcquiaCloudApi\Endpoints\Servers;
use AcquiaCloudApi\Endpoints\Domains;
use AcquiaCloudApi\Endpoints\Code;
use AcquiaCloudApi\Endpoints\DatabaseBackups;
use AcquiaCloudApi\Endpoints\Crons;

class AcquiaCliInjector implements ParameterInjector
{
Expand Down Expand Up @@ -45,6 +47,10 @@ public function get(CommandData $commandData, $interfaceName)
return new Domains($this->client);
case 'AcquiaCloudApi\Endpoints\Code':
return new Code($this->client);
case 'AcquiaCloudApi\Endpoints\DatabaseBackups':
return new DatabaseBackups($this->client);
case 'AcquiaCloudApi\Endpoints\Crons':
return new Crons($this->client);
}

return null;
Expand Down
6 changes: 1 addition & 5 deletions tests/Commands/ApplicationCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class ApplicationCommandTest extends AcquiaCliTestCase
/**
* @dataProvider applicationProvider
*/
public function testApplicationCommands($command, $fixture, $expected)
public function testApplicationCommands($command, $expected)
{
$actualResponse = $this->execute($command);
$this->assertSame($expected, $actualResponse);
Expand Down Expand Up @@ -38,23 +38,19 @@ public function applicationProvider()
return [
[
['application:list'],
'Applications/getAllApplications.json',
$getAllApplications . PHP_EOL

],
[
['application:tags', 'uuid'],
'Applications/getAllTags.json',
$getTags . PHP_EOL
],
[
['application:tag:create', 'uuid', 'name', 'color'],
'Applications/createTag.json',
'> Creating application tag name:color' . PHP_EOL
],
[
['application:tag:delete', 'uuid', 'name'],
'Applications/deleteTag.json',
'> Deleting application tag name' . PHP_EOL
]
];
Expand Down
56 changes: 56 additions & 0 deletions tests/Commands/CodeCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace AcquiaCli\Tests\Commands;

use AcquiaCli\Tests\AcquiaCliTestCase;

class CodeCommandTest extends AcquiaCliTestCase
{

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

public function codeProvider()
{

$codeList = <<<LIST
+-------------------+-----+
| Name | Tag |
+-------------------+-----+
| master | |
| feature-branch | |
| tags/2014-09-03 | ✓ |
| tags/2014-09-03.0 | ✓ |
+-------------------+-----+
LIST;

$codeDeploy = '> Backing up DB (database1) on Mock Env
> Backing up DB (database2) on Mock Env
> Deploying code from the Mock Env environment to the Mock Env environment';

$codeSwitch = '> Backing up DB (database1) on Mock Env
> Backing up DB (database2) on Mock Env
> Switching Mock Env enviroment to branch branch';

return [
[
['code:deploy', 'uuid', 'environmentFrom', 'environmentTo'],
$codeDeploy . PHP_EOL
],
[
['code:list', 'uuid'],
$codeList . PHP_EOL
],
[
['code:switch', 'uuid', 'environment', 'branch'],
$codeSwitch . PHP_EOL
]
];
}
}
Loading

0 comments on commit 54c866b

Please sign in to comment.