Skip to content

Commit

Permalink
Merge pull request #10 from deliciousbrains/get-site-command
Browse files Browse the repository at this point in the history
Sites get command
  • Loading branch information
A5hleyRich authored Nov 29, 2021
2 parents b4e2da8 + 5976e83 commit e417c14
Show file tree
Hide file tree
Showing 5 changed files with 256 additions and 18 deletions.
115 changes: 115 additions & 0 deletions app/Commands/Sites/GetCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

namespace App\Commands\Sites;

use App\Commands\BaseCommand;
use DeliciousBrains\SpinupWp\Resources\Site;

class GetCommand extends BaseCommand
{
protected $signature = 'sites:get {site_id} {--format=} {--profile=}';

protected $description = 'Get a site';

protected bool $largeOutput = true;

public function action()
{
$site = $this->spinupwp->sites->get($this->argument('site_id'));

if ($this->displayFormat() === 'json') {
return $site;
}
$additionalDomains = '';

if (!empty($site->additional_domains)) {
$additionalDomains = implode(PHP_EOL, array_map(fn ($domain) => $domain['domain'], $site->additional_domains));
}

$data = [
'ID' => $site->id,
'Server ID' => $site->server_id,
'Domain' => $site->domain,
'Additional Domains' => $additionalDomains,
'Site User' => $site->site_user,
'PHP Version' => $site->php_version,
'Public Folder' => $site->public_folder,
'Page Cache' => $site->page_cache['enabled'] ? 'Enabled' : 'Disabled',
'HTTPS' => $site->https['enabled'] ? 'Enabled' : 'Disabled',
];

if ($site->database['table_prefix']) {
$data['Database Table Prefix'] = $site->database['table_prefix'];
}

$data = $this->gitData($site, $data);

$data['WP Core Update'] = $site->wp_core_update ? 'Yes' : 'No';
$data['WP Theme Updates'] = $site->wp_theme_updates;
$data['WP Plugin Updates'] = $site->wp_plugin_updates;

$data = $this->backupsData($site, $data);

$data['Uploads Directory Protection'] = $site->nginx['uploads_directory_protected'] ? 'Enabled' : 'Disabled';
$data['XML-RPC Protection'] = $site->nginx['xmlrpc_protected'] ? 'Enabled' : 'Disabled';
$data['Multisite Rewrite Rules'] = $site->nginx['subdirectory_rewrite_in_place'] ? 'Enabled' : 'Disabled';

$data = $this->basicAuthData($site, $data);

$data['Created At'] = $site->created_at;
$data['Status'] = ucfirst($site->status);

return $data;
}

public function backupsData(Site $site, array $data): array
{
$scheduledBackups = (bool) $site->backups['next_run_time'];

$data['Scheduled Backups'] = $scheduledBackups ? 'Enabled' : 'Disabled';

$data['File Backups'] = ($site->backups['files'] ? 'Enabled' : 'Disabled');
$data['Database Backups'] = ($site->backups['database'] ? 'Enabled' : 'Disabled');

if ($site->backups['files'] || $site->backups['database']) {
$data['Backup Retention Period'] = $site->backups['retention_period'] . ' days';
}

if ($scheduledBackups) {
$data['Next Backup Time'] = $site->backups['next_run_time'];
}

return $data;
}

public function gitData(Site $site, array $data): array
{
$data['Git'] = 'Disabled';

if ($site->git['enabled']) {
$data['Git'] = 'Enabled';
$data['Repository'] = $site->git['repo'];
$data['Branch'] = $site->git['branch'];
$data['Deploy Script'] = $site->git['deploy_script'];
$data['Push-to-deploy'] = $site->git['push_enabled'] ? 'Enabled' : 'Disabled';
}

if ($site->git['enabled'] && $site->git['push_enabled']) {
$data['Deployment URL'] = $site->git['deployment_url'];
}

return $data;
}

public function basicAuthData(Site $site, array $data): array
{
$data['Basic Auth'] = 'Disabled';

if ($site->basic_auth['enabled']) {
$data['Basic Auth'] = 'Enabled';
$data['Basic Auth Username'] = $site->basic_auth['username'];
}

return $data;
}
}
12 changes: 3 additions & 9 deletions tests/Feature/Commands/ServersListCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,14 @@

test('servers json list command', function () use ($response) {
$this->clientMock->shouldReceive('request')->once()->with('GET', 'servers?page=1', [])->andReturn(
new Response(200, [], json_encode([
'data' => $response,
]))
new Response(200, [], listResponseJson($response))
);
$this->artisan('servers:list')->expectsOutput(json_encode($response, JSON_PRETTY_PRINT));
});

