Php 无法测试Symfony2控制台命令

Php 无法测试Symfony2控制台命令,php,symfony,phpunit,codesniffer,Php,Symfony,Phpunit,Codesniffer,作为学习Symfony2的一部分,我正在尝试编写一个非常简单的控制台命令,它只运行phpcs(PHP代码嗅探器) 下面是扩展ContainerWareCommand的类中的execute函数: protected function execute(InputInterface $input, OutputInterface $output) { $output->writeln('<info>Generating PHP Code Sniffer report...&l

作为学习Symfony2的一部分,我正在尝试编写一个非常简单的控制台命令,它只运行phpcs(PHP代码嗅探器)

下面是扩展ContainerWareCommand的类中的execute函数:

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>');
}
而且它工作得很好。输出文件按预期生成

我正在尝试使用以下函数测试mynamespace:ci:phpcs命令(它是PHPUnit_框架_测试用例的一部分):

但是,当尝试通过单元测试执行它时,它会失败,输出如下:

sh: phpcs: command not found
有人知道为什么会这样吗

PS:我观察到的一件事是,当我注释掉命令中调用“exec”的行时,测试运行通过(没有通过,但没有抱怨phpcs不存在),所以问题肯定出在exec命令上


是否将PHPUnt测试作为不同的用户运行,PHPs不可用?单元测试的

< P>您应该考虑嘲笑调用<代码> Excor()/<代码>。这将加快您的测试,并避免此类环境问题。对于这种情况,您可以简单地将调用
exec()
的方法添加到您可以为测试模拟的类中

class PhpCodeSnifferCommand extends ...
{
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        // ...
        runReport();
        // ...
                viewReport();
        // ...
    }

    protected function runReport() {
        exec('phpcs ./src > ./app/logs/phpcs.log');
    }

    protected function viewReport() {
        exec('mate ./app/logs/phpcs.log');
    }
}
模拟可以更容易地验证三种可能的路径:

  • 命令被告知不要提示用户查看报告
  • 命令提示;用户说没有
  • 命令提示;用户说是的
  • 将每个路径放置在其自己的测试中。您可以将公共代码放入一个测试助手方法中,使其变得更短

    public function testRunsReportWithoutAskingToView()
    {
        // ...
    
        $application = new Application($kernel);
        $phpcs = $this->getMock('PhpCodeSnifferCommand', array('runReport', 'viewReport'));
        $phpcs->expects($this->once())->method('runReport');
        $phpcs->expects($this->never())->method('viewReport');
        $application->add($phpcs);
    
        // Tell the command not to prompt to view the report ...
    }
    
    public function testRunsAndViewsReport()
    {
        // ...
    
        $application = new Application($kernel);
        $phpcs = $this->getMock('PhpCodeSnifferCommand', array('runReport', 'viewReport'));
        $phpcs->expects($this->once())->method('runReport');
        $phpcs->expects($this->once())->method('viewReport');
        $application->add($phpcs);
    
        // Tell the command to prompt to view and the dialog to hit "Y" for you ...
    }
    
    public function testRunsReportButDoesntViewIt()
    {
        // ...
    
        $application = new Application($kernel);
        $phpcs = $this->getMock('PhpCodeSnifferCommand', array('runReport', 'viewReport'));
        $phpcs->expects($this->once())->method('runReport');
        $phpcs->expects($this->never())->method('viewReport');
        $application->add($phpcs);
    
        // Tell the command to prompt to view and the dialog to hit "N" for you ...
    }
    

    您是如何从控制台运行PHPUnit的:在Jenkins或其他CI工具中,从IDE运行PHPUnit?Jenkins在我的Ubuntu系统上以
    Jenkins
    用户的身份运行,但我的IDE和控制台一样运行。我通常从IntelliJ运行它们,但也尝试从命令行运行。确保
    phpcs
    已安装在每个人的
    $PATH
    上,而不仅仅是你自己的。这是典型的情况,所以有点奇怪。嘲弄确实可以解决问题,但可能只是将问题推迟到下游。谢谢你的回答,这绝对是最好的解决方法。我不知道为什么我没有想到嘲笑对exec的呼叫。非常好的建议!
    class PhpCodeSnifferCommand extends ...
    {
        protected function execute(InputInterface $input, OutputInterface $output)
        {
            // ...
            runReport();
            // ...
                    viewReport();
            // ...
        }
    
        protected function runReport() {
            exec('phpcs ./src > ./app/logs/phpcs.log');
        }
    
        protected function viewReport() {
            exec('mate ./app/logs/phpcs.log');
        }
    }
    
    public function testRunsReportWithoutAskingToView()
    {
        // ...
    
        $application = new Application($kernel);
        $phpcs = $this->getMock('PhpCodeSnifferCommand', array('runReport', 'viewReport'));
        $phpcs->expects($this->once())->method('runReport');
        $phpcs->expects($this->never())->method('viewReport');
        $application->add($phpcs);
    
        // Tell the command not to prompt to view the report ...
    }
    
    public function testRunsAndViewsReport()
    {
        // ...
    
        $application = new Application($kernel);
        $phpcs = $this->getMock('PhpCodeSnifferCommand', array('runReport', 'viewReport'));
        $phpcs->expects($this->once())->method('runReport');
        $phpcs->expects($this->once())->method('viewReport');
        $application->add($phpcs);
    
        // Tell the command to prompt to view and the dialog to hit "Y" for you ...
    }
    
    public function testRunsReportButDoesntViewIt()
    {
        // ...
    
        $application = new Application($kernel);
        $phpcs = $this->getMock('PhpCodeSnifferCommand', array('runReport', 'viewReport'));
        $phpcs->expects($this->once())->method('runReport');
        $phpcs->expects($this->never())->method('viewReport');
        $application->add($phpcs);
    
        // Tell the command to prompt to view and the dialog to hit "N" for you ...
    }