Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 43 additions & 14 deletions cookbook/console/console_command.rst
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,26 @@ This command will now automatically be available to run:
$ app/console demo:greet Fabien
Getting Services from the Service Container
-------------------------------------------

By using :class:`Symfony\\Bundle\\FrameworkBundle\\Command\\ContainerAwareCommand`
as the base class for the command (instead of the more basic
:class:`Symfony\\Component\\Console\\Command\\Command`), you have access to the
service container. In other words, you have access to any configured service.
For example, you could easily extend the task to be translatable::

protected function execute(InputInterface $input, OutputInterface $output)
{
$name = $input->getArgument('name');
$translator = $this->getContainer()->get('translator');
if ($name) {
$output->writeln($translator->trans('Hello %name%!', array('%name%' => $name)));
} else {
$output->writeln($translator->trans('Hello!'));
}
}

Testing Commands
----------------

Expand Down Expand Up @@ -90,22 +110,31 @@ should be used instead of :class:`Symfony\\Component\\Console\\Application`::
}
}

Getting Services from the Service Container
-------------------------------------------
To be able to use the fully set up service container for your console tests
you can extend your test from
:class:`Symfony\Bundle\FrameworkBundle\Test\WebTestCase`::

By using :class:`Symfony\\Bundle\\FrameworkBundle\\Command\\ContainerAwareCommand`
as the base class for the command (instead of the more basic
:class:`Symfony\\Component\\Console\\Command\\Command`), you have access to the
service container. In other words, you have access to any configured service.
For example, you could easily extend the task to be translatable::
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Acme\DemoBundle\Command\GreetCommand;

protected function execute(InputInterface $input, OutputInterface $output)
class ListCommandTest extends WebTestCase
{
$name = $input->getArgument('name');
$translator = $this->getContainer()->get('translator');
if ($name) {
$output->writeln($translator->trans('Hello %name%!', array('%name%' => $name)));
} else {
$output->writeln($translator->trans('Hello!'));
public function testExecute()
{
$kernel = $this->createKernel();
$kernel->boot();

$application = new Application($kernel);
$application->add(new GreetCommand());

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

$this->assertRegExp('/.../', $commandTester->getDisplay());

// ...
}
}