Skip to content

Commit

Permalink
Merge pull request #8 from deliciousbrains/fix-empty-list-bug
Browse files Browse the repository at this point in the history
Fix empty list bug
  • Loading branch information
A5hleyRich authored Nov 22, 2021
2 parents 3571fcc + 7d5075f commit 6253c96
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 12 deletions.
4 changes: 4 additions & 0 deletions app/Commands/BaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ protected function profile(): string

protected function format($resource): void
{
if (empty($resource) || $resource->isEmpty()) {
return;
}

$this->setStyles();

if ($this->displayFormat() === 'table') {
Expand Down
5 changes: 5 additions & 0 deletions app/Commands/Servers/ListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ protected function action()
{
$servers = collect($this->spinupwp->servers->list());

if ($servers->isEmpty()) {
$this->warn('No servers found.');
return $servers;
}

if ($this->displayFormat() === 'json') {
return $servers;
}
Expand Down
5 changes: 5 additions & 0 deletions app/Commands/Sites/ListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ protected function action()
$sites = collect($this->spinupwp->sites->list());
}

if ($sites->isEmpty()) {
$this->warn('No sites found.');
return $sites;
}

if ($this->displayFormat() === 'json') {
return $sites;
}
Expand Down
26 changes: 20 additions & 6 deletions tests/Feature/Commands/ServersListCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,6 @@
];
beforeEach(function () use ($response) {
setTestConfigFile();
$this->clientMock->shouldReceive('request')->with('GET', 'servers?page=1', [])->andReturn(
new Response(200, [], json_encode([
'data' => $response,
]))
);
});

afterEach(function () {
Expand All @@ -57,10 +52,20 @@
});

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,
]))
);
$this->artisan('servers:list')->expectsOutput(json_encode($response, JSON_PRETTY_PRINT));
});

test('servers table list command', function () {
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,
]))
);
$this->artisan('servers:list --format table')->expectsTable(
['ID', 'Name', 'IP Address', 'Ubuntu', 'Database'],
[
Expand All @@ -81,3 +86,12 @@
]
);
});

test('empty servers list', function () {
$this->clientMock->shouldReceive('request')->with('GET', 'servers?page=1', [])->andReturn(
new Response(200, [], json_encode([
'data' => [],
]))
);
$this->artisan('servers:list')->expectsOutput('No servers found.');
});
26 changes: 20 additions & 6 deletions tests/Feature/Commands/SitesListCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@
];
beforeEach(function () use ($response) {
setTestConfigFile();
$this->clientMock->shouldReceive('request')->with('GET', 'sites?page=1', [])->andReturn(
new Response(200, [], json_encode([
'data' => $response,
]))
);
});

afterEach(function () {
Expand All @@ -50,10 +45,20 @@
});

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,
]))
);
$this->artisan('sites:list')->expectsOutput(json_encode($response, JSON_PRETTY_PRINT));
});

test('sites table list command', function () {
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,
]))
);
$this->artisan('sites:list --format table')->expectsTable(
['ID', 'Server ID', 'Domain', 'Site User', 'PHP', 'Page Cache', 'HTTPS'],
[
Expand All @@ -78,3 +83,12 @@
]
);
});

test('empty sites list', function () {
$this->clientMock->shouldReceive('request')->with('GET', 'sites?page=1', [])->andReturn(
new Response(200, [], json_encode([
'data' => [],
]))
);
$this->artisan('sites:list')->expectsOutput('No sites found.');
});

0 comments on commit 6253c96

Please sign in to comment.