php 从 Symfony 2 测试用例运行控制台命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10387857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Running console command from a Symfony 2 test case
提问by vinnylinux
Is there a way to run a console command from a Symfony 2 test case? I want to run the doctrine commands for creating and dropping schemas.
有没有办法从 Symfony 2 测试用例运行控制台命令?我想运行用于创建和删除模式的学说命令。
采纳答案by Klaus S.
The documentation has been updated since my last answer to reflect the proper Symfony 2 way of calling an existing command:
自从我上次回答以来,文档已经更新,以反映调用现有命令的正确 Symfony 2 方式:
http://symfony.com/doc/current/components/console/introduction.html#calling-an-existing-command
http://symfony.com/doc/current/components/console/introduction.html#calling-an-existing-command
回答by Vitalii Zurian
This documentation chapterexplains how to run commands from different places. Mind, that using exec()for your needs is quite dirty solution...
本文档章节解释了如何从不同位置运行命令。请注意,exec()根据您的需要使用是非常肮脏的解决方案......
The right wayof executing console command in Symfony2 is as below:
在 Symfony2 中执行控制台命令的正确方法如下:
Option one
选项一
use Symfony\Bundle\FrameworkBundle\Console\Application as App;
use Symfony\Component\Console\Tester\CommandTester;
class YourTest extends WebTestCase
{
public function setUp()
{
$kernel = $this->createKernel();
$kernel->boot();
$application = new App($kernel);
$application->add(new YourCommand());
$command = $application->find('your:command:name');
$commandTester = new CommandTester($command);
$commandTester->execute(array('command' => $command->getName()));
}
}
Option two
选项二
use Symfony\Component\Console\Input\StringInput;
use Symfony\Bundle\FrameworkBundle\Console\Application;
class YourClass extends WebTestCase
{
protected static $application;
public function setUp()
{
self::runCommand('your:command:name');
// you can also specify an environment:
// self::runCommand('your:command:name --env=test');
}
protected static function runCommand($command)
{
$command = sprintf('%s --quiet', $command);
return self::getApplication()->run(new StringInput($command));
}
protected static function getApplication()
{
if (null === self::$application) {
$client = static::createClient();
self::$application = new Application($client->getKernel());
self::$application->setAutoExit(false);
}
return self::$application;
}
}
P.S. Guys, don't shame Symfony2 with calling exec()...
PS 伙计们,不要因为打电话而羞辱 Symfony2 exec()...
回答by Squazic
The docstell you the suggested way to do it. The example code is pasted below:
该文件告诉你建议的方式做到这一点。示例代码粘贴如下:
protected function execute(InputInterface $input, OutputInterface $output)
{
$command = $this->getApplication()->find('demo:greet');
$arguments = array(
'command' => 'demo:greet',
'name' => 'Fabien',
'--yell' => true,
);
$input = new ArrayInput($arguments);
$returnCode = $command->run($input, $output);
// ...
}
回答by ContextSwitch
Yes, if your directory structure looks like
是的,如果您的目录结构看起来像
/symfony
/app
/src
then you would run
然后你会跑
phpunit -c app/phpunit.xml.dist
phpunit -c app/phpunit.xml.dist
from your unit tests you can run php commands either by using
从您的单元测试中,您可以通过使用运行 php 命令
passthru("php app/console [...]") (http://php.net/manual/en/function.passthru.php)
exec("php app/console [...]") (http://www.php.net/manual/en/function.exec.php)
or by putting the command in back ticks
或者将命令放在后面的勾号中
php app/consode [...]
php app/consode [...]
If you are running the unit tests from a directory other than symofny, you'll have to adjust the relative path to the app directory for it to work.
如果您从 symofny 以外的目录运行单元测试,则必须调整应用程序目录的相对路径才能使其工作。
To run it from the app:
要从应用程序运行它:
// the document root should be the web folder
$root = $_SERVER['DOCUMENT_ROOT'];
passthru("php $root/../app/console [...]");

