I'm using the Symfony Console component as a standalone library to create a CLI. I'm finished with the logic and wanted to write some tests to see how it performs during edge cases.
However, I'm not sure how to test Symfony Console apps, so I've been refering to Google and their documentation quite a lot, but without success.
My commands will be dealing with file creation, so I'm using the vfsStream for that. This is what my abstract TestCase class looks like:
<?php
namespace MyApp\Tests;
use org\bovigo\vfs\vfsStream;
use PHPUnit\Framework\TestCase as PhpUnitTestCase;
abstract class TestCase extends PhpUnitTestCase
{
/**
* @var vfsStream
*/
protected $fileRoot;
/**
* TestCase setup.
*
* @return void
*/
protected function setUp()
{
parent::setUp();
$this->fileRoot = vfsStream::setup('myapp_vspace');
}
}
And this is how my InitCommandTest file looks like:
namespace MyApp\Tests\Command;
use Mockery;
use MyApp\Tests\TestCase;
use org\bovigo\vfs\vfsStream;
use MyApp\Commands\InitCommand;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;
class InitCommandTest extends TestCase
{
/**
* @var InitCommand
*/
protected $initCommand;
/**
* Setup more stuff...
*
* @return void
*/
protected function setUp()
{
parent::setUp();
$this->initCommand = Mockery::mock(new InitCommand)
->shouldAllowMockingProtectedMethods()
->shouldReceive('currentPath')
->andReturnUsing(function ($path) {
return vfsStream::url("myapp_vspace/{$path}");
})->getMock();
}
public function testExecute()
{
$application = new Application;
$application->add($this->initCommand);
$command = $application->find('init');
$commandTester = new CommandTester($command);
$commandTester->setInputs(['This', 'That', 'This', 'That', 'This', 'That']);
$commandTester->execute(['command' => $command->getName()]);
// the output of the command in the console
$output = $commandTester->getDisplay();
var_dump($output);
}
}
Running this test causes the phpunit binary to hang and never finish. PHP-FPM starts using up a lot of processing power and the Terminal uses a lot of RAM. I'm not sure what's wrong here.
The InitCommand expects inputs from the user - 6 of them (so there are 6 $questionHelper->ask() calls).
Help would be appreciated.
Aucun commentaire:
Enregistrer un commentaire