use Symfony\Component\Console\Tester\CommandTester; use App\Command\HelloCommand; $command = new HelloCommand(); $commandTester = new CommandTester($command); $commandTester->execute([]); $output = $commandTester->getDisplay(); // ensure that the output contains the expected greeting $this->assertStringContainsString('Hello, world!', $output);
use Symfony\Component\Console\Tester\CommandTester; use App\Command\GreetCommand; $command = new GreetCommand(); $commandTester = new CommandTester($command); $commandTester->execute([ 'name' => 'Alice', ]); $output = $commandTester->getDisplay(); // ensure that the output contains the expected greeting for Alice $this->assertStringContainsString('Hello, Alice!', $output);In both examples, we create a CommandTester instance for a console command, execute it with certain arguments, and then check the output using getDisplay(). These snippets demonstrate how CommandTester can be used to test different console commands with various input/output scenarios.