@@ -68,6 +68,22 @@ add the following to it::
|
68 | 68 | }
|
69 | 69 | }
|
70 | 70 |
|
| 71 | +You also need to create the file to run at the command line which creates |
| 72 | +an ``Application`` and adds commands to it: |
| 73 | + |
| 74 | +.. code-block::php |
| 75 | +
|
| 76 | +#!/usr/bin/env php |
| 77 | +# app/console |
| 78 | +<?php |
| 79 | +
|
| 80 | +use Acme\DemoBundle\Command\GreetCommand; |
| 81 | +use Symfony\Component\Console\Application; |
| 82 | +
|
| 83 | +$application = new Application(); |
| 84 | +$application->add(new GreetCommand); |
| 85 | +$application->run(); |
| 86 | +
|
71 | 87 | Test the new console command by running the following
|
72 | 88 |
|
73 | 89 | .. code-block:: bash
|
@@ -247,8 +263,7 @@ console::
|
247 | 263 | {
|
248 | 264 | public function testExecute()
|
249 | 265 | {
|
250 |
| -// mock the Kernel or create one depending on your needs |
251 |
| -$application = new Application($kernel); |
| 266 | +$application = new Application(); |
252 | 267 | $application->add(new GreetCommand());
|
253 | 268 |
|
254 | 269 | $command = $application->find('demo:greet');
|
@@ -265,31 +280,39 @@ The :method:`Symfony\\Component\\Console\\Tester\\CommandTester::getDisplay`
|
265 | 280 | method returns what would have been displayed during a normal call from the
|
266 | 281 | console.
|
267 | 282 |
|
268 |
| -.. tip:: |
| 283 | +You can test sending arguments and options to the command by passing them |
| 284 | +as an array to the :method:`Symfony\\Component\\Console\\Tester\\CommandTester::getDisplay` |
| 285 | +method:: |
269 | 286 |
|
270 |
| -You can also test a whole console application by using |
271 |
| -:class:`Symfony\\Component\\Console\\Tester\\ApplicationTester`. |
| 287 | +use Symfony\Component\Console\Tester\CommandTester; |
| 288 | +use Symfony\Bundle\FrameworkBundle\Console\Application; |
| 289 | +use Acme\DemoBundle\Command\GreetCommand; |
272 | 290 |
|
273 |
| -Getting Services from the Service Container |
274 |
| -------------------------------------------- |
| 291 | +class ListCommandTest extends \PHPUnit_Framework_TestCase |
| 292 | +{ |
275 | 293 |
|
276 |
| -By using :class:`Symfony\\Bundle\\FrameworkBundle\\Command\\ContainerAwareCommand` |
277 |
| -as the base class for the command (instead of the more basic |
278 |
| -:class:`Symfony\\Component\\Console\\Command\\Command`), you have access to the |
279 |
| -service container. In other words, you have access to any configured service. |
280 |
| -For example, you could easily extend the task to be translatable:: |
| 294 | +//-- |
281 | 295 |
|
282 |
| -protected function execute(InputInterface $input, OutputInterface $output) |
283 |
| -{ |
284 |
| -$name = $input->getArgument('name'); |
285 |
| -$translator = $this->getContainer()->get('translator'); |
286 |
| -if ($name) { |
287 |
| -$output->writeln($translator->trans('Hello %name%!', array('%name%' => $name))); |
288 |
| -} else { |
289 |
| -$output->writeln($translator->trans('Hello!')); |
| 296 | +public function testNameIsOutput() |
| 297 | +{ |
| 298 | +$application = new Application(); |
| 299 | +$application->add(new GreetCommand()); |
| 300 | + |
| 301 | +$command = $application->find('demo:greet'); |
| 302 | +$commandTester = new CommandTester($command); |
| 303 | +$commandTester->execute( |
| 304 | +array('command' => $command->getName(), 'name' => 'Fabien') |
| 305 | +); |
| 306 | + |
| 307 | +$this->assertRegExp('/Fabien/', $commandTester->getDisplay()); |
290 | 308 | }
|
291 | 309 | }
|
292 | 310 |
|
| 311 | +.. tip:: |
| 312 | + |
| 313 | +You can also test a whole console application by using |
| 314 | +:class:`Symfony\\Component\\Console\\Tester\\ApplicationTester`. |
| 315 | + |
293 | 316 | Calling an existing Command
|
294 | 317 | ---------------------------
|
295 | 318 |
|
|
0 commit comments