Y
Y
Yaroslav Urbanevich2015-04-08 13:17:31
symfony
Yaroslav Urbanevich, 2015-04-08 13:17:31

How to call a console command in PHPUnit from a test?

It is necessary to test the operation of console commands in a symfony2 application.
That is, you need to call a command with a test, for example (php app/console router:debug ), then see the command response.
How to make the test run the command and then see the response that is displayed in the console.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
K
keltanas, 2015-04-08
@keltanas

Why test what the command outputs? Do you doubt the work of the Output class and helpers?
1. Make a subtle console command that uses the desired service.
2. Test the service.

O
OnYourLips, 2015-04-08
@OnYourLips

Our tests checked applications through and through, starting from the creation of a request. That is, without the http part and commands.
Therefore, we simply did additional routing for commands and checked them like regular controller methods.

Y
Yaroslav Urbanevich, 2015-04-08
@exekhua

Found a solution:

use Asme\Command\UpdateCurrencyCommand;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Console\Tester\CommandTester;
class UpdateCurrencyCommandTest extends WebTestCase
{
public function testUpdateCurrency()
{
$kernel = $this->createKernel();
$kernel->boot();
$application = new Application($kernel);
$application->add(new UpdateCurrencyCommand());
$command = $application->find('command name');
$commandTester = new CommandTester($command);
$commandTester->execute(array('command' => $command->getName()));
//checking the response
$this->assertNotEquals('...', $commandTester->getDisplay());

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question