@@ -62,6 +62,26 @@ This command will now automatically be available to run:
62
62
63
63
$ app/console demo:greet Fabien
64
64
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
+
65
85
Testing Commands
66
86
----------------
67
87
@@ -90,22 +110,31 @@ should be used instead of :class:`Symfony\\Component\\Console\\Application`::
90
110
}
91
111
}
92
112
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\B undle\F rameworkBundle\T est\W ebTestCase `::
95
116
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;
101
121
102
- protected function execute(InputInterface $input, OutputInterface $output)
122
+ class ListCommandTest extends WebTestCase
103
123
{
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
+ // ...
110
139
}
111
140
}
0 commit comments