I'm writing some custom artisan commands, and I've hit a snag in testing.
A simplified version of my test looks like this:
public function test_output(): void
{
$this->assertEquals(0, Artisan::call('app:hello'));
$output = Artisan::output();
dump($output);
$this->assertEquals(1,
preg_match('/Hello (\w+)/', $output, $matches));
$this->assertEquals('World', $matches[1]);
}
This fails, because $output is an empty string.
I think this is somehow related to testing, because the same commands work in tinker:
$ php artisan tinker
Psy Shell v0.12.9 (PHP 8.4.10 — cli) by Justin Hileman
> Artisan::call('app:hello');
= 0
> Artisan::output();
= "Hello World\n"
And to pre-empt this: I know about
$this->artisan('app:hello')->expectsOutputToContain('Hello');
That works, but is not what I want. I need the actual output text from the command, so I can extract some piece of it for use in the later parts of the test.