Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 0bbd890

Browse filesBrowse files
Adding info on setting up the container for console testing
1 parent 114ce6d commit 0bbd890
Copy full SHA for 0bbd890

File tree

Expand file treeCollapse file tree

1 file changed

+43
-14
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+43
-14
lines changed

‎cookbook/console/console_command.rst

Copy file name to clipboardExpand all lines: cookbook/console/console_command.rst
+43-14Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.