I work on laravel forge cli, this repository github. I'm trying to add new functionality to this project, so I've created a new command below:
<?php
namespace App\Commands;
use Laravel\Forge\Resources\User;
class UserShowCommand extends Command
{
/**
* The signature of the command.
*
* @var string
*/
protected $signature = 'user:show';
/**
* The description of the command.
*
* @var string
*/
protected $description = 'Retrieving the current user';
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$this->step('Retrieving the current user');
$user = $this->forge->user();
$this->table([
'ID', 'Email', 'Name', 'Github', 'Gitlab', 'Bitbucket', 'DO', 'AWS', 'Linode', 'Vultr', 'Can create server ?'
], collect([[
$user->id,
$user->email,
$user->name,
$user->connectedToGithub ? 'Yes' : 'No',
$user->connectedToGitlab ? 'Yes' : 'No',
$user->connectedToBitbucket ? 'Yes' : 'No',
$user->connectedToDigitalocean ? 'Yes' : 'No',
$user->connectedToAws ? 'Yes' : 'No',
$user->connectedToLinode ? 'Yes' : 'No',
$user->connectedToVultr ? 'Yes' : 'No',
$user->canCreateServers ? 'Yes' : 'No'
]])->all());
}
}
Here's the return of my command in a terminal:
Then I wrote the test, inspired by what was already available for the others. Here it is:
<?php
use Laravel\Forge\Resources\User;
it('displays the current user', function () {
$this
->client
->shouldReceive('user')
->andReturn(new User([
'id' => 0,
'name' => 'John Do',
'email' => 'john@do.com',
'cardLastFour' => '4444',
'connectedToGithub' => true,
'connectedToGitlab' => true,
'connectedToBitbucket' => true,
'connectedToBitbucketTwo' => true,
'connectedToDigitalocean' => true,
'connectedToLinode' => true,
'connectedToVultr' => true,
'connectedToAws' => true,
'readyForBilling' => true,
'stripeIsActive' => 1,
'stripePlan' => 'yearly-basic-199-trial',
'subscribed' => 1,
'canCreateServers' => true,
]));
$this
->artisan('user:show')
->expectsTable(
[
'ID',
'Email',
'Name',
'Github',
'Gitlab',
'Bitbucket',
'DO',
'AWS',
'Linode',
'Vultr',
'Can create server ?'
],
[
[
'id' => '0',
'email' => 'john@do.com',
'name' => 'John Do',
'connectedToGithub' => 'Yes',
'connectedToGitlab' => 'Yes',
'connectedToBitbucket' => 'Yes',
'connectedToDigitalocean' => 'Yes',
'connectedToLinode' => 'Yes',
'connectedToVultr' => 'Yes',
'connectedToAws' => 'Yes',
'canCreateServers' => 'Yes',
],
],
'compact'
)
->run();
});
But the return is as follows: my header line was not printed.
When I do a :
dd($this->output())
The return is an empty string. So I know that the output is empty in the test context, but outside the test context, everything works.
EDIT:
I found the answer thanks to @Kazz comment. In fact, you should have put 3 spaces in front of the prefix of each header and each value.
My business logic below:
$this
->artisan('user:show')
->expectsTable(
[
' ID',
' Email',
' Name',
' Github',
' Gitlab',
' Bitbucket',
' DO',
' AWS',
' Linode',
' Vultr',
' Can create server ?'
],
[
[
'id' => ' 0',
'email' => ' john@do.com',
'name' => ' John Do',
'connectedToGithub' => ' Yes',
'connectedToGitlab' => ' Yes',
'connectedToBitbucket' => ' Yes',
'connectedToDigitalocean' => ' Yes',
'connectedToLinode' => ' Yes',
'connectedToVultr' => ' Yes',
'connectedToAws' => ' Yes',
'canCreateServers' => ' Yes',
],
],
'compact'
);
the table header does not match, there is different spacing when you run test, remove table style 'compact' to use default instead