脚本宝典收集整理的这篇文章主要介绍了php – 无法测试Symfony2控制台命令,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
这是执行函数,它在扩展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不存在),所以问题肯定是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'); } }
Mocking可以更轻松地验证三种可能的路径:
>该命令被告知不提示用户查看报告.
>命令被告知提示;用户说没有.
>命令被告知提示;用户说是的.
将每个路径放在自己的测试中.您可以将公共代码放入测试助手方法中,以使其更短.
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','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','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 ... }
以上是脚本宝典为你收集整理的php – 无法测试Symfony2控制台命令全部内容,希望文章能够帮你解决php – 无法测试Symfony2控制台命令所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。