@@ -199,7 +199,7 @@ the ``generate()`` method of the ``app.admin_generator`` service and the admin
199
199
will be created.
200
200
201
201
Command Lifecycle
202
- ~~~~~~~~~~~~~~~~~
202
+ -----------------
203
203
204
204
Commands have three lifecycle methods that are invoked when running the
205
205
command:
@@ -223,73 +223,85 @@ command:
223
223
Testing Commands
224
224
----------------
225
225
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 ::
230
230
231
- use Symfony\Component\Console\Tester\CommandTester;
231
+ // tests/AppBundle/Command/GenerateAdminCommandTest.php
232
+ namespace Tests\AppBundle\Command;
233
+
234
+ use AppBundle\Command\GenerateAdminCommand;
232
235
use Symfony\Bundle\FrameworkBundle\Console\Application;
233
- use AppBundle\Command\GreetCommand ;
236
+ use Symfony\Component\Console\Tester\CommandTester ;
234
237
235
- class ListCommandTest extends \PHPUnit_Framework_TestCase
238
+ class GenerateAdminCommandTest extends \PHPUnit_Framework_TestCase
236
239
{
237
240
public function testExecute()
238
241
{
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());
242
244
243
- $command = $application->find(' demo:greet ' );
245
+ $command = $application->find(' app:generate-admin ' );
244
246
$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' ,
251
252
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);
253
260
254
261
// ...
255
262
}
256
263
}
257
264
265
+ .. tip::
266
+
267
+ You can also test a whole console application by using
268
+ :class:`Symfony\\Component\\Console\\Tester\\ApplicationTester`.
269
+
258
270
.. note::
259
271
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>`
263
276
264
277
To be able to use the fully set up service container for your console tests
265
278
you can extend your test from
266
279
:class:`Symfony\\Bundle\\FrameworkBundle\\Test\\KernelTestCase`::
267
280
281
+ // ...
268
282
use Symfony\Component\Console\Tester\CommandTester;
269
283
use Symfony\Bundle\FrameworkBundle\Console\Application;
270
284
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
271
- use AppBundle\Command\GreetCommand;
272
285
273
- class ListCommandTest extends KernelTestCase
286
+ class GenerateAdminCommandTest extends KernelTestCase
274
287
{
275
288
public function testExecute()
276
289
{
277
290
$kernel = $this->createKernel();
278
291
$kernel->boot();
279
292
280
293
$application = new Application($kernel);
281
- $application->add(new GreetCommand ());
294
+ $application->add(new GenerateAdminCommand ());
282
295
283
- $command = $application->find(' demo:greet ' );
296
+ $command = $application->find(' app:generate-admin ' );
284
297
$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);
293
305
294
306
// ...
295
307
}
0 commit comments