Skip to content

Commit cd142c7

Browse files
committed
Move Testing commands to the guide
1 parent d3b9f02 commit cd142c7

File tree

2 files changed

+46
-103
lines changed

2 files changed

+46
-103
lines changed

components/console.rst

-69
Original file line numberDiff line numberDiff line change
@@ -112,75 +112,6 @@ This prints::
112112

113113
HELLO FABIEN
114114

115-
.. _component-console-testing-commands:
116-
117-
Testing Commands
118-
----------------
119-
120-
Symfony provides several tools to help you test your commands. The most
121-
useful one is the :class:`Symfony\\Component\\Console\\Tester\\CommandTester`
122-
class. It uses special input and output classes to ease testing without a real
123-
console::
124-
125-
use Acme\Console\Command\GreetCommand;
126-
use Symfony\Component\Console\Application;
127-
use Symfony\Component\Console\Tester\CommandTester;
128-
129-
class ListCommandTest extends \PHPUnit_Framework_TestCase
130-
{
131-
public function testExecute()
132-
{
133-
$application = new Application();
134-
$application->add(new GreetCommand());
135-
136-
$command = $application->find('demo:greet');
137-
$commandTester = new CommandTester($command);
138-
$commandTester->execute(array('command' => $command->getName()));
139-
140-
$this->assertRegExp('/.../', $commandTester->getDisplay());
141-
142-
// ...
143-
}
144-
}
145-
146-
The :method:`Symfony\\Component\\Console\\Tester\\CommandTester::getDisplay`
147-
method returns what would have been displayed during a normal call from the
148-
console.
149-
150-
You can test sending arguments and options to the command by passing them
151-
as an array to the :method:`Symfony\\Component\\Console\\Tester\\CommandTester::execute`
152-
method::
153-
154-
use Acme\Console\Command\GreetCommand;
155-
use Symfony\Component\Console\Application;
156-
use Symfony\Component\Console\Tester\CommandTester;
157-
158-
class ListCommandTest extends \PHPUnit_Framework_TestCase
159-
{
160-
// ...
161-
162-
public function testNameIsOutput()
163-
{
164-
$application = new Application();
165-
$application->add(new GreetCommand());
166-
167-
$command = $application->find('demo:greet');
168-
$commandTester = new CommandTester($command);
169-
$commandTester->execute(array(
170-
'command' => $command->getName(),
171-
'name' => 'Fabien',
172-
'--iterations' => 5,
173-
));
174-
175-
$this->assertRegExp('/Fabien/', $commandTester->getDisplay());
176-
}
177-
}
178-
179-
.. tip::
180-
181-
You can also test a whole console application by using
182-
:class:`Symfony\\Component\\Console\\Tester\\ApplicationTester`.
183-
184115
Learn More
185116
----------
186117

console.rst

+46-34
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ the ``generate()`` method of the ``app.admin_generator`` service and the admin
199199
will be created.
200200
201201
Command Lifecycle
202-
~~~~~~~~~~~~~~~~~
202+
-----------------
203203
204204
Commands have three lifecycle methods that are invoked when running the
205205
command:
@@ -223,73 +223,85 @@ command:
223223
Testing 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
264277
To be able to use the fully set up service container for your console tests
265278
you 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

Comments
 (0)