Answer the question
In order to leave comments, you need to log in
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
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.
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.
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 questionAsk a Question
731 491 924 answers to any question