From 0e55c2c521bc718cfec375657bbbe8ebc6d3724b Mon Sep 17 00:00:00 2001 From: lkolndeep Date: Sat, 18 May 2024 22:29:48 +0200 Subject: [PATCH] [Console] Adding an example of command test using the ApplicationTester class --- console.rst | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/console.rst b/console.rst index e414ed15ced..c6fa3887574 100644 --- a/console.rst +++ b/console.rst @@ -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 @@ -566,7 +606,7 @@ call ``setAutoExit(false)`` on it to get the command result in ``CommandTester`` When testing commands using the :class:`Symfony\\Component\\Console\\Tester\\ApplicationTester` class, don't forget to disable the auto exit flag:: - $application = new Application(); + $application = new Application(self::$kernel); $application->setAutoExit(false); $tester = new ApplicationTester($application); pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy