The error message suggests that the $this->info() method is trying to call the writeln() method on a null object. This is likely because the $this->output property is not being set properly in the test environment.
To fix this, you can manually set the $this->output property in your test case before calling the handle() method. Here's an example:
use Illuminate\Support\Facades\Artisan;
use Tests\TestCase;
class YourCommandTest extends TestCase
{
public function testHandle()
{
$this->app->instance('Illuminate\Console\OutputStyle', $this->getMockOutput());
Artisan::call('your-command');
// Assert that the command ran successfully
$this->assertEquals(0, Artisan::output());
}
protected function getMockOutput()
{
$output = $this->getMockBuilder('Symfony\Component\Console\Output\ConsoleOutput')
->disableOriginalConstructor()
->getMock();
$output->expects($this->any())
->method('writeln')
->will($this->returnCallback(function ($input) {
echo $input . "\n";
}));
return new \Illuminate\Console\OutputStyle($this->app->make('Illuminate\Contracts\Console\Kernel'), $output);
}
}
This code sets up a mock output object that will be used by the command during testing. The getMockOutput() method creates a mock object that expects calls to the writeln() method and echoes the input to the console. The $this->app->instance() call sets the mock output object as the output for the command.
With this setup, you should be able to call $this->info() in your command's handle() method without causing any errors.