无法测试 Symfony2 控制台命令
作为学习 Symfony2 的一部分,我试图编写一个非常简单的控制台命令,它只运行 phpcs(PHP 代码嗅探器)。
这是扩展 ContainerAwareCommand 的类中的执行函数:
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln('<info>Generating PHP Code Sniffer report...</info>');
exec('phpcs ./src > ./app/logs/phpcs.log');
if ($input->getOption('noprompt') == null) {
$dialog = $this->getHelperSet()->get('dialog');
if ($dialog->askConfirmation($output, '<question>Open report in TextMate? (y/n)?</question>', false)) {
exec('mate ./app/logs/phpcs.log');
}
}
$output->writeln('<info>...done</info>');
}
我能够通过运行来执行控制台命令
app/console mynamespace:ci:phpcs
,并且它运行良好。输出文件按预期生成。
我正在尝试使用以下函数(它是 PHPUnit_Framework_TestCase 的一部分)测试 mynamespace:ci:phpcs 命令:
public function testExecute()
{
$kernel = new \AppKernel("test", true);
$kernel->boot();
$application = new Application($kernel);
$application->add(new PhpCodeSnifferCommand());
$command = $application->find('mynamespace:ci:phpcs');
$commandTester = new CommandTester($command);
$commandTester->execute(array('command' => $command->getName()));
// ... Test if output file was created here ... ommitted for brevity ... //
}
但是,当尝试通过单元测试执行它时,它会失败并显示以下输出:
sh: phpcs: command not found
有人知道吗为什么会发生这种情况?
PS:我确实观察到的一件事是,当我注释掉命令中调用“exec”的行时,测试会运行(没有通过,但不会抱怨 phpcs 不存在),所以问题肯定出在执行命令。
PHPUnit 测试是否以其他用户身份运行,而 phpcs 不可用?
As part of learning Symfony2, I'm trying to write a very simple console command that simply runs phpcs (PHP Code Sniffer).
Here's the execute function which is in a class extending ContainerAwareCommand:
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln('<info>Generating PHP Code Sniffer report...</info>');
exec('phpcs ./src > ./app/logs/phpcs.log');
if ($input->getOption('noprompt') == null) {
$dialog = $this->getHelperSet()->get('dialog');
if ($dialog->askConfirmation($output, '<question>Open report in TextMate? (y/n)?</question>', false)) {
exec('mate ./app/logs/phpcs.log');
}
}
$output->writeln('<info>...done</info>');
}
I am able to execute the console command by running
app/console mynamespace:ci:phpcs
and it works perfectly. The output file is generated as expected.
I'm trying to test the mynamespace:ci:phpcs command using the following function (which is part of a PHPUnit_Framework_TestCase):
public function testExecute()
{
$kernel = new \AppKernel("test", true);
$kernel->boot();
$application = new Application($kernel);
$application->add(new PhpCodeSnifferCommand());
$command = $application->find('mynamespace:ci:phpcs');
$commandTester = new CommandTester($command);
$commandTester->execute(array('command' => $command->getName()));
// ... Test if output file was created here ... ommitted for brevity ... //
}
However, when trying to execute it via the unit test, it fails with the following output:
sh: phpcs: command not found
Does anyone have an idea why this is happening?
PS: One thing I did observe was, that when I comment out the lines in the command that call 'exec' the test runs through (not passing, but not moaning that phpcs doesn't exist), so the problem is definitely with the exec commands.
Does the PHPUnit tests run as a different user, where phpcs is not available?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于单元测试,您应该考虑模拟对 exec() 的调用。这将加快您的测试速度并避免此类环境问题。对于这种情况,您可以简单地将调用
exec()
的方法添加到您可以模拟测试的类中。模拟可以更轻松地验证三种可能的路径:
将每条路径放入其自己的测试中。您可以将通用代码放入测试辅助方法中,以使其更短。
For unit tests you should consider mocking the calls to
exec()
. This will speed up your tests and avoid environmental issues such as this. For this case you can simply add methods that callexec()
to your class that you can mock for the tests.Mocking makes it easier to validate the three possible paths:
Place each path in its own test. You could put the common code into a test helper method to make this much shorter.