test('servers table list command', function () use ($response) {
$this->clientMock->shouldReceive('request')->once()->with('GET', 'servers?page=1', [])->andReturn(
new Response(200, [], json_encode([
'data' => $response,
]))
new Response(200, [], listResponseJson($response))
);
$this->artisan('servers:list --format table')->expectsTable(
['ID', 'Name', 'IP Address', 'Ubuntu', 'Database'],
Expand All @@ -89,9 +85,7 @@

test('empty servers list', function () {
$this->clientMock->shouldReceive('request')->with('GET', 'servers?page=1', [])->andReturn(
new Response(200, [], json_encode([
'data' => [],
]))
new Response(200, [], listResponseJson([]))
);
$this->artisan('servers:list')->expectsOutput('No servers found.');
});
123 changes: 123 additions & 0 deletions tests/Feature/Commands/SitesGetCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php

use GuzzleHttp\Psr7\Response;

$response = [
'id' => 1,
'server_id' => 1,
'domain' => 'hellfish.media',
'additional_domains' => [
[
'domain' => 'www.hellfish.media',
'redirect' => [
'enabled' => true,
],
'created_at' => '2019-08-24T14:15:22Z',
],
],
'site_user' => 'hellfishmedia',
'php_version' => '7.4',
'public_folder' => '/',
'is_wordpress' => true,
'page_cache' => [
'enabled' => true,
],
'https' => [
'enabled' => true,
'certificate_path' => '/etc/nginx/ssl/hellfish.media/certificate-bundle.crt',
'private_key_path' => '/etc/nginx/ssl/hellfish.media/private-key.key',
],
'nginx' => [
'uploads_directory_protected' => true,
'xmlrpc_protected' => true,
'subdirectory_rewrite_in_place' => false,
],
'database' => [
'id' => 1,
'user_id' => 1,
'table_prefix' => 'wp_',
],
'backups' => [
'files' => true,
'database' => true,
'paths_to_exclude' => 'node_modules\\n/files/vendor',
'retention_period' => 30,
'next_run_time' => '2021-01-01T12:00:00.000000Z',
'storage_provider' => [
'id' => 1,
'region' => 'nyc3',
'bucket' => 'hellfish-media',
],
],
'wp_core_update' => true,
'wp_theme_updates' => 0,
'wp_plugin_updates' => 3,
'git' => [
'enabled' => true,
'repo' => '[email protected]:deliciousbrains/spinupwp-composer-site.git',
'branch' => 'main',
'deploy_script' => 'composer install --optimize-autoload --no-dev',
'push_enabled' => true,
'deployment_url' => 'https://api.spinupwp.app/git/jeJLdKrl63/deploy',
],
'basic_auth' => [
'enabled' => true,
'username' => 'hellfish',
],
'created_at' => '2021-01-01T12:00:00.000000Z',
'status' => 'deployed',

];
beforeEach(function () use ($response) {
setTestConfigFile();
});

afterEach(function () {
deleteTestConfigFile();
});

test('sites json get command', function () use ($response) {
$this->clientMock->shouldReceive('request')->with('GET', 'sites/1', [])->andReturn(
new Response(200, [], json_encode(['data' => $response]))
);
$this->artisan('sites:get 1')->expectsOutput(json_encode($response, JSON_PRETTY_PRINT));
});

test('sites table get command', function () use ($response) {
$this->clientMock->shouldReceive('request')->with('GET', 'sites/1', [])->andReturn(
new Response(200, [], json_encode(['data' => $response]))
);
$this->artisan('sites:get 1 --format=table')->expectsTable([], [
['ID', '1'],
['Server ID', '1'],
['Domain', 'hellfish.media'],
['Additional Domains', 'www.hellfish.media'],
['Site User', 'hellfishmedia'],
['PHP Version', '7.4'],
['Public Folder', '/'],
['Page Cache', 'Enabled'],
['HTTPS', 'Enabled'],
['Database Table Prefix', 'wp_'],
['Git', 'Enabled'],
['Repository', '[email protected]:deliciousbrains/spinupwp-composer-site.git'],
['Branch', 'main'],
['Deploy Script', 'composer install --optimize-autoload --no-dev'],
['Push-to-deploy', 'Enabled'],
['Deployment URL', 'https://api.spinupwp.app/git/jeJLdKrl63/deploy'],
['WP Core Update', 'Yes'],
['WP Theme Updates', '0'],
['WP Plugin Updates', '3'],
['Scheduled Backups', 'Enabled'],
['File Backups', 'Enabled'],
['Database Backups', 'Enabled'],
['Backup Retention Period', '30 days'],
['Next Backup Time', '2021-01-01T12:00:00.000000Z'],
['Uploads Directory Protection', 'Enabled'],
['XML-RPC Protection', 'Enabled'],
['Multisite Rewrite Rules', 'Disabled'],
['Basic Auth', 'Enabled'],
['Basic Auth Username', 'hellfish'],
['Created At', '2021-01-01T12:00:00.000000Z'],
['Status', 'Deployed'],
]);
});
12 changes: 3 additions & 9 deletions tests/Feature/Commands/SitesListCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,14 @@

test('sites json list command', function () use ($response) {
$this->clientMock->shouldReceive('request')->with('GET', 'sites?page=1', [])->andReturn(
new Response(200, [], json_encode([
'data' => $response,
]))
new Response(200, [], listResponseJson($response))
);
$this->artisan('sites:list')->expectsOutput(json_encode($response, JSON_PRETTY_PRINT));
});

test('sites table list command', function () use ($response) {
$this->clientMock->shouldReceive('request')->with('GET', 'sites?page=1', [])->andReturn(
new Response(200, [], json_encode([
'data' => $response,
]))
new Response(200, [], listResponseJson($response))
);
$this->artisan('sites:list --format table')->expectsTable(
['ID', 'Server ID', 'Domain', 'Site User', 'PHP', 'Page Cache', 'HTTPS'],
Expand Down Expand Up @@ -86,9 +82,7 @@

test('empty sites list', function () {
$this->clientMock->shouldReceive('request')->with('GET', 'sites?page=1', [])->andReturn(
new Response(200, [], json_encode([
'data' => [],
]))
new Response(200, [], listResponseJson([]))
);
$this->artisan('sites:list')->expectsOutput('No sites found.');
});
12 changes: 12 additions & 0 deletions tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,15 @@ function deleteTestConfigFile($test = '')
}
unlink($configFile);
}

function listResponseJson(array $data): string
{
return json_encode([
'data' => $data,
'pagination' => [
'previous' => null,
'next' => null,
'count' => count($data),
],
]);
}

0 comments on commit e417c14

Please sign in to comment.