use Symfony\Component\Console\Tester\CommandTester; use MyApp\Console\HelloCommand; $command = new HelloCommand(); $commandTester = new CommandTester($command); $commandTester->execute(array('name' => 'Symfony')); $result = $commandTester->getDisplay(); assertContains('Hello Symfony', $result);
use Symfony\Component\Console\Tester\CommandTester; use MyApp\Console\GreetCommand; $command = new GreetCommand(); $commandTester = new CommandTester($command); $commandTester->execute(array( 'name' => 'Symfony', '--yell' => true, '--iterations' => 3 )); $result = $commandTester->getDisplay(); assertContains('HELLO SYMFONY', $result); assertContains('HELLO SYMFONY', $result); assertContains('HELLO SYMFONY', $result);In this example, we create an instance of the GreetCommand and initialize a CommandTester object with it. We then execute the command with an argument of 'Symfony', and options of '--yell' and '--iterations'. We retrieve the output of the command from the CommandTester and assert that it contains the expected output. The Symfony\Component\Console\Tester\CommandTester library is a part of the Symfony Console Component and is used in testing console commands.