Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running console command from a Symfony 2 test case

Tags:

Is there a way to run a console command from a Symfony 2 test case? I want to run the doctrine commands for creating and dropping schemas.

like image 868
vinnylinux Avatar asked Apr 30 '12 17:04

vinnylinux


People also ask

How do I run console commands?

Easily open Command Prompt by running Windows Run by holding the Windows button and hitting the R button on your keyboard. You can then type "cmd" and press enter, opening Command Prompt. If you're unsure of what commands to use, you can type "Help" into Command Prompt.

What is bin console in Symfony?

The Symfony framework provides lots of commands through the bin/console script (e.g. the well-known bin/console cache:clear command). These commands are created with the Console component. You can also use it to create your own commands.

What is Symfony Console component?

The Console component eases the creation of beautiful and testable command line interfaces. The Console component allows you to create command-line commands. Your console commands can be used for any recurring task, such as cronjobs, imports, or other batch jobs.

How do I know what version of Symfony I have?

If you have file system access to the project Look inside the file for a line like: const VERSION = '5.0. 4'; that's the Symfony version number.


1 Answers

This documentation chapter explains how to run commands from different places. Mind, that using exec() for your needs is quite dirty solution...

The right way of executing console command in Symfony2 is as below:

Option one

use Symfony\Bundle\FrameworkBundle\Console\Application as App; use Symfony\Component\Console\Tester\CommandTester;  class YourTest extends WebTestCase {     public function setUp()     {         $kernel = $this->createKernel();         $kernel->boot();          $application = new App($kernel);         $application->add(new YourCommand());          $command = $application->find('your:command:name');         $commandTester = new CommandTester($command);         $commandTester->execute(array('command' => $command->getName()));     } } 

Option two

use Symfony\Component\Console\Input\StringInput; use Symfony\Bundle\FrameworkBundle\Console\Application;  class YourClass extends WebTestCase {     protected static $application;      public function setUp()     {         self::runCommand('your:command:name');         // you can also specify an environment:         // self::runCommand('your:command:name --env=test');     }      protected static function runCommand($command)     {         $command = sprintf('%s --quiet', $command);              return self::getApplication()->run(new StringInput($command));     }      protected static function getApplication()     {         if (null === self::$application) {             $client = static::createClient();              self::$application = new Application($client->getKernel());             self::$application->setAutoExit(false);         }          return self::$application;     } } 

P.S. Guys, don't shame Symfony2 with calling exec()...

like image 109
Vitalii Zurian Avatar answered Sep 21 '22 14:09

Vitalii Zurian