Skip to content

Commit b376f52

Browse files
committed
Adding an example of command test using the ApplicationTester class
1 parent ba80985 commit b376f52

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

console.rst

+41-1
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,46 @@ call ``setAutoExit(false)`` on it to get the command result in ``CommandTester``
555555
You can also test a whole console application by using
556556
:class:`Symfony\\Component\\Console\\Tester\\ApplicationTester`.
557557

558+
Here an example of a test using this class::
559+
560+
use Symfony\Bundle\FrameworkBundle\Console\Application;
561+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
562+
use Symfony\Component\Console\Tester\ApplicationTester;
563+
use Symfony\Component\Console\Tester\CommandTester;
564+
565+
class WelcomeCommandTest extends KernelTestCase
566+
{
567+
public function testPerson(): void
568+
{
569+
self::bootKernel();
570+
$application = new Application(self::$kernel);
571+
$application->setAutoExit(false);
572+
573+
$applicationTester = new ApplicationTester($application);
574+
575+
$input = [
576+
// Pass the command name
577+
'command' => 'app:welcome-person',
578+
579+
// Pass the different arguments
580+
'firstName' => 'Michael',
581+
'lastName' => 'Jackson',
582+
'hobbies' => ['singing', 'dancing']
583+
];
584+
585+
// Call run to launch the application
586+
$applicationTester->run($input);
587+
588+
$applicationTester->assertCommandIsSuccessful();
589+
590+
$output = $applicationTester->getDisplay();
591+
592+
// Here $output value is "The person is Michael Jackson and his hobbies are the following singing and dancing."
593+
$this->assertStringContainsString('Michael Jackson', $output);
594+
$this->assertStringContainsString('singing and dancing', $output);
595+
}
596+
}
597+
558598
.. caution::
559599

560600
When testing commands using the ``CommandTester`` class, console events are
@@ -566,7 +606,7 @@ call ``setAutoExit(false)`` on it to get the command result in ``CommandTester``
566606
When testing commands using the :class:`Symfony\\Component\\Console\\Tester\\ApplicationTester`
567607
class, don't forget to disable the auto exit flag::
568608

569-
$application = new Application();
609+
$application = new Application(self::$kernel);
570610
$application->setAutoExit(false);
571611

572612
$tester = new ApplicationTester($application);

0 commit comments

Comments
 (0)