@@ -199,7 +199,7 @@ the ``generate()`` method of the ``app.admin_generator`` service and the admin
199199will be created.
200200
201201Command Lifecycle
202- ~~~~~~~~~~~~~~~~~
202+ -----------------
203203
204204Commands have three lifecycle methods that are invoked when running the
205205command:
@@ -223,73 +223,85 @@ command:
223223Testing Commands
224224----------------
225225
226- When testing commands used as part of the full-stack framework,
227- :class:`Symfony\\Bundle\\FrameworkBundle\\ Console\\Application <Symfony\\Bundle\\FrameworkBundle\\Console\\Application> `
228- should be used instead of
229- :class:`Symfony\\Component\\Console\\Application <Symfony\\Component\\Console\\Application>` ::
226+ Symfony provides several tools to help you test your commands. The most
227+ useful one is the :class:`Symfony\\Component\\ Console\\Tester\\CommandTester `
228+ class. It uses special input and output classes to ease testing without a real
229+ console ::
230230
231- use Symfony\Component\Console\Tester\CommandTester;
231+ // tests/AppBundle/Command/GenerateAdminCommandTest.php
232+ namespace Tests\AppBundle\Command;
233+
234+ use AppBundle\Command\GenerateAdminCommand;
232235 use Symfony\Bundle\FrameworkBundle\Console\Application;
233- use AppBundle\Command\GreetCommand ;
236+ use Symfony\Component\Console\Tester\CommandTester ;
234237
235- class ListCommandTest extends \PHPUnit_Framework_TestCase
238+ class GenerateAdminCommandTest extends \PHPUnit_Framework_TestCase
236239 {
237240 public function testExecute()
238241 {
239- // mock the Kernel or create one depending on your needs
240- $application = new Application($kernel);
241- $application->add(new GreetCommand());
242+ $application = new Application();
243+ $application->add(new GenerateAdminCommand());
242244
243- $command = $application->find(' demo:greet ' );
245+ $command = $application->find(' app:generate-admin ' );
244246 $commandTester = new CommandTester($command);
245- $commandTester->execute(
246- array(
247- ' name' => ' Fabien' ,
248- ' --yell' => true,
249- )
250- );
247+ $commandTester->execute(array(
248+ ' command' => $command->getName(),
249+
250+ // pass arguments to the helper
251+ ' username' => ' Wouter' ,
251252
252- $this->assertRegExp(' /.../' , $commandTester->getDisplay());
253+ // prefix the key with a double slash when passing options,
254+ // e.g: ' --some-option' => ' option_value' ,
255+ ));
256+
257+ // the output of the command in the console
258+ $output = $commandTester->getDisplay();
259+ $this->assertContains(' Username: Wouter' , $output);
253260
254261 // ...
255262 }
256263 }
257264
265+ .. tip::
266+
267+ You can also test a whole console application by using
268+ :class:`Symfony\\Component\\Console\\Tester\\ApplicationTester`.
269+
258270.. note::
259271
260- In the specific case above, the ``name`` parameter and the ``--yell`` option
261- are not mandatory for the command to work, but are shown so you can see
262- how to customize them when calling the command.
272+ When using the Console component in a standalone project, use
273+ :class:`Symfony\\Component\\Console\\Application <Symfony\\Component\\Console\\Application>`
274+ instead of
275+ :class:`Symfony\\Bundle\\FrameworkBundle\\Console\\Application <Symfony\\Bundle\\FrameworkBundle\\Console\\Application>`
263276
264277To be able to use the fully set up service container for your console tests
265278you can extend your test from
266279:class:`Symfony\\Bundle\\FrameworkBundle\\Test\\KernelTestCase`::
267280
281+ // ...
268282 use Symfony\Component\Console\Tester\CommandTester;
269283 use Symfony\Bundle\FrameworkBundle\Console\Application;
270284 use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
271- use AppBundle\Command\GreetCommand;
272285
273- class ListCommandTest extends KernelTestCase
286+ class GenerateAdminCommandTest extends KernelTestCase
274287 {
275288 public function testExecute()
276289 {
277290 $kernel = $this->createKernel();
278291 $kernel->boot();
279292
280293 $application = new Application($kernel);
281- $application->add(new GreetCommand ());
294+ $application->add(new GenerateAdminCommand ());
282295
283- $command = $application->find(' demo:greet ' );
296+ $command = $application->find(' app:generate-admin ' );
284297 $commandTester = new CommandTester($command);
285- $commandTester->execute(
286- array(
287- ' name' => ' Fabien' ,
288- ' --yell' => true,
289- )
290- );
291-
292- $this->assertRegExp(' /.../' , $commandTester->getDisplay());
298+ $commandTester->execute(array(
299+ ' command' => $command->getName(),
300+ ' username' => ' Wouter' ,
301+ ));
302+
303+ $output = $commandTester->getDisplay();
304+ $this->assertContains(' Username: Wouter' , $output);
293305
294306 // ...
295307 }
0 commit comments