Skip to content

Commit 22eafca

Browse files
committed
Merge pull request symfony#1986 from richardmiller/console_testing_container
Adding info on setting up the container for console testing
2 parents a865f99 + 0bbd890 commit 22eafca

File tree

1 file changed

+43
-14
lines changed

1 file changed

+43
-14
lines changed

cookbook/console/console_command.rst

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,26 @@ This command will now automatically be available to run:
6262
6363
$ app/console demo:greet Fabien
6464
65+
Getting Services from the Service Container
66+
-------------------------------------------
67+
68+
By using :class:`Symfony\\Bundle\\FrameworkBundle\\Command\\ContainerAwareCommand`
69+
as the base class for the command (instead of the more basic
70+
:class:`Symfony\\Component\\Console\\Command\\Command`), you have access to the
71+
service container. In other words, you have access to any configured service.
72+
For example, you could easily extend the task to be translatable::
73+
74+
protected function execute(InputInterface $input, OutputInterface $output)
75+
{
76+
$name = $input->getArgument('name');
77+
$translator = $this->getContainer()->get('translator');
78+
if ($name) {
79+
$output->writeln($translator->trans('Hello %name%!', array('%name%' => $name)));
80+
} else {
81+
$output->writeln($translator->trans('Hello!'));
82+
}
83+
}
84+
6585
Testing Commands
6686
----------------
6787

@@ -90,22 +110,31 @@ should be used instead of :class:`Symfony\\Component\\Console\\Application`::
90110
}
91111
}
92112

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

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

102-
protected function execute(InputInterface $input, OutputInterface $output)
122+
class ListCommandTest extends WebTestCase
103123
{
104-
$name = $input->getArgument('name');
105-
$translator = $this->getContainer()->get('translator');
106-
if ($name) {
107-
$output->writeln($translator->trans('Hello %name%!', array('%name%' => $name)));
108-
} else {
109-
$output->writeln($translator->trans('Hello!'));
124+
public function testExecute()
125+
{
126+
$kernel = $this->createKernel();
127+
$kernel->boot();
128+
129+
$application = new Application($kernel);
130+
$application->add(new GreetCommand());
131+
132+
$command = $application->find('demo:greet');
133+
$commandTester = new CommandTester($command);
134+
$commandTester->execute(array('command' => $command->getName()));
135+
136+
$this->assertRegExp('/.../', $commandTester->getDisplay());
137+
138+
// ...
110139
}
111140
}

0 commit comments

Comments
 (0)