@@ -555,6 +555,46 @@ call ``setAutoExit(false)`` on it to get the command result in ``CommandTester``
|
555 | 555 | You can also test a whole console application by using
|
556 | 556 | :class:`Symfony\\Component\\Console\\Tester\\ApplicationTester`.
|
557 | 557 |
|
| 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 | + |
558 | 598 | .. caution::
|
559 | 599 |
|
560 | 600 | 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``
|
566 | 606 | When testing commands using the :class:`Symfony\\Component\\Console\\Tester\\ApplicationTester`
|
567 | 607 | class, don't forget to disable the auto exit flag::
|
568 | 608 |
|
569 |
| -$application = new Application(); |
| 609 | +$application = new Application(self::$kernel); |
570 | 610 | $application->setAutoExit(false);
|
571 | 611 |
|
572 | 612 | $tester = new ApplicationTester($application);
|
|
0 commit comments