Skip to content

Commit

Permalink
Adding an example of command test using the ApplicationTester class
Browse files Browse the repository at this point in the history
  • Loading branch information
lkolndeep committed May 18, 2024
1 parent ba80985 commit c8e36be
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions console.rst
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,46 @@ call ``setAutoExit(false)`` on it to get the command result in ``CommandTester``
You can also test a whole console application by using
:class:`Symfony\\Component\\Console\\Tester\\ApplicationTester`.

Here an example of a test using this class::

use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Console\Tester\ApplicationTester;
use Symfony\Component\Console\Tester\CommandTester;

class WelcomeCommandTest extends KernelTestCase
{
public function testPerson(): void
{
self::bootKernel();
$application = new Application(self::$kernel);
$application->setAutoExit(false);

$applicationTester = new ApplicationTester($application);

$input = [
// Pass the command name
'command' => 'app:welcome-person',
// Pass the different arguments
'firstName' => 'Michael',
'lastName' => 'Jackson',
'hobbies' => ['singing', 'dancing']
];

// Call run to launch the application
$applicationTester->run($input);

$applicationTester->assertCommandIsSuccessful();

$output = $applicationTester->getDisplay();

// Here $output value is "The person is Michael Jackson and his hobbies are the following singing and dancing."
$this->assertStringContainsString('Michael Jackson', $output);
$this->assertStringContainsString('singing and dancing', $output);
}
}

.. caution::

When testing commands using the ``CommandTester`` class, console events are
Expand Down

0 comments on commit c8e36be

Please sign in to comment.