标签:php phpunit symfony codesniffer
作为学习Symfony2的一部分,我正在尝试编写一个非常简单的控制台命令,它只运行phpcs(PHP Code Sniffer).
这是执行函数,它在扩展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命令.
PHPUnit测试是作为不同的用户运行的,phpcs不可用吗?
解决方法:
对于单元测试,您应该考虑模拟对exec()的调用.这将加速您的测试并避免诸如此类的环境问题.对于这种情况,您只需添加调用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', 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 ...
}
标签:php,phpunit,symfony,codesniffer 来源: https://codeday.me/bug/20190621/1251447.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。