dongxin1999
2017-11-08 15:15
浏览 88
已采纳

使用phpunit测试命令symfony

I create some basic command with symfony3.2 to generate some newsletter periodically I'm dealing with some issue when i want to test my symfony command with phpunit 5.5.4. It fail from the beginning:

    /**
     * @param InputInterface $input
     * @param OutputInterface $output
     */
    protected function execute(InputInterface $input, OutputInterface $output){

        $output->writeln("<info>Script start</info>");
        //...
        $output->writeln("<info>done</info>");
     }

with this unit test:

use MyBundle\Command\MyCommand;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Console\Tester\CommandTester;

class MyCommandTest extends KernelTestCase
{

    public function testExecute(){

        $kernel = static::createKernel();
        $kernel->boot();

        $application = new Application($kernel);
        $application->add(new MyCommand());

        $command = $application->find('generate:newsletter');
        $commandTester = new CommandTester($command);
        $commandTester->execute(array(
            'command' => $command->getName()
        ));

        $output = $commandTester->getDisplay();
        $this->assertContains('done',$output);
    }
}

I follow this step by step but in my case i get :

Error: Call to a member function writeln() on string

MyBundle/Command/MyCommand.php:197
vendor/symfony/symfony/src/Symfony/Component/Console/Command/Command.php:262
vendor/symfony/symfony/src/Symfony/Component/Console/Tester/CommandTester.php:84
MyBundle/Command/MyCommandTest.php:34

It seems like commandTester don't put correct parameter in execute method from MyCommand. I'm wondering if it's not CommandTesterClass issue.

That's why i'm here, to share with you that and find some solution together.

Thank you in advance

图片转代码服务由CSDN问答提供 功能建议

我用symfony3.2创建一些基本命令,定期生成一些时事通讯 当我处理某些问题时 想用phpunit 5.5.4测试我的symfony命令。 从一开始就失败了:</ p>

  / ** 
 * @param InputInterface $ input 
 * @param OutputInterface $ output 
 * / 
 protected function execute(  InputInterface $ input,OutputInterface $ output){
 
 $ output-&gt; writeln(“&lt; info&gt; Script start&lt; / info&gt;”); 
 //... 
 $ output-&gt; writeln( 使用此单元测试“&lt; info&gt; done&lt; / info&gt;”); 
} 
 </ code> </ pre> 
 
 

:</ p>

 < 代码>使用MyBundle \ Command \ MyCommand; 
use Symfony \ Bundle \ FrameworkBundle \ Console \ Application; 
use Symfony \ Bundle \ FrameworkBundle \ Test \ KernelTestCase; 
use Symfony \ Component \ Console \ Tester \ CommandTester; 
 
class MyCommandTest 扩展KernelTestCase 
 {
 
 
 
公共函数testExecute(){
 
 $ kernel = static :: createKernel(); 
 $ kernel-&gt; boot(); 
 
 $ application = new Application(  $ kernel); 
 $ application-&gt; add(new MyCommand()); 
 
 $ command = $ application-&gt; find('generate:newsletter'); 
 $ commandTester = new C  ommandTester($ command); 
 $ commandTester-&gt; execute(array(
'command'=&gt;  $ command-&gt; getName()
)); 
 
 $ output = $ commandTester-&gt; getDisplay(); 
 $ this-&gt; assertContains('done',$ output); 
} \  n} 
 </ code> </ pre> 
 
 

我遵循这个 一步一步,但在我的情况下,我得到:</ p>

 错误:调用字符串
 
MyBundle / Command / MyCommand.php上的成员函数writeln()  :197 
vendor / symfony的/ symfony的/ SRC / Symfony的/组件/控制台/命令/ Command.php:262 
vendor / symfony的/ symfony的/ SRC / Symfony的/组件/控制台/测试仪/ CommandTester.php:84 
MyBundle /命令 /MyCommandTest.php:34
nn

似乎commandTester没有在MyCommand的execute方法中输入正确的参数。 我想知道它是不是CommandTesterClass问题。 </ p>

这就是我在这里的原因,与您分享,并找到一些解决方案。 </ p>

提前谢谢</ p> </ div>

1条回答 默认 最新

相关推荐 更多相似问题