1

I have a test that runs a Symfony 2.0 Command. In the test, I run the Command like this:

$application = new Application(static::$kernel);
$application->add(new MyCommand());

$command = $application->find('mycommand:name');
$commandTester = new CommandTester($command);
$commandTester->execute(array('command' => $command->getName()));

I want to run the Command with the --no-debug flag, which is a built-in flag to the app:console (I guess?). I've tried:

$commandTester->execute(array('command' => $command->getName(), '--no-debug' => true));

and I've also tried:

$application->run(new StringInput('mycommand:name --no-debug'));

as suggested here. I've tried some other permutations too. But none of them do what I want, which is turn off all the Debug messages in the logs. Is there a way to do what I want in the test?

Community
  • 1
  • 1
matt
  • 1,947
  • 1
  • 19
  • 29

2 Answers2

2

disabling debug in the kernel

You can initialize the kernel in your test's setUp() method with debug set to false.

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class YourCommandTest extends WebTestCase
{

   public function setUp()
   {
      static::$kernel = static::createKernel(array('debug' => false));
      static::$kernel->boot();

      // ...
   }

Have a look at WebTestCase->createKernel().

using enviroments

If you don't want the debug output in your logs edit/create app/config/config_test.yml and edit ...

monolog:
    handlers:
        file:
           type: stream
            level: debug       # log-level ...   

... to another log level that suits your needs.

Then execute your command using the test environment with --env=test

Nicolai Fröhlich
  • 51,330
  • 11
  • 126
  • 130
  • The [manual page](http://symfony.com/doc/2.0/cookbook/console/usage.html) says "In addition to changing the environment, you can also choose to disable debug mode. This can be useful where you want to run commands in the dev environment but avoid the performance hit of collecting debug data: `$ php app/console list --no-debug`." And my experience is exactly that, the --no-debug flag turns off debugging when I run it from the command line. I do know about setting the log level in the config file, but I only want to turn off debugging for this particular test, not all tests. – matt Jun 19 '13 at 00:10
  • good point how about initializing the kernel without debug in your test? $kernel = new AppKernel('test', false); – Nicolai Fröhlich Jun 19 '13 at 00:20
0

--no-debug obviously disable debug mode which show errors. Try to run command in test environment with --env=test.

Alexey B.
  • 11,965
  • 2
  • 49
  • 73