diff --git a/app/Commands/BaseCommand.php b/app/Commands/BaseCommand.php index 1109363..cf50242 100644 --- a/app/Commands/BaseCommand.php +++ b/app/Commands/BaseCommand.php @@ -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') { diff --git a/app/Commands/Servers/ListCommand.php b/app/Commands/Servers/ListCommand.php index 349d7dd..a8a4b7b 100644 --- a/app/Commands/Servers/ListCommand.php +++ b/app/Commands/Servers/ListCommand.php @@ -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; } diff --git a/app/Commands/Sites/ListCommand.php b/app/Commands/Sites/ListCommand.php index 99552a9..c957dee 100644 --- a/app/Commands/Sites/ListCommand.php +++ b/app/Commands/Sites/ListCommand.php @@ -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; } diff --git a/tests/Feature/Commands/ServersListCommandTest.php b/tests/Feature/Commands/ServersListCommandTest.php index 6f6ad05..bb38e88 100644 --- a/tests/Feature/Commands/ServersListCommandTest.php +++ b/tests/Feature/Commands/ServersListCommandTest.php @@ -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 () { @@ -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'], [ @@ -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.'); +}); diff --git a/tests/Feature/Commands/SitesListCommandTest.php b/tests/Feature/Commands/SitesListCommandTest.php index 6def05f..c5baddb 100644 --- a/tests/Feature/Commands/SitesListCommandTest.php +++ b/tests/Feature/Commands/SitesListCommandTest.php @@ -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 () { @@ -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'], [ @@ -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.'); +});